复现论文
Application of neural networks and fuzzy systems for the intelligent
prediction of CO2-induced strength alteration of coal
通过 pandas 读取 excel 数据
import pandas as pd
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()
DataFrame 转 NumPy,打乱原始数据
import numpy as np
import random
rawDataNP = rawData.values
random.shuffle(rawDataNP)
划分训练集和测试集
test_data = rawDataNP[-15:]
train_data = rawDataNP[:-15]
# 特征列和目标列选取
train_features, train_targets = train_data[:,0:3], train_data[:,4]
test_features, test_targets = test_data[:,0:3], test_data[:,4]
搭建模型
import torch
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
%matplotlib inline
input_size = train_features.shape[1]
hidden_size = 5
output_size = 1
batch_size = 5
learningRate = 1e-3
neu = torch.nn.Sequential(
torch.nn.Linear(input_size, hidden_size),
torch.nn.Tanh(),
#torch.nn.ReLU(),
torch.nn.Linear(hidden_size, output_size),
)
lossF = torch.nn.MSELoss()
optiF = torch.optim.Adam(neu.parameters(), lr = learningRate)
#optiF = torch.optim.SGD(neu.parameters(), lr=learningRate, momentum=0.9)
#optiF = torch.optim.Adam(neu.parameters(), lr=learningRate, betas=(0.9, 0.7))
实例化 Dataset 和 DataLoader
x = train_features.astype(float)
y = train_targets.astype(float)
inputsTorch = torch.from_numpy(x)
targetTorch = torch.from_numpy(y)
trainDataset = TensorDataset(inputsTorch, targetTorch)
trainDataLoader = DataLoader(trainDataset, batch_size, shuffle=True)
训练模型
losses = []
def training(numEpochs, model, lossFunction, optFunction):
batch_loss = []
for epoch in range(numEpochs):
for x,y in trainDataLoader:
predict =torch.squeeze(neu(x.float()), dim=1)
loss = lossFunction(predict, y.float())
optFunction.zero_grad()
loss.backward()
optFunction.step()
batch_loss.append(loss.data.numpy())
# 每隔100步输出一下损失值(loss)
if epoch % 200==0:
losses.append(np.mean(batch_loss))
print('Epoch [{}/{}], Loss:{:.4f}'.format(epoch+200, numEpochs, np.mean(batch_loss)))
training(2000, neu, lossF, optiF)
# 打印输出损失值
plt.plot(np.arange(len(losses)),losses)
plt.xlabel('epoch')
plt.ylabel('MSE')
拟合效果[训练集]
fig, ax = plt.subplots(figsize = (10, 7))
mean, std = scaled_features['Measured_UCS']
ax.plot(y * std + mean, label='Origin')
ax.plot(neu(inputsTorch.float()).detach().numpy() * std + mean, label='Fitting')
ax.legend()
ax.set_ylabel('UCS')
fig, ax = plt.subplots(figsize = (10, 7))
mean, std = scaled_features['Measured_UCS']
x = np.squeeze(neu(inputsTorch.float()).detach().numpy() * std + mean)
y = (y * 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('Fitted UCS')
预测效果[测试集]
inputsTorch = torch.from_numpy(test_features.astype(float))
Y = test_targets
fig, ax = plt.subplots(figsize = (10, 7))
ax.plot(Y * std + mean, label='Origin')
ax.plot(neu(inputsTorch.float()).detach().numpy() * std + mean, label='Prediction')
ax.legend()
ax.set_ylabel('UCS')
fig, ax = plt.subplots(figsize = (10, 7))
mean, std = scaled_features['Measured_UCS']
x = np.squeeze(neu(inputsTorch.float()).detach().numpy() * std + mean)
y = (Y * 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')