错误:float() 参数必须是字符串或数字,而不是“StandardScaler”

数据挖掘 Python scikit-学习 特征缩放
2022-02-28 12:12:46

我收到上述代码的此错误。请帮助我,因为我是初学者并正在学习机器学习

# Polynomial Regression

# Importing the libraries
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

#Feature scaling
from sklearn.preprocessing import StandardScaler
sc_x=StandardScaler()
sc_y=StandardScaler()
x=sc_x.fit(x)
x=sc_x.fit(y)

#Fitiing SVR to the dataset
from sklearn.svm import SVR
regressor = SVR(kernel = 'rbf')
regressor.fit(x,y)

#Predict the results
regressor.predict(np.array([6.5]).reshape(1, 1))

#Visualizing the results
plt.scatter(x,y,color = 'red')
plt.plot(x,regressor.predict(x))
plt.label("Positon Level")
plt.label("Salary")
plt.show()
2个回答

.fit 方法返回标准标量对象。您正在使用它来训练模型。请在拟合后使用 fit_transfor 或变换。像下面

sc_x.fit(x)
x = sc_x.transform(x)

或者

x = sc_x.fit_transform(x)

sc.fit(x)不会改变x它只会训练sc你应该使用sc.transform(x)受过训练sc的人来做你想做的事。改变

sc_x = StandardScaler()
sc_y = StandardScaler()
x = sc_x.fit(x)
x = sc_x.fit(y)

sc_x = StandardScaler()
sc_y = StandardScaler()
x = sc_x.fit_transform(x)
y = sc_y.transform(y)