python - 预期是二维数组,却得到一维数组,而是错误

标签 python machine-learning data-science

我得到的错误为

"ValueError: Expected 2D array, got 1D array instead: array=[ 45000. 50000. 60000. 80000. 110000. 150000. 200000. 300000. 500000. 1000000.]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample."

执行以下代码时:

# SVR

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Position_S.csv')
X = dataset.iloc[:, 1:2].values
y = dataset.iloc[:, 2].values

 # Feature Scaling
from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
sc_y = StandardScaler()
X = sc_X.fit_transform(X)
y = sc_y.fit_transform(y)

# Fitting SVR to the dataset
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(X, y)

# Visualising the SVR results
plt.scatter(X, y, color = 'red')
plt.plot(X, regressor.predict(X), color = 'blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

# Visualising the SVR results (for higher resolution and smoother curve)
X_grid = np.arange(min(X), max(X), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color = 'red')
plt.plot(X_grid, regressor.predict(X_grid), color = 'blue')
plt.title('Truth or Bluff (SVR)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

最佳答案

看来预期的尺寸是错误的。你能尝试一下吗:

regressor = SVR(kernel='rbf')
regressor.fit(X.reshape(-1, 1), y)

关于python - 预期是二维数组,却得到一维数组,而是错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52961851/

相关文章:

android - 运行 systrace 出现 "preexec_fn is not supported on Windows"错误

python - 逆位运算符

performance - TFJS 中 eager 风格的性能成本是多少?

python - 测量二进制列表之间的相似性

python - MySQL语句有什么错误?

machine-learning - 如何避免简单前馈网络的过度拟合

machine-learning - 如何使用 Mallet 获取给定查询的主题概率

python - 如何在mlflow中记录Hydra的多次运行

python - 有人能告诉我最后一个循环在做什么吗?

python - PackageNotInstalledError : Package is not installed in prefix