2024-programowanie-w-python.../zajecia3/1.ipynb

2241 lines
45 KiB
Plaintext
Raw Permalink Normal View History

2024-12-07 11:54:47 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "56b06287-d1ba-409a-a207-2125edc31719",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "fa9d3b45-d381-4cfa-91b3-701a7c2578f7",
"metadata": {},
"source": [
"# NumPy"
]
},
{
"cell_type": "markdown",
"id": "7d008b7b-b668-42bd-993e-64cc2de4ae17",
"metadata": {},
"source": [
"## wymiary"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "cb38fb13-3671-4a7b-89e7-247d8db91a13",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 2, 3, 4, 5, 6, 7])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "445ebe15-088f-4811-96c6-cc0043fb5ab4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4 5 6 7]\n"
]
}
],
"source": [
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9428e371-82e5-4f94-b6f6-e54808a7b72c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(arr)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "bea5efad-20cc-4284-8575-bd32becaddd7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"numpy.ndarray"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(arr)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "cb12cac9-cf3b-4924-90e3-a82f5ca7274c",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.array(123).shape"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7e589615-ab19-4786-b2bc-49f46c706adc",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([[1, 2, 3], [4, 5, 6]])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a0925cb2-1586-48a7-9256-2d07228547e1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 3)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.shape"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "10b857a9-0bf0-4ee4-9f86-dabef5452d59",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]]\n"
]
}
],
"source": [
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "0029c4f5-135b-4a37-bf95-7f8e9afed454",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "6fe7be82-a5d9-493f-8410-c1201d49606d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 2, 3)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.shape"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "61686743-b034-4fb7-a891-e4aed1d7bd66",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[[1 2 3]\n",
" [4 5 6]]\n",
"\n",
" [[1 2 3]\n",
" [4 5 6]]]\n"
]
}
],
"source": [
"print(arr)"
]
},
{
"cell_type": "markdown",
"id": "ae9cd324-0297-42bc-8010-4ea0fa7074cc",
"metadata": {},
"source": [
"### zadanie 1\n",
"\n",
"1. Utwórz jednowymiarową tablicę zawierającą liczby od 10 do 20 włącznie. Wyświetl:\n",
" - Tablicę,\n",
" - Jej kształt (`shape`),\n",
" - Jej typ (`type`).\n",
"\n",
"2. Utwórz macierz 3x2 o elementach (10,20,30,40,50,60) i wyświetl:\n",
" - Tablicę,\n",
" - Jej kształt (`shape`),\n",
" - Jej typ (`type`).\n"
]
},
{
"cell_type": "markdown",
"id": "d38ccaec-ac11-4bef-8d48-7087931f4157",
"metadata": {},
"source": [
"## dostęp do elementów"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "a1cf151f-9128-414f-b065-2060b8a8a411",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "89975e71-f996-48af-a50d-3dd5f86c6775",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1 2 3 4 5]\n",
" [ 6 7 8 9 10]]\n"
]
}
],
"source": [
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "3f3b6ab3-7c1f-491c-9b9d-648073e65378",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[1,2]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "66527b7d-b6a6-435a-b330-2890792ca56e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[1,-2]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "2bb01400-e860-4c75-9fd1-ce44268463b6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([8, 9])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[1,2:4]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "ed9309c6-9666-41fe-9310-0597408740f7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 8, 9, 10])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[1,2:]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "8b1a9f33-5e91-4ffc-ac7a-d79286dcf1da",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 6, 7, 8, 9, 10])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[1,:]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "d9ec87f1-7c83-44f4-a71b-c9f291a51644",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[0,:]"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "825d6cf5-4302-4b94-b00a-dd8b5c61f058",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([3, 4])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr[0,2:4]"
]
},
{
"cell_type": "markdown",
"id": "0cb14fd3-88aa-43a8-a11d-a71fd9f8aeb6",
"metadata": {},
"source": [
"### zadanie 2\n",
"1. Utwórz dwuwymiarową tablicę NumPy o wymiarach (3,3) zawierającą liczby od 1 do 9.\n",
"2. Wykonaj następujące operacje na tablicy:\n",
" - Wyświetl element znajdujący się w drugim wierszu\n",
" - Wyświetl wszystkie elementy znajdujące się w drugim wierszu\n",
" - Wyświetl wszystkie elementy znajdujące się w drugiej kolumnie\n",
" - Wyświetl macierz, ale bez pierwszego wiersza i bez pierwszej kolumny\n"
]
},
{
"cell_type": "markdown",
"id": "4ad7bed0-d812-4683-a03b-033db0911197",
"metadata": {},
"source": [
"## Typy danych"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "c529565a-fc38-47ae-8649-352cd0525178",
"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": 23,
"id": "64996026-ef86-4a43-bdb0-1dcb64e3228b",
"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": 24,
"id": "1b201f08-8ef4-439c-8715-643b9f502295",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<U12\n"
]
}
],
"source": [
"arr = np.array(['apple', 'banana', 'cherrycherry'])\n",
"\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "d0d27e39-f790-4542-bf96-6c1bbe9a8f11",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<U30\n"
]
}
],
"source": [
"arr = np.array(['apple', 'banana', 'cherry'], dtype='U30')\n",
"\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "ae5d1846-4627-4711-942f-3dd550b174f4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('S1')"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"# typ ASCII\n",
"arr = np.array([1, 2, 3, 4], dtype='S')\n",
"arr.dtype"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "be10ebe0-ecc6-4ec2-b123-568ab0119dce",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4]\n",
"int32\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4], dtype='i4')\n",
"\n",
"print(arr)\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "7c6bd301-4f9e-4df5-9ec6-93ac7efc08f0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4]\n",
"int16\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4], dtype='i2')\n",
"\n",
"print(arr)\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "a7acb13b-6803-4b0d-8f21-b1b391ec201d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4]\n",
"int8\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4], dtype='i1')\n",
"\n",
"print(arr)\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "c50c6803-5a47-40c1-a90a-9c87deb341ce",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1000000, 2000000, 3000000, 4000000], dtype=int32)"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr*1000000"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "ef2d2c24-8a36-4eba-a0d4-3152d8cc6c52",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3 4]\n",
"int64\n"
]
}
],
"source": [
"arr = np.array([1, 2, 3, 4])\n",
"\n",
"print(arr)\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "d6147354-3ff2-4c42-afa1-f8068a3cb945",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1000000, 2000000, 3000000, 4000000])"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr*1000000"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "f247645b-21d3-4ff6-abf3-75433f2d7a88",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1.1 2.2 3.9]\n",
"float64\n"
]
}
],
"source": [
"arr = np.array([1.1, 2.2, 3.9])\n",
"\n",
"print(arr)\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "8ef1c2a2-5e80-4a0e-ba01-1d593203256c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n"
]
}
],
"source": [
"arr = arr.astype('i')\n",
"print(arr)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "b488506a-0574-428c-8c1b-b4695f4cc1fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ True False True]\n",
"bool\n"
]
}
],
"source": [
"arr = np.array([1, 0, 3])\n",
"\n",
"newarr = arr.astype(bool)\n",
"\n",
"print(newarr)\n",
"print(newarr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "2c2bc445-271d-4726-bbc1-eebc78960cb2",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 0, 3.4])"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "57a7b329-81fe-40b9-9ac4-4bf010edf5f3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1. , 0. , 3.4])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "9339374d-3773-4a12-adbe-ba5757be14c1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dtype('float64')"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.dtype"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "f0edd0f6-35ad-4ea4-b1c9-b420c5bf4d29",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 0, 3.4, 'x'])"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "c74e925f-0442-41a1-8f3b-e1d07163e3b2",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array(['1', '0', '3.4', 'x'], dtype='<U32')"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "markdown",
"id": "c3872881-4cf4-49f2-98a6-b6abbfec1d2b",
"metadata": {},
"source": [
"### Zadanie 3\n",
"### Instrukcja:\n",
"1. Utwórz tablicę NumPy zawierającą liczby całkowite od 10 do 40 z krokiem co 10. Wyświetl:\n",
" - Tablicę,\n",
" - Jej typ danych (`dtype`).\n",
"\n",
"2. Zmień typ danych tablicy z punktu 1 na `float32`. Wyświetl:\n",
" - Zmienioną tablicę,\n",
" - Jej nowy typ danych.\n",
"\n",
"3. Utwórz tablicę NumPy zawierającą ciągi znaków (\"Python\", \"NumPy\", \"Coding\"). Wyświetl:\n",
" - Tablicę,\n",
" - Jej typ danych."
]
},
{
"cell_type": "markdown",
"id": "83c95b41-7deb-40b0-9715-b05b9162943c",
"metadata": {},
"source": [
"## Kopiowanie tablicy"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "8b486fc0-3687-4f52-822c-30189d6944d5",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = arr\n",
"arr[0] = 42"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "72fec8ca-b50d-4321-b4a7-0d1910c22785",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 2, 3, 4, 5])"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "0ef314ce-ee81-468a-bb29-d016b6e7cd87",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 2, 3, 4, 5])"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "aba87a28-096e-4582-93a6-73d094c8e7bd",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = arr.copy()\n",
"arr[0] = 42"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "7465cf19-98c0-42e3-89d1-168496d85e29",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 2, 3, 4, 5])"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "e4886995-8ef8-45f5-ad0c-844e911a5779",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5])"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "59ca4373-c6e5-4185-b7e0-7b046f175e67",
"metadata": {},
"outputs": [],
"source": [
"import copy"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "5b28c96c-9045-4c2b-bc2c-3ea5d7e9a48b",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = copy.deepcopy(arr)\n",
"arr[0] = 42"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "f2ea8a83-3f8f-496f-8f7a-0d535c62baa5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([42, 2, 3, 4, 5])"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "e91cc629-b20b-44b1-bd3d-6c7fc571abb8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4, 5])"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "5ffbd6ac-e303-4eac-be61-4fdb0b3776a7",
"metadata": {},
"outputs": [],
"source": [
"## Nowe"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "96731688-9700-49f4-90f1-2d5a741f0206",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4])"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr = np.array([1, 2, 3, 4])\n",
"arr"
]
},
{
"cell_type": "code",
"execution_count": 53,
"id": "460cc1d9-6685-431b-999c-08d8dc373033",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 2, 3, 4], ndmin=2)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "06c805f4-0be2-48e8-b161-84a56598c3ba",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3, 4]])"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "9840fea8-e874-4c1b-b658-59a9dcfd0b73",
"metadata": {},
"outputs": [
{
"ename": "IndexError",
"evalue": "index 3 is out of bounds for axis 0 with size 1",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[55], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m arr[\u001b[38;5;241m3\u001b[39m]\n",
"\u001b[0;31mIndexError\u001b[0m: index 3 is out of bounds for axis 0 with size 1"
]
}
],
"source": [
"arr[3]"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "2563e811-a4e4-4f6d-bef7-379ff24b1624",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4])"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.squeeze()"
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "9ca46070-ed05-4f09-8a51-bdd2ed121af9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr.squeeze()[3]"
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "ff357a34-afb3-4cb1-b898-33708b1ad9e0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3, 4]])"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "63318611-23d1-413a-bda7-d09cdf240ae2",
"metadata": {},
"outputs": [],
"source": [
"arr = np.array([1, 2, 3, 4])"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "245ae6b2-ee99-4537-bea6-3fbca70636ca",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 4])"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 61,
"id": "6a2c2e36-2b01-473e-b912-c1d042eaf4fe",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2, 3, 4]])"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.expand_dims(arr, axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"id": "dab2689b-fefb-4495-a958-61472d562ff1",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1],\n",
" [2],\n",
" [3],\n",
" [4]])"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.expand_dims(arr, axis=1)"
]
},
{
"cell_type": "markdown",
"id": "ed9bae4d-900f-4f09-b2eb-09155bc3ef9e",
"metadata": {},
"source": [
"### Zadanie 4"
]
},
{
2024-12-07 12:04:28 +01:00
"cell_type": "markdown",
"id": "d92593f8-0ddc-493c-b033-da1fdf3263eb",
2024-12-07 11:54:47 +01:00
"metadata": {},
"source": [
"### Zadanie 4\n",
"\n",
"\n",
"1. Utwórz tablicę NumPy zawierającą liczby [1, 2, 3, 4, 5]. Przypisz ją do zmiennej `x` i zmień pierwszy element tablicy na 50. Wyświetl:\n",
" - Tablicę `arr`,\n",
" - Tablicę `x`.\n",
"\n",
"2. Zrób kopię tablicy `arr` za pomocą metody `.copy()` i zmień pierwszy element tablicy `arr` na 50. Wyświetl:\n",
" - Tablicę `arr`,\n",
" - Tablicę `x` (po kopii).\n",
"\n",
"3. Zrób kopię tablicy `arr` przy użyciu modułu `copy.deepcopy` i zmień pierwszy element tablicy `arr` na 50. Wyświetl:\n",
" - Tablicę `arr`,\n",
" - Tablicę `x` (po głębokiej kopii).\n",
"\n",
"4. Utwórz tablicę NumPy o wymiarach (1,4) zawierającą elementy \\( [1, 2, 3, 4, 5] \\) i wykonaj następujące operacje:\n",
" - Wyświetl tablicę.\n",
2024-12-07 12:04:28 +01:00
" - Zmień tablicę, aby była jednowymiarowa za pomocą metody `.squeeze()`\n"
2024-12-07 11:54:47 +01:00
]
},
{
"cell_type": "markdown",
"id": "4562728a-8fea-4643-ac20-4880ee264e5c",
"metadata": {},
"source": [
"### Reshape jeszcze raze"
]
},
{
"cell_type": "code",
"execution_count": 64,
"id": "6b10a6a1-2f81-41a4-8466-222990ac904c",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 1 2 3 4 5 6 7 8 9 10 11 12]\n",
"[[ 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",
"print(arr)\n",
"newarr = arr.reshape(4, 3)\n",
"print(newarr)"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "8c982360-8838-4036-ac05-92069f789a30",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "cannot reshape array of size 12 into shape (4,2)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[65], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m newarr \u001b[38;5;241m=\u001b[39m arr\u001b[38;5;241m.\u001b[39mreshape(\u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m2\u001b[39m)\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(newarr)\n",
"\u001b[0;31mValueError\u001b[0m: cannot reshape array of size 12 into shape (4,2)"
]
}
],
"source": [
"newarr = arr.reshape(4, 2)\n",
"print(newarr)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "5ff60614-82d0-4421-9ebf-7b0598cda361",
"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": [
"newarr = arr.reshape(4, -1)\n",
"print(newarr)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "e951ca90-8d1e-4ca8-8196-f00db99cf866",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1 2]\n",
" [ 3 4]\n",
" [ 5 6]\n",
" [ 7 8]\n",
" [ 9 10]\n",
" [11 12]]\n"
]
}
],
"source": [
"newarr = arr.reshape(-1, 2)\n",
"print(newarr)"
]
},
{
"cell_type": "markdown",
"id": "d9ed66d4-9440-4076-8ee0-9855238762bf",
"metadata": {},
"source": [
"### Zadanie 5\n",
"\n",
"1. Utwórz tablicę NumPy zawierającą liczby [1, 2, 3, ..., 10] o wymiarach (2,5). Wyświetl:\n",
" - Tablicę (2,5)\n",
"\n",
"2. Zmień kształt tablicy na (5,2) za pomocą metody `reshape`. Wyświetl ją.\n",
"\n",
"3. Zmień kształt tablicy na (10,1) za pomocą metody `reshape`. Wyświetl ją\n",
" \n",
"4. Użyj wartości `-1` w jednym z wymiarów w metodzie `reshape`, aby automatycznie dostosować pozostałe wymiary na (5,-1)\n",
"\n",
"5. Utwórz tablicę o wymiarach (5,1,2,1) na podstawie powyższego przykładu"
]
},
{
"cell_type": "markdown",
"id": "7927210e-feb0-4e61-87a4-7bb9b8b76b17",
"metadata": {},
"source": [
"## Obliczenia wektorowe i macierzowe"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "5b8fd161-d367-47b7-a558-2ac275a7a308",
"metadata": {},
"outputs": [],
"source": [
"x = np.array([1,2,4])\n",
"y = np.array([100,101,102])"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "ef246bbf-66dc-42cd-b851-c56d477a4879",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([101, 103, 106])"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x+y"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "3085a99f-9461-4f7d-a141-d051fd670866",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([101, 102, 104])"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x+100"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "25d19f04-240b-4054-a8d0-e2cbf3056db1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([100, 200, 400])"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x*100"
]
},
{
"cell_type": "code",
"execution_count": 72,
"id": "6e71e5a0-427a-4d3d-93cf-f6e45cd69226",
"metadata": {},
"outputs": [],
"source": [
"x = np.array([[1,2,4], [10,11,12]])"
]
},
{
"cell_type": "code",
"execution_count": 73,
"id": "67e64578-3574-44c4-8f9d-d116412bc83e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 4],\n",
" [10, 11, 12]])"
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"id": "cdf9ffb5-6ce3-400a-bf8f-0f364f17fefc",
"metadata": {},
"source": [
"#### element-wise multiplication"
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "d182d4a9-d654-4f07-8577-b367a9892860",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 100, 200, 400],\n",
" [1000, 1100, 1200]])"
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x*100"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "db4ec9ff-7ca9-4d63-90cd-a513b3bb59ea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 4],\n",
" [10, 11, 12]])"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 76,
"id": "505e245c-d4e6-45ea-b932-0e4de1327fc4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([100, 101, 102])"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": 77,
"id": "7c80b3f2-4dc9-4662-9036-1d736ecb6af1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 4, 16],\n",
" [100, 121, 144]])"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x*x"
]
},
{
"cell_type": "code",
"execution_count": 78,
"id": "a49e39b2-f64e-4cbe-9fe2-cac29f217637",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 100, 202, 408],\n",
" [1000, 1111, 1224]])"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x*y"
]
},
{
"cell_type": "markdown",
"id": "75af9e4a-2d8f-4c77-8dac-f25c09c549f5",
"metadata": {},
"source": [
"#### mnożenie macierzy, iloczny skalarny\n",
"dot product- iloczyn skaralny dla wektorów \n",
"mnożenie macierzowe - dla macierzy"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "79413af7-c8b2-4cae-9979-992782c5ad74",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([100, 101, 102])"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "6bc6192b-fbce-4eb0-95de-e6e6bd6729a2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"30605"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y.dot(y)"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "8d2f6687-f4cc-450a-bd5c-ec9ddf220cb6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"30605"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y@y"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "fc5fd3fe-6b34-409f-ad34-25828988f0b2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"30605"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.matmul(y,y)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "640e1c27-4ab9-4cb7-bd00-a895fdcc5476",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 4],\n",
" [10, 11, 12]])"
]
},
"execution_count": 83,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "49d9ff43-3e93-4836-b87f-7095b2667854",
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[84], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m x\u001b[38;5;241m.\u001b[39mdot(x)\n",
"\u001b[0;31mValueError\u001b[0m: shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0)"
]
}
],
"source": [
"x.dot(x)"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "9e5a2c8f-d398-4246-b215-8c5fb5e1da00",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(2, 3)"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.shape"
]
},
{
"cell_type": "code",
"execution_count": 86,
"id": "c31c73f0-4848-43fa-97a7-fb5522a87862",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3,)"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y.shape"
]
},
{
"cell_type": "code",
"execution_count": 87,
"id": "47e8851e-47a2-4453-aee5-c1e3bdd01541",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 710, 3335])"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.dot(y)"
]
},
{
"cell_type": "code",
"execution_count": 88,
"id": "3b5ed3a6-591a-4181-b4eb-efe0c85a852c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 710, 3335])"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x @ y"
]
},
{
"cell_type": "code",
"execution_count": 89,
"id": "a1f68122-808c-40df-948f-08fa3993f336",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 710, 3335])"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.matmul(x,y)"
]
},
{
"cell_type": "code",
"execution_count": 90,
"id": "6d4ed5fa-6537-418e-b648-a64c14ae40cf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 4],\n",
" [10, 11, 12]])"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 91,
"id": "13ed3203-b5b8-47dc-a471-0eda865a8eed",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2, 4],\n",
" [11, 12]])"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:,1:]"
]
},
{
"cell_type": "code",
"execution_count": 92,
"id": "d7991eb9-2774-4424-aa19-373456fe18bb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 48, 56],\n",
" [154, 188]])"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.matmul(x[:,1:] , x[:,1:])"
]
},
{
"cell_type": "code",
"execution_count": 93,
"id": "74d2da83-8cf3-46f4-859d-6694fce7f194",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 4],\n",
" [10, 11, 12]])"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 94,
"id": "06981ab2-4b03-4dcd-87d2-f4f689e75f2d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 10],\n",
" [ 2, 11],\n",
" [ 4, 12]])"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.T"
]
},
{
"cell_type": "markdown",
"id": "aaf38f60-7ef3-47c9-922c-64016e35196e",
"metadata": {},
"source": [
"### Zadanie 6\n",
"\n",
"\n",
"1. Utwórz dwie tablice jednowymiarowe `x` i `y`:\n",
" - `x` zawiera liczby \\( [3, 5, 7] \\),\n",
" - `y` zawiera liczby \\( [50, 60, 70] \\).\n",
"\n",
"2. Wykonaj następujące operacje i wyświetl wyniki:\n",
" - Dodaj tablicę `x` i `y`.\n",
" - Dodaj do każdego elementu tablicy `x` liczbę 10.\n",
" - Pomnóż każdy element tablicy `x` przez 5.\n",
"\n",
"3. Utwórz dwuwymiarową tablicę `z` zawierającą:\n",
2024-12-07 12:04:28 +01:00
" [[ 3 5 7 ], [8,10,12], [10,10,10]]\n",
" \n",
2024-12-07 11:54:47 +01:00
" Następnie wykonaj następujące operacje:\n",
" - Pomnóż macierzowo x i z\n",
" - Oblicz iloczyn skalrny x i y\n"
]
},
{
"cell_type": "code",
2024-12-07 12:04:28 +01:00
"execution_count": 2,
2024-12-07 11:54:47 +01:00
"id": "eb34ddb2-6f92-4444-87dd-00fb392d416e",
"metadata": {},
"outputs": [],
"source": [
"## Tworzenie macierzy zerowych, jednostkowych, itp"
]
},
{
"cell_type": "code",
2024-12-07 12:04:28 +01:00
"execution_count": 6,
2024-12-07 11:54:47 +01:00
"id": "de2ca8df-31fe-4fef-ba7f-bd2fa2bfa766",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
]
},
2024-12-07 12:04:28 +01:00
"execution_count": 6,
2024-12-07 11:54:47 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.zeros((10,10))"
]
},
{
"cell_type": "code",
2024-12-07 12:04:28 +01:00
"execution_count": 7,
2024-12-07 11:54:47 +01:00
"id": "e5850227-6736-4867-ba93-6d56685d6d64",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])"
]
},
2024-12-07 12:04:28 +01:00
"execution_count": 7,
2024-12-07 11:54:47 +01:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones((10,10))"
]
},
{
"cell_type": "code",
"execution_count": 98,
"id": "69f3a9e8-5ffa-4eb6-916f-d89bab25438a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[123., 123., 123., 123., 123., 123., 123., 123., 123., 123.],\n",
" [123., 123., 123., 123., 123., 123., 123., 123., 123., 123.],\n",
" [123., 123., 123., 123., 123., 123., 123., 123., 123., 123.],\n",
" [123., 123., 123., 123., 123., 123., 123., 123., 123., 123.],\n",
" [123., 123., 123., 123., 123., 123., 123., 123., 123., 123.],\n",
" [123., 123., 123., 123., 123., 123., 123., 123., 123., 123.],\n",
" [123., 123., 123., 123., 123., 123., 123., 123., 123., 123.],\n",
" [123., 123., 123., 123., 123., 123., 123., 123., 123., 123.],\n",
" [123., 123., 123., 123., 123., 123., 123., 123., 123., 123.],\n",
" [123., 123., 123., 123., 123., 123., 123., 123., 123., 123.]])"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.ones((10,10)) * 123"
]
},
{
"cell_type": "code",
"execution_count": 99,
"id": "7f9568c6-cb77-452c-9298-cfa4229157fc",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]])"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.eye(10)"
]
},
{
"cell_type": "code",
"execution_count": 100,
"id": "6fda600b-9422-45b2-a593-8afe96738a7c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n"
]
}
],
"source": [
"#\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": 101,
"id": "af0eb0d3-4ec1-41c4-9860-63cdd54b818b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n"
]
}
],
"source": [
"#\n",
"arr = np.array([[1, 2, 3], [4, 5, 6]])\n",
"\n",
"for i in range(len(arr)):\n",
" for j in range(len(arr[i])):\n",
" print(arr[i,j])"
]
}
],
"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
}