1、三國(guó)演義Top10人物分析
import jieba
from wordcloud import WordCloud
import imageio
# 1.讀取小說(shuō)內(nèi)容
with open('./novel/threekingdom.txt', 'r', encoding='utf-8') as f:
words = f.read()
counts = {} # counts = {'姓名':出現(xiàn)頻率}
excludes = {"將軍", "卻說(shuō)", "丞相", "二人", "不可", "荊州", "不能", "如此", "商議",
"如何", "主公", "軍士", "軍馬", "左右", "次日", "引兵", "大喜", "天下",
"東吳", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人馬", "不知",
"孔明曰", "玄德曰", "劉備", "云長(zhǎng)"}
# 2.分詞
words_list = jieba.lcut(words)
print(words_list)
for word in words_list:
if len(word) <= 1:
continue
else:
# 更新字典中的值
# counts[word] = 取出字典中原來(lái)鍵對(duì)應(yīng)的值 + 1
# counts[word] = counts[word] + 1
# 字典.get(k) 如果字典中沒(méi)有這個(gè)鍵 ,(返回NONE)添加一個(gè)默認(rèn)值:0
counts[word] = counts.get(word, 0) + 1
print(counts)
# 3.詞語(yǔ)過(guò)濾,刪除無(wú)關(guān)詞,重復(fù)詞
counts['孔明'] = counts['孔明'] + counts['孔明曰']
counts['玄德'] = counts['玄德'] + counts['玄德曰'] + counts['劉備']
counts['關(guān)公'] = counts['關(guān)公'] + counts['云長(zhǎng)']
for word in excludes:
del counts[word]
# 4.排序[(), ()]
items = list(counts.items())
print(items)
# def sort_by_count(x):
# return x[1]
# items.sort(key=sort_by_count, reverse=True)
# 用列表解析排序
items.sort(key=lambda x: x[1], reverse=True)
# print(items)
li = [] # ['孔明', '孔明', '孔明',..., '曹操'...]
for i in range(10):
# 序列解包
role, count = items[i]
print(role, count)
# _ 是告訴看代碼的人,循環(huán)里面不需要使用臨時(shí)變量
for _ in range(count):
li.append(role)
# 得出結(jié)論
# 繪制中文詞云,在WordCloud()里面設(shè)置參數(shù)
text = ' '.join(li)
wc = WordCloud(
font_path='msyh.ttc',
background_color='white',
width=800,
height=600,
# 相鄰兩個(gè)重復(fù)詞之間的匹配,關(guān)掉
collocations=False
).generate(text)
wc.to_file('三國(guó)TOP10人物詞云.png')
結(jié)果:
Top10人物
2、匿名函數(shù) lambda
- 結(jié)構(gòu): lambda x1, x2, ...xn: 表達(dá)式
eg:
sum_num = lambda x1, x2: x1+x2
print(sum_num(2, 3))
# 結(jié)果:5
- 參數(shù)可以是無(wú)限多個(gè),但是表達(dá)式只有一個(gè)
eg1:從大到小排序
name_info_list = [
('張三', 4500),
('李四', 9500),
('王五', 2000),
('趙六', 5500),
]
name_info_list.sort(key=lambda x: x[1], reverse=True)
print(name_info_list)
# 結(jié)果:[('李四', 9500), ('趙六', 5500), ('張三', 4500), ('王五', 2000)]
eg2:從小到大排序
stu_info = [
{"name": 'zhangsan', "age": 18},
{"name": 'lisi', "age": 30},
{"name": 'wangwu', "age": 99},
{"name": 'tianqi', "age": 3},
]
stu_info.sort(key=lambda i: i['age'])
print(stu_info)
# 結(jié)果:[{'name': 'tianqi', 'age': 3}, {'name': 'zhangsan', 'age': 18}, {'name': 'lisi', 'age': 30}, {'name': 'wangwu', 'age': 99}]
3、列表推導(dǎo)式,列表解析和字典解析
1、列表推導(dǎo)式
- 之前用普通for 創(chuàng)建列表
li = []
for i in range(10):
li.append(i)
print(li)
# 結(jié)果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- 使用列表推導(dǎo)式創(chuàng)建列表
結(jié)構(gòu):[表達(dá)式 for 臨時(shí)變量 in 可迭代對(duì)象 可以追加條件]
print([i for i in range(10)])
# 結(jié)果:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2、 列表解析
- 普通篩選出列表中所有的偶數(shù)
# 篩選出列表中所有的偶數(shù)
li = []
for i in range(10):
if i % 2 == 0:
li.append(i)
print(li)
# 結(jié)果:[0, 2, 4, 6, 8]
- 使用列表解析篩選偶數(shù)
print([i for i in range(10) if i % 2 == 0])
# 結(jié)果:[0, 2, 4, 6, 8]
- 篩選出列表中 大于0 的數(shù)
# 隨機(jī)生成(-10, 10)的10個(gè)數(shù)
from random import randint
num_list = [randint(-10, 10) for _ in range(10)]
print(num_list)
# 輸出num_list中 大于0 的數(shù)
print([i for i in num_list if i > 0])
# 結(jié)果:[-5, -1, -4, -8, -8, -1, -8, 7, 10, -4]
# [7, 10]
3、字典解析
from random import randint
stu_grades = {'student{}'.format(i): randint(50, 100) for i in range(1, 101)}
print(stu_grades)
# 篩選 大于60 分的所有學(xué)生
print({k: v for k, v in stu_grades.items() if v > 60})
結(jié)果:
結(jié)果
4、Matplotlib
- 導(dǎo)入
from matplotlib import pyplot as plt
- 使用100個(gè)點(diǎn) 繪制[0, 2π]正余弦曲線圖
from matplotlib import pyplot as plt
import numpy as np
# 處理中文亂碼
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
# 使用100個(gè)點(diǎn) 繪制[0, 2π]正弦曲線圖
# .linspace 左閉右閉區(qū)間的等差數(shù)列
x = np.linspace(0, 2*np.pi, num=100)
print(x)
y = np.sin(x)
# 正弦和余弦在同一坐標(biāo)系下
cosy = np.cos(x)
plt.plot(x, y, color='g', linestyle='--')
plt.plot(x, cosy, color='r')
plt.xlabel('時(shí)間(s)')
plt.ylabel('電壓(v)')
plt.title('歡迎來(lái)到python世界')
# 圖例
plt.legend()
plt.show()
結(jié)果:
正余弦圖
- 柱狀圖
# 切片
# print(string.ascii_uppercase[0: 6])
# 結(jié)果:ABCDEF
# 柱狀圖
from matplotlib import pyplot as plt
# 處理中文亂碼
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = ['口紅{}'.format() for x in string.ascii_uppercase[:5]]
y = [randint(200, 500) for _ in range(5)]
print(x)
print(y)
plt.xlabel('口紅品牌')
plt.ylabel('價(jià)格(元)')
plt.bar(x, y)
plt.show()
結(jié)果:
柱狀圖
- 餅圖
# 餅圖
from matplotlib import pyplot as plt
# 處理中文亂碼
plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
from random import randint
counts = [randint(3500, 9000) for _ in range(6)]
labels = ['員工{}'.format(x) for x in string.ascii_lowercase[:6]]
# 距離圓心點(diǎn)距離
explode = [0.1, 0, 0, 0, 0, 0]
color = ['red', 'purple', 'blue', 'yellow', 'gray', 'green']
plt.pie(counts, explode=explode, shadow=True, labels=labels, autopct='%1.1f%%', colors=color)
plt.axis('equal')
plt.legend(loc=2)
plt.show()
結(jié)果:
餅圖
- 散點(diǎn)圖
from matplotlib import pyplot as plt
import numpy as np
# 均值為 0 標(biāo)準(zhǔn)差為 1 的正態(tài)分布數(shù)據(jù)
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
plt.scatter(x, y)
plt.show()
結(jié)果:
散點(diǎn)圖
- 有透明度的散點(diǎn)圖
from matplotlib import pyplot as plt
import numpy as np
x = np.random.normal(0, 1, 100)
y = np.random.normal(0, 1, 100)
# alpha透明度
plt.scatter(x, y, alpha=0.1)
plt.show()
結(jié)果:
有透明度的散點(diǎn)圖
5、繪制 三國(guó)top10人物 餅圖
with open('./novel/threekingdom.txt', 'r', encoding='utf-8') as f:
words = f.read()
counts = {} # counts = {'姓名':出現(xiàn)頻率}
excludes = {"將軍", "卻說(shuō)", "丞相", "二人", "不可", "荊州", "不能", "如此", "商議",
"如何", "主公", "軍士", "軍馬", "左右", "次日", "引兵", "大喜", "天下",
"東吳", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人馬", "不知",
"孔明曰", "玄德曰", "劉備", "云長(zhǎng)"}
# 2.分詞
words_list = jieba.lcut(words)
print(words_list)
for word in words_list:
if len(word) <= 1:
continue
else:
# 字典.get(k) 如果字典中沒(méi)有這個(gè)鍵 ,(返回NONE)添加一個(gè)默認(rèn)值:0
counts[word] = counts.get(word, 0) + 1
print(counts)
# 3.詞語(yǔ)過(guò)濾,刪除無(wú)關(guān)詞,重復(fù)詞
counts['孔明'] = counts['孔明'] + counts['孔明曰']
counts['玄德'] = counts['玄德'] + counts['玄德曰'] + counts['劉備']
counts['關(guān)公'] = counts['關(guān)公'] + counts['云長(zhǎng)']
for word in excludes:
del counts[word]
# 4.排序[(), ()]
items = list(counts.items())
# print(items)
# 普通排序
# def sort_by_count(x):
# return x[1]
# items.sort(key=sort_by_count, reverse=True)
# 用列表解析排序
items.sort(key=lambda x: x[1], reverse=True)
# print(items)
li = [] # ['孔明', '孔明', '孔明',..., '曹操'...]
counts = [] # 人物名
labels = [] # 次數(shù)
for i in range(10):
# 序列解包
role, count = items[i]
counts.append(count)
labels.append(role)
# 距離圓心點(diǎn)距離
explode = [0.1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
color = ['red', 'purple', 'blue', 'yellow', 'gray', 'green', 'pink']
plt.pie(counts, explode=explode, shadow=True, labels=labels, autopct='%1.1f%%')
plt.axis('equal')
plt.legend(loc=2)
plt.show()
結(jié)果:
三國(guó)Top10人物餅圖
6、紅樓夢(mèng) Top10 人物分析
import jieba
from wordcloud import WordCloud
import imageio
# 1.讀取小說(shuō)內(nèi)容
with open('./novel/all.txt', 'r', encoding='utf-8') as f:
words = f.read()
counts = {} # counts = {'姓名':出現(xiàn)頻率}
excludes = {"什么", "一個(gè)", "我們", "你們", "如今", "說(shuō)道", "太太", "知道", "姑娘",
"起來(lái)", "這里", "出來(lái)", "眾人", "那里", "自己", "一面", "只見(jiàn)", "兩個(gè)",
"沒(méi)有", "怎么", "不是", "不知", "這個(gè)", "聽(tīng)見(jiàn)", "奶奶", "老太太", "不知",
"這樣", "進(jìn)來(lái)", "咱們", "就是", "東西", "告訴", "回來(lái)", "只是", "大家",
"老爺", "只得", "丫頭", "這些", "他們", "不敢", "出去", "所以", "賈寶玉",
"林黛玉", "薛寶釵", "鳳姐兒", "王熙鳳"}
# 2.分詞
words_list = jieba.lcut(words)
print(words_list)
for word in words_list:
if len(word) <= 1:
continue
else:
# 更新字典中的值
# counts[word] = 取出字典中原來(lái)鍵對(duì)應(yīng)的值 + 1
# counts[word] = counts[word] + 1
# 字典.get(k) 如果字典中沒(méi)有這個(gè)鍵 ,(返回NONE)添加一個(gè)默認(rèn)值:0
counts[word] = counts.get(word, 0) + 1
print(counts)
# 3.詞語(yǔ)過(guò)濾,刪除無(wú)關(guān)詞,重復(fù)詞
counts['寶玉'] = counts['寶玉'] + counts['賈寶玉']
counts['黛玉'] = counts['黛玉'] + counts['林黛玉']
counts['寶釵'] = counts['寶釵'] + counts['薛寶釵']
counts['賈母'] = counts['賈母'] + counts['老太太']
counts['鳳姐'] = counts['鳳姐'] + counts['鳳姐兒'] + counts['王熙鳳']
for word in excludes:
del counts[word]
# 4.排序[(), ()]
items = list(counts.items())
print(items)
# def sort_by_count(x):
# return x[1]
# items.sort(key=sort_by_count, reverse=True)
# 用列表解析排序
items.sort(key=lambda x: x[1], reverse=True)
# print(items)
li = [] # ['孔明', '孔明', '孔明',..., '曹操'...]
for i in range(10):
# 序列解包
role, count = items[i]
print(role, count)
# _ 是告訴看代碼的人,循環(huán)里面不需要使用臨時(shí)變量
for _ in range(count):
li.append(role)
# 得出結(jié)論
# 繪制中文詞云,在WordCloud()里面設(shè)置參數(shù)
text = ' '.join(li)
wc = WordCloud(
font_path='msyh.ttc',
background_color='white',
width=800,
height=600,
# 相鄰兩個(gè)重復(fù)詞之間的匹配,關(guān)掉
collocations=False
).generate(text)
wc.to_file('紅樓夢(mèng)TOP10人物詞云.png')
結(jié)果:
紅樓夢(mèng)TOP10人物詞云