一、基础知识
1、介绍
2、基础操作
(1)、创建新数组
np.array(〈list〉)
np.arange()
(2)、得到维度
- 获取数组array1的维度:array1.shape
(3)、改变维度
reshape()
3、创建随机数
- 注意:参数中a到b,但不包括b
4、基本运算
和矩阵运算方式相同。必须是同型矩阵
np.histogram()输出直方图结果
二、Python代码
import matplotlib.pyplot as plt
import numpy as np
#解决中文图释不显示的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
def main():
"""
主函数
"""
total_times = 10000
#记录骰子的结果
roll1_arr = np.random.randint(1, 7, size=total_times)
roll2_arr = np.random.randint(1, 7, size=total_times)
result_arr = roll1_arr + roll2_arr
hist, bins = np.histogram(result_arr, bins=range(2, 14))
print(hist)
print(bins)
#数据可视化(直方图)
plt.hist(result_arr, bins=range(2, 14), normed=1, edgecolor='black', linewidth=1)
#设置x轴坐标点显示
tick_labels = ['2点', '3点', '4点', '5点', '6点', '7点', '8点', '9点', '10点', '11点', '12点']
tick_pos = np.arange(2,13) + 0.5
plt.xticks(tick_pos, tick_labels) #xticks函数,第一个参数是位置,第二个参数位置标签
plt.title('骰子点数统计')
plt.xlabel('点数')
plt.ylabel('频率')
plt.show()
if __name__ == '__main__':
main()