Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 91 additions & 32 deletions Fundamentals/Numpy/essentials.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@
"### Basic Array Creation:"
]
},
{
"cell_type": "markdown",
"id": "469b2a83",
"metadata": {},
"source": [
"**Note:** In NumPy, the term \"tensor\" is often used to refer to arrays of any dimension. A 1D array is a vector, a 2D array is a matrix, and higher-dimensional arrays are called tensors."
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 2,
"id": "dbd5d81e",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -104,7 +112,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 3,
"id": "83802b50",
"metadata": {},
"outputs": [
Expand All @@ -123,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 4,
"id": "39dfb97c",
"metadata": {},
"outputs": [
Expand All @@ -140,6 +148,14 @@
"print(y.flags.f_contiguous) # Is it Fortran contiguous? "
]
},
{
"cell_type": "markdown",
"id": "0e6707f1",
"metadata": {},
"source": [
"**C order** means rows are stored one after another (row-major). **Fortran order** means columns are stored one after another (column-major). This affects how data is laid out in memory and can impact performance for some operations."
]
},
{
"cell_type": "markdown",
"id": "cc7bba51",
Expand All @@ -150,7 +166,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 5,
"id": "51a1dece",
"metadata": {},
"outputs": [
Expand All @@ -176,7 +192,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 6,
"id": "b1676c5e",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -213,7 +229,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "8ba3caa5",
"metadata": {},
"outputs": [
Expand All @@ -230,36 +246,54 @@
"print(a + 5) # adds 5 to each element in the array"
]
},
{
"cell_type": "markdown",
"id": "26fdab69",
"metadata": {},
"source": [
"Vectorized operations in NumPy are fast because they are implemented in compiled C code under the hood, allowing operations on entire arrays without explicit Python loops. This leads to significant speedups for large data."
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 8,
"id": "03ec7a77",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ -0.02447243 -4.58678592 2.79514857 4.37957598 -5.01027197\n",
" 0.79991992 1.06516404 -6.75187828 -5.5635052 -2.75984379]\n",
" [ 5.07939064 -6.08044128 -2.0107042 -3.23687038 -1.9939912\n",
" -1.43513934 4.59965931 -1.24282259 -4.99097576 11.89869832]\n",
" [ -1.54279366 -5.47952922 3.42598619 4.50755354 3.87820602\n",
" -4.19837516 2.73886503 -5.62062562 4.98620218 4.14222144]\n",
" [ 10.81880802 -3.0316239 1.3985158 -8.4999524 8.4718774\n",
" 6.96113947 7.70438246 0.23762215 -1.93385122 3.39785957]\n",
" [ 2.22880788 2.62092499 1.47809868 -0.47977848 4.65140253\n",
" -0.25928333 5.94391666 4.55825208 -3.7509756 -8.45029718]\n",
" [ -1.25629205 -0.51275671 -8.27743996 1.48546517 0.08115635\n",
" 4.33144519 -12.68775831 5.41431455 -0.59448426 -7.32965964]\n",
" [ -1.39553701 -14.52411923 -0.93647355 -2.92908507 -0.73956153\n",
" 2.17402357 -1.53052591 -11.38280558 -4.81727417 6.00257577]\n",
" [ -2.49392955 -3.25959384 -0.51832825 -5.34498951 7.89476589\n",
" 1.51228891 -6.49448237 0.48378173 1.01601658 -0.53514001]\n",
" [ -6.83392948 2.16687329 -3.33322049 2.78308091 -0.59196055\n",
" 5.62299811 -2.12529449 4.46238733 2.07122165 -10.7969715 ]\n",
" [ -1.37557556 7.22198767 7.67152946 -1.69603545 -8.77947343\n",
" -7.58507589 7.01532742 3.83841619 2.02003209 -7.60197409]]\n"
"[[-8.42345984e+00 -1.01916172e+01 -7.45918410e-01 3.96268543e+00\n",
" -5.72833093e+00 2.91438727e+00 4.06279948e+00 4.12523109e+00\n",
" 3.00733531e-01 9.78858080e-02]\n",
" [ 1.59576119e+00 3.26355065e+00 -1.06319812e+01 4.70533281e+00\n",
" -1.48214898e+00 -2.52620053e+00 2.65741680e-01 4.16388773e-01\n",
" 9.22887221e+00 2.71577359e+00]\n",
" [-9.66200392e-01 -2.17186285e+00 6.75360270e+00 5.99673669e+00\n",
" -9.85259070e+00 -4.46350024e+00 -3.23534501e+00 -3.02228031e+00\n",
" 6.64894340e+00 -2.07698746e-01]\n",
" [ 4.43036087e+00 -3.90701380e+00 -4.51090876e+00 4.16867809e+00\n",
" -5.70280260e-01 1.95887322e+00 -1.60934887e-01 -1.66273426e+00\n",
" 2.61067655e+00 -2.44358783e+00]\n",
" [ 3.51765638e+00 4.65760575e+00 -8.96849909e-01 -1.06300686e+00\n",
" -5.01294087e+00 3.82284722e+00 3.77360233e+00 2.68585738e+00\n",
" 8.32719266e+00 -9.34176480e+00]\n",
" [-3.47242746e+00 -3.58623833e+00 -9.61253561e-01 1.39347319e+00\n",
" 1.32623086e+00 -1.41734816e+00 -3.96135494e+00 -3.41007842e+00\n",
" -4.21338940e+00 -7.24046272e+00]\n",
" [-1.66593856e+01 -1.88375474e-01 5.39244429e+00 9.63068424e-01\n",
" 4.29069316e+00 -6.24799863e+00 -6.41083656e+00 4.27256832e+00\n",
" -7.06174952e+00 6.95411295e+00]\n",
" [ 2.57333024e-02 7.24614163e+00 5.25180381e+00 6.67631814e+00\n",
" 2.84832739e-01 -5.26630346e+00 -1.59437223e-03 -9.95875547e+00\n",
" 2.31109628e+00 6.40286987e-02]\n",
" [ 1.19716494e+01 1.71500697e-01 2.77296476e-01 -1.01661562e+01\n",
" -1.77701099e+00 -5.46968459e+00 8.52619956e+00 2.01530821e+00\n",
" 6.04138132e+00 9.10008895e+00]\n",
" [ 4.87703646e+00 -1.68493879e+00 -7.19759065e+00 1.09044935e-01\n",
" 5.07082145e-01 -1.14342681e+01 4.98130023e+00 9.19224842e-01\n",
" -5.40917816e+00 -2.29185723e+00]]\n"
]
}
],
Expand Down Expand Up @@ -296,7 +330,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 9,
"id": "9456ddb6",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -327,7 +361,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 10,
"id": "2032b66c",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -379,6 +413,31 @@
"id": "0afac608",
"metadata": {},
"source": [
"#### Advanced Indexing\n",
"\n",
"- Advanced indexing occurs when you use lists or boolean arrays to index into an array.\n",
"- **Warning:** Advanced indexing creates copies of the data, and modifying the copy will not affect the original array.\n",
"\n",
"For example:\n",
"\n",
"```python\n",
"my_array = np.arange(10)\n",
"print('my_array:', my_array) # [0 1 2 3 4 5 6 7 8 9]\n",
"\n",
"# Using a list for advanced indexing\n",
"advanced_indexed_array = my_array[[1, 2, 8]]\n",
"print('advanced_indexed_array:', advanced_indexed_array) # [1 2 8]\n",
"\n",
"# Modify the advanced indexed array\n",
"advanced_indexed_array[0] = 99\n",
"print('After modifying advanced_indexed_array:')\n",
"print('my_array:', my_array) # [0 1 2 3 4 5 6 7 8 9]\n",
"print('advanced_indexed_array:', advanced_indexed_array) # [99 2 8]\n",
"```\n",
"\n",
"Even though `advanced_indexed_array` was created from `my_array`, it is a separate copy, and changes to it do not affect the original `my_array`.\n",
"\n",
"\n",
"### NumPy Internals"
]
},
Expand All @@ -392,7 +451,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 11,
"id": "fb47a3b2",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -503,7 +562,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "kernel_from_scratch",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -517,7 +576,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.2"
"version": "3.12.1"
}
},
"nbformat": 4,
Expand Down