5------youtube数据的练习(numpy)

【动手】
(1)英国和美国各自YouTube的数据结合之前的matplotlib绘制出各自的评论数量的直方图 ---- 直方图
注意:可以传列表,解决组距除不尽的情况

(2)希望了解英国的YouTube中视频的评论数和喜欢数的关系,应该如何绘制该图 ---- 散点图
注意:趋势是折线图,相关是散点图
【问题1】
英国和美国各自YouTube的数据结合之前的matplotlib绘制出各自的评论数量的直方图 ---- 直方图
注意:可以传列表,解决组距除不尽的情况
# 第一次绘图
'''
点击,       喜欢,      不喜欢,     评论数量 
([“views”,“likes”,“dislikes”,“comment_total”])
'''
import numpy as np
from matplotlib import pyplot as plt

us_path = './code/youtube_video_data/US_video_data_numbers.csv'
t_us = np.loadtxt(us_path,dtype='int',delimiter=',')

t_us_comment = t_us[:,-1]                        # 评论:最后一列
print(t_us_comment.max(),t_us_comment.min())

#  组距取值为250,则组数为20
d = 10000    # 组距为10000
bin_nums = (t_us_comment.max()-t_us_comment.min())//d     # 组数为58
print(bin_nums)   # 组数除不尽,直方图会有偏移



# 直方图 plt.hist(a,组数)
plt.figure(figsize=(20,8),dpi=80)
plt.hist(t_us_comment,bin_nums)
plt.grid()
plt.show()


# 结论:
'''
(1)大多数数据集中在0---50000之间
(2)可以传列表,解决组距除不尽的情况
'''
582624 0
58

在这里插入图片描述

'\n(1)大多数数据集中在0---50000之间\n(2)可以传列表,解决组距除不尽的情况\n'
# 第二次绘图
'''
点击,       喜欢,      不喜欢,     评论数量 
([“views”,“likes”,“dislikes”,“comment_total”])
'''
import numpy as np
from matplotlib import pyplot as plt

us_path = './code/youtube_video_data/US_video_data_numbers.csv'
t_us = np.loadtxt(us_path,dtype='int',delimiter=',')
print(t_us)


# 由于大多数数据集中于 0---50000,故组距取值为250,则组数为20
t_us = t_us[t_us[:,-1]<=50000]
t_us_comment = t_us[:,-1]
print(t_us_comment.max(),t_us_comment.min())

d = 250
bin_nums = (t_us_comment.max()-t_us_comment.min())//d

# 直方图 plt.hist(a,组数)
plt.figure(figsize=(20,8),dpi=80)
plt.hist(t_us_comment,bin_nums)
plt.grid()
plt.show()

# 结论
'''
大多数数据集中在0---5000,故准备调整组距为50,组数为100
'''
[[4394029  320053    5931   46245]
 [7860119  185853   26679       0]
 [5845909  576597   39774  170708]
 ...
 [ 142463    4231     148     279]
 [2162240   41032    1384    4737]
 [ 515000   34727     195    4722]]
46245 0

在这里插入图片描述

'\n大多数数据集中在0---5000,故准备调整组距为50,组数为100\n'
# 第三次绘图
'''
点击,       喜欢,      不喜欢,     评论数量 
([“views”,“likes”,“dislikes”,“comment_total”])
'''
import numpy as np
from matplotlib import pyplot as plt

us_path = './code/youtube_video_data/US_video_data_numbers.csv'
t_us = np.loadtxt(us_path,dtype='int',delimiter=',')
print(t_us)


# 由于大多数数据集中于 0---5000,故组距取值为250,则组数为20
t_us = t_us[t_us[:,-1]<=5000]
t_us_comment = t_us[:,-1]
print(t_us_comment.max(),t_us_comment.min())

d = 250
bin_nums = (t_us_comment.max()-t_us_comment.min())//d

# 直方图 plt.hist(a,组数)
plt.figure(figsize=(20,8),dpi=80)
plt.hist(t_us_comment,bin_nums)
plt.grid()
plt.show()

# 结论
'''
大多数数据集中在0---5000,故准备调整组距为250,组数为20
'''
[[4394029  320053    5931   46245]
 [7860119  185853   26679       0]
 [5845909  576597   39774  170708]
 ...
 [ 142463    4231     148     279]
 [2162240   41032    1384    4737]
 [ 515000   34727     195    4722]]
4995 0

在这里插入图片描述

'\n大多数数据集中在0---5000,故准备调整组距为250,组数为20\n'
【问题2】
希望了解英国的YouTube中视频的评论数和喜欢数的关系,应该如何绘制该图 ---- 散点图
注意:趋势是折线图,相关是散点图
# 第一次绘图
'''
点击,       喜欢,      不喜欢,     评论数量 
([“views”,“likes”,“dislikes”,“comment_total”])
'''

import numpy as np
from matplotlib import pyplot as plt

uk_path = './code/youtube_video_data/US_video_data_numbers.csv'
us_path = './code/youtube_video_data/US_video_data_numbers.csv'

# t1 = np.loadtxt(us_file_path,delimiter=',',dtype='int',unpack=True)
t_uk = np.loadtxt(uk_path,dtype='int',delimiter=',')
print(t_uk)            

t_uk_comment = t_uk[:,-1]      # Y轴----评论:第4列
t_uk_like = t_uk[:,1]          # x轴----喜欢:第2列

# 散点图
plt.figure(figsize=(20,8),dpi=80)
plt.scatter(t_uk_like,t_uk_comment)
plt.show()

# 结论
'''
大多数数据集中于 0 ---- 500000
保持数据的一致性
'''
[[4394029  320053    5931   46245]
 [7860119  185853   26679       0]
 [5845909  576597   39774  170708]
 ...
 [ 142463    4231     148     279]
 [2162240   41032    1384    4737]
 [ 515000   34727     195    4722]]

在这里插入图片描述

# 第二次绘图 
'''
点击,       喜欢,      不喜欢,     评论数量 
([“views”,“likes”,“dislikes”,“comment_total”])
'''
import numpy
from matplotlib import pyplot as plt

uk_path = './code/youtube_video_data/US_video_data_numbers.csv'
t_uk = np.loadtxt(uk_path,dtype='int',delimiter=',')
print(t_uk)

# (1)切片:选择部分数据  t_uk_lke <= 500000
# (2)保持数据的一致性
t_uk = t_uk[t_uk[:,1]<=500000]

t_uk_comment = t_uk[:,-1]
t_uk_like = t_uk[:,1]

# 散点图
plt.figure(figsize=(20,8),dpi=80)
plt.scatter(t_uk_like,t_uk_comment)
plt.show()
[[4394029  320053    5931   46245]
 [7860119  185853   26679       0]
 [5845909  576597   39774  170708]
 ...
 [ 142463    4231     148     279]
 [2162240   41032    1384    4737]
 [ 515000   34727     195    4722]]

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值