-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsklearn1.py
43 lines (34 loc) · 1014 Bytes
/
sklearn1.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
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metric import mean_squared_error, r2_score
from sklearn import datasets
%matplotlib inline
# load dataset
diabetes = datasets.load_diabetes()
# split into X and y
X = diabetes.data[:, np.newaxis, 2]
y = diabetes.target[:]
# split dataset randomly
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size = 0.3, random_state = 2)
# initialize model
model = LinearRegression()
# fit trainig set into the model
model.fit(X_train, y_train)
# predict values
prediction = model.predict(X_val)
# get mse
mse = mean_squared_error(prediction, y_val)
# get variance
variance = r2_score(prediction, y_val)
print('MSE : ', mse)
print('Variance :' variance)
# plot graph
# plot value points
plt.scatter(X_val, y_val, color = 'black')
# plot the straight line
plt.plot(X_val, prediction, color = 'red')
plt.xticks()
plt.yticks()
plt.show()