使用kmeans运行管道时,它在打印时中断

2024-10-01 22:41:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用kmeans运行一个管道,但它破坏了一个我已经尝试过单独运行,但仍然不起作用

这是我的原始代码

def proces_test(df):
    numeric_transformer = Pipeline(steps=[
                                        ('imputer', SimpleImputer(strategy='median')),
                                        ('scaler', StandardScaler())])
    categorical_transformer = Pipeline(steps=[
                                ('imputer', SimpleImputer(strategy='constant', fill_value='missing')),
                                ('onehot', OneHotEncoder(handle_unknown='ignore'))])
    preprocessor = ColumnTransformer(
                    transformers=[
                            ('num', numeric_transformer, numeric_features),
                            ('cat', categorical_transformer, categorical_features)])
    return preprocessor







def pipecl(df):

    km = KMeans(n_clusters=5)
    pipe = Pipeline(steps=[('preprocessor', proces_test(df)),
                      ('classifier', km)])


    pipe.fit(df)

    km_pred = pipe.predict(df)
    print(km_pred)
    centroids = pipe.named_steps['classifier'].cluster_centers_
    print(centroids)


    plt.scatter(df[:,0],df[:,1], c=km_pred, s=50, cmap='viridis')
    plt.scatter(centroids[:,0],centroids[:,1], c='black', s=200, alpha=.5)
    plt.show()





pipecl(df_test)

这就是我犯的错误

回溯(最近一次呼叫最后一次):

文件“”,第1行,在 pipecl(df_famsa_试验)

文件“”,第16行,在pipecl中 plt.散射(df[:,0],df[:,1],c=km_pred,s=50,cmap='viridis')

文件“C:\ProgramData\Anaconda3\lib\site packages\pandas\core\frame.py”,第2927行,位于getitem indexer=self.columns.get_loc(键)

文件“C:\ProgramData\Anaconda3\lib\site packages\pandas\core\index\base.py”,第2657行,在get\u loc中 返回发动机。获取位置(钥匙)

文件“pandas/_libs/index.pyx”,第108行,在pandas._libs.index.IndexEngine.get_loc中

文件“pandas/_libs/index.pyx”,第110行,在pandas._libs.index.IndexEngine.get_loc中

TypeError:“(片(无,无,无),0)”是无效键


Tags: 文件pandasdfgetindexpltstepsloc

热门问题