以下代码来源于本博文作者观看大神视频并纯手敲。
目录
numpy的属性
创建array
numpy的运算1
随机数生成以及矩阵的运算2
numpy的索引
array合并
array分割
numpy的浅拷贝和深拷贝
numpy的属性
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(array)
print(array.ndim) # 维度 2
print(array.shape) # 形状 (3, 3)
print(array.size) # 大小 9
print(array.dtype) # 元素类型 int32
numpy创建array
import numpy as np
a = np.array([1, 2, 3], dtype=np.int32)
print(a.dtype) # int32
b = np.array([1, 2, 3], dtype=np.float)
print(b.dtype) # float64
c = np.array([1, 2, 3])
d = np.array([[1, 2, 3], [4, 5, 6]])
print(d) # 二维矩阵
zero = np.zeros((2, 3))
print(zero) # 生成两行三列全为零的矩阵
one = np.ones((3, 4))
print(one) # 生成三行四列全为1的矩阵
empty = np.empty((3, 2))
print(empty) # 生成三行两列全都接近于零的矩阵(但不等于0)
e = np.arange(10)
print(e)
f = np.arange(4, 12)
print(f) # [ 4 5 6 7 8 9 10 11]
g = np.arange(1, 20, 3)
print(g)
h = np.arange(8).reshape(2, 4)
print(h) # 重新定义矩阵形状
numpy矩阵的运算
import numpy as np
arr1 = np.array([[1, 2, 3], [4, 5, 6]])
arr2 = np.array([[1, 1, 2], [2, 3, 3]])
print(arr1 + arr2) # 按照位置相加
print(arr1 - arr2)
print(arr1 * arr2)
print(arr1 ** arr2)
print(arr1 / arr2)
print(arr1 % arr2)
print(arr1 // arr2)
print(arr1 + 2) # 所有的元素都加2
arr3 = arr1 > 3
print(arr3) # 判断哪些元素大于3
arr4 = np.ones((3, 5))
print(arr4)
print(arr1)
res = np.dot(arr1, arr4) # 矩阵的乘法
print(res)
res1 = arr1.dot(arr4) # 矩阵的乘法
print(res1)
print(arr1.T) # 转置矩阵
print(np.transpose(arr1)) # 转置矩阵
随机数生成以及矩阵的运算2
import numpy as np
sample1 = np.random.random((3, 2)) # 生成3行2列的从0到1的随机数
print(sample1)
sample2 = np.random.normal(size=(3, 2)) # 生成3行2列符合标准正太分布的随机数
print(sample2)
sample3 = np.random.randint(0, 10, size=(3, 2)) # 生成3行2列的从0-10的随机整数
print(sample3)
print(np.sum(sample1)) # 求和
print(np.min(sample1)) # 求最小值
print(np.sum(sample1, axis=0)) # 对每一列进行求和
print(np.sum(sample1, axis=1)) # 对每一行进行求和
print(np.argmin(sample1)) # 求最小值的索引
print(np.argmax(sample1)) # 求最大值的索引
print(np.mean(sample1)) # 求平均值
print(sample1.mean()) # 求平均值
print(np.median(sample1)) # 求中位数
print(np.sqrt(sample1)) # 开方
sample4 = np.random.randint(0, 10, size=(1, 10))
print(sample4)
print(np.sort(sample4)) # 排序:按行升序
print(np.sort(sample1))
print(np.clip(sample4, 2, 7)) # 小于2的变成2,大于7的变成7
numpy的索引
import numpy as np
arr1 = np.arange(2, 14)
print(arr1) # [ 2 3 4 5 6 7 8 9 10 11 12 13]
print(arr1[2]) # 4
print(arr1[1: 4]) # [3 4 5]
print(arr1[2: -1]) # [ 4 5 6 7 8 9 10 11 12]
print(arr1[: 5]) # [2 3 4 5 6]
print(arr1[-2:]) # [12 13]
arr2 = arr1.reshape(3, 4)
print(arr2) #
print(arr2[1]) # [6 7 8 9]
print(arr2[1][1]) # 7
print(arr2[1, 2]) # 8
print(arr2[:, 2]) # [ 4 8 12] 所有行,第2列
for i in arr2: # 迭代行
print(i)
for i in arr2.T: # 迭代列
print(i)
for i in arr2.flat: # 迭代一个个元素
print(i)
array的合并
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr3 = np.vstack((arr1, arr2)) # 垂直合并
print(arr3)
print(arr3.shape)
arr4 = np.hstack((arr1, arr2)) # 水平合并
print(arr4) # [1 2 3 4 5 6]
print(arr4.shape)
arrv = np.vstack((arr1, arr2, arr3))
print(arrv)
arrh = np.hstack((arr1, arr2, arr4))
print(arrh)
arr = np.concatenate((arr1, arr2, arr1)) # 合并
print(arr)
arr = np.concatenate((arr3, arrv), axis=0) # 垂直合并。合并的array维度要相同,array形状要匹配,axis=0纵向合并
print(arr)
arr = np.concatenate((arr3, arr3), axis=1) # 水平合并
print(arr)
print(arr1.T) # 一维的array不能转置
print(arr1.shape) # (3,)
arr1_1 = arr1[np.newaxis, :]
print(arr1_1) # [[1 2 3]]
print(arr1_1.shape) # (1, 3)
print(arr1_1.T)
arr1_2 = arr1[:, np.newaxis]
print(arr1_2)
print(arr1_2.shape) # (3, 1)
arr1_3 = np.atleast_2d(arr1)
print(arr1_3) # [[1 2 3]]
print(arr1_3.T)
array分割
import numpy as np
arr1 = np.arange(12).reshape((3, 4))
print(arr1)
arr2, arr3 = np.split(arr1, 2, axis=1) # 水平方向分割,分成2份
print(arr2)
print(arr3)
arr4, arr5, arr6 = np.split(arr1, 3, axis=0) # 垂直方向分割,分成2份
print(arr4)
print(arr5)
print(arr6)
arr7, arr8, arr9 = np.array_split(arr1, 3, axis=1) # 水平方向分割成3份,不等分割
print(arr7)
print(arr8)
print(arr9)
arrv1, arrv2, arrv3 = np.vsplit(arr1, 3) # 垂直分割
print(arrv1)
print(arrv2)
print(arrv3)
arrh1, arrh2 = np.hsplit(arr1, 2) # 水平分割
print(arrh1)
print(arrh2)
numpy的浅拷贝和深拷贝
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = arr1 # 引用赋值,共享一块内存,浅拷贝
arr2[0] = 5
print(arr1)
print(arr2)
arr3 = arr1.copy() # 深拷贝
arr3[0] = 10
print(arr1)
print(arr3)
转载请注明博文出处。