sklearn进行情感分析

-- coding: utf-8 --

“”"
@Time : 19-9-26 下午2:39
@Author : lei
@Site :
@File : 情感分析.py
@Software: PyCharm
“”"

情感分析

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report
import jieba
import pandas as pd
import numpy as np

data = pd.read_csv("./Train.csv", error_bad_lines=False, names=[“label”])

data.drop(0, axis=0, inplace=True)
data = np.array(data).tolist()

print(data)

data_list = [temp[0].split(’\t’) for temp in data]

print(data_list)

将得到的数据进行遍历得到目标值和特征值 并将数据进行转置

data = np.array([temp[1] for temp in data_list]).T
label = np.array([temp[0] for temp in data_list]).T

print(y_test.shape)

将字符串转换成Tf可识别的字符加空格形式

data = [" ".join(list(jieba.cut(temp))) for temp in data]

x_train, x_test, y_train, y_test = train_test_split(data, label, test_size=0.25)

print(x_train)

tf = TfidfVectorizer()
x_train = tf.fit_transform(x_train).toarray()
x_test = tf.transform(x_test).toarray()

mt = MultinomialNB(alpha=0.1)

mt.fit(x_train, y_train)
predict = mt.predict(x_test)
score = mt.score(x_test, y_test)
print(predict)
print(score)
print(classification_report(y_test, predict, target_names=[“1”, “2”, “0”, “4”, “5”]))

你可能感兴趣的:(机器学习,深度学习,sklearn)