error on preprocessing machine learning

我在尝试对训练数据进行预处理,并且还尝试了reshape函数,但那没有奏效,我得到了以下错误:

ValueError: Found input variables with inconsistent numbers of samples: [34, 12700]

这是我的代码:

import pandas as pdimport numpy as npfrom sklearn import preprocessing,neighborsfrom sklearn.model_selection import train_test_splitfrom sklearn.ensemble import RandomForestRegressordf=pd.read_csv('train.csv')df.drop(['ID'],1,inplace=True)X=np.array(df.drop(['label'],1))y=np.array(df['label'])print(X.shape)X = preprocessing.StandardScaler().fit(X)X=X.mean_X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)clf = RandomForestRegressor(n_estimators=1900,max_features='log2',max_depth=25)clf.fit(X_train,y_train)accuracy=clf.score(X_test,y_test)print(accuracy)

回答:

问题出在 X = preprocessing.StandardScaler().fit(X)X=X.mean_

在这之后,你的X将只包含每列的均值。

要转换数据,请使用以下代码:

from sklearn.preprocessing import StandardScalerscaler = StandardScaler()scaler.fit(X)X = scaler.transform(X)

更多详情请参考 scikit-doc

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注