gpt4 book ai didi

python - 使用 array.reshape(-1, 1) reshape 数组

转载 作者:行者123 更新时间:2023-12-01 06:50:14 28 4
gpt4 key购买 nike

我有一个名为 data 的数据框,我试图从中识别任何异常价格。

数据框头部看起来像:

         Date  Last Price
0 29/12/2017 487.74
1 28/12/2017 422.85
2 27/12/2017 420.64
3 22/12/2017 492.76
4 21/12/2017 403.95

我发现了一些代码,我需要针对加载数据的数据稍微调整,然后将时间序列与缩放器进行比较。代码如下:

    data = pd.read_csv(path) 
data = rawData['Last Price']

data = data['Last Price']
scaler = StandardScaler()
np_scaled = scaler.fit_transform(data)
data = pd.DataFrame(np_scaled)
# train oneclassSVM
outliers_fraction = 0.01
model = OneClassSVM(nu=outliers_fraction, kernel="rbf", gamma=0.01)
model.fit(data)
data['anomaly3'] = pd.Series(model.predict(data))

fig, ax = plt.subplots(figsize=(10,6))
a = data.loc[data['anomaly3'] == -1, ['date_time_int', 'Last Price']] #anomaly

ax.plot(data['date_time_int'], data['Last Price'], color='blue')
ax.scatter(a['date_time_int'],a['Last Price'], color='red')
plt.show();

def getDistanceByPoint(data, model):
distance = pd.Series()
for i in range(0,len(data)):
Xa = np.array(data.loc[i])
Xb = model.cluster_centers_[model.labels_[i]-1]
distance.set_value(i, np.linalg.norm(Xa-Xb))
return distance

但是收到错误消息:

ValueError: Expected 2D array, got 1D array instead:
array=[487.74 422.85 420.64 ... 461.57 444.33 403.84].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

并且我不确定需要在哪里调整数组大小。

有关信息,这里是回溯:

 File "<ipython-input-23-628125407694>", line 1, in <module>
runfile('C:/Users/stacey/Downloads/techJob.py', wdir='C:/Users/stacey/Downloads')

File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)

File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/staceyDownloads/techJob.py", line 92, in <module>
main()

File "C:/Users/stacey/Downloads/techJob.py", line 56, in main
np_scaled = scaler.fit_transform(data)

File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\sklearn\base.py", line 464, in fit_transform
return self.fit(X, **fit_params).transform(X)

File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\sklearn\preprocessing\data.py", line 645, in fit
return self.partial_fit(X, y)

File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\sklearn\preprocessing\data.py", line 669, in partial_fit
force_all_finite='allow-nan')

File "C:\Anaconda_Python 3.7\2019.03\lib\site-packages\sklearn\utils\validation.py", line 552, in check_array
"if it contains a single sample.".format(array))

ValueError: Expected 2D array, got 1D array instead:
array=[7687.77 7622.88 7620.68 ... 5261.57 5244.37 5203.89].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

最佳答案

您应该能够通过更改此行来修复错误:

np_scaled = scaler.fit_transform(data)

这样:

np_scaled = scaler.fit_transform(data.values.reshape(-1,1))

关于python - 使用 array.reshape(-1, 1) reshape 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59050251/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com