day11-SVM

今天学习了SVM的基本思想

通过代码实现认识了SVM,并举例用sklearn中的SVC库函数来实现人脸识别,用SVR预测波士顿地区的房价。

代码实现链接如下:SVM算法思想

其中用SVR预测波士顿地区房价的代码如下:

from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
from sklearn.metrics import r2_score,mean_squared_error,mean_absolute_error
import numpy as np

boston =load_boston()
#print(boston.DESCR)
x=boston.data
y=boston.target

x_train,x_test,y_train,y_test =train_test_split(x,y,test_size =0.25,random_state=42)

#训练数据和测试数据标准化
scale =StandardScaler()
x_train =scale.fit_transform(x_train)
x_test =scale.transform(x_test)
y_train =scale.fit_transform(y_train.reshape(-1,1))
y_test =scale.transform(y_test.reshape(-1,1))

#线性核函数配置支持向量机
linear_svr =SVR(kernel ='linear')
#训练
linear_svr.fit(x_train,y_train.ravel())
#预测,保存预测结果
linearpredict =linear_svr.predict(x_test)
#模型评估
print("默认评估值为:",linear_svr.score(x_test,y_test))
print("R_squared值为:",r2_score(y_test,linear_svr_v_predict))
print("均方误差为:",mean_squared_error(scale.inverse_transform(y_test),scale.inverse_transform(linear_svr_v_predict)))
print("平均绝对误差为:",mean_absolute_error(scale.inverse_transform(y_test),scale.inverse_transform(linearpredict)))

你可能感兴趣的:(day11-SVM)