我正在尝试为我的训练/测试数据集绘制一条最佳拟合线,并与原始值进行比较以查看它的拟合程度。
我能够绘制原始数据集的拟合线。但是当涉及到训练/测试数据集时,它就乱套了。感谢对此的任何投入。
# Importing the dataset
dataset =
pd.read_excel('experimental_data.xlsx',
sheet_name = 'Sheet1')
x = dataset['λ'].values
y = dataset['P'].values
df = pd.DataFrame(dataset[["λ", "P"]])
#Training the dataset
X_train, X_test, y_train, y_test =
train_test_split(x, y, test_size = 0.3,
random_state = 1)
regressor =
RandomForestRegressor(n_estimators = 10,
random_state = 1)
regressor.fit(X_train.reshape(-1,1),
y_train.reshape(-1,1))
y_pred =
regressor.predict(X_test.reshape(-1,1))
x_pred =
regressor.predict(y_test.reshape(-1,1))
RanDF = pd.DataFrame({'Real Values of P
[MPa]':y_test.reshape(-1),
'Predicted Values of P
[MPa]':y_pred.reshape(-1)})
RandF = RanDF.sort_values(by='Real Values of
P [MPa]')
# Plotting the graph
plt.figure(figsize=(12,12))
X_grid = np.arange(min(x), max(x), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.plot(x, y,'r', linestyle = ':', lw =
1.5, zorder = 1,)
plt.scatter(x = "λ", y = "P", data = df,
linestyle = 'dashed', marker = 'o', s = 0,
color = 'indigo', zorder = 2)
plt.plot(X_test, y_pred,'b', linestyle =
':', lw = 1.5, zorder = 3)
plt.scatter(x = "λ", y = "P", data = df,
linestyle = 'dashed', marker = 'o', s = 0,
color = 'indigo', zorder = 4)
RealVRand = plt.scatter(X_test, y_test,
color = 'forestgreen', s=40, zorder=5)
PredVRand = plt.scatter(X_test, y_pred,
color = 'darkorchid', s=40, zorder=6)
plt.title('Random Forest Regression',
fontsize = 15)
plt.xlabel('λ', fontsize = 14)
plt.ylabel('P [MPa]', fontsize = 14)
plt.legend ((RealVRand, PredVRand),
('Actual', 'Predicted'), scatterpoints = 1,
loc='upper left', ncol = 40, fontsize = 12)
plt.show()
数据
λ P
1.00 0.00
1.02 0.03
1.04 0.07
1.06 0.10
1.08 0.13
1.10 0.16
1.12 0.18
1.14 0.21
1.16 0.24
1.18 0.26
1.20 0.29
1.22 0.31
1.24 0.33
1.26 0.36
1.28 0.38
1.30 0.40
1.32 0.42
1.34 0.44
1.36 0.46
1.38 0.48
1.40 0.50
1.42 0.52
1.44 0.54
1.46 0.56
1.48 0.58
1.50 0.60
1.52 0.62
1.54 0.63
1.56 0.65
1.58 0.67
1.60 0.69
1.62 0.70
1.64 0.72
1.66 0.74
1.68 0.75
1.70 0.77
1.72 0.78
1.74 0.80
1.76 0.82
1.78 0.83
1.80 0.85
1.82 0.86
1.84 0.88
1.86 0.89
1.88 0.91
1.90 0.92
1.92 0.94
1.94 0.95
1.96 0.96
1.98 0.98
2.00 0.99
2.02 1.01
2.04 1.02
2.06 1.03
2.08 1.05
2.10 1.06
2.12 1.08
2.14 1.09
2.16 1.10
2.18 1.12
2.20 1.13
2.22 1.14
2.24 1.16
2.26 1.17
2.28 1.18
2.30 1.20
2.32 1.21
2.34 1.22
2.36 1.24
2.38 1.25
2.40 1.26
2.42 1.28
2.44 1.29
2.46 1.30
2.48 1.31
2.50 1.33
2.52 1.34
2.54 1.35
2.56 1.37
2.58 1.38
2.60 1.39
2.62 1.40
2.64 1.42
2.66 1.43
2.68 1.44
2.70 1.45
2.72 1.47
2.74 1.48
2.76 1.49
2.78 1.50
2.80 1.52
2.82 1.53
2.84 1.54
2.86 1.55
2.88 1.57
2.90 1.58
2.92 1.59
2.94 1.60
2.96 1.61
2.98 1.63
3.00 1.64
3.02 1.65
3.04 1.66
3.06 1.68
3.08 1.69
3.10 1.70
3.12 1.71
3.14 1.72
3.16 1.74
3.18 1.75
3.20 1.76
3.22 1.77
3.24 1.78
3.26 1.80
3.28 1.81
3.30 1.82
3.32 1.83
3.34 1.84
3.36 1.86
3.38 1.87
3.40 1.88
3.42 1.89
3.44 1.90
3.46 1.92
3.48 1.93
3.50 1.94
3.52 1.95
3.54 1.96
3.56 1.97
3.58 1.99
3.60 2.00
3.62 2.01
3.64 2.02
3.66 2.03
3.68 2.05
3.70 2.06
3.72 2.07
3.74 2.08
3.76 2.09
3.78 2.10
3.80 2.12
3.82 2.13
3.84 2.14
3.86 2.15
3.88 2.16
3.90 2.18
3.92 2.19
3.94 2.20
3.96 2.21
3.98 2.22
4.00 2.23
4.02 2.25
4.04 2.26
4.06 2.27
4.08 2.28
4.10 2.29
4.12 2.30
4.14 2.32
4.16 2.33
4.18 2.34
4.20 2.35
4.22 2.36
4.24 2.37
4.26 2.39
4.28 2.40
4.30 2.41
4.32 2.42
4.34 2.43
4.36 2.44
4.38 2.46
4.40 2.47
4.42 2.48
4.44 2.49
4.46 2.50
4.48 2.51
4.50 2.52
4.52 2.54
4.54 2.55
4.56 2.56
4.58 2.57
4.60 2.58
4.62 2.59
4.64 2.61
4.66 2.62
4.68 2.63
4.70 2.64
4.72 2.65
4.74 2.66
4.76 2.68
4.78 2.69
4.80 2.70
4.82 2.71
4.84 2.72
4.86 2.73
4.88 2.74
4.90 2.76
4.92 2.77
4.94 2.78
4.96 2.79
4.98 2.80
5.00 2.81
5.02 2.83
5.04 2.84
5.06 2.85
5.08 2.86
5.10 2.87
5.12 2.88
5.14 2.89
5.16 2.91
5.18 2.92
5.20 2.93
5.22 2.94
5.24 2.95
5.26 2.96
5.28 2.97
5.30 2.99
5.32 3.00
5.34 3.01
5.36 3.02
5.38 3.03
5.40 3.04
5.42 3.06
5.44 3.07
5.46 3.08
5.48 3.09
5.50 3.10
5.52 3.11
5.54 3.12
5.56 3.14
5.58 3.15
5.60 3.16
5.62 3.17
5.64 3.18
5.66 3.19
5.68 3.20
5.70 3.22
5.72 3.23
5.74 3.24
5.76 3.25
5.78 3.26
5.80 3.27
5.82 3.28
5.84 3.30
5.86 3.31
5.88 3.32
5.90 3.33
5.92 3.34
5.94 3.35
5.96 3.37
5.98 3.38
6.00 3.39
6.02 3.40
6.04 3.41
6.06 3.42
6.08 3.43
6.10 3.45
6.12 3.46
6.14 3.47
6.16 3.48
6.18 3.49
6.20 3.50
6.22 3.51
6.24 3.53
6.26 3.54
6.28 3.55
6.30 3.56
6.32 3.57
6.34 3.58
6.36 3.59
6.38 3.61
6.40 3.62
6.42 3.63
6.44 3.64
6.46 3.65
6.48 3.66
6.50 3.67
6.52 3.69
6.54 3.70
6.56 3.71
6.58 3.72
6.60 3.73
6.62 3.74
6.64 3.75
6.66 3.77
6.68 3.78
6.70 3.79
6.72 3.80
6.74 3.81
6.76 3.82
6.78 3.83
6.80 3.85
6.82 3.86
6.84 3.87
6.86 3.88
6.88 3.89
6.90 3.90
6.92 3.91
6.94 3.93
6.96 3.94
6.98 3.95
7.00 3.96