我得到的错误是
"ValueError:预期的2D数组,得到1D数组: array= 45000. 50000. 60000. 80000. 110000. 150000. 200000. 300000. 500000.使用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()发布于 2018-10-24 06:26:16
似乎,预期的维度是错误的。你能试试:
regressor = SVR(kernel='rbf')
regressor.fit(X.reshape(-1, 1), y)https://stackoverflow.com/questions/52961851
复制相似问题