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

#print(boston.DESCR)

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

import matplotlib.pyplot as plt
plt.scatter(x,y)
plt.plot(x,x*9-30,'y')
plt.show()

12.10作业

12.10作业

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
x = x.reshape(-1,1)
lr.fit(x,y)
w = lr.coef_         #y=wx+b,w为斜率,b为截距
b = lr.intercept_
print(w)
print(b)

 12.10作业

 

import matplotlib.pyplot as plt
x = boston.data[:,10].reshape(-1,1)
y = boston.target
plt.figure(figsize=(8,8))
plt.scatter(x,y)

from sklearn.linear_model import LinearRegression
lineR=LinearRegression()
lineR.fit(x,y)
y_pred = lineR.predict(x)
plt.plot(x,y_pred)

plt.show()

12.10作业

from sklearn.preprocessing import PolynomialFeatures
poly=PolynomialFeatures(degree=2)
x_poly=poly.fit_transform(x)

lrp=LinearRegression()
lrp.fit(x_poly,y)
y_poly_pred=lrp.predict(x_poly)
plt.scatter(x,y)
plt.scatter(x,y_pred)
plt.scatter(x,y_poly_pred)
plt.show()

12.10作业

 

相关文章:

  • 2021-07-20
  • 2021-07-11
  • 2022-12-23
  • 2021-10-11
  • 2022-01-23
  • 2021-07-27
猜你喜欢
  • 2021-12-12
  • 2021-09-29
  • 2021-08-24
  • 2021-05-29
  • 2021-09-18
  • 2022-01-08
  • 2022-03-09
相关资源
相似解决方案