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

312 lines
6.2 KiB
Plaintext
Raw Normal View History

2024-12-07 11:54:47 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "23ed41a0-7a05-493e-a640-4bfb10c42164",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fa3799c5-d3a0-4967-98d4-a340d19dbfc6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10 11 12 13 14 15 16 17 18 19 20]\n",
"(11,)\n",
"<class 'numpy.ndarray'>\n"
]
}
],
"source": [
"#Zadanie 1.1\n",
"# Tworzenie tablicy jednowymiarowej\n",
"arr = np.array([10,11,12,13,14,15,16,17,18,19,20])\n",
"print(arr)\n",
"print(arr.shape)\n",
"print(type(arr))\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b6b4fa7d-7ee5-416c-8060-39057b49d77b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[10 20]\n",
" [30 40]\n",
" [50 60]]\n",
"(3, 2)\n",
"<class 'numpy.ndarray'>\n"
]
}
],
"source": [
"# Zadanie 1.2\n",
"arr = np.array([[10, 20], [30, 40], [50, 60]])\n",
"\n",
"print(arr)\n",
"\n",
"print(arr.shape)\n",
"\n",
"print(type(arr))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f96f774c-d6cd-440f-b6bf-a2d373404de3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n",
"8\n",
"[[7 8 9]]\n",
"[3 6 9]\n",
"[[5 6]\n",
" [8 9]]\n"
]
}
],
"source": [
"# Zadanie 2\n",
"# Tworzenie dwuwymiarowej tablicy\n",
"arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"\n",
"print(arr)\n",
"\n",
"\n",
"print(arr[2, 1])\n",
"\n",
"print(arr[2:])\n",
"\n",
"\n",
"print(arr[:,2])\n",
"\n",
"print(arr[1:,1:])\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "43216855-9d5d-4d03-9512-557f4d228571",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10 20 30 40]\n",
"int64\n",
"[10. 20. 30. 40.]\n",
"float32\n",
"['Python' 'NumPy' 'Coding']\n",
"<U6\n"
]
}
],
"source": [
"# Zadanie 3\n",
"\n",
"# Punkt 1\n",
"arr = np.array([10, 20, 30, 40])\n",
"print(arr)\n",
"print(arr.dtype)\n",
"\n",
"# Punkt 2\n",
"arr = arr.astype('float32')\n",
"print(arr)\n",
"print(arr.dtype)\n",
"\n",
"# Punkt 3\n",
"arr = np.array([\"Python\", \"NumPy\", \"Coding\"])\n",
"print(arr)\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0a700a92-fb6e-498d-bf2f-d5f9758d0147",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[50 2 3 4 5]\n",
"[50 2 3 4 5]\n",
"[50 2 3 4 5]\n",
"[1 2 3 4 5]\n",
"[50 2 3 4 5]\n",
"[[1 2 3 4]]\n",
"[1 2 3 4]\n"
]
}
],
"source": [
"### Zadanie 4\n",
"import copy\n",
"\n",
"\n",
"# Punkt 1 - Przypisanie do zmiennej\n",
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = arr\n",
"arr[0] = 50\n",
"print(arr) # Tablica arr po zmianie\n",
"print(x) # Tablica x po zmianie\n",
"\n",
"# Punkt 2 - Kopia tablicy\n",
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = arr.copy()\n",
"arr[0] = 50\n",
"print(arr) # Tablica arr po zmianie\n",
"print(x) # Tablica x po kopii\n",
"\n",
"# Punkt 3 - Głęboka kopia\n",
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = copy.deepcopy(arr)\n",
"arr[0] = 50\n",
"print(arr) # Tablica arr po zmianie\n",
"\n",
"arr2 = np.array([1, 2, 3, 4], ndmin=2)\n",
"print(arr2)\n",
"\n",
"# Zmiana wymiaru na jednowymiarowy\n",
"arr_squeezed = arr2.squeeze()\n",
"print(arr_squeezed)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "fbf7f5ee-aace-47f6-9a73-14fdf7595ff4",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[ 1],\n",
" [ 2]]],\n",
"\n",
"\n",
" [[[ 3],\n",
" [ 4]]],\n",
"\n",
"\n",
" [[[ 5],\n",
" [ 6]]],\n",
"\n",
"\n",
" [[[ 7],\n",
" [ 8]]],\n",
"\n",
"\n",
" [[[ 9],\n",
" [10]]]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## Zadanie 5\n",
"\n",
"\n",
"arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])\n",
"\n",
"arr\n",
"\n",
"arr.reshape(5,2)\n",
"\n",
"arr.reshape(10,1)\n",
"\n",
"arr.reshape(5,-1)\n",
"\n",
"arr.reshape(5,1,2,1)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5afcad53-ce4a-408d-bc44-f33fd7b8e276",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[53 65 77]\n",
"[13 15 17]\n",
"[15 25 35]\n",
"[[ 3 5 7]\n",
" [ 8 10 12]\n",
" [10 10 10]]\n",
"[119 135 151]\n",
"940\n"
]
}
],
"source": [
"### Zadanie 6\n",
"\n",
"\n",
"x = np.array([3, 5, 7])\n",
"y = np.array([50, 60, 70])\n",
"\n",
"print(x + y)\n",
"print(x + 10)\n",
"print(x * 5)\n",
"z = np.array([[3, 5, 7], [8, 10, 12], [10,10,10]])\n",
"print(z)\n",
"\n",
"print(x.dot(z))\n",
"print(x.dot(y))"
]
}
],
"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
}