Skip to content

Commit

Permalink
add sklearn linear regression
Browse files Browse the repository at this point in the history
  • Loading branch information
SoroushGhaderi committed Sep 7, 2020
1 parent 44fe535 commit b7d5f48
Showing 1 changed file with 115 additions and 28 deletions.
143 changes: 115 additions & 28 deletions Concrete_Data_Analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\ProgramData\\Anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
" return f(*args, **kwds)\n"
]
}
],
"source": [
"import numpy as np\n",
"import pandas as pd \n",
Expand All @@ -13,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -24,7 +33,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -133,7 +142,7 @@
"4 198.6 132.4 0.0 192.0 0.0 978.4 825.5 360 44.30"
]
},
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -144,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -176,7 +185,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -185,7 +194,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -199,7 +208,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -233,7 +242,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -242,7 +251,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -252,7 +261,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -291,7 +300,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -346,7 +355,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -359,7 +368,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand All @@ -386,7 +395,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -420,7 +429,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 17,
"metadata": {},
"outputs": [
{
Expand All @@ -438,7 +447,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -473,7 +482,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 19,
"metadata": {},
"outputs": [
{
Expand All @@ -491,7 +500,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 20,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -523,7 +532,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -534,7 +543,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -548,7 +557,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -558,7 +567,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -587,7 +596,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -596,7 +605,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -605,7 +614,7 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 27,
"metadata": {},
"outputs": [
{
Expand All @@ -623,7 +632,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 28,
"metadata": {},
"outputs": [
{
Expand All @@ -639,6 +648,84 @@
"print(\"Adjusted R Squared for Linear Regression is: {}\".format(adjusted_r_squared_predicted_with_normal_equation))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Implementation with SKlearn"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import r2_score"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"concrete = pd.read_csv(\"data/Concrete_Data.csv\")\n",
"# Rename the columns\n",
"concrete.columns = [\"cement\", \"slag\", \"ash\", \"water\", \"splast\", \"coarse\", \"fine\", \"age\", \"strength\"]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"#Initializing X and Y according to shape and converting to numpy arrays\n",
"X = concrete.iloc[:,0:number_of_features-1].values\n",
"y = concrete.iloc[:,number_of_features-1:number_of_features].values"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"linear_regreesion= LinearRegression()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"linear_regreesion.fit(X,y)\n",
"model_accuracy= linear_regreesion.predict(X)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6155198704142721"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"r2_score(y, model_accuracy)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit b7d5f48

Please sign in to comment.