Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:数据分析的PCA问题_Python_Python 3.x_Scikit Learn_Ascii_Pca - Fatal编程技术网

Python:数据分析的PCA问题

Python:数据分析的PCA问题,python,python-3.x,scikit-learn,ascii,pca,Python,Python 3.x,Scikit Learn,Ascii,Pca,我试图用PCA sklearn软件包做一些数据分析。我目前遇到的问题是我的代码分析数据的方式 下面是一些数据的示例 波长强度 ; [嗯][W/m**2/um/sr] 196.078431372549 1.10837039265022E-003 192.3076920307692 1.1634280008597600E-003 188.679245283019 1.223639983609668E-003 目前编写的代码如下: scaler = StandardScaler(with

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

下面是一些数据的示例

波长强度 ; [嗯][W/m**2/um/sr] 196.078431372549 1.10837039265022E-003 192.3076920307692 1.1634280008597600E-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模型,以产生预期的结果。任何周围的工作将不胜感激

问题在于,通过执行:
principalCrescentY=pca.fit\u transform(standard\u y\u crescent)
为pca对象提供了一个功能。实际上,您只为pca算法提供了一个维度。粗略地说:主成分分析采用多个特征时间序列,并将它们组合成特征组合的成分。如果需要2个组件,则需要1个以上的功能


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

你是对的。X只是一个向量。采用两个组件是没有意义的,因为甚至没有两个特性。请注意,您得到的错误是由于
X
的形状造成的。它应该是二维阵列,因此,您应该执行
X.restrape(-1,1)
以获得单个特征阵列。但是你仍然会遇到前面提到的问题。注意,应该使用
sklearn.pipeline.pipeline
连接标准化和PCA:
transformer=make_管道(StandardScaler(),PCA());transformer.fit_transform(X)
是的,就是这样。此外,数据不一定需要标准化。感谢您的帮助。@domryan很高兴它起了作用!别忘了为其他用户接受答案