使用分类和文本数据作为输入的机器学习分类

我有一个大约400行的数据集,其中包含几个分类数据列,还有一列文本形式的描述作为我的分类模型的输入。我计划使用SVM作为我的分类模型。由于模型无法接受非数值数据作为输入,因此我已经将输入特征转换为数值数据。

我对描述列进行了TF-IDF转换,将术语转换成了矩阵形式。

我是否需要使用标签编码转换分类特征,然后将其与TF-IDF合并后输入到机器学习模型中?


回答:

使用ColumnTransformer对不同数据类型的列应用不同的管道转换。这里是一个例子:

from sklearn.compose import ColumnTransformerfrom sklearn.pipeline import Pipelinefrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.preprocessing import OneHotEncoderfrom sklearn.svm import SVC# pipeline for text datatext_features = 'text_column'text_transformer = Pipeline(steps=[    ('vectorizer', TfidfVectorizer(stop_words="english"))])# pipeline for categorical datacategorical_features = ['cat_col1', 'cat_col2',]categorical_transformer = Pipeline(steps=[    ('imputer', SimpleImputer(strategy='constant', fill_value='missing')),    ('onehot', OneHotEncoder(handle_unknown='ignore'))])# you can add other transformations for other data types# combine preprocessing with ColumnTransformerpreprocessor = ColumnTransformer(    transformers=[        ('text', text_transformer, text_features),        ('cat', categorical_transformer, categorical_features)])# add model to be part of pipelineclf_pipe =  Pipeline(steps=[('preprocessor', preprocessor),                   ("model", SVC())])# ...## you can just use preprocessor by itself# X_train = preprocessor.fit_transform(X_train)# X_test = preprocessor.transform(X_test)# clf_s= SVC().fit(X_train, y_train)# clf_s.score(X_test, y_test)## or better, you can use the whole.# clf_pipe.fit(X_train, y_train) # clf_pipe.score(X_test, y_test)

查看Scikit-learn示例了解更多详情

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中创建了一个多类分类项目。该项目可以对…

发表回复

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