diff --git a/docs/_toc.yml b/docs/_toc.yml index 6a61d41..62fae20 100644 --- a/docs/_toc.yml +++ b/docs/_toc.yml @@ -61,8 +61,7 @@ parts: - caption: NumPy chapters: - file: numpy/intro_to_numpy - - file: numpy/numpy_quickstart - - file: numpy/numpy_fundamentals + - file: numpy/visual_intro_to_numpy - caption: Pandas chapters: diff --git a/docs/images/numpy/create_numpy_array.png b/docs/images/numpy/create_numpy_array.png new file mode 100644 index 0000000..d046c3e Binary files /dev/null and b/docs/images/numpy/create_numpy_array.png differ diff --git a/docs/images/numpy/create_numpy_array_1.png b/docs/images/numpy/create_numpy_array_1.png new file mode 100644 index 0000000..9d54d1d Binary files /dev/null and b/docs/images/numpy/create_numpy_array_1.png differ diff --git a/docs/images/numpy/create_numpy_array_2.png b/docs/images/numpy/create_numpy_array_2.png new file mode 100644 index 0000000..278b340 Binary files /dev/null and b/docs/images/numpy/create_numpy_array_2.png differ diff --git a/docs/images/numpy/create_numpy_array_ones_zeros_random.png b/docs/images/numpy/create_numpy_array_ones_zeros_random.png new file mode 100644 index 0000000..278b340 Binary files /dev/null and b/docs/images/numpy/create_numpy_array_ones_zeros_random.png differ diff --git a/docs/images/numpy/mean_square_error_formula.png b/docs/images/numpy/mean_square_error_formula.png new file mode 100644 index 0000000..1af929d Binary files /dev/null and b/docs/images/numpy/mean_square_error_formula.png differ diff --git a/docs/images/numpy/numpy_3d_array.png b/docs/images/numpy/numpy_3d_array.png new file mode 100644 index 0000000..cb156ae Binary files /dev/null and b/docs/images/numpy/numpy_3d_array.png differ diff --git a/docs/images/numpy/numpy_3d_array_creation.png b/docs/images/numpy/numpy_3d_array_creation.png new file mode 100644 index 0000000..35d71e8 Binary files /dev/null and b/docs/images/numpy/numpy_3d_array_creation.png differ diff --git a/docs/images/numpy/numpy_array.png b/docs/images/numpy/numpy_array.png new file mode 100644 index 0000000..450a92a Binary files /dev/null and b/docs/images/numpy/numpy_array.png differ diff --git a/docs/images/numpy/numpy_array_aggregation.png b/docs/images/numpy/numpy_array_aggregation.png new file mode 100644 index 0000000..8ce2bb0 Binary files /dev/null and b/docs/images/numpy/numpy_array_aggregation.png differ diff --git a/docs/images/numpy/numpy_array_broadcast.png b/docs/images/numpy/numpy_array_broadcast.png new file mode 100644 index 0000000..05279b3 Binary files /dev/null and b/docs/images/numpy/numpy_array_broadcast.png differ diff --git a/docs/images/numpy/numpy_array_create_2d.png b/docs/images/numpy/numpy_array_create_2d.png new file mode 100644 index 0000000..8ba6b46 Binary files /dev/null and b/docs/images/numpy/numpy_array_create_2d.png differ diff --git a/docs/images/numpy/numpy_array_slice.png b/docs/images/numpy/numpy_array_slice.png new file mode 100644 index 0000000..0c11aa9 Binary files /dev/null and b/docs/images/numpy/numpy_array_slice.png differ diff --git a/docs/images/numpy/numpy_array_subtract_multiply_divide.png b/docs/images/numpy/numpy_array_subtract_multiply_divide.png new file mode 100644 index 0000000..361050f Binary files /dev/null and b/docs/images/numpy/numpy_array_subtract_multiply_divide.png differ diff --git a/docs/images/numpy/numpy_arrays_adding_1.png b/docs/images/numpy/numpy_arrays_adding_1.png new file mode 100644 index 0000000..0a3da2e Binary files /dev/null and b/docs/images/numpy/numpy_arrays_adding_1.png differ diff --git a/docs/images/numpy/numpy_arrays_example_1.png b/docs/images/numpy/numpy_arrays_example_1.png new file mode 100644 index 0000000..610ccfa Binary files /dev/null and b/docs/images/numpy/numpy_arrays_example_1.png differ diff --git a/docs/images/numpy/numpy_audio.png b/docs/images/numpy/numpy_audio.png new file mode 100644 index 0000000..6ab7ba0 Binary files /dev/null and b/docs/images/numpy/numpy_audio.png differ diff --git a/docs/images/numpy/numpy_color_image.png b/docs/images/numpy/numpy_color_image.png new file mode 100644 index 0000000..dec1fef Binary files /dev/null and b/docs/images/numpy/numpy_color_image.png differ diff --git a/docs/images/numpy/numpy_grayscale_image.png b/docs/images/numpy/numpy_grayscale_image.png new file mode 100644 index 0000000..35c9031 Binary files /dev/null and b/docs/images/numpy/numpy_grayscale_image.png differ diff --git a/docs/images/numpy/numpy_matrix_aggregation_1.png b/docs/images/numpy/numpy_matrix_aggregation_1.png new file mode 100644 index 0000000..a1e7499 Binary files /dev/null and b/docs/images/numpy/numpy_matrix_aggregation_1.png differ diff --git a/docs/images/numpy/numpy_matrix_aggregation_2.png b/docs/images/numpy/numpy_matrix_aggregation_2.png new file mode 100644 index 0000000..4689ed2 Binary files /dev/null and b/docs/images/numpy/numpy_matrix_aggregation_2.png differ diff --git a/docs/images/numpy/numpy_matrix_aggregation_3.png b/docs/images/numpy/numpy_matrix_aggregation_3.png new file mode 100644 index 0000000..7a2ab57 Binary files /dev/null and b/docs/images/numpy/numpy_matrix_aggregation_3.png differ diff --git a/docs/images/numpy/numpy_matrix_aggregation_4.png b/docs/images/numpy/numpy_matrix_aggregation_4.png new file mode 100644 index 0000000..84e1988 Binary files /dev/null and b/docs/images/numpy/numpy_matrix_aggregation_4.png differ diff --git a/docs/images/numpy/numpy_matrix_arithmetic.png b/docs/images/numpy/numpy_matrix_arithmetic.png new file mode 100644 index 0000000..113511a Binary files /dev/null and b/docs/images/numpy/numpy_matrix_arithmetic.png differ diff --git a/docs/images/numpy/numpy_matrix_broadcast.png b/docs/images/numpy/numpy_matrix_broadcast.png new file mode 100644 index 0000000..758e387 Binary files /dev/null and b/docs/images/numpy/numpy_matrix_broadcast.png differ diff --git a/docs/images/numpy/numpy_matrix_dot_product_1.png b/docs/images/numpy/numpy_matrix_dot_product_1.png new file mode 100644 index 0000000..3ab4a04 Binary files /dev/null and b/docs/images/numpy/numpy_matrix_dot_product_1.png differ diff --git a/docs/images/numpy/numpy_matrix_dot_product_2.png b/docs/images/numpy/numpy_matrix_dot_product_2.png new file mode 100644 index 0000000..15d3a3c Binary files /dev/null and b/docs/images/numpy/numpy_matrix_dot_product_2.png differ diff --git a/docs/images/numpy/numpy_matrix_indexing.png b/docs/images/numpy/numpy_matrix_indexing.png new file mode 100644 index 0000000..777b285 Binary files /dev/null and b/docs/images/numpy/numpy_matrix_indexing.png differ diff --git a/docs/images/numpy/numpy_matrix_ones_zeros_random.png b/docs/images/numpy/numpy_matrix_ones_zeros_random.png new file mode 100644 index 0000000..fae71dd Binary files /dev/null and b/docs/images/numpy/numpy_matrix_ones_zeros_random.png differ diff --git a/docs/images/numpy/numpy_mean_square_error_formula.png b/docs/images/numpy/numpy_mean_square_error_formula.png new file mode 100644 index 0000000..d82ce1c Binary files /dev/null and b/docs/images/numpy/numpy_mean_square_error_formula.png differ diff --git a/docs/images/numpy/numpy_mse_1.png b/docs/images/numpy/numpy_mse_1.png new file mode 100644 index 0000000..dbeb5a6 Binary files /dev/null and b/docs/images/numpy/numpy_mse_1.png differ diff --git a/docs/images/numpy/numpy_mse_2.png b/docs/images/numpy/numpy_mse_2.png new file mode 100644 index 0000000..fd68f2b Binary files /dev/null and b/docs/images/numpy/numpy_mse_2.png differ diff --git a/docs/images/numpy/numpy_mse_3.png b/docs/images/numpy/numpy_mse_3.png new file mode 100644 index 0000000..f051895 Binary files /dev/null and b/docs/images/numpy/numpy_mse_3.png differ diff --git a/docs/images/numpy/numpy_mse_4.png b/docs/images/numpy/numpy_mse_4.png new file mode 100644 index 0000000..6bfc2a0 Binary files /dev/null and b/docs/images/numpy/numpy_mse_4.png differ diff --git a/docs/images/numpy/numpy_nlp_bert_shape.png b/docs/images/numpy/numpy_nlp_bert_shape.png new file mode 100644 index 0000000..5a72c96 Binary files /dev/null and b/docs/images/numpy/numpy_nlp_bert_shape.png differ diff --git a/docs/images/numpy/numpy_nlp_embeddings.png b/docs/images/numpy/numpy_nlp_embeddings.png new file mode 100644 index 0000000..a62813e Binary files /dev/null and b/docs/images/numpy/numpy_nlp_embeddings.png differ diff --git a/docs/images/numpy/numpy_nlp_ids.png b/docs/images/numpy/numpy_nlp_ids.png new file mode 100644 index 0000000..44e2763 Binary files /dev/null and b/docs/images/numpy/numpy_nlp_ids.png differ diff --git a/docs/images/numpy/numpy_nlp_tokenization.png b/docs/images/numpy/numpy_nlp_tokenization.png new file mode 100644 index 0000000..51249c3 Binary files /dev/null and b/docs/images/numpy/numpy_nlp_tokenization.png differ diff --git a/docs/images/numpy/numpy_nlp_vocabulary.png b/docs/images/numpy/numpy_nlp_vocabulary.png new file mode 100644 index 0000000..7a4516c Binary files /dev/null and b/docs/images/numpy/numpy_nlp_vocabulary.png differ diff --git a/docs/images/numpy/numpy_reshape.png b/docs/images/numpy/numpy_reshape.png new file mode 100644 index 0000000..702daf5 Binary files /dev/null and b/docs/images/numpy/numpy_reshape.png differ diff --git a/docs/images/numpy/numpy_transpose.png b/docs/images/numpy/numpy_transpose.png new file mode 100644 index 0000000..622bef1 Binary files /dev/null and b/docs/images/numpy/numpy_transpose.png differ diff --git a/docs/images/numpy/rename.sh b/docs/images/numpy/rename.sh new file mode 100755 index 0000000..384cec0 --- /dev/null +++ b/docs/images/numpy/rename.sh @@ -0,0 +1,11 @@ +for file in *; do + # Check if the file name contains a hyphen + if [[ "$file" == *-* ]]; then + # Replace hyphens with underscores in the file name + new_file="${file//-/_}" + # Rename the file + mv "$file" "$new_file" + fi +done + +echo "Renaming completed." diff --git a/docs/images/pandas/excel_to_pandas.png b/docs/images/pandas/excel_to_pandas.png new file mode 100644 index 0000000..6a3c569 Binary files /dev/null and b/docs/images/pandas/excel_to_pandas.png differ diff --git a/docs/numpy/intro_to_numpy.md b/docs/numpy/intro_to_numpy.md index 56b5acd..84ccd0e 100644 --- a/docs/numpy/intro_to_numpy.md +++ b/docs/numpy/intro_to_numpy.md @@ -20,3 +20,35 @@ Berikut beberapa perbedaan antara Numpy array dengan Python list: 2. Numpy array memiliki elemen-elemen yang seragam, sehingga ukuran mereka di dalam memori akan tetap sama. 3. Numpy array mendukung operasi matematika pada array ukuran besar secara efisien, sedangkan Python list tidak. 4. Bertambah banyaknya library scientific computing di Python yang menggunakan NumPy sebagai basisnya. + +## NumPy Quickstart + +Untuk mulai menggunakan NumPy dan mempelajari konsep dasar NumPy, kamu dapat membaca tutorial dari halaman resmi NumPy: + +[NumPy quickstart](https://numpy.org/doc/stable/user/quickstart.html) + +Tutorial di atas akan membantu kamu memahami tentang: + +1. Perbedaan antara one-, two-, dan n-dimensional arrays di NumPy +2. Bagaimana cara menggunakan operasi-operasi aljabar linear untuk n-dimensional arrays tanpa menggunakan for-loops +3. Memaham properti `axis` dan `shape` dari n-dimensional arrays + +--- + +Selain itu, kamu juga bisa membaca tutorial NumPy di beberapa situs berikut: + +1. [Scientific Computing in Python: Introduction to NumPy and Matplotlib](https://sebastianraschka.com/blog/2020/numpy-intro.html) + +## NumPy Fundamentals + +Dokumen-dokumen dari situs resmi NumPy di bawah ini akan membantu kamu untuk lebih memahami konsep, design decisions, dan technical constrains dari NumPy. + +- [Array creation](https://numpy.org/doc/stable/user/basics.creation.html) +- [Indexing on ndarrays](https://numpy.org/doc/stable/user/basics.indexing.html) +- [I/O with NumPy](https://numpy.org/doc/stable/user/basics.io.html) +- [Data types](https://numpy.org/doc/stable/user/basics.types.html) +- [Broadcasting](https://numpy.org/doc/stable/user/basics.broadcasting.html) +- [Copies and views](https://numpy.org/doc/stable/user/basics.copies.html) +- [Working with Arrays of Strings And Bytes](https://numpy.org/doc/stable/user/basics.strings.html) +- [Structured arrays](https://numpy.org/doc/stable/user/basics.rec.html) +- [Universal functions (ufunc) basics](https://numpy.org/doc/stable/user/basics.ufuncs.html) diff --git a/docs/numpy/numpy_fundamentals.md b/docs/numpy/numpy_fundamentals.md deleted file mode 100644 index 998d773..0000000 --- a/docs/numpy/numpy_fundamentals.md +++ /dev/null @@ -1,13 +0,0 @@ -# NumPy Fundamentals - -Dokumen-dokumen dari situs resmi NumPy di bawah ini akan membantu kamu untuk lebih memahami konsep, design decisions, dan technical constrains dari NumPy. - -- [Array creation](https://numpy.org/doc/stable/user/basics.creation.html) -- [Indexing on ndarrays](https://numpy.org/doc/stable/user/basics.indexing.html) -- [I/O with NumPy](https://numpy.org/doc/stable/user/basics.io.html) -- [Data types](https://numpy.org/doc/stable/user/basics.types.html) -- [Broadcasting](https://numpy.org/doc/stable/user/basics.broadcasting.html) -- [Copies and views](https://numpy.org/doc/stable/user/basics.copies.html) -- [Working with Arrays of Strings And Bytes](https://numpy.org/doc/stable/user/basics.strings.html) -- [Structured arrays](https://numpy.org/doc/stable/user/basics.rec.html) -- [Universal functions (ufunc) basics](https://numpy.org/doc/stable/user/basics.ufuncs.html) diff --git a/docs/numpy/numpy_quickstart.md b/docs/numpy/numpy_quickstart.md deleted file mode 100644 index 5eb3683..0000000 --- a/docs/numpy/numpy_quickstart.md +++ /dev/null @@ -1,17 +0,0 @@ -# NumPy Quickstart - -Untuk mulai menggunakan NumPy dan mempelajari konsep dasar NumPy, kamu dapat membaca tutorial dari halaman resmi NumPy: - -[NumPy quickstart](https://numpy.org/doc/stable/user/quickstart.html) - -Tutorial di atas akan membantu kamu memahami tentang: - -1. Perbedaan antara one-, two-, dan n-dimensional arrays di NumPy -2. Bagaimana cara menggunakan operasi-operasi aljabar linear untuk n-dimensional arrays tanpa menggunakan for-loops -3. Memaham properti `axis` dan `shape` dari n-dimensional arrays - ---- - -Selain itu, kamu juga bisa membaca tutorial NumPy di beberapa situs berikut: - -1. [Scientific Computing in Python: Introduction to NumPy and Matplotlib](https://sebastianraschka.com/blog/2020/numpy-intro.html) diff --git a/docs/numpy/visual_intro_to_numpy.ipynb b/docs/numpy/visual_intro_to_numpy.ipynb new file mode 100644 index 0000000..ce8b682 --- /dev/null +++ b/docs/numpy/visual_intro_to_numpy.ipynb @@ -0,0 +1,1507 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "2e751d8c-6c3e-48b8-8b52-60c55295d8e2", + "metadata": {}, + "source": [ + "# Visual Introduction to NumPy\n", + "\n", + "Pada notebook ini, kita akan melihat beberapa cara menggunakan NumPy dan bagaimana NumPy mampu merepresentasikan tipe data-tipe data (tabel, gambar, teks, dll).\n", + "\n", + "```{contents}\n", + ":local:\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "7f6f16c1-51a2-46c0-bdbc-6cc4f90fd0bd", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "id": "e2d1e450-8fa5-49ad-82e6-71be2e7a7cf1", + "metadata": {}, + "source": [ + "## Creating Arrays\n", + "\n", + "Kita bisa membuat sebuah NumPy array (alias [`ndarray`](https://docs.scipy.org/doc/numpy/reference/arrays.ndarray.html)) dengan menggunakan method `np.array()` dan memberikan Python list sebagai parameter input." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3ba55b89-fc2a-4fae-bac9-e039d651e242", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3])" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.array([1, 2, 3])" + ] + }, + { + "cell_type": "markdown", + "id": "1af4764b-bf8a-4f07-9798-d0a6155c1d7d", + "metadata": {}, + "source": [ + "Python akan membuat NumPy array seperti gambar di bawah:\n", + "\n", + "![create numpy array](../images/numpy/create_numpy_array_1.png)\n", + "\n", + "Sering kali, kita akan menggunakan NumPy untuk meng-inisialisasi nilai dari array. NumPy memiliki methods seperti `ones()`, `zeros()`, dan `random.random()` untuk membantu kita. Kita cukup memberi nilai untuk element-element yang mau dibuat:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "de5acf00-94b5-400f-be78-0492e65af10b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1., 1., 1.])" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "29d7e7ef-ab08-4073-a0b8-adc13eee35bf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0., 0., 0.])" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9aa80ae6-3f0a-4428-93cf-1bba11697f53", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0.72460707, 0.85334514, 0.06176135])" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.random(3)" + ] + }, + { + "cell_type": "markdown", + "id": "a00afa2f-2b7c-4f31-a3f7-d94d9ad0d455", + "metadata": {}, + "source": [ + "![create numpy array 2](../images/numpy/create_numpy_array_2.png)\n", + "\n", + "Setelah kita membuat NumPy array, kita bisa mulai memanipulasi mereka." + ] + }, + { + "cell_type": "markdown", + "id": "3ed48410-a61b-4520-a686-6b4869977ede", + "metadata": {}, + "source": [ + "## Array Arithmetic\n", + "\n", + "Ayo kita buat 2 NumPy arrays untuk mendemonstrasikan bagaimana menggunakan mereka. Kita beri nama `data` dan `ones`:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "ffacb7b8-f773-4116-acd8-0a14cfc89fec", + "metadata": {}, + "outputs": [], + "source": [ + "data = np.array([1, 2])\n", + "ones = np.ones(2)" + ] + }, + { + "cell_type": "markdown", + "id": "a4880d36-91eb-4334-9b0c-71b17e85a741", + "metadata": {}, + "source": [ + "![numpy_arrays_example_1](../images/numpy/numpy_arrays_example_1.png)\n", + "\n", + "Untuk melakukan operasi penjumlahan pada tiap barisnya, kita bisa menggunakan operator `+`:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "a26dba3d-d50c-407c-849c-d1ff232111b0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([2., 3.])" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data + ones" + ] + }, + { + "cell_type": "markdown", + "id": "63533cd0-4f58-4737-8d1a-f74c3e17aaf1", + "metadata": {}, + "source": [ + "![numpy_arrays_adding_1](../images/numpy/numpy_arrays_adding_1.png)\n", + "\n", + "Ini adalah salah satu keunggulan dalam menggunakan NumPy, dimana kompleksitas dalam melakukan penjumlahan dua array / list, telah diabstraksi ke level yang lebih tinggi. Bisa kalian bayangkan bagaimana melakukan operasi penjumlahan dua list menggunakan vanilla Python?\n", + "\n", + "Dan bukan hanya penjumlahan yang bisa NumPy lakukan:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "bff52e7a-346b-49b3-9e00-d7564a6b0f14", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([0., 1.])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data - ones" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "c9025127-3f10-43fe-a41d-1ceca54db8b0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 4])" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data * data" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "9346cc6f-67af-412a-9e0a-18ec45a772e7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1., 1.])" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data / data" + ] + }, + { + "cell_type": "markdown", + "id": "ad2ef0ce-7c70-454b-8ae1-f0327e502ba5", + "metadata": {}, + "source": [ + "![numpy_array_subtract_multiply_divide](../images/numpy/numpy_array_subtract_multiply_divide.png)" + ] + }, + { + "cell_type": "markdown", + "id": "d8492792-3835-4db8-a703-1aced6c419cc", + "metadata": {}, + "source": [ + "Kita juga bisa melakukan operasi antara array dan angka (atau biasa disebut operasi antara vector dan skalar). Misalnya, kita punya array yang merepresentasikan jarak dalam miles dan kita mau mengkonversi ke km. Kita bisa melakukannya seperti ini `data * 1.6`:" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "d71839b2-577d-42fe-a02b-8c01429991f1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1.6, 3.2])" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data * 1.6" + ] + }, + { + "cell_type": "markdown", + "id": "aa2abfde-0337-4c81-8039-73a58e498e2a", + "metadata": {}, + "source": [ + "![numpy_array_broadcast](../images/numpy/numpy_array_broadcast.png)" + ] + }, + { + "cell_type": "markdown", + "id": "2c2b8051-a24e-48ef-9a35-449365bbdeda", + "metadata": {}, + "source": [ + "NumPy secara otomatis melakukan operasi perkalian pada setiap baris. Konsep ini dinamakan **broadcasting**." + ] + }, + { + "cell_type": "markdown", + "id": "cc3f02b3-8b91-4eb8-87cb-57f5879c34b0", + "metadata": {}, + "source": [ + "## Indexing\n", + "\n", + "Kita juga bisa meng-index dan memotong (slice) NumPy arrays seperti di Python list:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "b92aff7d-c89c-4874-a911-90d0709c52fb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2])" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "5c8809f1-492e-42a4-8c8d-aa0d603028b8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "73b2b8ef-c256-444e-8b28-0ada056a026a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "4aace9ed-026b-4815-9881-9d6252a82013", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2])" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[0:2]" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "e7864ae2-dbc8-429d-ace8-18b972ee281c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([2])" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[1:]" + ] + }, + { + "cell_type": "markdown", + "id": "4da6e406-bc3f-4f2a-92bf-2d7612641c5c", + "metadata": {}, + "source": [ + "![numpy_array_slice](../images/numpy/numpy_array_slice.png)" + ] + }, + { + "cell_type": "markdown", + "id": "1fb76e76-2901-4449-8f72-726946943ff6", + "metadata": {}, + "source": [ + "## Aggregation\n", + "\n", + "Benefit lain dari NumPy adalah fungsi-fungsi untuk agregasi data:" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "dbd44cb9-25e6-4bde-b68e-adb57d25353a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.max()" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "42662f94-b410-447e-ba36-c901dde3ecf1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.min()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "2229cb93-fbc8-4761-a1b0-1630670b7788", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.sum()" + ] + }, + { + "cell_type": "markdown", + "id": "b6b008c1-0ed3-46a6-8672-a57187483a65", + "metadata": {}, + "source": [ + "![numpy_array_aggregation](../images/numpy/numpy_array_aggregation.png)\n", + "\n", + "Selain `min()`, `max()`, dan `sum()`, kamu juga bisa menggunakan `mean()` untuk mendapatkan nilai rata-rata, `prod()` untuk hasil perkalian semua elemen, `std()` untuk standar deviasi, dan [lebih banyak lagi](https://jakevdp.github.io/PythonDataScienceHandbook/02.04-computation-on-arrays-aggregates.html)." + ] + }, + { + "cell_type": "markdown", + "id": "ca820e40-9f26-4909-bb0d-9126dae49087", + "metadata": {}, + "source": [ + "## In more dimensions\n", + "\n", + "Sampai sini, kita melakukan operasi pada vector satu dimensi. Salah satu kelebihan NumPy adalah melakukan semua operasi yang sudah lihat di atas pada array dengan dimensi berapapun.\n", + "\n", + "### Creating Matrices\n", + "\n", + "Untuk membuat matrix, kita bisa memberikan lists of lists:" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "d5cfd4e0-156d-4909-b940-b963411d7580", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2],\n", + " [3, 4]])" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.array([[1, 2], [3, 4]])" + ] + }, + { + "cell_type": "markdown", + "id": "56a44aa8-0d40-469a-b486-ccd314c5ff9b", + "metadata": {}, + "source": [ + "![numpy_array_create_2d](../images/numpy/numpy_array_create_2d.png)\n", + "\n", + "Kita juga bisa menggunakan methods di atas (`ones()`, `zeros()`, dan `random.random()`) untuk membuat matriks, asalkan kita memberikan tuple yang mendeskripsikan dimensi matrix yang mau kita buat:" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "0eabcef8-0d7b-4acb-8c0f-a8c38eb42dc0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1., 1.],\n", + " [1., 1.],\n", + " [1., 1.]])" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones((3, 2))" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "a809ba70-f64c-4442-9a15-f11b204cef17", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0., 0.],\n", + " [0., 0.],\n", + " [0., 0.]])" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros((3, 2))" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "aa4f7614-3e8c-4739-bddd-8dd494b68818", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[0.4087331 , 0.43415853],\n", + " [0.25579427, 0.27746966],\n", + " [0.29308408, 0.70499797]])" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.random((3, 2))" + ] + }, + { + "cell_type": "markdown", + "id": "a43a1922-ca13-4ef0-a63a-4b9f6734b58d", + "metadata": {}, + "source": [ + "![numpy_matrix_ones_zeros_random](../images/numpy/numpy_matrix_ones_zeros_random.png)" + ] + }, + { + "cell_type": "markdown", + "id": "129df954-d0d1-4901-95b2-03f846b13688", + "metadata": {}, + "source": [ + "### Matrix Arithmetic\n", + "\n", + "Kita bisa melakukan operasi aritmatika pada matriks (`+-*/`) asalkan kedua matriksnya memiliki ukuran yang sama. NumPy akan menganggapnya sebagai operasi *position-wise*:" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "5ffd19af-8cce-416b-8e5c-09e59015fa1b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[2., 3.],\n", + " [4., 5.]])" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = np.array([[1, 2], [3, 4]])\n", + "ones = np.ones((2, 2))\n", + "data + ones" + ] + }, + { + "cell_type": "markdown", + "id": "713f84c0-3cab-4bab-9747-3a99577ed63c", + "metadata": {}, + "source": [ + "![numpy_matrix_arithmetic](../images/numpy/numpy_matrix_arithmetic.png)\n", + "\n", + "Kita juga bisa melakukan operasi aritmatika dengan matriks yang berukuran beda asalkan dimensinya satu (matriksnya hanya punya satu kolom atau satu baris). NumPy akan melakukan broadcasting secara otomatis:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "756e0882-799b-4be2-b890-2de1a6d63036", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[2., 3.],\n", + " [4., 5.],\n", + " [6., 7.]])" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = np.array([[1, 2], [3, 4], [5, 6]])\n", + "ones_row = np.ones(2)\n", + "data + ones_row" + ] + }, + { + "cell_type": "markdown", + "id": "75212ad9-9461-4a8b-a493-9c9db2792483", + "metadata": {}, + "source": [ + "![numpy_matrix_broadcast](../images/numpy/numpy_matrix_broadcast.png)" + ] + }, + { + "cell_type": "markdown", + "id": "f956c2db-22b3-4c4a-ac2f-13dac5416c5d", + "metadata": {}, + "source": [ + "### Dot Product\n", + "\n", + "Untuk melakukan [operasi perkalian pada matriks](https://www.mathsisfun.com/algebra/matrix-multiplying.html), kita harus menggunakan method `dot()`." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "327e3dbb-f1b9-49b4-839f-c0e0831ca4ba", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ 30201, 302010])" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = np.array([1, 2, 3])\n", + "powers_of_ten = np.array([[1, 10], [100, 1000], [10000, 100000]])\n", + "data.dot(powers_of_ten)" + ] + }, + { + "cell_type": "markdown", + "id": "109df6dd-0421-45c4-8607-25bf25572779", + "metadata": {}, + "source": [ + "![numpy_matrix_dot_product_1](../images/numpy/numpy_matrix_dot_product_1.png)" + ] + }, + { + "cell_type": "markdown", + "id": "60665a85-3a67-4b32-84de-70aa868698a9", + "metadata": {}, + "source": [ + "Perlu diingat bahwa perkalian matriks mewajibkan agar kedua matriks memiliki dimensi kolom dan baris yang sama. Kamu juga bisa memvisualisasikan operasi di atas seperti berikut:\n", + "\n", + "![numpy_matrix_dot_product_2](../images/numpy/numpy_matrix_dot_product_2.png)" + ] + }, + { + "cell_type": "markdown", + "id": "ecc1d033-e237-40a0-ae9f-9dcd928d71c4", + "metadata": {}, + "source": [ + "### Matrix Indexing\n", + "\n", + "Indexing dan slicing pada NumPy akan sangat berguna untuk memanipulasi matriks:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "93d100a6-5eb4-4486-bc88-021b01269902", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2],\n", + " [3, 4],\n", + " [5, 6]])" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = np.array([[1, 2], [3, 4], [5, 6]])\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "780565a8-1c3d-4567-839b-0f78bab7a68e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[0, 1]" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "d4b645e0-68a2-4bca-be06-843fa8ec4475", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[3, 4],\n", + " [5, 6]])" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[1:3]" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "1939f552-318c-4b1c-9c4e-3f74b2bd6478", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 3])" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data[0:2, 0]" + ] + }, + { + "cell_type": "markdown", + "id": "15ae1bb2-43ed-45c4-99db-0b182d39232d", + "metadata": {}, + "source": [ + "![numpy_matrix_indexing](../images/numpy/numpy_matrix_indexing.png)" + ] + }, + { + "cell_type": "markdown", + "id": "196527c1-b469-4b66-90bf-6931a8e7fab9", + "metadata": {}, + "source": [ + "### Matrix Aggregation\n", + "\n", + "Kita bisa mengagregat matrix sebagaimana kita mengagregat vector:" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "8c590260-bd83-494c-98ee-f89bc7498749", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2],\n", + " [3, 4],\n", + " [5, 6]])" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = np.array([[1, 2], [3, 4], [5, 6]])\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "c19c410b-0b6e-414e-8460-49877875c31f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.max()" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "4853c83b-ff7e-4b39-a503-41f8d54a761b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.min()" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "c16eec6f-70c4-4186-bbc2-b22286104224", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "21" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.sum()" + ] + }, + { + "cell_type": "markdown", + "id": "73c0d5c1-39d9-4a66-8876-2ca80ec24ff4", + "metadata": {}, + "source": [ + "![numpy_matrix_aggregation_1](../images/numpy/numpy_matrix_aggregation_1.png)\n", + "\n", + "Selain dapat melakukan agregat pada semua elemen di matrix, kita juga bisa melakukan agregat pada baris atau kolom dengan menggunakan parameter `axis`:" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "3bed362b-d268-489f-8c55-bb6e6ff62abf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2],\n", + " [5, 3],\n", + " [4, 6]])" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = np.array([[1, 2], [5, 3], [4, 6]])\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "9233fcb0-357a-49f8-9fe5-ea36b41d247c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([5, 6])" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.max(axis=0)" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "a70e3d61-21fa-4cfc-908c-e43c961c743d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([2, 5, 6])" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.max(axis=1)" + ] + }, + { + "cell_type": "markdown", + "id": "e1c9f6cf-de4a-4f91-a680-ccd44fae7e23", + "metadata": {}, + "source": [ + "![numpy_matrix_aggregation_4](../images/numpy/numpy_matrix_aggregation_4.png)" + ] + }, + { + "cell_type": "markdown", + "id": "d73489c6-2c40-4de3-b3c1-de2272ee53eb", + "metadata": {}, + "source": [ + "### Transpose and Reshape\n", + "\n", + "Salah satu operasi yang sering dilakukan pada matrix adalah transpose, terutama saat kita ingin mendapatkan dot product dari dua matriks dan kita perlu menyamakan dimensinya. NumPy array memiliki properti `T` yang sangat berguna untuk mendapatkan hasil transpose dari matriks:" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "3b976ed1-8283-498d-a7a4-6f744e7f14ef", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2],\n", + " [3, 4],\n", + " [5, 6]])" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = np.array([[1, 2], [3, 4], [5, 6]])\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "66f7700d-de46-462f-9f7c-638a0e2db00c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 3, 5],\n", + " [2, 4, 6]])" + ] + }, + "execution_count": 39, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.T" + ] + }, + { + "cell_type": "markdown", + "id": "5bce74ed-3337-42eb-88c4-4629d2b83fd1", + "metadata": {}, + "source": [ + "![numpy_transpose](../images/numpy/numpy_transpose.png)" + ] + }, + { + "cell_type": "markdown", + "id": "7e71a7f3-74c9-4283-8952-cfc1d06f3df8", + "metadata": {}, + "source": [ + "Untuk kasus yang lebih kompleks, kamu mungkin perlu mengubah dimensi dari suatu matrix. Hal ini akan sering kamu temukan pada aplikasi machine learning ketika suatu model mengharapkan input dengan dimensi yang berbeda dari dataset kamu. NumPy memiliki method `reshape()` yang berguna untuk kasus ini. Kamu bisa memberikan dimensi baru yang kamu inginkan." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "47d4587c-aac7-42f1-a2e1-6299eff6d791", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 2, 3, 4, 5, 6])" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = np.array([1, 2, 3, 4, 5, 6])\n", + "data" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "dfc72646-c4fb-49fc-82c4-db82bb8100b2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2, 3],\n", + " [4, 5, 6]])" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.reshape(2, 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "8b7ba757-c8b8-4546-8d92-99cf69a0437d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2],\n", + " [3, 4],\n", + " [5, 6]])" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data.reshape(3, 2)" + ] + }, + { + "cell_type": "markdown", + "id": "c2f4af97-073d-42d3-ab03-03f47ce70177", + "metadata": {}, + "source": [ + "![numpy_reshape](../images/numpy/numpy_reshape.png)" + ] + }, + { + "cell_type": "markdown", + "id": "03e3d546-00b7-4a26-9bc2-48de36325e3b", + "metadata": {}, + "source": [ + "## Yet More Dimensions\n", + "\n", + "Semua hal yang telah kita bahas di atas, bisa NumPy terapkan pada array dalam dimensi berapapun." + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "b278ed6a-92c0-44b4-9be7-0615b72cce1a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1, 2],\n", + " [3, 4]],\n", + "\n", + " [[5, 6],\n", + " [7, 8]]])" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])" + ] + }, + { + "cell_type": "markdown", + "id": "c9274bcd-049c-4629-a94c-b645347644bd", + "metadata": {}, + "source": [ + "![numpy_3d_array](../images/numpy/numpy_3d_array.png)" + ] + }, + { + "cell_type": "markdown", + "id": "558c4a28-3e87-40f8-a74c-01c96bf39b9b", + "metadata": {}, + "source": [ + "Untuk menambahkan dimensi baru, kamu cukup menambahkan tanda koma dan nilai dimensinya sebagai parameter input pada fungsi-fungsi NumPy:" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "ae69d39d-9377-483e-8047-21fc849cc8ce", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[1., 1.],\n", + " [1., 1.],\n", + " [1., 1.]],\n", + "\n", + " [[1., 1.],\n", + " [1., 1.],\n", + " [1., 1.]],\n", + "\n", + " [[1., 1.],\n", + " [1., 1.],\n", + " [1., 1.]],\n", + "\n", + " [[1., 1.],\n", + " [1., 1.],\n", + " [1., 1.]]])" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones((4, 3, 2))" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "36141e1b-6fe4-4b77-b0ba-ae4e2dbb3e50", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0., 0.],\n", + " [0., 0.],\n", + " [0., 0.]],\n", + "\n", + " [[0., 0.],\n", + " [0., 0.],\n", + " [0., 0.]],\n", + "\n", + " [[0., 0.],\n", + " [0., 0.],\n", + " [0., 0.]],\n", + "\n", + " [[0., 0.],\n", + " [0., 0.],\n", + " [0., 0.]]])" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros((4, 3, 2))" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "f674cbbc-0fcc-4e69-9d7c-0b8b4fd77caa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0.90498395, 0.03851184],\n", + " [0.21415917, 0.95431541],\n", + " [0.85389651, 0.58808646]],\n", + "\n", + " [[0.49770434, 0.75591488],\n", + " [0.91092166, 0.589791 ],\n", + " [0.15598801, 0.34621563]],\n", + "\n", + " [[0.59502785, 0.91617785],\n", + " [0.32737737, 0.0324181 ],\n", + " [0.49116377, 0.8892512 ]],\n", + "\n", + " [[0.19408499, 0.9912059 ],\n", + " [0.0789867 , 0.6225484 ],\n", + " [0.56906944, 0.50474263]]])" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.random.random((4, 3, 2))" + ] + }, + { + "cell_type": "markdown", + "id": "4a5dc076-1fdd-4b06-b812-d50195a908f0", + "metadata": {}, + "source": [ + "![numpy_3d_array_creation](../images/numpy/numpy_3d_array_creation.png)" + ] + }, + { + "cell_type": "markdown", + "id": "bb7e6eb7-0afe-4a74-89fd-b97298e29876", + "metadata": {}, + "source": [ + "## Practical Usage\n", + "\n", + "Sekarang kita akan melihat contoh-contoh penggunaan NumPy.\n", + "\n", + "### Formulas\n", + "\n", + "Untuk mengimplementasikan rumus matematika pada matriks dan vector adalah salah satu alasan untuk menggunakan NumPy.\n", + "\n", + "NumPy dapat mengimplementasikan rumus-rumus matematika pada data matriks dan vector dengan mudah -- ini menjadi salah satu alasan kenapa NumPy banyak digemari oleh komunitas Python. Contohnya, perhatikan rumus *mean squared error* (kuadrat dari rata-rata kesalahan) yang sering digunakan pada supervised ML models terutama untuk regression:\n", + "\n", + "![mean_square_error_formula](../images/numpy/mean_square_error_formula.png)\n", + "\n", + "Di NumPy, kita bisa menulisnya seperti ini:\n", + "\n", + "![numpy_mean_square_error_formula](../images/numpy/numpy_mean_square_error_formula.png)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "9d3abdba-c36f-4d3a-8803-5fdb39ea0cb9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1.6666666666666665" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "predictions = np.array([1, 1, 1])\n", + "labels = np.array([1, 2, 3])\n", + "\n", + "error = (1 / predictions.size) * np.sum(np.square(predictions - labels))\n", + "error" + ] + }, + { + "cell_type": "markdown", + "id": "b1d962d0-1fc4-4727-b2c7-1bd2b8588c2d", + "metadata": {}, + "source": [ + "Kelebihan dari kode di atas adalah kita tidak perlu khawatir terhadap ukuran array `predictions` dan `labels`, apakah ukuran mereka hanya 1 atau ribuan elemen, asalakan mereka memiliki ukuran yang sama.\n", + "\n", + "Marilah kita lihat langkah-langkah dari 4 operasi matematika rumus MSE di atas:\n", + "\n", + "![numpy_mse_1](../images/numpy/numpy_mse_1.png)\n", + "\n", + "Kedua array `predictions` dan `labels` memiliki 3 elemen, yang berarti nilai variabel `n` juga 3. Setelah kita melakukan operasi pengurangan, hasil sementara adalah berikut:\n", + "\n", + "![numpy_mse_2](../images/numpy/numpy_mse_2.png)\n", + "\n", + "Selanjutnya, hasil dari pemangkatan adalah:\n", + "\n", + "![numpy_mse_3](../images/numpy/numpy_mse_3.png)\n", + "\n", + "Dan hasil penjumlahannya:\n", + "\n", + "![numpy_mse_4](../images/numpy/numpy_mse_4.png)\n", + "\n", + "Akhirnya, kita mendapatkan nilai error dari prediksi model yang merupakan nilai kualitas dari model." + ] + }, + { + "cell_type": "markdown", + "id": "83103b28-a487-4ce6-a0a3-7c97828b7f5f", + "metadata": {}, + "source": [ + "### Data Representation\n", + "\n", + "Bayangkan semua tipe data yang perlu kamu proses untuk membuat model machine learning, bisa jadi data tersebut adalah spreadsheets, images, audio, dll. Tipe data-tipe data tersebut sangat cocok untuk direpresentaikan dalam n-dimensional array.\n", + "\n", + "#### Tables and Spreadsheets\n", + "\n", + "Spreadsheet atau tabel merupakan matriks 2 dimensi. Library Python yang paling populer untuk bekerja dengan tabel adalah [Pandas](../pandas/intro_to_pandas) yang dibuat menggunakan NumPy.\n", + "\n", + "![excel_top_pandas](../images/pandas/excel_to_pandas.png)\n", + "\n", + "#### Audio and Timeseries\n", + "\n", + "File audio adalah array 1 dimensi dimana elemennya berupa sampel angka yang merepresentasikan sinyal audio.\n", + "\n", + "![numpy_audio](../images/numpy/numpy_audio.png)\n", + "\n", + "#### Images\n", + "\n", + "Gambar adalah matriks yang biasa diukur dalam bentuk pixel (panjang x lebar).\n", + "\n", + "Jika gambarnya hanya berupa hitam dan putih, setiap pixel bisa direpresentasikan dalam satu nilai (biasanya antara 0 (hitam) dan 255 (putih)). Berikut contoh representasi gambar hitam putih:\n", + "\n", + "![numpy_grayscale_image](../images/numpy/numpy_grayscale_image.png)\n", + "\n", + "Namun, jika gambarnya berwarna, setiap pixel direpresentasikan oleh 3 nilai - dimana setiap nilainya untuk warna merah, hijau, dan biru. Karena itu kita perlu 3 dimensi untuk gambar berwarna karena setiap cell hanya bisa menyimpan satu warna:\n", + "\n", + "![numpy_color_image](../images/numpy/numpy_color_image.png)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}