【问题标题】:Expected 2D array error not getting resolved预期的二维数组错误未得到解决
【发布时间】:2018-07-05 18:57:03
【问题描述】:

我正在尝试在数据集上使用我的机器学习模型,其中我只有两列,而标准缩放它们时,我得到了预期的 2D 数组的错误,但得到了 1。

下面是代码:

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

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

# Splitting the dataset into the Training set and Test set
"""from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)"""

# 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)
y_pred = sc_y.inverse_transform(y_pred)

# 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()

当我尝试放

y = sc_y.fit_transform([y])

像这样我没有收到任何错误,但是当我执行接下来的 3 行时,我收到另一个错误。

这是bad input shape (1, 10)

谁能帮我解决这个问题?

【问题讨论】:

  • 两列都输入吗?
  • 不,X是输入值,y是实际输出...

标签: python arrays machine-learning scikit-learn svm


【解决方案1】:

sklearn 中的 StandardScaler() 函数要求 input(X) 采用以下格式:

X : numpy 形状数组 [n_samples, n_features]

因此,如果您只有一个特征列,请将 X 重新整形为 (-1,1)。

sc_X.fit_transform(X.reshape[-1,1])

这应该可行!

【讨论】:

  • 我刚刚更改了 sc_X.fit_transform(X.reshape(-1,1)) 并且效果很好。非常感谢你的帮助。你太棒了。
  • 乐于助人 :) 谢谢
猜你喜欢
  • 2019-03-28
  • 2019-08-17
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2021-09-19
  • 2018-06-06
  • 2021-03-01
相关资源
最近更新 更多