python - 使用管道进行逻辑回归的文本分类

标签 python machine-learning sklearn-pandas

我正在尝试使用 LogisticRegression用于文本分类。我正在使用 FeatureUnion用于DataFrame 的功能然后 cross_val_score来测试分类器的准确性。但是,我不知道如何在自由文本中包含该功能,名为 tweets ,管道内。我正在使用 TfidfVectorizer对于词袋模型。

nominal_features = ["tweeter", "job", "country"]
numeric_features = ["age"]

numeric_pipeline = Pipeline([
    ("selector", DataFrameSelector(numeric_features))
])

nominal_pipeline = Pipeline([
    ("selector", DataFrameSelector(nominal_features)), 
     "onehot", OneHotEncoder()])

text_pipeline = Pipeline([
    ("selector", DataFrameSelector("tweets")),    
    ("vectorizer", TfidfVectorizer(stop_words='english'))])

pipeline = Pipeline([("union", FeatureUnion([("numeric_pipeline", numeric_pipeline),
                                             ("nominal_pipeline", nominal_pipeline)])), 
                                             ("estimator", LogisticRegression())])

np.mean(cross_val_score(pipeline, df, y, scoring="accuracy", cv=5))

这是包含 tweets 的正确方法吗?管道中的自由文本数据?

最佳答案

pipeline = Pipeline([
('vect', CountVectorizer(stop_words='english',lowercase=True)),
("tfidf1", TfidfTransformer(use_idf=True,smooth_idf=True)),
('clf', MultinomialNB(alpha=1)) #Laplace smoothing
 ])

 train,test=train_test_split(df,test_size=.3,random_state=42, shuffle=True)
 pipeline.fit(train['Text'],train['Target'])

 predictions=pipeline.predict(test['Text'])
 print(test['Target'],predictions)

 score = f1_score(test['Target'],predictions,pos_label='positive',average='micro')
 print("Score of Naive Bayes is :" , score)

关于python - 使用管道进行逻辑回归的文本分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53468055/

相关文章:

python - 使用 PyCluster 优化 K(理想的簇数)

python - 使用 python 比较两个目录中的文件以查找一个目录中的文件而不是另一个目录中的文件 - 与子目录结构无关

python - 加速子数组的洗牌和存储

python - 使用 twinx 时控制跟踪器

machine-learning - VC 维度和 PAC 学习

python - sklearn已安装但无法导入

python - statsmodels OLS 在 python 中给出 TypeError

python - 如何匹配整个数据框中的元素并返回该特定匹配元素的整行或索引?

scikit-learn - Sklearn 错误 : predict(x, y) 需要 2 个位置参数,但给出了 3 个

python - 在 Python 中使用 cx_freeze 时,未添加 VCRUNTIME140.DLL