gpt4 book ai didi

python - sklearn.preprocessing.StandardScaler ValueError : Expected 2D array, 得到一维数组

转载 作者:行者123 更新时间:2023-12-04 15:12:50 31 4
gpt4 key购买 nike

关闭。这个问题需要debugging details .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

去年关闭。




Improve this question




我正在尝试在 http://www.semspirit.com/artificial-intelligence/machine-learning/regression/support-vector-regression/support-vector-regression-in-python/ 上完成教程
但是没有包含 csv 文件,所以我使用的是我自己的数据。这是到目前为止的代码:

import numpy as np
import pandas as pd
from matplotlib import cm
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy import stats

# Here's where I import my data; there's no csv file included in the tutorial
import quasar_functions as qf
dataset, datasetname, mags = qf.loaddata('sdss12')

S = np.asarray(dataset[mags])
t = np.asarray(dataset['z'])
t.reshape(-1,1)

# Feature scaling
from sklearn.preprocessing import StandardScaler as scale
sc_S = scale()
sc_t = scale()
S2 = sc_S.fit_transform(S)
t2 = sc_t.fit_transform(t)
最后一行抛出错误:
ValueError: Expected 2D array, got 1D array instead:
array=[4.17974 2.06468 5.46959 ... 0.41398 0.3672 1.9235 ].
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.
是的,我 reshape 了我的目标阵列 tt.reshape(-1,1)如图 here , here , herehere ,但无济于事。我整形正确吗?
这是我的所有变量:
enter image description here

最佳答案

我猜你有一个数据框,所以你需要重新分配变量 t = t.reshape(-1,1) :

import pandas as pd

dataset = pd.DataFrame(np.random.normal(2,1,(100,4)),columns=['z','x1','x2','x3'])
mags = ['x1','x2','x3']

S = np.asarray(dataset[mags])
t = np.asarray(dataset['z'])
t = t.reshape(-1,1)

from sklearn.preprocessing import StandardScaler as scale
sc_S = scale()
sc_t = scale()
S2 = sc_S.fit_transform(S)
t2 = sc_t.fit_transform(t)
要检查它是否有效:
np.mean(t2)
2.4646951146678477e-16

关于python - sklearn.preprocessing.StandardScaler ValueError : Expected 2D array, 得到一维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64882432/

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