Files
python_practise/numpy_tutorials_udemy.ipynb
abiton 2a24c5e16f Upload files to "/"
Numpy in JupyterLab
2026-02-18 02:38:43 +00:00

597 lines
13 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "666ccd0c-8076-4dc6-a6c0-204c1fb3d239",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "67adc236-04c9-4e21-9c8d-19d08dc0c378",
"metadata": {},
"outputs": [],
"source": [
"a1 = np.array([2,3.27,4,4.13,5.44,6,8,10,12,14,16,18,20,22,24,26,28,30], dtype=int)\n",
"a2 = np.array([[1,3,5,7,9],\n",
" [2,4,6,8,10],\n",
" [0,1,2,3,4]])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6d0671f7-fbf3-4bdd-aaa4-6f5050506c4f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, 3, 4, 4, 5, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28,\n",
" 30])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7721494d-2e08-4f81-9a77-9fe45068b0f9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('int64')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1.dtype"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7845ebde-ad26-4fb6-a070-a84ed41e9602",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a2.itemsize #in bytes"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "d6492342-2763-4413-a342-87d41f2ff62c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3, 5)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a2.shape #rows n columns"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "df9cdc73-0d24-4376-adae-d578aa30ab6d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1 9 25 49 81]\n",
" [ 4 16 36 64 100]\n",
" [ 0 1 4 9 16]]\n"
]
}
],
"source": [
"a3 = a2 ** 2\n",
"print(a3)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "6f77c338-8883-406d-aba9-91eda00cdbe1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 3, 4])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1[0:3]"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "4b1d2fa9-156d-428f-80e0-811933c06dba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 4, 5, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1[3:]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "74d1bf1a-c856-4b76-89fe-31957ab38505",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(30)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1[-1]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "0facf598-826f-4430-9675-5919e85c07db",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, 3, 4, 4, 5, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1[:-2]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a43e74d5-da97-4989-b1b2-43c559fc971e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2, 4, 8, 14, 20, 26])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a1[0::3] #skips numbers according to the last number in brackets"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e31bc6d9-6835-457d-9f80-eb72a209611b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 2 3 4 4 5 6 8 10 12 14 16 18 20 22 24 26 28 30]\n"
]
},
{
"data": {
"text/plain": [
"array([10, 6, 4, 3])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(a1)\n",
"a1[7::-2]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "bb542f32-a3d7-4cbe-ba3e-7d389c2c7039",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 3],\n",
" [2, 4]])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a2[:2, :2] #print specified rows and columns"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "89ae5245-f506-464f-a5d6-e4eedd90065b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a2[:1, :1]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "667f9641-e3f6-42b5-964d-655216c05153",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['1' '1' '1' '1']\n",
" ['1' '1' '1' '1']\n",
" ['1' '1' '1' '1']]\n"
]
}
],
"source": [
"a4 = np.ones([3,4], dtype=str) #print zeros or ones...change data type - str, float, \n",
"print(a4)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "a2353214-e87b-4e04-baaf-7299f870bca9",
"metadata": {},
"outputs": [],
"source": [
"#create an array and sequence\n",
"\n",
"x = 4\n",
"b = np.arange(4,64, x-2)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "c192f6a8-ad4d-4f23-8470-42bd02d5a628",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36,\n",
" 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "6bfc6c92-addc-4d46-bd4d-8be1029e88f9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([3. , 3.33333333, 3.66666667, 4. , 4.33333333,\n",
" 4.66666667, 5. , 5.33333333, 5.66666667, 6. ])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Import numpy library which contains the linspace function\n",
"import numpy as np\n",
"\n",
"# Create values within spaced numbers\n",
"c = np.linspace(3, 6, 10)\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "620b3488-5da0-4730-9b9d-415713651df5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3. , 3.33333333],\n",
" [3.66666667, 4. ],\n",
" [4.33333333, 4.66666667],\n",
" [5. , 5.33333333],\n",
" [5.66666667, 6. ]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#reshape arrange into new dim\n",
"d = c.reshape(5,2)\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "5a9d12bc-5abc-4615-a0ae-29e2dffee3c8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3. , 3.33333333, 3.66666667, 4. , 4.33333333],\n",
" [4.66666667, 5. , 5.33333333, 5.66666667, 6. ]])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = np.reshape(d,(2,5))\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "6c7f2bc9-4314-4c59-90f9-6b6b3fbcdfa5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2. , 3. , 4. , 4. , 5. ,\n",
" 6. , 8. , 10. , 12. , 14. ,\n",
" 16. , 18. , 20. , 22. , 24. ,\n",
" 26. , 28. , 30. , 3. , 3.33333333,\n",
" 3.66666667, 4. , 4.33333333, 4.66666667, 5. ,\n",
" 5.33333333, 5.66666667, 6. ])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#concatenate - join 2 arrays\n",
"b =np.concatenate([a1,c])\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "168329ae-cbb4-4e98-b2dc-eede58f086a6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1 2 3 4 5 6 7 8 9 10]\n",
"[ 1 3 5 7 9 11 13 15 17 19]\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.arange(1,11,1)\n",
"b = np.arange(1,20,2)\n",
"\n",
"print(a)\n",
"print(b)\n",
"c = np.array_equal(a,b)\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "c16e4581-696a-435f-b7dd-d236bca75ebc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([False, False, False, False, False, False, False, False, False,\n",
" False])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = np.logical_xor(a,b)\n",
"d"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "c010c8d6-546c-4f10-847e-8930078b0601",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([102, 48, 19, 94, 30, 25, 10, 92, 37, 73, 74, 64, 61,\n",
" 102, 77, 11, 21, 76, 20, 58, 92, 68, 54, 33, 92, 11,\n",
" 38, 23, 6, 85, 30, 100, 16, 37, 102, 70, 74, 10, 62,\n",
" 54, 37, 67, 44, 36, 8, 70, 8, 98, 45, 26, 4, 51,\n",
" 16, 72, 17, 42, 88, 50, 10, 62, 74, 11, 96, 70, 32,\n",
" 46, 76, 57, 44, 85, 11, 41, 65, 44, 98, 10, 50, 81,\n",
" 45, 81, 50, 48, 18, 50, 41, 5, 91, 94, 29, 101, 58,\n",
" 17, 27, 32, 94, 1, 84, 42, 82, 71, 1, 1, 28, 81,\n",
" 101, 59, 28, 55, 91, 83, 30, 17, 86, 12, 48, 86, 100,\n",
" 73, 50, 5, 83, 48, 98, 27, 6, 73, 15, 85, 92, 27,\n",
" 82, 91, 11, 71, 94, 59, 8, 59, 91, 83, 7, 56, 59,\n",
" 62, 59, 91, 17, 73, 8, 88, 65, 21, 7, 38, 14, 42,\n",
" 15, 11, 41, 38, 28, 31, 12, 93, 101, 36, 96, 7, 1,\n",
" 74, 97, 71, 81, 90, 63, 18, 85, 75, 74, 98, 45, 45,\n",
" 79, 73, 19, 40, 17, 8, 25, 24, 15, 7, 88, 3, 74,\n",
" 43, 72, 9, 24, 30])"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# big_data = np.random.random_integers(1,101,1000)\n",
"big_data2 = np.random.randint(1,101 + 2,200)\n",
"big_data2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0b96d8f4-d6fb-4f0a-8816-2f1dca85f43b",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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.13.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}