{
"cells": [
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Slope is 0.9523809523809523\n",
"intercept term is 14.571428571428573\n",
"Regression equation is 14.571428571428573 + 0.9523809523809523 x\n",
"Mean square error is 36.05714285714286\n",
"R-Square value is 0.8094231350045303\n",
"The maintenance cost is 16.476190476190478\n"
]
}
],
"source": [
"#1\n",
"import numpy as np\n",
"import itertools\n",
"op_hr=[18,6,30,48,6,36,18,18,30,36]\n",
"main_cost=[25,17,48,58,23,40,30,39,40,60]\n",
"mean_op_hr=np.mean(op_hr)\n",
"mean_main_cost=np.mean(main_cost)\n",
"sum1=0\n",
"for (i,j) in zip(op_hr,main_cost):\n",
" val=(i-mean_op_hr)*(j-mean_main_cost)\n",
" sum1=sum1+val\n",
"sum2=0\n",
"for i in op_hr:\n",
" val1=(i-mean_op_hr)**2\n",
" sum2=sum2+val1\n",
"slope=sum1/sum2\n",
"print(\"Slope is\",slope)\n",
"intercept=mean_main_cost-slope*mean_op_hr\n",
"print(\"intercept term is\",intercept)\n",
"print(\"Regression equation is\",intercept,\"+\",slope,\"x\")\n",
"#calculating mean square error\n",
"pred=[]\n",
"for i in op_hr:\n",
" predict=intercept+slope*i\n",
" pred.append(predict)\n",
"error=0\n",
"for (i,j) in zip(main_cost,pred):\n",
" val2=(i-j)**2\n",
" error=error+val2\n",
"mse=error/len(main_cost)\n",
"print(\"Mean square error is\",mse)\n",
"#R-square calculation\n",
"ss_total=0\n",
"for i in main_cost:\n",
" val3=(i-mean_main_cost)**2\n",
" ss_total=ss_total+val3\n",
"r_square=1-(error/ss_total)\n",
"print(\"R-Square value is\",r_square)\n",
"#Prediction when operation hours increases 2 hours\n",
"prediction=intercept+slope*2\n",
"print(\"The maintenance cost is\",prediction)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
"\n",
"\n",
" | \n",
"CRIM | \n",
"ZM | \n",
"INDUS | \n",
"CHAS | \n",
"NOX | \n",
"RM | \n",
"AGE | \n",
"DIS | \n",
"RAD | \n",
"TAX | \n",
"PTRATIO | \n",
"B | \n",
"LSTAT | \n",
"MEDV | \n",
"
\n",
"\n",
"\n",
"\n",
"0 | \n",
"0.00632 | \n",
"18.0 | \n",
"2.31 | \n",
"0 | \n",
"0.538 | \n",
"6.575 | \n",
"65.2 | \n",
"4.0900 | \n",
"1 | \n",
"296.0 | \n",
"15.3 | \n",
"396.90 | \n",
"4.98 | \n",
"24.0 | \n",
"
\n",
"\n",
"1 | \n",
"0.02731 | \n",
"0.0 | \n",
"7.07 | \n",
"0 | \n",
"0.469 | \n",
"6.421 | \n",
"78.9 | \n",
"4.9671 | \n",
"2 | \n",
"242.0 | \n",
"17.8 | \n",
"396.90 | \n",
"9.14 | \n",
"21.6 | \n",
"
\n",
"\n",
"2 | \n",
"0.02729 | \n",
"0.0 | \n",
"7.07 | \n",
"0 | \n",
"0.469 | \n",
"7.185 | \n",
"61.1 | \n",
"4.9671 | \n",
"2 | \n",
"242.0 | \n",
"17.8 | \n",
"392.83 | \n",
"4.03 | \n",
"34.7 | \n",
"
\n",
"\n",
"3 | \n",
"0.03237 | \n",
"0.0 | \n",
"2.18 | \n",
"0 | \n",
"0.458 | \n",
"6.998 | \n",
"45.8 | \n",
"6.0622 | \n",
"3 | \n",
"222.0 | \n",
"18.7 | \n",
"394.63 | \n",
"2.94 | \n",
"33.4 | \n",
"
\n",
"\n",
"4 | \n",
"0.06905 | \n",
"0.0 | \n",
"2.18 | \n",
"0 | \n",
"0.458 | \n",
"7.147 | \n",
"54.2 | \n",
"6.0622 | \n",
"3 | \n",
"222.0 | \n",
"18.7 | \n",
"396.90 | \n",
"5.33 | \n",
"36.2 | \n",
"
\n",
"\n",
"
\n",
"
"
],
"text/plain": [
" CRIM ZM INDUS CHAS NOX RM AGE DIS RAD TAX \\\n",
"0 0.00632 18.0 2.31 0 0.538 6.575 65.2 4.0900 1 296.0 \n",
"1 0.02731 0.0 7.07 0 0.469 6.421 78.9 4.9671 2 242.0 \n",
"2 0.02729 0.0 7.07 0 0.469 7.185 61.1 4.9671 2 242.0 \n",
"3 0.03237 0.0 2.18 0 0.458 6.998 45.8 6.0622 3 222.0 \n",
"4 0.06905 0.0 2.18 0 0.458 7.147 54.2 6.0622 3 222.0 \n",
"\n",
" PTRATIO B LSTAT MEDV \n",
"0 15.3 396.90 4.98 24.0 \n",
"1 17.8 396.90 9.14 21.6 \n",
"2 17.8 392.83 4.03 34.7 \n",
"3 18.7 394.63 2.94 33.4 \n",
"4 18.7 396.90 5.33 36.2 "
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"data=pd.read_csv(\"C:/Users/Hp/Downloads/housing.csv\")\n",
"data.head()"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"x=data.iloc[:,12:13]\n",
"y=data.iloc[:,13:14]"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"#modeling\n",
"from sklearn.linear_model import LinearRegression\n",
"lin_reg=LinearRegression()\n",
"model=lin_reg.fit(x,y)\n",
"pred=model.predict(x)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.5441462975864799\n"
]
}
],
"source": [
"#r-square\n",
"from sklearn.metrics import r2_score\n",
"r2=r2_score(y,pred)\n",
"print(r2)"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"
"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png":...