First and machine learning to fight face to face, yet have not learned python. In addition he stumbled on a taste of the magic machine learning, but also the threshold, without more ado, fell wrestle eat bitter. Past difficult at the beginning.
The first is to install, spent a lot for a long time, always being given, Well, I've saved the screenshot evidence of its uniforms
A preliminary understanding of what is characterized label, samples, models, regression and classification (refer blog: https://blog.csdn.net/weixin_41445387/article/details/96024886 )
Height and weight problem
Data visualization may be more directly observed data characteristics
#创建数据集,写入numpy数组
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets, linear_model data=np.array([[152,51],[156,53],[160,54],[164,55],[168,57],[172,60],[176,62],[180,65],[184,69],[188,72]]) print(data.shape) x,y=data[:,0].reshape(-1,1),data[:,1] plt.scatter(x,y,color='black') plt.xlabel('height (cm)') plt.ylabel('weight (kg)') plt.show()
print(data.shape) 数组大小:10行,2列
然后,plt.show() 显示图片,//我简直刘姥姥进大观园,看啥都神奇,居然它就画出图了,虽然云里雾里,不知道发生了啥,反正很厉害的样子
plt.xlabel('height (cm)') //横坐标显示 height (cm)
plt.ylabel('weight (kg)') //纵坐标显示 weight (kg)
用一个线性回归模型来拟合身高-体重的数据
regr = linear_model.LinearRegression() regr.fit(x,y) plt.plot(x,regr.predict(x),color='blue') plt.xlabel('height (cm)') plt.ylabel('weight (kg)') plt.show() print("Standard weight for person with 163 is %.2f"%regr.predict([[163]]))
调用sklearn的线性模型,通过sklearn中的fit(x,y)来实现模型的训练
regr.predict([[163]]) 用模型预测 [163] 的对应标签
%f 表示输出浮点型数 %.2f 表示小数点后保留两位
plt.plot()函数画出一条线
No,pain. No gain. 加油呀