gpt4 book ai didi

python - 在管道 sklearn 中包含特征提取

转载 作者:行者123 更新时间:2023-11-30 09:51:00 25 4
gpt4 key购买 nike

对于一个文本分类项目,我为特征选择和分类器创建了一个管道。现在我的问题是是否可以将特征提取模块包含在管道中以及如何包含。我查了一些相关内容,但它似乎不适合我当前的代码。

这就是我现在拥有的:

# feature_extraction module.  
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.feature_extraction import DictVectorizer
import numpy as np

vec = DictVectorizer()
X = vec.fit_transform(instances)
scaler = StandardScaler(with_mean=False) # we use cross validation, no train/test set
X_scaled = scaler.fit_transform(X) # To make sure everything is on the same scale

enc = LabelEncoder()
y = enc.fit_transform(labels)

# Feature selection and classification pipeline
from sklearn.feature_selection import SelectKBest, mutual_info_classif
from sklearn import model_selection
from sklearn.metrics import classification_report
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import LinearSVC
from sklearn import linear_model
from sklearn.pipeline import Pipeline

feat_sel = SelectKBest(mutual_info_classif, k=200)
clf = linear_model.LogisticRegression()
pipe = Pipeline([('mutual_info', feat_sel), ('logistregress', clf)]))
y_pred = model_selection.cross_val_predict(pipe, X_scaled, y, cv=10)

如何将 dictvectorizer 直到标签编码器放入管道中?

最佳答案

以下是您的操作方法。假设 instances 是一个类似字典的对象,如 API 中指定的那样。 ,然后像这样构建你的管道:

pipe = Pipeline([('vectorizer', DictVectorizer()),
('scaler', StandardScaler(with_mean=False)),
('mutual_info', feat_sel),
('logistregress', clf)])

要进行预测,请调用cross_val_predict,并将实例作为X传递:

y_pred = model_selection.cross_val_predict(pipe, instances, y, cv=10)

关于python - 在管道 sklearn 中包含特征提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45173321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com