使用混合类型特征的scikit learn分类器在测试数据上返回0%准确率

我刚开始学习机器学习和Python。我想使用sklearn中的DecisionTreeClassifier。由于我的特征部分是数值型,部分是分类型,我需要对它们进行转换,因为DecisionTreeClassifier只接受数值型特征作为输入。为此,我使用了ColumnTransformer和管道。想法如下:

  1. 分类和数值特征在不同的管道中进行转换
  2. 两者结合形成分类器的输入

然而,使用我的测试数据的准确率总是0%,而我的训练数据的准确率约为85%。此外,调用cross_val_score()返回

ValueError: Found unknown categories ['Holand-Netherlands'] in column 7 during transform

这很奇怪,因为我用这些数据训练了full_pipeline。使用不同的分类器会导致相同的结果,这让我认为转换存在问题。非常感谢您的帮助!

以下是我的代码:

names = ["age",         "workclass",         "final-weight",         "education",         "education-num",         "martial-status",         "occupation",         "relationship",         "race",         "sex",         "capital-gain",         "capial-loss",         "hours-per-week",         "native-country",         "agrossincome"]categorical_features = ["workclass", "education", "martial-status", "occupation", "relationship", "race", "sex", "native-country"]numerical_features = ["age","final-weight", "education-num", "capital-gain", "capial-loss", "hours-per-week"] features = np.concatenate([categorical_features, numerical_features])# create pandas dataframe for adult datasetadult_train = pd.read_csv(filepath_or_buffer= "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data" ,            delimiter= ',',            index_col = False,            skipinitialspace = True,            header = None,            names = names )adult_test = pd.read_csv( filepath_or_buffer= "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.test" ,            delimiter= ',',            index_col = False,            skipinitialspace = True,            header = None,            names = names )adult_test.drop(0, inplace =True)adult_test.reset_index(inplace = True)adult_train.replace(to_replace= "?", value = np.NaN, inplace = True)adult_test.replace(to_replace= "?", value = np.NaN, inplace= True)# split data into features and targetsx_train = adult_train[features]y_train = adult_train.agrossincomex_test = adult_test[features]y_test = adult_test.agrossincome# create pipeline for preprocessing + classifiercategorical_pipeline = Pipeline( steps = [ ( 'imputer', SimpleImputer(strategy='constant', fill_value='missing') ),                                           ( 'encoding', OrdinalEncoder() )                                          ])numerical_pipeline = Pipeline( steps = [ ( 'imputer', SimpleImputer(strategy='median') ),                                         ( 'std_scaler', StandardScaler( with_mean = False ) )                                        ])preprocessing = ColumnTransformer( transformers = [ ( 'categorical_pipeline', categorical_pipeline, categorical_features ),                                                    ( 'numerical_pipeline', numerical_pipeline, numerical_features ) ] )full_pipeline = Pipeline(steps= [ ('preprocessing', preprocessing),                                  ('model', DecisionTreeClassifier(random_state= 0, max_depth = 5) ) ])full_pipeline.fit(x_train, y_train)print(full_pipeline.score(x_test, y_test))#print(cross_val_score(full_pipeline, x_train, y_train, cv=3).mean())

回答:

错误来自于y_test,它看起来像

enter image description here

enter image description here

删除末尾的’.’应该可以解决这个问题

enter image description here

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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