python - 预期的二维数组,得到一维数组而不是错误
问题描述
我收到错误为
“ValueError:预期的 2D 数组,得到 1D 数组:array=[ 45000. 50000. 60000. 80000. 110000. 150000. 200000. 300000. 500000. 1000000.]。使用 array.reshape(-1, 1) 重塑数据) 如果您的数据具有单个特征或 array.reshape(1, -1) 如果它包含单个样本。"
在执行以下代码时:
# 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)
推荐阅读
- graph - Louvain算法的Julia实现
- javascript - chrome 开发者工具控制台中的 BroadcastChannel 测试不起作用
- android - 谷歌是否提供安卓功能的源代码?例如,快速滚动条
- javascript - 使用矩区库的区域缩写是动态的吗
- sql-server - MS SQL 2012 - 如何插入时间戳以延续时间序列图表?
- javascript - 如果不是窗口,谁会保留对变量的引用?
- php - php7 fileInfo 扩展名,用于上传。无法在“null”加载魔法数据库。我忘记了哪些文件?
- laravel - Laravel + Carbon + 时区
- html - 没有方法属性的表单可以正常工作吗?
- java - Android Studio:我有 100 个按钮,使用 onclick 函数是否可以获得我为其指定的 ID?