【发布时间】:2020-04-13 10:27:26
【问题描述】:
我正在尝试使用 PCA sklearn 包进行一些数据分析。我目前遇到的问题是我的代码分析数据的方式。
部分数据示例如下
波长强度 ; [嗯] [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 模型,以产生预期的结果。任何解决方法将不胜感激!
【问题讨论】:
标签: python python-3.x scikit-learn ascii pca