import numpy
np = numpy.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
print(np[:, 1]) # ":"表示所有的行,"1"表示第二列
print(np[:, 0:2])
print("------------------------------------------------")
np1 = numpy.array([1, 2, 3, 4, 5, 6])
equal = (np1 == 6) # 挨着挨着匹配np1数组,dtype为bool类型
print(equal)
print(np1[equal])
print("------------------------------------------------")
np2 = numpy.array(["1", "2", "3"])
print(np2.dtype)
f2 = np2.astype(float) # numpy中的数据类型转换,不能直接改原数据的dtype! 只能用函数astype()
print(f2.dtype)
print(f2)
print("------------------------------------------------")
np3 = numpy.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])
sum1 = np3.sum(axis=1) # 按行求和
sum2 = np3.sum(axis=0) # 按列求和
print(sum1)
print(sum2)
print("------------------------------------------------")
np4 = numpy.arange(15) # 构造15个从0开始的数
print(np4)
np4_1 = numpy.arange(5, 30, 5) # 构造5到30之间,以5为步长的数组
print(np4_1)
np5 = numpy.arange(15).reshape(5, 3) # 把15个数分成5行3列
print(np5.shape) # 打印行数,列数
print(np5.ndim) # 打印数组维度
print(np5.size) #打印数组大小
print(np5.sum(axis=1))
print("------------------------------------------------")
np6 = numpy.zeros((4, 3)) # 构造 0
print(np6)
np7 = numpy.ones((2, 3, 4), dtype = numpy.int32) # 构造一个三维数组,值全为1
print(np7)
print("------------------------------------------------")
A = numpy.array([[1, 2], [0, 3]])
B = numpy.array([[2, 4],[5, 0]])
print(A * B) # 算数的乘法,每个位置都相乘
print(A.dot(B)) # 矩阵的乘法 (行和列相乘)
print(numpy.dot(A, B)) # 矩阵的乘法另一种写法
print(A.T) # 打印A的转置,实际就是A的行变成了列
print("-------------------矩阵常用操作-----------------------------")
a = numpy.floor(10 * numpy.random.random((2, 2))) # floor向下取整
b = numpy.floor(10 * numpy.random.random((2, 2)))
print(a)
print(b)
print(numpy.hstack((a, b))) # 数组横拼接
print(numpy.vstack((a, b))) # 数组竖拼接
print("------------------矩阵常用操作------------------------------")
np8 = numpy.floor(10 * numpy.random.random((2, 12)))
print(np8)
np8_h = numpy.hsplit(np8, 3) # 将数组分成三份
print(np8_h)
print(numpy.hsplit(np8, (3, 4))) # 在第三行,第四行各切一刀,分成了三份
print(numpy.hsplit(np8, (3, 4, 5))) # 在第三行,第四行,第五行各切一刀,分成了四份
print("-----------------不同复制操作对比-------------------------------")
m = numpy.arange(12)
n = m
print(n is m)
n.shape = (3, 4)
print(m.shape)
print(id(m))
print(id(n))
print("------------------------------------------------")
np9 = numpy.arange(0, 20, 5)
print(numpy.tile(np9, (3, 3))) # 对数组进行扩展
print("------------------------------------------------")
np10 = numpy.array([[2, 3, 1],[4, 2, 5]])
p = numpy.sort(np10, axis=1) # 按行进行升序排序
print(p)
np10.sort(axis=0) # 按列进行升序排序
print(np10)
print("-------------------求升序排序的索引值-----------------------------")
h = numpy.array([2, 3, 1, 4])
print(h.argsort(axis=0))
j = h.argsort(axis=0)
print(h[j]) # 把索引当做参数求原来的值
---------------------
CSDN 网址 https://blog.csdn.net/qq_33543737/article/details/86480622