请问一下如何生成指定要求的随机数,要求绝大部分值在5000上下小范围内浮动,偶尔会有较大的偏移。最好是能用python或excel实现。
# 1、建立包含年份与电影类型数量的关系数据框:
for genre in genres_list:
moviesdf[genre] = moviesdf['genres'].str.contains(genre).apply(lambda x:1 if x else 0)
genre_year = moviesdf.loc[:,genres_list]
genre_year.head()
Animation | Foreign | Drama | War | Music | Western | History | Documentary | TV | Action | ... | Horror | Comedy | Mystery | Thriller | Fantasy | Crime | Movie | Fiction | Adventure | Science | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | ... | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 1 | 1 |
1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | ... | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | ... | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 1 | 0 |
3 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | ... | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 |
5 rows × 22 columns
# 把年份作为索引标签
genre_year.index = moviesdf['year']
# 将数据集按年份分组并求和,得出每个年份,各电影类型的总数
genresdf = genre_year.groupby('year').sum()
# 查看数据集.tail默认查看后五行数据
genresdf.tail()
Animation | Foreign | Drama | War | Music | Western | History | Documentary | TV | Action | ... | Horror | Comedy | Mystery | Thriller | Fantasy | Crime | Movie | Fiction | Adventure | Science | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
year | |||||||||||||||||||||
2013 | 17 | 0 | 110 | 3 | 12 | 1 | 8 | 10 | 2 | 56 | ... | 25 | 71 | 5 | 53 | 21 | 37 | 2 | 27 | 36 | 27 |
2014 | 14 | 0 | 110 | 10 | 9 | 3 | 7 | 7 | 0 | 54 | ... | 21 | 62 | 15 | 66 | 16 | 27 | 0 | 26 | 37 | 26 |
2015 | 13 | 0 | 95 | 2 | 8 | 7 | 9 | 7 | 0 | 46 | ... | 33 | 52 | 20 | 67 | 10 | 26 | 0 | 28 | 35 | 28 |
2016 | 4 | 0 | 37 | 3 | 1 | 1 | 6 | 0 | 0 | 39 | ... | 20 | 26 | 6 | 27 | 13 | 10 | 0 | 11 | 23 | 11 |
2017 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ... | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
5 rows × 22 columns
# 汇总电影类型的数量
genresdfSum = genresdf.sum(axis=0).sort_values(ascending=False)
genresdfSum
Drama 2297
Comedy 1722
Thriller 1274
Action 1154
Romance 894
Adventure 790
Crime 696
Science 535
Fiction 535
Horror 519
Family 513
Fantasy 424
Mystery 348
Animation 234
History 197
Music 185
War 144
Documentary 110
Western 82
Foreign 34
TV 8
Movie 8
dtype: int64
# 2、数据可视化
# 绘制各种电影类型的数量柱状图
# 设置画板大小
fig=plt.figure(figsize=(12,8))
# 创建画纸,这里只使用1
ax1 = plt.subplot(111)
# 在画纸上绘图
# 电影类型的数量按降序排序
rects = genresdfSum.sort_values(ascending=True).plot(kind='barh', label='genres')
plt.title('1各种电影类型的数量统计图')
plt.xlabel('2电影数量(部)',fontsize=15)
plt.ylabel('3电影类型',fontsize = 15)
plt.show()
# 绘制各种电影类型占比的饼状图:
genres_pie = genresdfSum / genresdfSum.sum()
# 设置other类,当电影类型所占比例小于%1时,全部归到other类中
others = 0.01
genres_pie_otr = genres_pie[genres_pie >= others]
genres_pie_otr['Other'] = genres_pie[genres_pie < others].sum()
# 所占比例小于或等于%2时,对应的饼状图往外长高一截
explode = (genres_pie_otr <= 0.02) / 20 + 0.05
# 设置饼状图的参数
genres_pie_otr.plot(kind='pie', label='',startangle=50,shadow=False,
figsize=(10,10),autopct='%1.11f%%', explode=explode)
plt.title('a各种电影类型所占比例')
Text(0.5, 1.0, 'a各种电影类型所占比例')
分析结论:
从上面的结果可以看出,在所有的电影类型中,Drama(戏剧)类型电影最多,占所有电影类型的18.9%,其次为Comedy(喜剧),占所有电影类型的14.2%。
在所有电影类型中,电影数量排名前5的电影类型分别为:
Drama(戏剧)、Comedy(喜剧)、Thriller(惊悚)、Action(动作)、Romance(冒险)。
# 3、电影类型随时间变化的趋势分析:
plt.figure(figsize=(12,8))
plt.plot(genresdf, label=genresdf.columns)
plt.xticks(range(1910,2018,5))
plt.legend(genresdf)
plt.title('1电影类型随时间变化的趋势',fontsize=15)
plt.xlabel('2年份',fontsize=15)
plt.ylabel('3数量(部)',fontsize=15)
plt.grid(True)
plt.show()
分析结论:
从图中观察到,随着时间的推移,所有电影类型都呈现出增长趋势,尤其是1992年以后各个类型的电影均增长迅速,其中Drama(戏剧)和Comedy(喜剧)增长最快,目前仍是最热门的电影类型。