处理的数据:
Position,Level,Salary
Business Analyst,1,45000
Junior Consultant,2,50000
Senior Consultant,3,60000
Manager,4,80000
Country Manager,5,110000
Region Manager,6,150000
Partner,7,200000
Senior Partner,8,300000
C-level,9,500000
CEO,10,1000000
feature Scaling:
1、从常识来看salary跟position是基本没关系,去除position这一列
2、level与salary之间数值差异很大,需要feature Scaling:具体处理方式这里使用sklearn.preprocessing 中的StandardScaler预处理level、salary这两列。当模型训练好之后,使用模型预测某个具体level的salary。最后需要进行Scaling的逆操作——inverse_transform(predicd_value)
fitting SVR to the dataset:
sklearn.svm中的SVR
regressor=SVR(kernel='rbf')
regressor.fit(x,y)
进行预测,predict_value=regressor.predict(6.5)
之后可以使用画图工具展示真实值与预测值对比图,使用 matplotlib.pyplot
import matplotlib.pyplot as plt
plt.scatter(x,y,color='red')
plt.plot(x,regressor.predict(x),clolor='blue')
plt.title('Truth or Bluff(SVR)')
plt.xlabel('Position level')
plt.ylable('Salary')
pit.show()
运行结果:
另一种绘图方式:
import numpy as np
x_grid=np.arange(min(x),max(x),0.01) // 起始值是min(x),结束值max(x),步长0.01
x_grid=x_grid.reshape(len(x_grid),1) //形成一个len(x_grid)行1列的matrix
plt.scatter(x,y,color='red')
plt.plot(x_grid,regressor.predict(x_grid),color='blue')
这种绘图方式,用于预测的点增多,从而blue这条预测线更加平滑smoother curve!
王家林老师人工智能AI第9课 老师微信13928463918