/
cw_task_two.py
193 lines (153 loc) · 5.23 KB
/
cw_task_two.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import warnings
from sklearn.linear_model import LinearRegression
df = pd.read_csv("car_data.csv", header=0)
x_list = ['Mileage', 'Cylinder', 'Liter', 'Doors',
'Cruise', 'Sound', 'Leather']
def calc_task_two_one():
warnings.warn("deprecated", DeprecationWarning)
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
return model, X, y
with warnings.catch_warnings():
warnings.simplefilter("ignore")
calc_task_two_one()
def print_task_two_one(model):
print("Line of best fit:\nY = ", end='')
for idx in range(len(x_list)):
print('{}*{} + '.format(format(model.coef_[idx], '.2f'),
x_list[idx]), end='')
print('{}'.format(format(model.intercept_, '.2f')))
def print_task_two_two(model, X, y):
print('Coefficient of determination: {0:.2f}'.format(model.score(X, y)))
def print_task_two_three():
missing_liter()
missing_sound()
missing_mileage()
missing_doors()
missing_cylinder()
missing_leather()
missing_cruise()
print("")
missing_lit_sou()
missing_lit_mil()
missing_lit_doo()
missing_lit_lea()
missing_lit_cru()
missing_lit_cyl()
def missing_lit_mil():
x_list = ['Cylinder', 'Doors',
'Cruise', 'Sound', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Liter/Mileage: {0:.2f}'.format(model.score(X, y)))
def missing_lit_cyl():
x_list = ['Mileage', 'Doors',
'Cruise', 'Sound', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Liter/Cylinder: {0:.2f}'.format(model.score(X, y)))
def missing_lit_doo():
x_list = ['Mileage', 'Cylinder',
'Cruise', 'Sound', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Liter/Doors: {0:.2f}'.format(model.score(X, y)))
def missing_lit_cru():
x_list = ['Mileage', 'Cylinder',
'Doors', 'Sound', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Liter/Cruise: {0:.2f}'.format(model.score(X, y)))
def missing_lit_sou():
x_list = ['Mileage', 'Cylinder',
'Doors', 'Cruise', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Liter/Sound: {0:.2f}'.format(model.score(X, y)))
def missing_lit_lea():
x_list = ['Mileage', 'Cylinder',
'Doors', 'Cruise', 'Sound']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Liter/Leather: {0:.2f}'.format(model.score(X, y)))
def missing_leather():
x_list = ['Mileage', 'Cylinder', 'Liter',
'Doors', 'Cruise', 'Sound']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Leather: {0:.2f}'.format(model.score(X, y)))
def missing_sound():
x_list = ['Mileage', 'Cylinder', 'Liter',
'Doors', 'Cruise', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Sound: {0:.2f}'.format(model.score(X, y)))
def missing_cruise():
x_list = ['Mileage', 'Cylinder', 'Liter',
'Doors', 'Sound', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Cruise: {0:.2f}'.format(model.score(X, y)))
def missing_doors():
x_list = ['Mileage', 'Cylinder', 'Liter',
'Cruise', 'Sound', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Doors: {0:.2f}'.format(model.score(X, y)))
def missing_liter():
x_list = ['Mileage', 'Cylinder', 'Doors',
'Cruise', 'Sound', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Liter: {0:.2f}'.format(model.score(X, y)))
def missing_cylinder():
x_list = ['Mileage', 'Liter', 'Doors',
'Cruise', 'Sound', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Cylinder: {0:.2f}'.format(model.score(X, y)))
def missing_mileage():
x_list = ['Cylinder', 'Liter', 'Doors',
'Cruise', 'Sound', 'Leather']
model = LinearRegression()
X = np.array(df[x_list].values)
y = df['Price'].values
model.fit(X, y)
print('R^2 no Mileage: {0:.2f}'.format(model.score(X, y)))
# plt.scatter(X, y, color='b')
# plt.plot(X, model.predict(X), color='r', linewidth=2)
# plt.title('Price vs Mileage', fontsize=20)
# plt.xlabel('Mileage', fontsize=14)
# plt.ylabel('Price', fontsize=14)
# plt.xlim((-5000, 55000))
# plt.ylim((0, 75000))
# plt.show()