【发布时间】:2020-04-14 16:04:42
【问题描述】:
这里我有四个输入,我试图预测未来的价值。在此之前,我将输入数据缩放为 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
~\Anaconda3\lib\site-packages\sklearn\base.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)
~\Anaconda3\lib\site-packages\sklearn\preprocessing\data.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):
~\Anaconda3\lib\site-packages\sklearn\preprocessing\data.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)
~\Anaconda3\lib\site-packages\sklearn\utils\validation.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.
谁能帮我解决这个错误?
【问题讨论】:
-
iloc是一种pandas索引方法,而不是numpy一种。 -
@hpaulj 是的,我知道,我正在寻找一种将我的 np 数组值放入数据框的方法
标签: python-3.x pandas numpy