我正在尝试用PCA sklearn包做一些数据分析。我目前遇到的问题是我的代码分析数据的方式。
下面是一些数据的示例
波长强度;um 196.078431372549 1.108370393265022E-003 192.307692307692 1.163428008597600E-003 188.679245283019 1.223639983609668E-003
到目前为止编写的代码如下:
scaler = StandardScaler(with_mean=True, with_std=True) #scales the data
data_crescent=ascii.read('earth_crescent.dat',data_start=4958, data_end=13300, delimiter=' ')#where the data is being read
#where each variable comes from in the dat
y_intensity_crescent=data_crescent['col2'][:]
x_wave_crescent=data_crescent['col1'][:]
standard_y_crescent=StandardScaler().fit_transform(y_intensity_crescent)#standardizing the intensity variable
#PCA runthrough of data
pca= PCA(n_components=2)
principalCrescentY=pca.fit_transform(standard_y_crescent)
principalDfcrescent = pd.DataFrame(data = principalCrescentY
, columns = ['principal component 1', 'principal component 2'])
finalDfcrescent = pd.concat([principalDfcrescent, [y_intensity_crescent]], axis = 1)一旦运行,数据就会产生这个错误:
ValueError: Expected 2D array, got 1D array instead:
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为了通过PCA分析数据,需要将数据转换为2D模型,以产生预期的结果。周围的任何工作都将非常感谢!
发布于 2019-12-21 11:09:42
问题是,您通过执行以下操作为您的pca对象提供了一个功能y_intensity_crescent:principalCrescentY=pca.fit_transform(standard_y_crescent)。实际上,你只给了你的pca算法一个维度。粗略地说:主成分分析采用多个特征时间序列,并将它们组合成多个特征组合的分量。如果你想要2个组件,你需要1个以上的功能。
下面是一些如何正确使用它的示例:PCA tutorial using sklearn
https://stackoverflow.com/questions/59433184
复制相似问题