uczenie-glebokie/l1/.ipynb_checkpoints/lab1-checkpoint.ipynb

95 lines
3.1 KiB
Plaintext
Raw Normal View History

2023-05-06 11:27:31 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "fc2a6be1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Predicted amount of thefts for 50 fires: 100.5454538681846\n",
"Predicted amount of thefts for 100 fires: 195.86844057898603\n",
"Predicted amount of thefts for 200 fires: 386.5144140005889\n"
]
}
],
"source": [
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"data = pd.read_csv('fires_thefts.csv', names = ['fires', 'thefts'])\n",
"\n",
"x = data[['fires']].to_numpy().flatten()\n",
"y = data[['thefts']].to_numpy().flatten()\n",
"\n",
"def gradient_descent(h, cost_fun, theta, x, y, alpha, eps, max_steps = 1000000):\n",
" current_cost = cost_fun(h, theta, x, y)\n",
" log = [[current_cost, theta]]\n",
" m = len(y)\n",
" steps_counter = 0\n",
" while True and steps_counter < max_steps:\n",
" steps_counter += 1\n",
" new_theta = [\n",
" theta[0] - alpha/float(m) * sum(h(theta, x[i]) - y[i]\n",
" for i in range(m)), \n",
" theta[1] - alpha/float(m) * sum((h(theta, x[i]) - y[i]) * x[i]\n",
" for i in range(m))]\n",
" theta = new_theta\n",
" prev_cost = current_cost\n",
" current_cost = cost_fun(h, theta, x, y)\n",
" if abs(prev_cost - current_cost) <= eps:\n",
" break\n",
" log.append([current_cost, theta])\n",
" return theta, log\n",
"\n",
"def J(h, theta, x, y):\n",
" m = len(y)\n",
" return 1.0 / (2 * m) * sum((h(theta, x[i]) - y[i])**2 for i in range(m))\n",
"\n",
"def h(theta, x):\n",
" return theta[0] + theta[1] * x\n",
"\n",
"def mse(expected, predicted):\n",
" m = len(expected)\n",
" if len(predicted) != m:\n",
" raise Exception('Wektory mają różne długości!')\n",
" return 1.0 / (2 * m) * sum((expected[i] - predicted[i])**2 for i in range(m))\n",
"\n",
"best_theta, log = gradient_descent(h, J, [0.0, 0.0], x, y, alpha=0.001, eps=0.0000001, max_steps = 200)\n",
"\n",
"predicted_50 = h(best_theta, 50)\n",
"predicted_100 = h(best_theta, 100)\n",
"predicted_200 = h(best_theta, 200)\n",
"print(f'Predicted amount of thefts for 50 fires: {predicted_50}')\n",
"print(f'Predicted amount of thefts for 100 fires: {predicted_100}')\n",
"print(f'Predicted amount of thefts for 200 fires: {predicted_200}')"
]
}
],
"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.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}