-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalyzing_car_sales_data.py
200 lines (154 loc) · 6.18 KB
/
Analyzing_car_sales_data.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
194
195
196
197
198
199
200
# -*- coding: utf-8 -*-
"""
Quick introduction to the 93cars data.
Here we try to predict MidrangePrice from HighwayMPG.
-Doug Galarus, CS 5665, Spring 2019
"""
import pandas
import random
import numpy as np
import matplotlib.pyplot as plt
import sklearn.linear_model
# Read in the data using Pandas. The result is stored in a Pandas Data Frame.
df = pandas.read_csv("93cars.csv")
def question1():
# Assign to X and y. We have to reshape X to match
# what the subsequent method expects.
X = df['Length'].values.reshape(-1,1)
y = df['MidrangePrice'].values
# Specify the model.
model = sklearn.linear_model.LinearRegression(fit_intercept=True)
# Fit the data to the model.
model.fit(X,y)
# Extract the coeffecients.
print("Bias (intercept) =",model.intercept_)
print("Coefficients (slope) =", model.coef_[0])
# Compute SSE and R-squared.
predicted = model.predict(X)
SSE = ((predicted - y)**2).sum()
print("SSE = ", SSE)
R_sq = model.score(X,y)
print("R-squared = ", R_sq)
# The predict function can be applied to a vector. Here we apply to it a
# sequence of evenly-spaced values corresponding to our x-axis.
xfit = np.linspace(140,230,600).reshape(-1,1)
yfit = model.predict(xfit)
# Plot the data
plt.scatter(X,y)
plt.title('1993 Car Data')
plt.xlabel('Length')
plt.ylabel('MidrangePrice ($10K)')
plt.plot(xfit,yfit)
plt.show()
def question2():
# Assign to X and y. We have to reshape X to match
# what the subsequent method expects.
headers = ['CityMPG', 'HighwayMPG', 'EngineSize', 'Horsepower', 'RPM', 'EngineRevPerMile', 'ManualTransmission',
'FuelTankCapacity', 'PassengerCapacity', 'Length', 'Wheelbase', 'Width', 'UturnSpace', 'Weight',
'Domestic']
best = 0
for h in headers:
if (h!='MinimumPrice' and h!='MaximumPrice'):
X = df[h].values.reshape(-1,1)
y = df['MidrangePrice'].values
# Specify the model.
model = sklearn.linear_model.LinearRegression(fit_intercept=True)
# Fit the data to the model.
model.fit(X,y)
R_sq = model.score(X,y)
if(R_sq > best):
best = R_sq
bestcriteria = h
print('Best criteria that fits MidrangePrice, other than MinimumPrice and MaximumPrice = ',bestcriteria)
def question3():
# Assign to X and y. We have to reshape X to match
# what the subsequent method expects.
headers = ['CityMPG', 'EngineSize', 'Horsepower', 'RPM', 'EngineRevPerMile', 'ManualTransmission',
'FuelTankCapacity', 'PassengerCapacity', 'Length', 'Wheelbase', 'Width', 'UturnSpace', 'Weight',
'Domestic']
best = 0
R_sq_list = []
for h in headers:
if (h!='CityMPG'):
X = df[h].values.reshape(-1,1)
y = df['HighwayMPG'].values
# Specify the model.
model = sklearn.linear_model.LinearRegression(fit_intercept=True)
# Fit the data to the model.
model.fit(X,y)
R_sq = model.score(X,y)
R_sq_list.append([R_sq,h])
R_sq_list.sort(reverse=True)
print('Best 3 criteria that fits HighwayMPG are = {}, {} and {}'.format((R_sq_list[0])[1], (R_sq_list[1])[1], (R_sq_list[2])[1]))
headers = [(R_sq_list[0])[1], (R_sq_list[1])[1], (R_sq_list[2])[1]]
for h in headers:
X = df[h].values.reshape(-1, 1)
y = df['MidrangePrice'].values
# Specify the model.
model = sklearn.linear_model.LinearRegression(fit_intercept=True)
# Fit the data to the model.
model.fit(X, y)
print(h)
# Extract the coeffecients.
print("Bias (intercept) =", model.intercept_)
print("Coefficients (slope) =", model.coef_[0])
# Compute SSE and R-squared.
predicted = model.predict(X)
SSE = ((predicted - y) ** 2).sum()
print("SSE = ", SSE)
R_sq = model.score(X, y)
print("R-squared = ", R_sq,"\n")
def question4to7():
score = "0100001000100001000010001110101100010110000000100010000100001101100001110101000010000100000000011001" # My score
x = []
y = []
for i in range(0,100):
x.append([i + 1])
y.append(int(score[i]))
# Specify the model.
model = sklearn.linear_model.LinearRegression(fit_intercept=True)
# Fit the data to the model.
model.fit(x, y)
# Find test statistic (Slope)
test_statistic = model.coef_[0]
print("Test Statistic (Slope) = ", test_statistic)
test_stat_list = [] # List to store the test statistic for 10000 randomized permutations
iterations = 10000
for j in range(0, iterations):
temp = list(score)
random.shuffle(temp) # Shuffling randomly
x = []
y = []
for i in range(0, 100):
x.append([i + 1])
y.append(int(temp[i]))
# Specify the model.
model = sklearn.linear_model.LinearRegression(fit_intercept=True)
# Fit the data to the model.
model.fit(x, y)
# Find test statistic (Slope)
temp_stat = model.coef_[0]
test_stat_list.append(temp_stat) # Appending current test statistic
hist(test_stat_list)
test_stat_list.sort() # Sorting in ascending
index = np.searchsorted(test_stat_list, test_statistic) # Finding index corresponding to our test statistic
pvalue = 1.0 - (index + 1) / iterations
alpha = 0.05
confidence = 1 - alpha
cutoff = test_stat_list[int(confidence * iterations)]
print('P value = ', pvalue)
print('Alpha value = ', alpha)
print('Cutoff = ', cutoff)
if (test_statistic > cutoff):
print('We reject the null hypothesis. I improved.')
else:
print('We cannot reject the null hypothesis. I did not improve.')
def hist(x):
plt.hist(x, density = True, bins = 50)
plt.xlabel('Slope (Test Statistic)')
plt.ylabel('Frequency')
plt.show()
#question1()
#question2()
#question3()
question4to7()