-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.py
153 lines (132 loc) · 3.81 KB
/
code.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
import matplotlib.pyplot as plt
import pandas as pd
import pylab as pl
import numpy as np
%matplotlib inline
import regression_model
"""
df = pd.read_csv('day.csv', encoding='cp1252')
# take a look at the dataset
df.head()
# summarize the data
df.describe()
cdf = df[['season', 'weathersit','temp', 'atemp', 'hum', 'windspeed', 'cnt']]
cdf.head(9)
viz = cdf[['season', 'weathersit','temp', 'atemp', 'hum', 'windspeed', 'cnt']]
viz.hist()
plt.show()
#log transformation:
log_inf=np.log(df['cnt'])
df['log_inf']=log_inf
cdf = df[['season', 'weathersit','temp', 'atemp', 'hum', 'windspeed', 'cnt', 'log_inf']]
cdf.head(9)
plt.scatter(cdf.temp, cdf.cnt, color='blue')
plt.xlabel("temp")
plt.ylabel("cnt")
plt.show()
plt.scatter(cdf.temp, cdf.log_info, color='red')
plt.xlabel("temp")
plt.ylabel("log_info")
plt.show()
plt.scatter(cdf.atemp, cdf.cnt, color='blue')
plt.xlabel("atemp")
plt.ylabel("cnt")
plt.show()
plt.scatter(cdf.atemp, cdf.log_info, color='red')
plt.xlabel("atemp")
plt.ylabel("log_info")
plt.show()
plt.scatter(cdf.hum, cdf.cnt, color='blue')
plt.xlabel("hum")
plt.ylabel("cnt")
plt.show()
plt.scatter(cdf.hum, cdf.log_info, color='red')
plt.xlabel("hum")
plt.ylabel("log_info")
plt.show()
"""
msk = np.random.rand(len(df)) < 0.8
train = cdf[msk]
test = cdf[~msk]
#print(msk)
#print(~msk)
#print(train)
#print(test)
#temperture:
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(test.temp, test.log_inf, color='red')
ax1.scatter(train.temp, train.log_inf, color='blue')
plt.xlabel("temp")
plt.ylabel("answer")
plt.show()
from sklearn import linear_model
regr = linear_model.LinearRegression()
train_x = np.asanyarray(train[['temp']])
train_y = np.asanyarray(train[['log_inf']])
regr.fit (train_x, train_y)
# The coefficients
print ('Coefficients: ', regr.coef_)
print ('Intercept: ',regr.intercept_)
"""
Coefficients: [[1.94455693]]
Intercept: [7.32038657]
"""
plt.scatter(train.temp, train.log_inf, color='blue')
plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-r')
plt.xlabel("temp")
plt.ylabel("answer")
from sklearn.metrics import r2_score
test_x = np.asanyarray(test[['temp']])
test_y = np.asanyarray(test[['log_inf']])
test_y_ = regr.predict(test_x)
print("Mean absolute error: %.2f" % np.mean(np.absolute(test_y_ - test_y)))
print("Residual sum of squares (MSE): %.2f" % np.mean((test_y_ - test_y) ** 2))
print("R2-score: %.4f" % r2_score(test_y , test_y_) )
per = r2_score(test_y , test_y_)*100
print("R2-score percent: %.2f" % per)
"""
Mean absolute error: 0.34
Residual sum of squares (MSE): 0.19
R2-score: 0.3814
R2-score percent: 38.14
"""
#A temperture:
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.scatter(test.atemp, test.log_inf, color='red')
ax1.scatter(train.atemp, train.log_inf, color='blue')
plt.xlabel("atemp")
plt.ylabel("answer")
plt.show()
from sklearn import linear_model
regr = linear_model.LinearRegression()
train_x = np.asanyarray(train[['atemp']])
train_y = np.asanyarray(train[['log_inf']])
regr.fit (train_x, train_y)
# The coefficients
print ('Coefficients: ', regr.coef_)
print ('Intercept: ',regr.intercept_)
"""
Coefficients: [[2.20298942]]
Intercept: [7.23951191]
"""
plt.scatter(train.atemp, train.log_inf, color='blue')
plt.plot(train_x, regr.coef_[0][0]*train_x + regr.intercept_[0], '-r')
plt.xlabel("atemp")
plt.ylabel("answer")
from sklearn.metrics import r2_score
test_x = np.asanyarray(test[['atemp']])
test_y = np.asanyarray(test[['log_inf']])
test_y_ = regr.predict(test_x)
print("Mean absolute error: %.2f" % np.mean(np.absolute(test_y_ - test_y)))
print("Residual sum of squares (MSE): %.2f" % np.mean((test_y_ - test_y) ** 2))
print("R2-score: %.4f" % r2_score(test_y , test_y_) )
per = r2_score(test_y , test_y_)*100
print("R2-score percent: %.2f" % per)
"""
Mean absolute error: 0.34
Residual sum of squares (MSE): 0.18
R2-score: 0.3912
R2-score percent: 39.12
"""