【发布时间】:2021-10-19 07:10:21
【问题描述】:
我正在尝试使用以下代码将散点图与回归曲线一起使用。我正在使用不同的算法,如线性回归、SVM 和高斯过程等。我尝试了不同的选项来绘制下面提到的数据
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVR
from sklearn.gaussian_process import GaussianProcessRegressor
df=pd.read_excel(coded.xlsx)
dfnew=df[['FL','FW','TL','LL','KH']]
Y = df['KH']
X = df[['FL']]
X=X.values.reshape(len(X),1)
Y=Y.values.reshape(len(Y),1)
# Split the data into training/testing sets
X_train = X[:-270]
X_test = X[-270:]
# Split the targets into training/testing sets
Y_train = Y[:-270]
Y_test = Y[-270:]
#regressor = SVR(kernel = 'rbf')
#regressor.fit(X_train, np.ravel(Y_train))
#training the algorithm
regressor = GaussianProcessRegressor(random_state=42)
regressor.fit(X_train, Y_train)
y_pred = regressor.predict(X_test)
mse = np.sum((y_pred - Y_test)**2)
# root mean squared error
# m is the number of training examples
rmse = np.sqrt(mse/270)
print(rmse)
#X_grid = np.arange(min(X), max(X), 0.01) #this step required because data is feature scaled.
#X_grid = np.arange(0, 15, 0.01) #this step required because data is feature scaled.
#X_grid = X_grid.reshape((len(X_grid), 1))
#plt.scatter(X, Y, color = 'red')
print('size of Y_train = {0}'.format(Y_train.size))
print('size of y_pred = {0}'.format(y_pred.size))
#plt.scatter(Y_train, y_pred, color = 'red')
#plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
#plt.title('GPR')
#plt.xlabel('Measured')
#plt.ylabel('Predicted')
#plt.show()
fig, ax = plt.subplots(1, figsize=(12, 6))
plt.plot(X[:, 0], Y_train, marker='o', color='black', linewidth=0)
plt.plot(X[:, 0], y_pred, marker='x', color='steelblue')
plt.suptitle("$GaussianProcessRegressor(kernel=RBF)$ [default]", fontsize=20)
plt.axis('off')
pass
但我收到如下错误: ValueError: x 和 y 必须具有相同的第一个维度,但形状为 (540,) 和 (270, 1)
什么是可能的解决方案?
【问题讨论】:
-
当您向我们提供一些上下文(数据)时,它会更容易为您提供帮助。从代码中我猜你应该使用 X 和 Y 或者 X_train 和 Y_train。从 X 你应该只使用感兴趣的列,例如
X['some_var_name'] -
对于这么多注释掉的代码,很难理解真正的问题是什么。供日后参考,请参阅How to create a Minimal, Reproducible Example
-
感谢比尔的回复,但是,注释代码只是我使用的不同选项。
标签: python pandas numpy matplotlib