Files
PythonProgramozas_GKNB_MSTM032/12_gyak_mego.ipynb
2024-09-21 11:20:44 +02:00

419 lines
8.4 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. feladat\n",
"\n",
"Hozzunk létre egy 3×3-as, csupa True logikai értéket tartalmazó, NumPy tömböt!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True, True],\n",
" [ True, True, True],\n",
" [ True, True, True]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 1. megoldás\n",
"np.array([[True] * 3] * 3)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ True, True, True],\n",
" [ True, True, True],\n",
" [ True, True, True]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 2. megoldás\n",
"np.ones((3, 3), dtype='bool')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. feladat\n",
"\n",
"Írjuk ki a páratlan elemeket az alábbi 2 dimenziós NumPy tömbben!"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3 5 7]\n"
]
}
],
"source": [
"b = np.array([[2, 3, 4], [5, 6, 7]])\n",
"print(b[b % 2 == 1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. feladat\n",
"\n",
"Írjuk ki a 3-nál nagyobb elemeket az alábbi 2 dimenziós NumPy tömbben!"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4 5 6 7]\n"
]
}
],
"source": [
"b = np.array([[2, 3, 4], [5, 6, 7]])\n",
"print(b[b > 3])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. feladat\n",
"\n",
"Írjuk ki az átlag feletti elemeket az alábbi NumPy tömbben!"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10 8 9]\n"
]
}
],
"source": [
"b = np.array([1, 10, 2, 8, 3, 5, 9])\n",
"print(b[b > b.mean()])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. feladat\n",
"\n",
"Készítsünk programot, amely egyenletes eloszlás szerint generál 1000 db térbeli pontot az egységkockában, majd meghatározza az 1. ponttól legtávolabb eső pontot!"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0.37454012, 0.95071431, 0.73199394],\n",
" [0.59865848, 0.15601864, 0.15599452],\n",
" [0.05808361, 0.86617615, 0.60111501],\n",
" ...,\n",
" [0.80000348, 0.55270708, 0.39655368],\n",
" [0.13171503, 0.86529576, 0.15727321],\n",
" [0.30978786, 0.29004553, 0.87141403]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# pontok generálása\n",
"rs = np.random.RandomState(42)\n",
"P = rs.uniform(size=(1000, 3))\n",
"P"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.89633582 0.01300192 0.08550853]\n"
]
}
],
"source": [
"# legtávolabbi pont kiszámítása, 1. megoldás\n",
"\n",
"p = P[0]\n",
"dmax = 0\n",
"for pi in P:\n",
" act = ((p[0] - pi[0])**2 + (p[1] - pi[1])**2 +(p[2] - pi[2])**2)**0.5\n",
" if act > dmax:\n",
" dmax = act\n",
" pmax = pi\n",
"print(pmax)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.89633582, 0.01300192, 0.08550853])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# legtávolabbi pont kiszámítása, 2. megoldás (vektorizálással)\n",
"\n",
"# P: 1000 3\n",
"# p: - 3\n",
"\n",
"P[((P - p)**2).sum(axis=1).argmax()]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 6. feladat\n",
"\n",
"A [baseball.txt](baseball.txt) szövegfájl professzionális baseball játékosok testmagasságáról és testsúlyáról tartalmaz adatokat. Készítsünk programot, amely lineáris modellt ad a testsúly testmagasságból történő előrejelzésére! Részfeladatok:\n",
"- Határozzuk meg a minimális SSE (sum squared error) hibát adó modellparamétert!\n",
"- Adjunk becslést egy 180 cm magas játékos testsúlyára!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"linreg_1d.jpg\" width=\"600\"></img>"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[188., 82.],\n",
" [188., 98.],\n",
" [183., 95.],\n",
" ...,\n",
" [190., 93.],\n",
" [190., 86.],\n",
" [185., 88.]])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Adatok betöltése.\n",
"data = np.genfromtxt('baseball.txt', delimiter=',')\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# bemeneti vektor\n",
"x = data[:, 0]\n",
"\n",
"# célvektor\n",
"y = data[:, 1]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# átlag kivonása\n",
"xm = x.mean()\n",
"ym = y.mean()\n",
"x -= xm\n",
"y -= ym"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0.92836399, 0.92836399, -4.07163601, ..., 2.92836399,\n",
" 2.92836399, -2.07163601])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-9.50338819, 6.49661181, 3.49661181, ..., 1.49661181,\n",
" -5.50338819, -3.50338819])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.8561919786085497"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Optimális modellparaméter.\n",
"w = (x @ y) / (x @ x)\n",
"w"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"85.4487101609531"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Becslés egy 180 cm magas játékos testsúlyára.\n",
"(180 - xm) * w + ym"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}