博客开通第一天-仅为代码学习

hello world

 

 1 import numpy as np
 2 from sklearn import linear_model
 3 from mpl_toolkits.mplot3d import Axes3D
 4 import matplotlib.pyplot as plt
 5 xx, yy = np.meshgrid(np.linspace(0,10,10), np.linspace(0,100,10))
 6 zz = 1.0 * xx + 3.5 * yy + np.random.randint(0,100,(10,10))
 7 # 构建成特征、值的形式
 8 X, Z = np.column_stack((xx.flatten(),yy.flatten())), zz.flatten()
 9 # 建立线性回归模型
10 regr = linear_model.LinearRegression()
11 # 拟合
12 regr.fit(X, Z)
13 # 不难得到平面的系数、截距
14 a, b = regr.coef_, regr.intercept_
15 # 给出待预测的一个特征
16 x = np.array([[5.8, 78.3]])
17 # 方式1:根据线性方程计算待预测的特征x对应的值z(注意:np.sum)
18 print(np.sum(a * x) + b)
19 # 方式2:根据predict方法预测的值z
20 print(regr.predict(x))
21 # 画图
22 fig = plt.figure()
23 ax = fig.gca(projection='3d')
24 # 1.画出真实的点
25 ax.scatter(xx, yy, zz)
26 # 2.画出拟合的平面
27 ax.plot_wireframe(xx, yy, regr.predict(X).reshape(10,10))
28 ax.plot_surface(xx, yy, regr.predict(X).reshape(10,10), alpha=0.3)
29 plt.show()

 

posted @ 2020-07-01 18:36  林子鸟  阅读(51)  评论(0)    收藏  举报