简体   繁体   English

类型错误:PCA() 得到了一个意外的关键字参数“n_components”

[英]TypeError: PCA() got an unexpected keyword argument 'n_components'

Hi I was trying to implement the PCA(), but I'm getting an error, '嗨,我正在尝试实现 PCA(),但出现错误,'

TypeError: PCA() got an unexpected keyword argument 'n_components'.类型错误:PCA() 得到了一个意外的关键字参数“n_components”。

from sklearn.decomposition import PCA
#Principal component analysis
def PCA(X,Y):
    pca = PCA(n_components=2)
    X = pca.fit_transform(X)

    plot_2d_space(X, Y, 'Imbalanced dataset (2 PCA components)')

Can someone please tell me a possible reason for this有人可以告诉我一个可能的原因吗

First you are importing from sklearn.decomposition import PCA and then you're using same name for your function def PCA首先,您from sklearn.decomposition import PCA ,然后您的函数def PCA使用相同的名称

So next time you'll call the function, it will call your function not from the scikit-learn function.因此,下次您调用该函数时,它将不会从scikit-learn函数中调用您的函数。

So basically pca = PCA(n_components=2) expects the arguments X and Y where you're passing n_components .所以基本上pca = PCA(n_components=2)期望参数 X 和 Y 传递n_components

Solution :解决方案

Change the name of your function and it should work:更改函数的名称,它应该可以工作:

def PCA_2(X,Y):
    pca = PCA(n_components=2)
    X = pca.fit_transform(X)

    plot_2d_space(X, Y, 'Imbalanced dataset (2 PCA components)')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:__init __()获得了意外的关键字参数'n_components' - TypeError: __init__() got an unexpected keyword argument 'n_components' TypeError: __init__() 得到了一个意外的关键字参数“n_folds” - TypeError: __init__() got an unexpected keyword argument 'n_folds' 类型错误:__init__() 得到了一个意外的关键字参数“n_iter” - TypeError: __init__() got an unexpected keyword argument 'n_iter' TypeError:KMeans()获得了意外的关键字参数'n_clusters' - TypeError: KMeans() got an unexpected keyword argument 'n_clusters' TypeError: plotImages() 得到了一个意外的关键字参数 'n_images' Python - TypeError: plotImages() got an unexpected keyword argument 'n_images' Python TypeError:得到一个意外的关键字参数 - TypeError: got an unexpected keyword argument sklearn.pca()和n_components,线性代数难题 - sklearn.pca() and n_components, linear algebra dilemma 确定pca分析中n_components变量的值 - Determine the value of n_components variable in pca analysis 确定 PCA 的 n_components 使得解释的方差比为 0.99 - Determine n_components of PCA such that the explained variance ratio is 0.99 sklearn.cluster.KMeans 得到“TypeError:__init__() 得到了一个意外的关键字参数‘n_jobs’” - sklearn.cluster.KMeans got "TypeError: __init__() got an unexpected keyword argument 'n_jobs'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM