-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviet.py
More file actions
59 lines (44 loc) · 1.54 KB
/
viet.py
File metadata and controls
59 lines (44 loc) · 1.54 KB
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
import numpy as np
import pandas as pd
df_house = pd.read_csv("data/housing.csv")
#df_house = df_house.sample(frac=1)
print (df_house.shape)
print (df_house.head(10))
list = ["Avg. Area Income", "Avg. Area House Age", "Avg. Area Number of Rooms", "Avg. Area Number of Bedrooms",
"Area Population"]
X = df_house[list]
print (X.head(10))
y = df_house["Price"]
print (y.head(10))
from sklearn.decomposition import PCA
X = PCA(1).fit_transform(X)
print (X[:10])
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=35)
from sklearn import linear_model
regr = linear_model.LinearRegression().fit(X_train, y_train)
y_pred = regr.predict(X_test)
from sklearn.metrics import mean_squared_error, r2_score
# The coefficients
print('Coefficients: \n', regr.coef_)
print('Bias: \n', regr.intercept_)
# The mean squared error
print('Mean squared error: %.2f'
% mean_squared_error(y_test, y_pred))
# The coefficient of determination: 1 is perfect prediction
print('Coefficient of determination: %.2f'
% r2_score(y_test, y_pred))
import matplotlib.pyplot as plt
plt.scatter(X_train, y_train, color='red')
plt.scatter(X_train, regr.predict(X_train), color='green')
plt.scatter(X_test[:10,:], y_test[:10], color='black')
plt.title('hồi quy tuyến tính cho giá nhà')
plt.xlabel('X')
plt.ylabel('y')
plt.show()
plt.plot([min(y_test), max(y_test)],[min(y_pred),max(y_pred)])
plt.scatter(y_test, y_pred, color='red')
plt.title('Compare')
plt.xlabel('y_test')
plt.ylabel('y_pred')
plt.show()