Python 数据获取与可视化分析综合练习题

Python 数据获取与可视化分析综合练习题

实验内容:
(1) 用自己的姓名或学号新建一个文件夹,在文件夹中新建一个文本文件 “111.txt”。
(2) 从网上复制“当好学生成长的引路人——习近平总书记给全国高校黄大 年 式 教 师 团 队 代 表 的 回 信 引 发 强 烈 反 响 ( 网 址 : http://www.xinhuanet.com/2021-09/13/c_1127858064.htm)”中的文本内容 保存到“111.txt”中。
(3) 将“111.txt”文件中的内容的中文提取到“222.txt”文档中(使用 re 方法)。
(4) 使用 jieba 对提取的中文文档(222.txt)进行分词处理,并统计出现频 率最高的前 100 个。
(5) 使用 wordcloud 和 matplotlib.pyplot 方法对“222.txt”中的分词制作 分词云图,并保存为“分词云图 1.png”。
(6) 使用“222.txt”中出现频率最高的前 10 个词语,制作条形图,X 轴对应 10 个词语,y 轴对应每个词语的频率值,保存成“条形图 2.png”。
(附:有余力的同学可以将实验内容的第(1)(2)步换成爬取网页内容的操作。)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#原始版本

import os
import requests
import re
import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import numpy as np
from PIL import Image
import matplotlib

url = r'http://www.xinhuanet.com/2021-09/13/c_1127858064.htm'
response = requests.get(url)
response.encoding = 'utf-8'

path = r'C:\Users\zhangguozhi\Desktop\2020416177'
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
print('创建成功')
else:
print('目录已存在')

path1 = path + '\\' + '111.txt'
file = open(path1, 'w', encoding='utf')
file.write(response.text)
file.close()
file = open(path1, 'r',encoding='utf') #r表示是文本文件,rb是二进制文件
content = file.read()
file.close()

pattern1 = '[\u4e00-\u9fa5]+' #形成字符串列表(提取中文(默认一个),+(至少匹配一个))
#pattern2 = '[A-Za-z0-9\!\%\<\>\,\。]'
re1 = re.compile(pattern1) #正则表达式对象:匹配或替换方式
res1 = re1.findall(content) #指定字符串进行匹配
#res2 = re1.sub(pattern2, '',content) #过滤

path2 = path + '\\' + '222.txt'
file = open(path2, 'w', encoding='utf-8')
n = len(res1)
for i in range(n):
file.write(res1[i])
file.close()

file = open(path2, 'r', encoding='utf-8')
content = file.read()
jb_li = list(jieba.cut(content, cut_all=False))
#print(jb_li)
file.close()

list1 = []
list4 = []
dict1 = {}
n = len(jb_li)
for i in range(n):
if len(jb_li[i])>1:
num = jb_li.count(jb_li[i])
dict1[jb_li[i]] = num
list4.append(jb_li[i])
for i in dict1:
tup = (dict1[i],i)
list1.append(tup)
list1.sort(key=lambda x:x[0],reverse=True)
list2 = list1[0:99]
list3 = [i[1] for i in list2]

fcl = ' '.join(list4)
bgl = np.array(Image.open(r'C:\Users\zhangguozhi\Pictures\Saved Pictures\QQ截图20210529160439.png') ) #功能:将数据转化为矩阵
cyl = WordCloud(
font_path = 'C:\WINDOWS\Fonts\STFANGSO.TTF',
background_color = 'white',
width = 500,
height = 400,
max_font_size = 100,
min_font_size = 10,
mask = bgl
)
cyl.generate(fcl)
plt.figure('词云图')
plt.imshow(cyl) #?
plt.axis('off') #关闭坐标轴
plt.savefig(r'C:\Users\zhangguozhi\Desktop\2020416177\分词云图1.png',dpi=300)
#plt.show()

matplotlib.rcParams['font.family']='SimHei'
x = [list1[0][1],list1[1][1],list1[2][1],list1[3][1],list1[4][1],list1[5][1],list1[6][1],list1[7][1],list1[8][1],list1[9][1]]
y = [list1[0][0],list1[1][0],list1[2][0],list1[3][0],list1[4][0],list1[5][0],list1[6][0],list1[7][0],list1[8][0],list1[9][0]]
figure = plt.figure()
plt.bar(x,y)
plt.savefig(r'C:\Users\zhangguozhi\Desktop\2020416177\条形图2.png',dpi=300)

实验2

在已有网络拓扑基础上,给每个节点随机生成剩余能量(取值范围:1~10)

骨干节点:

  • 随机性
  • 成为骨干节点的要求:剩余能量>=4.2
  • 骨干节点之间是连通的
  • 普通节点表示、普通节点之间连线
  • 骨干节点表示、骨干节点之间连线
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle

n = 80
rl = 0
rr = 100
r = 20

energy = np.random.randint(1,10,size=[n])
a = np.random.randint(rl,rr,size=[n])
b = np.random.randint(rl,rr,size=[n])
fig = plt.figure()
ax = fig.add_subplot(111)
plt.scatter(a,b,c='r',marker='o',s=5)

pre = {}
for i in range(n):
pre[(a[i],b[i])]=[0,0,0]

for i in range(n):
for j in range(i+1,n):
if (a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j])<r*r:
if energy[i]>=4.2 and energy[j]>=4.2:
aa = [a[i],a[j]]
bb = [b[i],b[j]]
plt.plot(aa,bb,color='gold',linewidth=1)
pre[(a[i],b[i])][1] += 1
pre[(a[j],b[j])][1] += 1
else:
aa = [a[i],a[j]]
bb = [b[i],b[j]]
plt.plot(aa,bb,color='skyblue',linewidth=1)
pre[(a[i],b[i])][2] += 1
pre[(a[j],b[j])][2] += 1
pre[(a[i],b[i])][0] += 1
pre[(a[j],b[j])][0] += 1

mini0 = 0;
mini1 = 0;
mini2 = 0;
for j in range(n):
if pre[(a[j],b[j])][0]<mini0:mini0=j
if pre[(a[j],b[j])][1]<mini1:mini1=j
if pre[(a[j],b[j])][2]<mini2:mini2=j
print(pre[(a[mini0],b[mini0])][0],pre[(a[mini1],b[mini1])][1],pre[(a[mini2],b[mini2])][2])
#print(pre)


for i in range(n):
circle = plt.Circle(xy=(a[i],b[i]),radius=20,alpha=0.05,color='lightseagreen')
ax.add_patch(circle)

plt.show()