我正在尝试在数据集上使用我的机器学习模型,其中我只有两列,而标准缩放它们,我得到错误预期2D数组但得到1.
以下是代码:
Rename-Item
当我试图把
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()
像这样我没有收到任何错误,但是当我执行接下来的3行时,我收到另一个错误。
是y = sc_y.fit_transform([y])
任何人都可以帮我吗?
答案 0 :(得分:1)
sklearn中的StandardScaler()函数要求输入(X)采用以下格式:
X:numpy数组形状[n_samples,n_features]
因此,如果您只有一个要素列,请将X重塑为(-1,1)。
sc_X.fit_transform(X.reshape[-1,1])
这应该有效!