学习Python的第三天

实现词云的绘制

步骤:

1.绘制词云的形状

from wordcloud import WordCloud
import jieba
import imageio

mask = imageio.imread('./china.jpg')    #要绘制词云的形状

2.读取小说内容

with open('./novel/threekingdom.txt', 'r', encoding='utf-8') as f:
    words = f.read()

    counts = {}  # {‘曹操’:234,‘回寨’:56}
    excludes = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",
                "如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",
                "东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",
                "孔明曰","玄德曰","刘备","云长"}

3.分词

    words_list = jieba.lcut(words)
    # print(words_list)
    for word in words_list:
        if len(word) <= 1:
            continue
        else:
            # 更新字典中的值
            # counts[word] = 取出字典中原来键对应的值 + 1
            # counts[word] = counts[word] + 1  # counts[word]如果没有就要报错
            # 字典。get(k) 如果字典中没有这个键 返回 NONE
            counts[word] = counts.get(word, 0) + 1

4.词语过滤,删除无关词,重复词

    counts['孔明'] =  counts['孔明'] +  counts['孔明曰']
    counts['玄德'] = counts['玄德'] + counts['玄德曰'] +counts['刘备']
    counts['关公'] = counts['关公'] +counts['云长']
    for word in excludes:
        del counts[word]

5.排序

    items = list(counts.items())
    print(items)

    def sort_by_count(x):
        return x[1]
    items.sort(key=sort_by_count, reverse=True)

6.序列解包

    li=[]
    for i in range(10):
        # 序列解包
        role, count = items[i]
        print(role, count)
        for _ in range(count):      #_是告诉看代码的人循环里不需要使用临时变量
            li.append(role)

7.结论

text=' '.join(li)
    WordCloud(
        font_path='msyh.ttc',
        background_color='white',
        width=800,
        height=600,
        mask=mask,
        #相邻两个值的重复
        collocations=False
    ).generate(text).to_file('Top10.png')

匿名函数

匿名函数:lambda函数是一种快速定义单行的最小函数,可以用在任何需要用到函数的地方。
匿名函数返回值是一个函数对象,可以使用变量去接收这个对象。例如 x = lambda x,y:x*y ,代表x是一个计算两数想成的函数,调用时可以写成x(3,5)

匿名函数的优点:

-使代码精简
-有些只使用一次的函数,不用为其命名
-让代码更容易理解

格式:

lambda 参数:返回值

例子

sum=lambda x1,x2:x1+x2
print(sum (2,3))

参数可以有无数个,但是表达式只能有一个
下例函数改为为匿名函数

name_info_list = [
    ('张三',4500),
    ('李四',9900),
    ('王五',2000),
    ('赵六',5500),
]
def sort_by_gz(x):
     return x[1]
name_info_list.sort(key = sort_by_gz)
print('排序后', name_info_list)

修改之后

name_info_list = [
    ('张三',4500),
    ('李四',9900),
    ('王五',2000),
    ('赵六',5500),
]
name_info_list.sort(key=lambda x:x[1],reverse=True)
print(name_info_list)

lambda匿名函数是python语言的一种特色,当我们不需要定义一个函数的时候,可以使用匿名函数来做。

匿名函数的限制:

就是只能有一个表达式,不用写return,返回值就是该表达式的结果。

匿名函数的好处:

即函数没有名字,不用担心函数名冲突,此外,匿名函数也是一个函数对象,也可以把匿名函数赋值给一个变量,再利用变量来调用该函数。

列表推导式,列表解析和字典解析

推导式comprehensions(又称解析式),是Python的一种独有特性。推导式是可以从一个数据序列构建另一个新的数据序列的结构体。

1.列表推导式

之前我们创建列表是利用for循环

li=[]
for i in range(10):
    li.append(i)
print(li)

列表推导式,也叫列表解析式,英文名称为list comprehension,可以使用非常简洁的方式来快速生成满足特定需求的列表,代码具有非常强的可读性。另外,Python的内部实现对列表推导式做了大量优化,可以保证很快的运行速度。

格式:[表达式 for 临时变量 in 可迭代对象 可以追加条件]

使用列表推导式

print([i for i in range(10)])

利用列表推导式只用一条语句就可以创建列表了。

列表解析

筛选出列表中所以的偶数

li=[]
for i in range(10):
    if i % 2 ==0:
        li.append(i)
print(li)
使用列表解析
print([i for i in range(10) if i%2==0])

筛选出列表中大于0的数(随机产生10个数)

from  random import  randint
num_list=[randint(-10,10) for _ in range(10)]
print(num_list)
print([i for i in num_list if i>0])
字典解析

生成100给学生的成绩

stu_grades={'student{}'.format(i):randint(50,100) for i in range (1,101)}
print(stu_grades)

筛选大于60分的所以学生

print({k:v for k,v in stu_grades.items() if v>60})

matplotlib库

1.曲线图

from matplotlib import pyplot as plt

用100个点绘制正弦曲线图[0,2pi]

import numpy as np

plt.rcParams["font.sans-serif"] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False


x=np.linspace(0,2*np.pi,num=100)
print(x)
y=np.sin(x)
plt.plot(x,y,color='g',linestyle='--',label='sin(x)')

cosy=np.cos(x)

plt.plot(x,cosy, color='r',label='cos(x)')
plt.xlabel('时间(s)')
plt.ylabel('电压(v)')
plt.title('欢迎来到python世界')

plt.legend()
plt.show()

2.柱状图

import string
from random import randint
# print(string.ascii_uppercase[0:6])
# ['A', 'B', 'C'...]
x = ['口红{}'.format(x) for x in string.ascii_uppercase[0:5]]

y = [randint(200, 500) for _ in range(5)]
print(x)
print(y)
plt.xlabel('口红品牌')
plt.ylabel('价格(元)')
plt.bar(x, y)
plt.show()

3.饼图

随机产生6个员工的工资范围在(3500, 9000),并画出饼图

from random import randint
import string
counts = [randint(3500, 9000) for _ in range(6)]
labels = ['员工{}'.format(x) for x in string.ascii_lowercase[:6] ]
# 距离圆心点距离
explode = [0.1,0,0, 0, 0,0]
colors = ['red', 'purple','blue', 'yellow','gray','green']
plt.pie(counts,explode = explode,shadow=True, labels=labels, autopct = '%1.1f%%',colors=colors)
plt.legend(loc=2)
plt.axis('equal')
plt.show()

4.散点图

均值为0,标准差为1的正太分布数据

x=np.random.normal(0,1,100)
y=np.random.normal(0,1,100)
plt.scatter(x,y,alpha=0.5)
plt.show()
x=np.random.normal(0,1,100000)
y=np.random.normal(0,1,100000)
plt.scatter(x,y,alpha=0.1)
plt.show()

将之前做好的三国top10人物以饼图的方式展示

    li=[]
    peo_li=[]
    for i in range(10):
        # 序列解包
        role, count = items[i]
        a={'name':'','count':0}
        a['name']=role
        a['count']=count
        peo_li.append(a)
        print(role, count)
        for _ in range(count):      #_是告诉看代码的人循环里不需要使用临时变量
            li.append(role)
#在解包的同时将前十的人物即出现次数存放当peo_list列表中
    counts = []
    labels = []
    for i in range(len(peo_li)):
        counts.append(peo_li[i]['count'])
        labels.append(peo_li[i]['name'])
    # 距离圆心点距离
    explode = [0.1, 0, 0, 0, 0, 0,0,0,0,0]
    #colors = ['red', 'purple', 'blue', 'yellow', 'gray', 'green']
    plt.pie(counts, explode=explode, shadow=True, labels=labels, autopct = '%1.1f%%')
    plt.legend(loc=2)
    plt.axis('equal')
    plt.show()

完整代码:

from wordcloud import WordCloud
import jieba
import imageio

mask = imageio.imread('./china.jpg')
1.读取小说内容
with open('./novel/threekingdom.txt', 'r', encoding='utf-8') as f:
    words = f.read()

    counts = {}  # {‘曹操’:234,‘回寨’:56}
    excludes = {"将军", "却说", "丞相", "二人", "不可", "荆州", "不能", "如此", "商议",
                "如何", "主公", "军士", "军马", "左右", "次日", "引兵", "大喜", "天下",
                "东吴", "于是", "今日", "不敢", "魏兵", "陛下", "都督", "人马", "不知",
                "孔明曰","玄德曰","刘备","云长"}
2. 分词
    words_list = jieba.lcut(words)
    # print(words_list)
    for word in words_list:
        if len(word) <= 1:
            continue
        else:
            # 更新字典中的值
            # counts[word] = 取出字典中原来键对应的值 + 1
            # counts[word] = counts[word] + 1  # counts[word]如果没有就要报错
            # 字典。get(k) 如果字典中没有这个键 返回 NONE
            counts[word] = counts.get(word, 0) + 1

    print(len(counts))
3. 词语过滤,删除无关词,重复词
    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 i: i[1], reverse=True)
    li=[]
    peo_li=[]
    for i in range(10):
        # 序列解包
        role, count = items[i]
        a={'name':'','count':0}
        a['name']=role
        a['count']=count
        peo_li.append(a)
        print(role, count)
        for _ in range(count):      #_是告诉看代码的人循环里不需要使用临时变量
            li.append(role)
5.得出结论
    text=' '.join(li)
    WordCloud(
        font_path='msyh.ttc',
        background_color='white',
        width=800,
        height=600,
        mask=mask,
        #相邻两个值的重复
        collocations=False
    ).generate(text).to_file('Top10.png')

    #用饼图显示人物
    from random import randint
    import string
    from matplotlib import pyplot as plt

    plt.rcParams["font.sans-serif"] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False

    counts = []
    labels = []
    for i in range(len(peo_li)):
        counts.append(peo_li[i]['count'])
        labels.append(peo_li[i]['name'])
    # 距离圆心点距离
    explode = [0.1, 0, 0, 0, 0, 0,0,0,0,0]
    #colors = ['red', 'purple', 'blue', 'yellow', 'gray', 'green']
    plt.pie(counts, explode=explode, shadow=True, labels=labels, autopct = '%1.1f%%')
    plt.legend(loc=2)
    plt.axis('equal')
    plt.show()

练习:将红楼梦的top10人物绘制饼图

完整代码

from wordcloud import WordCloud
import jieba
import imageio

mask = imageio.imread('./china.jpg')
1.读取小说内容
with open('./novel/all.txt', 'r', encoding='utf-8') as f:
    words = f.read()
    #print(words)
    counts = {}
    excludes = {"什么", "一个", "我们", "你们", "如今", "说道", "知道", "起来", "这里",
               "出来", "众人", "那里", "自己", "一面", "只见", "太太", "两个", "没有",
               "怎么", "不是", "不知", "这个", "听见", "这样", "进来", "咱们", "就是",
               "老太太", "东西", "告诉", "回来", "只是", "大家", "姑娘", "奶奶", "凤姐儿","分节"}
2. 分词
    words_list = jieba.lcut(words)
    for word in words_list:
        if len(word) <= 1:
            continue
        else:
            # 更新字典中的值
            # counts[word] = 取出字典中原来键对应的值 + 1
            # counts[word] = counts[word] + 1  # counts[word]如果没有就要报错
            # 字典。get(k) 如果字典中没有这个键 返回 NONE
            counts[word] = counts.get(word, 0) + 1

    print(len(counts))
3. 词语过滤,删除无关词,重复词
    counts['贾母'] =  counts['贾母'] +  counts['老太太']
    counts['宝钗'] = counts['宝钗'] + counts['薛宝钗']
    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)
   items.sort(key=lambda i: i[1], reverse=True)
    li=[]
    peo_li=[]
    for i in range(10):
        # 序列解包
        role, count = items[i]
        a={'name':'','count':0}
        a['name']=role
        a['count']=count
        peo_li.append(a)
        print(role, count)
        for _ in range(count):      #_是告诉看代码的人循环里不需要使用临时变量
            li.append(role)
5.得出结论
    text=' '.join(li)
    WordCloud(
        font_path='msyh.ttc',
        background_color='white',
        width=800,
        height=600,
        mask=mask,
        #相邻两个值的重复
        collocations=False
    ).generate(text).to_file('红楼Top10.png')

    #用饼图显示人物
    from random import randint
    import string
    from matplotlib import pyplot as plt

    plt.rcParams["font.sans-serif"] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False

    counts = []
    labels = []
    for i in range(len(peo_li)):
        counts.append(peo_li[i]['count'])
        labels.append(peo_li[i]['name'])
    # 距离圆心点距离
    explode = [0.1, 0, 0, 0, 0, 0,0,0,0,0]
    #colors = ['red', 'purple', 'blue', 'yellow', 'gray', 'green']
    plt.pie(counts, explode=explode, shadow=True, labels=labels, autopct = '%1.1f%%')
    plt.legend(loc=2)
    plt.axis('equal')
    plt.show()

你可能感兴趣的:(学习Python的第三天)