import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
#显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
#设置正常显示字符
mpl.rcParams["axes.unicode_minus"] = False
一、 ndarry运算
1.1 逻辑运算
#生成10名同学,5门功课的数据
score1 = np.random.randint(40,101,(10,5))
#取出最后4名同学的成绩,用于逻辑判断
test_score = score1[6:,:]
#逻辑判断,如果大于60的就标记为True,否则为False
test_score > 60
#BOOL赋值,将满足条件的设置为指定的值-布尔索引
test_score[test_score < 60]=0
test_score[test_score > 60]=1
test_score
array([[1, 1, 1, 1, 1],
[0, 0, 0, 0, 1],
[1, 1, 1, 0, 1],
[1, 1, 0, 1, 1]])
1.2 通用判断函数
score2 = np.random.randint(40,101,(10,5))
score2
array([[83, 89, 46, 43, 43],
[92, 59, 43, 50, 41],
[70, 74, 61, 85, 43],
[54, 92, 89, 96, 44],
[99, 53, 51, 64, 61],
[50, 80, 41, 96, 58],
[45, 85, 56, 70, 54],
[82, 82, 68, 67, 72],
[88, 40, 56, 92, 44],
[49, 64, 91, 67, 75]])
np.all() => 判断是否全部符合要求
# 判断前两名同学的成绩是否全部及格
np.all(score2[0:2,:] > 60)
False
np.any() => 判断是否有任意符合要求的
# 判断前两名同学的成绩是否有大于90分的
np.any(score2[0:2,:] > 90)
True
1.3 np.where(三元运算符)
score3 = np.random.randint(40,101,(10,5))
score3
array([[ 57, 74, 87, 67, 43],
[ 73, 73, 61, 55, 47],
[ 80, 95, 91, 47, 98],
[ 56, 69, 77, 92, 100],
[ 47, 67, 48, 50, 72],
[ 52, 66, 87, 59, 89],
[100, 86, 90, 61, 85],
[ 48, 40, 68, 90, 73],
[ 57, 81, 70, 53, 62],
[ 89, 41, 95, 53, 62]])
# np.where() => 通过使用np.where能够进行更加复杂的运算
#判断前四名学生,前四门课程中,成绩大于60的设置为1,否则为0
temp = score3[:4,:4]
np.where(temp > 60,1,0)
array([[0, 1, 1, 1],
[1, 1, 1, 0],
[1, 1, 1, 0],
[0, 1, 1, 1]])
np.where(np.logical_and(temp > 60,temp < 90),1,0)
array([[0, 1, 1, 1],
[1, 1, 1, 0],
[1, 0, 0, 0],
[0, 1, 1, 0]])
np.where(np.logical_or(temp > 90,temp < 60),1,0)
array([[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 1, 1, 1],
[1, 0, 0, 1]])
1.4 统计指标
score4 = np.random.randint(40,101,(10,5))
score4
array([[51, 75, 99, 54, 65],
[41, 91, 74, 48, 86],
[62, 88, 59, 40, 85],
[60, 63, 46, 70, 87],
[60, 66, 69, 62, 51],
[70, 78, 55, 48, 42],
[52, 81, 63, 69, 96],
[53, 94, 81, 58, 78],
[80, 42, 42, 73, 60],
[60, 81, 54, 58, 47]])
# min(a,axis) => 最小值
# max(a, axis) => 最大值
# median(a, axis) => 中值
# mean(a, axis, dtype) => 平均值
# std(a, axis, dtype) => 标准差
# var(a,axis, dtype) => 方差
#对前四名学生的成绩,进行统计
temp = score4[:4,:]
temp
array([[51, 75, 99, 54, 65],
[41, 91, 74, 48, 86],
[62, 88, 59, 40, 85],
[60, 63, 46, 70, 87]])
#进行统计的时候,axis 轴的取值并不一定,Numpy中不同的API轴的值都不一样
#在这里,axis O代表列,axis 1代表行去进行统计
print("前四名学生,各成绩的最大分数:{}".format(np.max(temp,axis=0)))
print("前四名学生,各成绩的最小分数:{}".format(np.min(temp,axis=0)))
print("前四名学生,各成绩的波动情况:{}".format(np.std(temp,axis=0)))
print("前四名学生,各成绩的平均分数:{}".format(np.mean(temp,axis=0)))
前四名学生,各成绩的最大分数:[62 91 99 70 87]
前四名学生,各成绩的最小分数:[41 63 46 40 65]
前四名学生,各成绩的波动情况:[ 8.32165849 11.14394454 19.7040605 11. 9.12071817]
前四名学生,各成绩的平均分数:[53.5 79.25 69.5 53. 80.75]
二、 数组间的运算
2.1 四则运算
#数组和列表的对比
arr = np.random.randint(0,10,(2,3))
lt = [1,2,3,4,5,6]
arr,lt
(array([[5, 4, 2],
[4, 1, 2]]),
[1, 2, 3, 4, 5, 6])
#加法
arr + 3
array([[8, 7, 5],
[7, 4, 5]])
# 除法
arr / 2
array([[2.5, 2. , 1. ],
[2. , 0.5, 1. ]])
# 乘法
arr *3,lt * 3
(array([[15, 12, 6],
[12, 3, 6]]),
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6])
2.2 数组与数组的运算
a1 = np.array([[27,15,6],[9,18,27]],dtype=np.int32)
a2 = np.array([[9,5,2],[3,6,9]],dtype=np.int32)
a3 = np.array([[1,2,3,4,5],[6,7,8,9,10]],dtype=np.int32)
#数组相同的结构可以运算,不同的则会报错
a1 * a2
# a1 * a
array([[243, 75, 12],
[ 27, 108, 243]])
2.3 广播机制
#满足条件
# 1.数组的某一维度等长;
# 2.其中一个数组的某一维度为1.
a4 = np.array([[9,5,8]],dtype=np.int32)
a5 = np.array([[1],[2],[3],[4]],dtype=np.int32)
a4 + a5
array([[10, 6, 9],
[11, 7, 10],
[12, 8, 11],
[13, 9, 12]])
# 测试
a1.shape,a2.shape,a3.shape,a4.shape,a5.shape
((2, 3), (2, 3), (2, 5), (1, 3), (4, 1))
#有一就ok,相同就ok
a1 + a4
array([[36, 20, 14],
[18, 23, 35]])
三、 矩阵
3.1 矩阵和向量
3.1.1 矩阵
#矩阵(natrix),和array的区别矩阵必须是二维的,但是array可以是多维的
3.1.5 向量
#向量是一种特殊的矩阵,一般都是列向量
3.2 加法和标量乘法
#矩阵的加法:行列数相等的可以相加
#矩阵的乘法:每个元素都要乘
3.3 矩阵向量乘法
#矩阵乘法遵循准则:(M行,N列)*(N行,L列)=(M行,L行)
3.4矩阵乘法
# m×n矩阵乘以n×o矩阵,变成m×o矩阵
3.5 矩阵乘法的性质
# 矩阵的乘法不满足交换律:A×B≠B×A 矩阵的乘法满足结合律。即:A×(B×C)=(A×B)×C 单位矩阵:在矩阵的乘法中,有一种矩阵起着特殊的作用,如同数的乘法中的 1,我们称 这种矩阵为单位矩阵.它是个方阵,一般用 I 或者 E 表 示,从 左上角到右下角的对角线(称为主对角线)上的元素均为 1 以外全都为 0。
四、 资料网盘
百度云盘:https://pan.baidu.com/s/1puAwMn8QFGiUcrmQtnoiPw ;
提取码:echo