Skip to content

Commit 07596b7

Browse files
authored
Add files via upload
1 parent 99ea617 commit 07596b7

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

DecisionRegressionTreeMPG.ipynb

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import numpy as np\n",
10+
"import pandas as pd\n",
11+
"import matplotlib.pyplot as plt\n",
12+
"import seaborn as sns\n"
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": 2,
18+
"metadata": {},
19+
"outputs": [],
20+
"source": [
21+
"data = pd.read_csv(\"mpg.csv\")"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 3,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"data = data[data.horsepower != \"?\"]"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 4,
36+
"metadata": {},
37+
"outputs": [
38+
{
39+
"data": {
40+
"text/plain": [
41+
"mpg float64\n",
42+
"cylinders int64\n",
43+
"displacement float64\n",
44+
"horsepower object\n",
45+
"weight int64\n",
46+
"acceleration float64\n",
47+
"model_year int64\n",
48+
"origin int64\n",
49+
"name object\n",
50+
"dtype: object"
51+
]
52+
},
53+
"execution_count": 4,
54+
"metadata": {},
55+
"output_type": "execute_result"
56+
}
57+
],
58+
"source": [
59+
"data.dtypes"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 5,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"data.horsepower = data.horsepower.astype(\"float\") #object type has been changed to float"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 6,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"from sklearn.metrics import mean_squared_error, r2_score"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": 7,
83+
"metadata": {},
84+
"outputs": [],
85+
"source": [
86+
"from sklearn.model_selection import train_test_split\n",
87+
"df=data\n",
88+
"training_features = [ 'acceleration','origin','displacement','model_year','horsepower']\n",
89+
"target = 'mpg'\n",
90+
"\n",
91+
"X_train, X_test, Y_train, Y_test = train_test_split(df[training_features],\n",
92+
" df[target],\n",
93+
" test_size=0.10, random_state=169)"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": 8,
99+
"metadata": {},
100+
"outputs": [],
101+
"source": [
102+
"from sklearn.tree import DecisionTreeRegressor\n",
103+
"model = DecisionTreeRegressor(criterion='mae',max_depth=6,min_samples_leaf=7,min_samples_split=8)\n",
104+
"\n",
105+
"#criterion can be mse or mae"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 9,
111+
"metadata": {},
112+
"outputs": [
113+
{
114+
"data": {
115+
"text/plain": [
116+
"DecisionTreeRegressor(criterion='mae', max_depth=6, min_samples_leaf=7,\n",
117+
" min_samples_split=8)"
118+
]
119+
},
120+
"execution_count": 9,
121+
"metadata": {},
122+
"output_type": "execute_result"
123+
}
124+
],
125+
"source": [
126+
"model.fit(X_train,Y_train)"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 10,
132+
"metadata": {},
133+
"outputs": [
134+
{
135+
"name": "stdout",
136+
"output_type": "stream",
137+
"text": [
138+
"Mean squared error: 3.66\n",
139+
"R Square score: 0.93\n"
140+
]
141+
}
142+
],
143+
"source": [
144+
"predicted=model.predict(X_test)\n",
145+
"print(\"Mean squared error: %.2f\"\n",
146+
" % mean_squared_error(Y_test, predicted))\n",
147+
"print('R Square score: %.2f' % r2_score(Y_test, predicted))"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"metadata": {},
154+
"outputs": [],
155+
"source": []
156+
}
157+
],
158+
"metadata": {
159+
"kernelspec": {
160+
"display_name": "Python 3",
161+
"language": "python",
162+
"name": "python3"
163+
},
164+
"language_info": {
165+
"codemirror_mode": {
166+
"name": "ipython",
167+
"version": 3
168+
},
169+
"file_extension": ".py",
170+
"mimetype": "text/x-python",
171+
"name": "python",
172+
"nbconvert_exporter": "python",
173+
"pygments_lexer": "ipython3",
174+
"version": "3.8.3"
175+
}
176+
},
177+
"nbformat": 4,
178+
"nbformat_minor": 4
179+
}

0 commit comments

Comments
 (0)