首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:数据分析的PCA问题

Python:数据分析的PCA问题
EN

Stack Overflow用户
提问于 2019-12-21 11:01:20
回答 1查看 57关注 0票数 1

我正在尝试用PCA sklearn包做一些数据分析。我目前遇到的问题是我的代码分析数据的方式。

下面是一些数据的示例

波长强度;um 196.078431372549 1.108370393265022E-003 192.307692307692 1.163428008597600E-003 188.679245283019 1.223639983609668E-003

到目前为止编写的代码如下:

代码语言:javascript
复制
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)

一旦运行,数据就会产生这个错误:

代码语言:javascript
复制
    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模型,以产生预期的结果。周围的任何工作都将非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-21 11:09:42

问题是,您通过执行以下操作为您的pca对象提供了一个功能y_intensity_crescentprincipalCrescentY=pca.fit_transform(standard_y_crescent)。实际上,你只给了你的pca算法一个维度。粗略地说:主成分分析采用多个特征时间序列,并将它们组合成多个特征组合的分量。如果你想要2个组件,你需要1个以上的功能。

下面是一些如何正确使用它的示例:PCA tutorial using sklearn

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59433184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档