-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtan.py
134 lines (124 loc) · 4.26 KB
/
tan.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
# Reading the dataset
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import accuracy_score
import numpy as np
import time
#đọc dữ liệu
df = pd.read_csv("day.csv")
print(df.shape)
print(df.head())
# Kiểu dữ liệu mỗi cột
print(df.dtypes)
print("\n")
#Lựa chọn các thuộc tính, df.drop ở đây là loại bỏ các thuộc tính được liệt kê ở đây
df = df.drop(columns=['dteday','instant','season','yr','weathersit','casual','registered'])
#show các thuộc tính còn lại
df.hist(figsize=(12,10))
plt.show()
#Cnt vs Temp
plt.scatter(df['temp'],df['cnt'])
plt.suptitle('Số lượng xe được thuê với nhiệt độ')
plt.xlabel('Nhiệt độ (Temp)')
plt.ylabel('Số lượng xe đạp được thuê (Cnt)')
plt.show()
#Cnt vs atemp
plt.scatter(df['atemp'], df['cnt'])
plt.suptitle('Số lượng xe được thuê với nhiệt độ không khí')
plt.xlabel('Nhiệt độ không khí (Atemp)')
plt.ylabel('Số lượng xe đạp được thuê (Cnt)')
plt.show()
#Cnt vs hum
plt.scatter(df['hum'], df['cnt'])
plt.suptitle('Số lượng xe được thuê với độ ẩm')
plt.xlabel('Độ ẩm (Hum)')
plt.ylabel('Số lượng xe đạp được thuê (Cnt)')
plt.show()
#cnt vs windspedd
plt.scatter(df['windspeed'], df['cnt'])
plt.suptitle('Số lượng xe được thuê với tốc độ gió')
plt.xlabel('Tốc độ gió (Windspeed)')
plt.ylabel('Số lượng xe đạp được thuê (Cnt)')
plt.show()
# Normalizing the data
from sklearn import preprocessing
#gán x = dataframe nhưng loại bỏ cnt vì đó là nhãn
x=df.drop(['cnt'],axis=1)
print(x)
#gán nhãn cho y = cnt
y=df['cnt']
print(y)
x = preprocessing.normalize(x)
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
#Phân chia tập dữ liệu
from sklearn.model_selection import train_test_split
start_time = time.time()
for i in range(100):
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = i)
#Hồi quy tuyến tính
linearRegressor = LinearRegression()
linearRegressor.fit(x_train, y_train)
y_predicted = linearRegressor.predict(x_test)
end_time = time.time()
mse = mean_squared_error(y_test, y_predicted)
rmse = np.sqrt(mse)
acc0 = linearRegressor.score(x_test, y_test)*100
mae = mean_absolute_error(y_test,y_predicted)
print("Hoi quy tuyen tinh")
print("Mean Squared Error:",mse)
print("Root Mean Squared Error:", rmse)
print("Do chinh xac:",acc0)
print("Mean Absolute Error:",mae)
print ("Thoi Gian Thuc Thi: %.4f (s)" % (end_time - start_time))
print("\n")
plt.title("Hồi quy tuyến tính")
plt.scatter(y_test,y_predicted)
plt.xlabel("Test")
plt.ylabel("Predict")
plt.grid(True)
plt.show()
# Cây quyết định
from sklearn.tree import DecisionTreeRegressor
start_time = time.time()
for i in range(100):
regressor = DecisionTreeRegressor(random_state = i)
regressor.fit(x_train, y_train)
y_predicted_d = regressor.predict(x_test)
end_time = time.time()
mse = mean_squared_error(y_test, y_predicted_d)
rmse = np.sqrt(mse)
acc1 = regressor.score(x_test,y_test)*100
mae = mean_absolute_error(y_test,y_predicted_d)
rmse = np.sqrt(mse)
print("Cay quyet dinh")
print("Mean Squared Error:",mse)
print("Root Mean Squared Error:", rmse)
print("Do chinh xac:",acc1)
print("Mean Absolute Error:",mae)
print ("Thoi gian thuc thi: %.4f (s)" % (end_time - start_time))
print("\n")
plt.title("Cây quyết định")
plt.scatter(y_test,y_predicted_d)
plt.xlabel("Test")
plt.ylabel("Predict")
plt.grid(True)
plt.show()
from prettytable import PrettyTable
table = PrettyTable()
table.field_names = ["Giai Thuat", "MSE","RMSE" ,"MAE","Do chinh xac"]
models = [
LinearRegression(),
DecisionTreeRegressor(random_state = 100)
]
for model in models:
model.fit(x_train, y_train)
y_res = model.predict(x_test)
mse = mean_squared_error(y_test, y_res)
score = model.score(x_test, y_test)*100
mae = mean_absolute_error(y_test,y_res)
rmse = np.sqrt(mse)
table.add_row([type(model).__name__, format(mse, '.2f'),format(rmse, '.2f'),format(mae, '.2f'),format(score, '.2f')])
plt.show()
print(table)