'numpy.ndarray'对象在x_test中没有属性'iloc'



在这里,我有四个输入,我试图预测未来的值。在此之前,我将输入数据缩放为 0,1。然后我创造了x_test价值。

然后,在预测代码之前,我必须编写另一个代码来预测我每小时的值。为此,我想提取到我的x_test_n值中的行中。然后我使用了 iloc 代码。但不幸的是,由于 numpy 数组,它不起作用。然后我找到了代码并尝试了该代码,它也给了我一个错误。这是我尝试过的代码,

data10 = pd.read_csv('data.csv',"," )
data10 = data10.replace(np.nan, 0)
data10 = pd.DataFrame(data10,columns=['date','x1','x2','x3','x4'])
data10.set_index('date', inplace=True)
data10 = data10.values
X = 1
n_out = 1
x,y=list(),list()
start =0

for _ in range(len(data10)):
in_end = start+X
out_end= in_end + n_out
if out_end < len(data10):
x_input = data10[start:in_end]
x.append(x_input)
y.append(data10[in_end:out_end,0])
start +=1
x = np.asanyarray(x)
y = np.asanyarray(y)

scaler_x = preprocessing.MinMaxScaler(feature_range =(0, 1))
x = np.array(x).reshape ((len(x),4 ))
x = scaler_x.fit_transform((x))
scaler_y = preprocessing.MinMaxScaler(feature_range =(0, 1))
y = np.array(y).reshape ((len(y), 1))
y = scaler_y.fit_transform(y)
train_end = 150
x_test=x[train_end: ,]
y_test=y[train_end:] 
x_test,y_test = np.array(x_test),np.array(y_test)
x_test = np.reshape(x_test,(x_test.shape[0], x_test.shape[1],1))

那么我x_test是这样的:

[[[0.0000000e+00 0.0000000e+00 1.4332613e-01 0.0000000e+00]
[0.0000000e+00 0.0000000e+00 0.0000000e+00 6.8191981e-01]]
[[0.0000000e+00 0.0000000e+00 0.0000000e+00 6.8191981e-01]
[0.0000000e+00 1.4034396e-02 0.0000000e+00 0.0000000e+00]]
[[0.0000000e+00 1.4034396e-02 0.0000000e+00 0.0000000e+00]
[0.0000000e+00 0.0000000e+00 6.3639030e-02 0.0000000e+00]]

之后,我想使用 iloc 提取x_test_n中的行

filtered_3 = x_test_n
new_df = pd.DataFrame(scaler_x.fit_transform(filtered_3), columns=filtered_3.columns, index=df.index)

然后得到一个错误:

ValueError                                Traceback (most recent call last)
<ipython-input-26-715b662d895d> in <module>()
101 
102 filtered_3 = x_test_n
--> 103 new_df = pd.DataFrame(scaler_x.fit_transform(filtered_3), columns=filtered_3.columns, index=df.index)
104 # current_calorie = filtered_3.iloc[:,]
105 # last_calorie_record = 0
~Anaconda3libsite-packagessklearnbase.py in fit_transform(self, X, y, **fit_params)
515         if y is None:
516             # fit method of arity 1 (unsupervised transformation)
--> 517             return self.fit(X, **fit_params).transform(X)
518         else:
519             # fit method of arity 2 (supervised transformation)
~Anaconda3libsite-packagessklearnpreprocessingdata.py in fit(self, X, y)
306         # Reset internal state before fitting
307         self._reset()
--> 308         return self.partial_fit(X, y)
309 
310     def partial_fit(self, X, y=None):
~Anaconda3libsite-packagessklearnpreprocessingdata.py in partial_fit(self, X, y)
332 
333         X = check_array(X, copy=self.copy, warn_on_dtype=True,
--> 334                         estimator=self, dtype=FLOAT_DTYPES)
335 
336         data_min = np.min(X, axis=0)
~Anaconda3libsite-packagessklearnutilsvalidation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
449         if not allow_nd and array.ndim >= 3:
450             raise ValueError("Found array with dim %d. %s expected <= 2."
--> 451                              % (array.ndim, estimator_name))
452         if force_all_finite:
453             _assert_all_finite(array)
ValueError: Found array with dim 3. MinMaxScaler expected <= 2.

谁能帮我解决这个错误?

如果"x_test.shape"是3d,(结果中有3个值(你可以尝试下面的代码,否则我可以看起来不同的方式。

nsamples, nx, ny = x_test.shape
x_test_reshape = x_test.reshape((nsamples,nx*ny))

如果有效,则可以使用相同的方式将数组转换为数据帧。

最新更新