我一直在努力了解为什么fit_transform不断抛出错误。调试并没有多大帮助,因为它只是将我引向数组的定义,而且我不确定自己缺少什么。有什么想法吗?
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import pickle
def split_data(inputs, outputs, p_train=0.9, p_test=0.1):
train_size = int(len(inputs) * p_train)
test_size = int(len(inputs) * p_test)
inputs_train, inputs_test = inputs[0:train_size, :], inputs[train_size:, :]
outputs_train, outputs_test = outputs[0:train_size, :], outputs[train_size:, :]
return inputs_train, inputs_test, outputs_train, outputs_test
rawinputs = pickle.load(open('rawinputs.pck', 'rb'))
rawoutputs = pickle.load(open('rawoutputs.pck', 'rb'))
#split
inputs_train, inputs_test, outputs_train, outputs_test = split_data(rawinputs, rawoutputs, p_train=0.90, p_test=0.10)
#normalize
scaler_inputs = MinMaxScaler()
inputs_train_scaled = scaler_inputs.fit_transform(inputs_train)
inputs_test_scaled = scaler_inputs.transform(inputs_test)
outputs_train = np.asmatrix(outputs_train)
使用fit_transform的第一行引发错误:
inputs_train_scaled = scaler_inputs.fit_transform(inputs_train)
例如一排原始输入数据:
['28,7170876207375' '339,050018316624' '0,173448071160097' '158,211319524893']
引发错误:
File "C:\****\venv\lib\site-packages\sklearn\base.py", line 690, in fit_transform return self.fit(X, **fit_params).transform(X)
答案 0 :(得分:0)
关键点是inputs_train
变量必须是二维数组。
这里有一个简单的例子:
from sklearn.datasets import fetch_olivetti_faces
我们将MinMaxScaler
应用于面部数据集。
from sklearn.datasets import fetch_olivetti_faces
olivetti = fetch_olivetti_faces()
X = olivetti.images
y = olivetti.target
X是人脸图像,其中y是每个人脸的标签。
接下来,我们将数据集分为训练集和测试集。
类似于您的split_data
方法。
from sklearn.datasets import fetch_olivetti_faces
from sklearn.model_selection import train_test_split
olivetti = fetch_olivetti_faces()
X = olivetti.images
y = olivetti.target
x_train, x_test, y_train, y_test = train_test_split(X, y,
test_size=0.2,
random_state=42)
random_state
变量将帮助我们在每次运行代码时产生相同的训练和测试集。
现在,我们来看一下x_train
变量:
print(x_train.shape)
有320张火车图像,每个图像为64 x 64像素。我们必须重塑形状以使用MinMaxScaler
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1] * x_train.shape[2])
现在新的图像尺寸:
最终代码:
from sklearn.datasets import fetch_olivetti_faces
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
olivetti = fetch_olivetti_faces()
X = olivetti.images
y = olivetti.target
x_train, x_test, y_train, y_test = train_test_split(X,
y,
test_size=0.2,
random_state=42)
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1] * x_train.shape[2])
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1] * x_test.shape[2])
min_max_obj = MinMaxScaler()
inputs_train_scaled = min_max_obj.fit_transform(X=x_train)
inputs_test_scaled = min_max_obj.transform(X=x_test)