from sklearn.datasets import load_boston
     boston = load_boston()
     print(boston.keys())

from sklearn.datasets import load_boston
import matplotlib.pyplot as plt

boston = load_boston()
data = boston.data
print(boston.keys())

x = data[:,5]
y = boston.target

plt.scatter(x,y)
plt.plot(x,9*x-30,c = 'r') # y = wx+b
plt.show()
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

boston = load_boston()
data = boston.data

x = data[:,5]
y = boston.target

LineR = LinearRegression()
LineR.fit(x.reshape(-1,1),y)
w = LineR.coef_
b = LineR.intercept_

plt.scatter(x,y)
plt.plot(x,w*x+b,c = 'r') # y = wx+b
plt.show()
import numpy as np
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
boston = load_boston()

x_train, x_test, y_train, y_test = train_test_split(boston.data,boston.target,test_size=0.3)

LineR = LinearRegression()
LineR.fit(x_train,y_train)

x_predict = LineR.predict(x_test)

print("预测的均方误差:", np.mean(x_predict - y_test)**2)
print("模型的分数:",LineR.score(x_test, y_test))

x = boston.data[:,12].reshape(-1,1)
y = boston.target

plt.figure(figsize=(10,6))
plt.scatter(x,y)

LineR2 = LinearRegression()
LineR2.fit(x,y)
y_pred=LineR2.predict(x)
plt.plot(x,y_pred,'r')
plt.show()
posted on 2018-12-19 13:29  詫秺  阅读(103)  评论(0编辑  收藏  举报