【问题标题】:I cannot solve the error "ValueError: Expected 2D array, got 1D array instead:" in SVR | ML我无法解决 SVR | 中的错误“ValueError: Expected 2D array, got 1D array:”机器学习
【发布时间】:2019-07-09 12:07:08
【问题描述】:

我正在尝试制作一个简单的 svr,并且代码需要一些特征缩放,因此当我使用此代码将缩放应用于数据集时,它总是会出现此错误! 我尝试在 fit_transform() 函数和数据集拆分部分中重塑数据,但这总是将 y 的值更改为绝对零!你的值应该在-0.7202.643之间(总共10个值)..

ps:当我适应并转换“y”时,错误就出现了。

我已经看到了不同的问题和应用技术,但我认为这对我不起作用,所以请帮忙,因为我陷入了困境......

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('Position_Salaries.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)

#predicting a new result
y_pred = regressor.predict([[6.5]])

#visualising the svr results
plt.scatter(X,y ,color = 'red')
plt.plot(X , regressor.predict(X) , color = 'blue')
plt.title('truth or bluff')
plt.xlabel('positon level')
plt.ylabel('salary')
plt.show()

介于 -0.720 和 2.643 之间(总共 10 个值)

【问题讨论】:

  • y 的数据集 45000 50000 60000 80000 110000 150000 200000 300000 500000 1000000
  • 不要将 cmets 空间用于此类附加信息 - 改为编辑和更新您的代码!

标签: machine-learning svm data-science


【解决方案1】:

在您的代码中,ySerieslistStandardScaler 不接受。要将 y 转换为 numpy 数组,请运行以下命令:

import numpy as np
y = np.reshape(y, (-1,1))
y = sc_y.fit_transform(y)

【讨论】:

  • 非常感谢 Rajat :) 非常感谢 :)
猜你喜欢
  • 2018-08-25
  • 2021-05-05
  • 2021-11-01
  • 2021-07-28
  • 2018-12-23
  • 1970-01-01
  • 2020-10-10
  • 2020-11-22
  • 2023-03-26
相关资源
最近更新 更多