我正在尝试使用PCA sklearn软件包进行一些数据分析。我当前遇到的问题是我的代码分析数据的方式。
其中一些数据的示例如下
波长强度 ; [um] [W / m ** 2 / um / sr] 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模型,以产生预期的结果。任何解决方法将不胜感激!
答案 0 :(得分:0)
问题是您通过执行以下操作为pca对象提供了一个功能y_intensity_crescent
:principalCrescentY=pca.fit_transform(standard_y_crescent)
。实际上,您只给pca算法一个维度。粗略地讲:主成分分析采用多个特征时间序列,并将它们组合为特征的组合成分。如果需要2个组件,则需要多个功能。
以下是一些如何正确使用它的示例:PCA tutorial using sklearn