【问题标题】:Text Classification with Python使用 Python 进行文本分类
【发布时间】:2020-10-14 12:48:53
【问题描述】:

您好,我是 python 编程语言的新手,根据我使用逻辑回归构建文本分类模型的各种参考资料,下面是代码。

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer

import pandas as pd
import numpy as np
import string

import nltk
from collections import Counter
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import sent_tokenize, word_tokenize

from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, accuracy_score, classification_report


Train = pd.read_excel("/Desktop/ML Based Text classification/test.xlsx")
real = pd.read_excel("/Desktop/ML Based Text classification/test.xlsx", sheet_name = 'Test')
Train_data = Train['description']
Test_data = real['description']

stop = stopwords.words('english')
porter = PorterStemmer()

def remove_stopwords(text):
    text = [word.lower() for word in text.split() if word.lower() not in stop]
    return " ".join(text)
def stemmer(stem_text):
    stem_text = [porter.stem(word) for word in stem_text.split()]
    return " ".join(stem_text)

def clean_data(data):
     text_clean =  (data.str.replace('[^\w\s]','')
                  .str.replace('\d+', '')
                  .apply(remove_stopwords)
                  .apply(stemmer)
                  .astype(str))
     return (text_clean)
Train_data = clean_data(Train_data)

counter = Counter(Train['tags'].tolist())
top_10_varieties = {i[0]: idx for idx, i in enumerate(counter.most_common(50))}
Train['Mapping'] = Train['tags'].map(top_10_varieties)
#top_10_varieties = {'Outlook Related Issue': 0, 'Password Reset': 1, 'VPN Issue': 2}


tfidf_converter = TfidfVectorizer()
model_log = LogisticRegression()

X = Train_data
Y = Train['Mapping']
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.95, random_state = 0)

svc = Pipeline([('tfidf', TfidfVectorizer()),
               ('clf',LogisticRegression()),
               ])

svc.fit(X_train, y_train)

ytest = np.array(y_test)
y_pred = svc.predict(X_test)

Test_data = clean_data(Test_data)
y_pred = svc.predict(Test_data)

现在我运行此代码没有错误,当我打印“y_pred”时,我得到的输出为

array([0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 2, 1, 2, 0, 2, 2, 2, 1, 0, 1,
       1, 2, 1, 2, 0, 0, 2, 2, 1, 0, 0, 2, 0, 0, 0], dtype=int64)

我不确定,如何将其转换为映射字符串并将其标记为原始数据,我想要这样的输出:

【问题讨论】:

    标签: python machine-learning text nlp logistic-regression


    【解决方案1】:

    请尝试:

    reverse_top_10_varieties = {idx:i[0] for idx, i in enumerate(counter.most_common(50))}
    [reverse_top_10_varieties[id] for id in y_pred]
    

    看看这是否能解决你的问题

    【讨论】:

      猜你喜欢
      • 2018-05-13
      • 2012-03-27
      • 2014-04-30
      • 2013-07-16
      • 2016-12-27
      • 2016-03-03
      • 2023-03-21
      • 2015-09-30
      相关资源
      最近更新 更多