Technologische_Grundlagen/course/numpy/01_basics.ipynb

1170 lines
22 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b4db2677-a229-40ea-bb5b-d5064390c906",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "5e6e64f1-5820-4907-90f1-d314cebd8b21",
"metadata": {},
"source": [
"### Creating Arrays"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "7b612c05-8510-4c75-98b9-aa1dd82638ce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5]\n",
"<class 'numpy.ndarray'>\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4, 5])\n",
"\n",
"print(arr)\n",
"\n",
"print(type(arr))\n",
"print"
]
},
{
"cell_type": "markdown",
"id": "ee4dfe3a-d0bc-44e8-8c13-c81b669fe029",
"metadata": {},
"source": [
"### Access Array Elements\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "46ed2f18-325e-433f-b8e8-81a7fcb55a68",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4])\n",
"\n",
"print(arr[0])"
]
},
{
"cell_type": "markdown",
"id": "577d8ad4-1ea1-4766-95d0-eca9eaa2e139",
"metadata": {},
"source": [
"### Access 2-D Arrays"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f611eb2f-1a51-4922-98cc-e0ca9c23d70c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2nd element on 1st row: 2\n"
]
}
],
"source": [
"arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])\n",
"\n",
"print('2nd element on 1st row: ', arr[0, 1])"
]
},
{
"cell_type": "markdown",
"id": "039dd714-035a-47ff-9189-4738d0439a25",
"metadata": {},
"source": [
"### Slicing arrays"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d4b7ab6b-337b-43cc-94a3-80286023cb64",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 3 4 5]\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4, 5, 6, 7])\n",
"\n",
"print(arr[1:5])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "9f4155d9-85be-412b-a60a-d3d501b3f37e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5 6 7]\n"
]
}
],
"source": [
"print(arr[4:])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c2a24299-e29a-4ac4-a972-b6c29825974e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4]\n"
]
}
],
"source": [
"print(arr[:4])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1fb6581f-012f-4e76-92f4-e8334e9d72e1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2 4]\n"
]
}
],
"source": [
"# step -> index 1 to index 5, max 2 elements\n",
"arr = np.array([1, 2, 3, 4, 5, 6, 7])\n",
"\n",
"print(arr[1:5:2])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "68dc5552-2f13-40dd-9bd6-3db60f025063",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[7 8 9]\n"
]
}
],
"source": [
"# 2 dim array\n",
"arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])\n",
"\n",
"print(arr[1, 1:4])"
]
},
{
"cell_type": "markdown",
"id": "dbeb9676-d6cf-44f2-83a5-18fc2cfc1999",
"metadata": {},
"source": [
"### Data-Types "
]
},
{
"cell_type": "markdown",
"id": "79240bea-6521-4bcb-b630-7bf61e62ce69",
"metadata": {},
"source": [
"#### Python\n",
"- strings - used to represent text data, the text is given under quote marks. e.g. \"ABCD\"\n",
"- integer - used to represent integer numbers. e.g. -1, -2, -3\n",
"- float - used to represent real numbers. e.g. 1.2, 42.42\n",
"- boolean - used to represent True or False.\n",
"- complex - used to represent complex numbers. e.g. 1.0 + 2.0j, 1.5 + 2.5j\n",
"\n",
"#### Numpy\n",
"- i - integer\n",
"- b - boolean\n",
"- u - unsigned integer\n",
"- f - float\n",
"- c - complex float\n",
"- m - timedelta\n",
"- M - datetime\n",
"- O - object\n",
"- S - string\n",
"- U - unicode string\n",
"- V - fixed chunk of memory for other type ( void )\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "0be7eb7c-1f64-400a-88f4-6104bb778646",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"int64\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4])\n",
"\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "ccf3a0c3-7132-4800-9dc3-2c7966ddf0e8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<U6\n"
]
}
],
"source": [
"arr = np.array(['apple', 'banana', 'cherry'])\n",
"\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "7875ec79-b7ed-4b3a-a2b4-139e958994dc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[b'1' b'2' b'3' b'4']\n",
"|S1\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4], dtype='S')\n",
"\n",
"print(arr)\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "e8854d24-35df-44e3-bdb5-383e2baa92b2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"int32\n"
]
}
],
"source": [
"arr = np.array([1.1, 2.1, 3.1])\n",
"\n",
"newarr = arr.astype('i')\n",
"\n",
"print(newarr)\n",
"print(newarr.dtype)"
]
},
{
"cell_type": "markdown",
"id": "0c61e36d-f3fd-41a1-8bc0-bd8a7ed114c7",
"metadata": {},
"source": [
"### Copy vs. View\n",
"The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view of the original array.\n",
"\n",
"The copy owns the data and any changes made to the copy will not affect original array, and any changes made to the original array will not affect the copy.\n",
"\n",
"The view does not own the data and any changes made to the view will affect the original array, and any changes made to the original array will affect the view."
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "15121d39-bed1-4508-a88e-90e931b4c39d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[42 2 3 4 5]\n",
"[1 2 3 4 5]\n"
]
}
],
"source": [
"# copy\n",
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = arr.copy()\n",
"arr[0] = 42\n",
"\n",
"print(arr)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "3f7ee1d4-ab6c-4511-9684-858bf7507250",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[42 2 3 4 5]\n",
"[42 2 3 4 5]\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = arr.view()\n",
"arr[0] = 42\n",
"\n",
"print(arr)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "cbc20cde-62cd-4114-8c7a-22fad1d98963",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[31 2 3 4 5]\n",
"[31 2 3 4 5]\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = arr.view()\n",
"x[0] = 31\n",
"\n",
"print(arr)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "126d79dd-6a6c-4944-91e8-99bca5d71ee4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n",
"[1 2 3 4 5]\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4, 5])\n",
"\n",
"x = arr.copy()\n",
"y = arr.view()\n",
"\n",
"print(x.base)\n",
"print(y.base)"
]
},
{
"cell_type": "markdown",
"id": "7f07f77a-4344-4e91-89bb-7b328918eeb3",
"metadata": {},
"source": [
"### Shape of an Array"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "10a14265-ab85-4055-b18b-6e922cb5b249",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 4)\n"
]
}
],
"source": [
"arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
"\n",
"print(arr.shape)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "462f1f64-dda3-4fb9-b24f-5b7bc6f66c00",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[[[1 2 3 4]]]]]\n",
"shape of array : (1, 1, 1, 1, 4)\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4], ndmin=5)\n",
"\n",
"print(arr)\n",
"print('shape of array :', arr.shape)\n",
"# at index-4 we have value 4, so we can say that 5th ( 4 + 1 th) dimension has 4 elements."
]
},
{
"cell_type": "markdown",
"id": "e1c88c47-7707-49a0-a844-211419979186",
"metadata": {},
"source": [
"### Reshape From 1-D to 2-D"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "c3d02cc8-9b48-478c-9a50-84a68a42c8a8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1 2 3]\n",
" [ 4 5 6]\n",
" [ 7 8 9]\n",
" [10 11 12]]\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])\n",
"\n",
"newarr = arr.reshape(4, 3)\n",
"\n",
"print(newarr)"
]
},
{
"cell_type": "markdown",
"id": "e7a700dd-bc22-44d4-85c3-5931418a473a",
"metadata": {},
"source": [
"#### Flattening the arrays"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "0189914c-8a7b-4bba-a7be-73db0f6b3b38",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5 6]\n"
]
}
],
"source": [
"arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
"\n",
"newarr = arr.reshape(-1)\n",
"\n",
"print(newarr)"
]
},
{
"cell_type": "markdown",
"id": "bc649e80-767d-4570-932a-6e074b17fdd3",
"metadata": {},
"source": [
"### Iterating Arrays"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "662af486-9b6b-4bd7-8ade-2e6a3ebb437d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3])\n",
"\n",
"for x in arr:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "7463237c-c2e5-43a1-97ea-74c970df938a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"[4 5 6]\n"
]
}
],
"source": [
"arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
"\n",
"for x in arr:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "113a726f-9f1f-49b3-83c4-942fbd6bd981",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n"
]
}
],
"source": [
"# 2 dim array\n",
"arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
"\n",
"for x in arr:\n",
" for y in x:\n",
" print(y)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "95202463-8c24-479b-98e3-1104778bbbf0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n"
]
}
],
"source": [
"#nditer --> n-for loops\n",
"arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])\n",
"\n",
"for x in np.nditer(arr):\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "3b2ca7d7-28e7-429d-9c1b-a35ae789f50f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n"
]
}
],
"source": [
"# different step size\n",
"arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
"# Iterate through every scalar element of the 2D array skipping 1 element:\n",
"for x in np.nditer(arr[:, ::2]):\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "b3257129-9727-4647-84e6-ac985580fe73",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0, 0) 1\n",
"(0, 1) 2\n",
"(0, 2) 3\n",
"(0, 3) 4\n",
"(1, 0) 5\n",
"(1, 1) 6\n",
"(1, 2) 7\n",
"(1, 3) 8\n"
]
}
],
"source": [
"#ndenumerate --> returns index too\n",
"arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
"\n",
"for idx, x in np.ndenumerate(arr):\n",
" print(idx, x)"
]
},
{
"cell_type": "markdown",
"id": "299e02df-ed56-4c1c-a4c8-779a0a71f97a",
"metadata": {},
"source": [
"### Joining Arrays"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "72b19370-5292-4987-b79e-bd8772683bea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5 6]\n"
]
}
],
"source": [
"arr1 = np.array([1, 2, 3])\n",
"\n",
"arr2 = np.array([4, 5, 6])\n",
"\n",
"arr = np.concatenate((arr1, arr2))\n",
"\n",
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "bb39d7aa-a1ca-41a4-899e-193610ea4bd8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 5 6]\n",
" [3 4 7 8]]\n"
]
}
],
"source": [
"#2 dim array\n",
"arr1 = np.array([[1, 2], [3, 4]])\n",
"\n",
"arr2 = np.array([[5, 6], [7, 8]])\n",
"\n",
"arr = np.concatenate((arr1, arr2), axis=1)\n",
"\n",
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "177a8c9a-2cf8-40a9-bed0-39436910c45b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 4]\n",
" [2 5]\n",
" [3 6]]\n"
]
}
],
"source": [
"# stack\n",
"arr1 = np.array([1, 2, 3])\n",
"\n",
"arr2 = np.array([4, 5, 6])\n",
"\n",
"arr = np.stack((arr1, arr2), axis=1)\n",
"\n",
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "d3519009-19ac-4175-913e-8c5d47b1896f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5 6]\n"
]
}
],
"source": [
"# Stacking Along Rows\n",
"arr1 = np.array([1, 2, 3])\n",
"\n",
"arr2 = np.array([4, 5, 6])\n",
"\n",
"arr = np.hstack((arr1, arr2))\n",
"\n",
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "d3a2b831-6e4d-4caa-b270-372958b70d49",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]]\n"
]
}
],
"source": [
"# Stacking Along Columns\n",
"arr1 = np.array([1, 2, 3])\n",
"\n",
"arr2 = np.array([4, 5, 6])\n",
"\n",
"arr = np.vstack((arr1, arr2))\n",
"\n",
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "64a57420-2507-4abd-b573-d9976d2bb385",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[1 4]\n",
" [2 5]\n",
" [3 6]]]\n"
]
}
],
"source": [
"# Stacking Along Height (depth)\n",
"arr1 = np.array([1, 2, 3])\n",
"\n",
"arr2 = np.array([4, 5, 6])\n",
"\n",
"arr = np.dstack((arr1, arr2))\n",
"\n",
"print(arr)"
]
},
{
"cell_type": "markdown",
"id": "7b226ad5-addb-4ee1-93d4-748df1de2e85",
"metadata": {},
"source": [
"### Splitting Arrays"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "f357a0b4-7dbf-4cd0-b586-23e06c882f6d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[array([1, 2]), array([3, 4]), array([5, 6])]\n"
]
}
],
"source": [
"# The return value is a list containing three arrays.\n",
"arr = np.array([1, 2, 3, 4, 5, 6])\n",
"\n",
"newarr = np.array_split(arr, 3)\n",
"\n",
"print(newarr)"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "437b8f01-1309-48c5-a4ef-13dd26f73025",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[array([[1, 2],\n",
" [3, 4]]), array([[5, 6],\n",
" [7, 8]]), array([[ 9, 10],\n",
" [11, 12]])]\n"
]
}
],
"source": [
"# splitting 2dim arrays\n",
"arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])\n",
"\n",
"newarr = np.array_split(arr, 3)\n",
"\n",
"print(newarr)"
]
},
{
"cell_type": "markdown",
"id": "54416dbe-202c-461c-9417-79043e44207f",
"metadata": {},
"source": [
"### Searching Arrays"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "c3dc56a2-9928-40b1-9e89-0f61e22efc11",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(array([3, 5, 6]),)\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4, 5, 4, 4])\n",
"\n",
"x = np.where(arr == 4)\n",
"\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "b07a60bd-ddb1-45b8-bae8-452b4140133b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(array([1, 3, 5, 7]),)\n"
]
}
],
"source": [
"# Find the indexes where the values are even (modulo)\n",
"arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n",
"\n",
"x = np.where(arr%2 == 0)\n",
"\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"id": "0400ef9f-09f4-4e8d-b183-56e401fa8a5a",
"metadata": {},
"source": [
"#### Sorting Arrays"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "ad6ff4df-0260-46c6-ace8-f716baaa81ff",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0 1 2 3]\n"
]
}
],
"source": [
"arr = np.array([3, 2, 0, 1])\n",
"sorted_array = np.sort(arr)\n",
"print(sorted_array)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "2f51f21d-4ddc-4677-907f-07038c4e3a80",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3 2 1 0]\n"
]
}
],
"source": [
"# reverse\n",
"reverse_array = sorted_array[::-1]\n",
"print(reverse_array)"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "7264d51f-6d8b-4571-91ef-d8550b539d86",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['apple' 'banana' 'cherry']\n"
]
}
],
"source": [
"arr = np.array(['banana', 'cherry', 'apple'])\n",
"\n",
"print(np.sort(arr))"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "056524c9-a534-4b82-bb7f-4772efb94296",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2 3 4]\n",
" [0 1 5]]\n"
]
}
],
"source": [
"# 2-dim\n",
"arr = np.array([[3, 2, 4], [5, 0, 1]])\n",
"\n",
"print(np.sort(arr))"
]
},
{
"cell_type": "markdown",
"id": "f5a6b9f1-6a39-4f3b-8a98-a45bb6a803fb",
"metadata": {},
"source": [
"### Filter Arrays"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "101256a3-98a7-4ee8-bb6d-5386a42043f8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[False False True True]\n",
"[43 44]\n"
]
}
],
"source": [
"arr = np.array([41, 42, 43, 44])\n",
"\n",
"filter_arr = arr > 42\n",
"\n",
"newarr = arr[filter_arr]\n",
"\n",
"print(filter_arr)\n",
"print(newarr)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "79b7bfaf-1056-42f8-948e-bb6c2811a25a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[False True False True False True False]\n",
"[2 4 6]\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4, 5, 6, 7])\n",
"\n",
"filter_arr = arr % 2 == 0\n",
"\n",
"newarr = arr[filter_arr]\n",
"\n",
"print(filter_arr)\n",
"print(newarr)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8fcf398-28f3-4380-8aab-7692578ab277",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}