2020-04-07 回归预测 SVR方法

数据来源于论文:Application of neural networks and fuzzy systems for the intelligent prediction of CO2-induced strength alteration of coal

预测方法:支持向量回归(Support Vector Regression)

参考: https://scikit-learn.org/stable/auto_examples/svm/plot_svm_regression.html#sphx-glr-auto-examples-svm-plot-svm-regression-py

代码如下:

# #############################################################################
# 声明需要用到的包
import numpy as np
import pandas as pd
import random
from sklearn.svm import SVR
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
%matplotlib inline

# #############################################################################
# 导入原始数据
rawData = pd.read_csv('raw_data.csv')
print("原始数据集大小:",rawData.shape)
rawData.head()

# #############################################################################
# 对所有数值类型的特征进行标准化处理
# 每一个因素的均值和方差都存储到 scaled_features 变量中。
quant_features = [ 'FC', 'Interaction_time', 'Saturation_pressure', 'Measured_UCS']
scaled_features = {}
for each in quant_features:
    mean, std = rawData[each].mean(), rawData[each].std()
    scaled_features[each] = [mean, std]
    rawData.loc[:, each] = (rawData[each] - mean)/std
rawData.head()

# #############################################################################
# 转化为 numpy 数组
rawDataNP = rawData.values
rawDataNP[0:5,:]

完成了数据预处理过后,就可以开始进行回归预测了:

# #############################################################################
# 定义输入 输出
x=rawDataNP[:,0:3]
y=rawDataNP[:,4]
# 分割训练数据和测试数据
# 随机采样25%作为测试 75%作为训练
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25, random_state=0)

线性核函数,预测结果:

# 线性核函数配置支持向量机
linear_svr = SVR(kernel="linear")
# 训练
linear_svr.fit(x_train, y_train)
# 预测 保存预测结果
linear_svr_y_predict = linear_svr.predict(x_test)

plt.plot(y_test)
plt.plot(linear_svr_y_predict)
plt.show()
线性核函数,预测结果
fig, ax = plt.subplots(figsize = (10, 7))
mean, std = scaled_features['Measured_UCS']
y = (y_test * std + mean).astype(float)
x = (linear_svr_y_predict * std + mean).astype(float)
poly = np.polyfit(x,y,deg=1)
z = np.polyval(poly, x)
ax.plot(x, y, 'o')
ax.plot(x, z,label='Linear Regression')
ax.legend()
ax.set_ylabel('Measured UCS')
ax.set_xlabel('Predicted UCS')
线性核函数,预测结果

多项式核函数,预测结果:

# 多项式核函数配置支持向量机
poly_svr = SVR(kernel='poly', C=100, gamma='auto', degree=3, epsilon=.1, coef0=1)
# 训练
poly_svr.fit(x_train, y_train)
# 预测 保存预测结果
poly_svr_y_predict = poly_svr.predict(x_test)
plt.plot(y_test)
plt.plot(poly_svr_y_predict)
plt.show()
多项式核函数,预测结果
fig, ax = plt.subplots(figsize = (10, 7))
mean, std = scaled_features['Measured_UCS']
y = (y_test * std + mean).astype(float)
x = (poly_svr_y_predict * std + mean).astype(float)
poly = np.polyfit(x,y,deg=1)
z = np.polyval(poly, x)
ax.plot(x, y, 'o')
ax.plot(x, z,label='Linear Regression')
ax.legend()
ax.set_ylabel('Measured UCS')
ax.set_xlabel('Predicted UCS')
多项式核函数,预测结果

径向基核函数,预测结果:

# 径向基核函数配置支持向量机
rbf_svr = SVR(kernel='rbf', C=100, gamma=0.1, epsilon=.1)
# 训练
rbf_svr.fit(x_train, y_train)
# 预测 保存预测结果
rbf_svr_y_predict = rbf_svr.predict(x_test)
plt.plot(y_test)
plt.plot(rbf_svr_y_predict)
plt.show()

径向基核函数,预测结果
fig, ax = plt.subplots(figsize = (10, 7))
mean, std = scaled_features['Measured_UCS']
y = (y_test * std + mean).astype(float)
x = (rbf_svr_y_predict * std + mean).astype(float)
poly = np.polyfit(x,y,deg=1)
z = np.polyval(poly, x)
ax.plot(x, y, 'o')
ax.plot(x, z,label='Linear Regression')
ax.legend()
ax.set_ylabel('Measured UCS')
ax.set_xlabel('Predicted UCS')
径向基核函数,预测结果
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容