# coding: utf-8
import sys
import re
import csv
import numpy as np
import random
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import pylab as pl
if sys.version_info[0] > 2:
is_py3 = True
else:
reload(sys)
sys.setdefaultencoding("utf-8")
is_py3 = False
def native_word(word, encoding='utf-8'):
"""如果在python2下面使用python3训练的模型,可考虑调用此函数转化一下字符编码"""
if not is_py3:
return word.encode(encoding)
else:
return word
def native_content(content):
if not is_py3:
return content.decode('utf-8')
else:
return content
def open_file(filename, mode='r'):
"""
常用文件操作,可在python2和python3间切换.
mode: 'r' or 'w' for read or write
"""
if is_py3:
return open(filename, mode, encoding='utf-8', errors='ignore')
else:
return open(filename, mode)
def read_csvfile(filename):
with open(filename) as csvfile:
readCSV = csv.reader(csvfile,delimiter=',') # 读取训练数据
data = []
label = []
emo = []
for row in readCSV:
if row[0] == '序号':
continue
data.append(row[2])
label.append(row[1])
emo.append(int(row[3]))
return data, label, emo
if __name__ == '__main__':
# dataframe = pd.DataFrame({'confidence': con, 'featureNum': fea, 'F值': F})
# dataframe.to_csv('sem.csv', index=True, sep=',',encoding='utf-8')
data, label, emo = read_csvfile('emotion.csv')
sta = []
for i in range(len(label)):
if label[i] not in sta:
sta.append(label[i])
print(len(sta))
num = [[0, 0, 0, 0, 0, 0, 0] for i in range(len(sta))]
nn = [1 ,2, 3, 4, 5, 6, 7]
print(sta)
for i in range(len(label)):
for j in range(len(sta)):
if label[i] == sta[j]:
num[j][emo[i] - 1] += 1
print(num)
# plot1 = pl.plot(fea, F, label='me') # use pylab to plot x and y
pl.title("比喻")
pl.xlabel("emotion")
pl.ylabel("num")
# pl.show()
# dataframe = pd.DataFrame({'confidence': con, 'featureNum': fea, 'F值': F})
# dataframe.to_csv('sem_svm.csv', index=True, sep=',', encoding='utf-8')
plot = pl.plot(nn, num[0], label='svm') # use pylab to plot x and y
# pl.legend() # make legend
pl.show()
with open('emo.txt', 'w', encoding='utf-8') as f:
for i in range(len(num)):
f.write(sta[i])
f.write('\t')
for j in range(len(num[i])):
f.write(str(num[i][j]))
f.write('\t')
f.write('\n')
【折线图】python简单绘制折线图
最新推荐文章于 2024-08-26 08:54:09 发布