线性回归模型:
流程:
读取数据,划分训练测试数据集,生成模型实例(SVR),预测,计算其loss值。
训练数据集拟合data与label之间的关系。
代码示例如下所示:
导入相应的库函数
# from sklearn.model_selection import train_test_split
# from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error, mean_absolute_error
import numpy as np
import json
读取数据、划分数据集等操作
train_data = json.load(open('./train_file_name'))
X = np.asarray(train_data)
#label data
test_data = json.load(open('test_file_name'))
y = np.asarray(test_data)
def mape(y_ture,y_label):
return np.mean(np.abs((y_true - y_label) / y_label))
x_train = x[:10000,:]
x_test = x[10000:,:]
y_train = y[:10000]
y_test = y[10000:]
linear_svr = SVR(kernel='linear')
linear_svr.fit(x_train,y_train)
linear_predict = linear_svr.predict(x_test)
#calculate the different criterion
mape(linear_predict,y_test)
mean_squared_error(linear_predict,y_test)
mean_absolute_error(linear_predict,y_test)