import numpy
'''
-使用场景:创建向量和矩阵(numpy.ndarray)
-注意:numpy的ndarray要求所有元素的类型必须一致
- 假如你输入的list元素类型不一致,转换为ndarry的时候,会自动转型。
- 例如,某个元素是str,其他元素是int,那么,所有元素都会被转型为str
'''
from sklearn.utils.fixes import astype
vector = numpy.array(['001','zhangsan','man',24])
print(type(vector))
print(vector.dtype)
print(vector)
print(vector.shape)
# <class 'numpy.ndarray'>
# <U8
# ['001' 'zhangsan' 'man' '24']
# (4,)
matrix = numpy.array([[1.0,777,999.]])
print(type(matrix))
print(matrix.dtype)
print(matrix)
print(matrix.shape)
# <class 'numpy.ndarray'>
# float64
# [[ 1. 777. 999.]]
# (1, 3)
matrix = numpy.array([['001','zhangsan','man','24'],['002','lisi','man','24']])
print(type(matrix))
print(matrix.dtype)
print(matrix)
print(matrix.shape)
# <class 'numpy.ndarray'>
# <U8
# [['001' 'zhangsan' 'man' '24']
# ['002' 'lisi' 'man' '24']]
# (2, 4)
'''
矩阵操作:
'''
matrix = numpy.array([['00','01','02','03'],
['10','11','12','13'],
['20','21','22','23'],
['30','31','32','33']])
print(matrix.shape,matrix.ndim,matrix.size) # 行列数shape,纬度数ndim,元素个数size
# (4, 4) 2 16
print(matrix[:,1]) #只取第2列
# ['01' '11' '21' '31']
print(matrix[1,:]) #只取第2行
# ['10' '11' '12' '13']
print(matrix[1,1]) #只取第2行第2列
# 11
print(matrix[1:3,:]) #只取第2、3行 (注意 1:3 不含右边边界 第4行)
# [['10' '11' '12' '13']
# ['20' '21' '22' '23']]
print(matrix[:,1:3]) #只取第2、3列
# [['01' '02']
# ['11' '12']
# ['21' '22']
# ['31' '32']]
print(matrix[1:,1:]) #只取第2行之后的行,第2列之后的列
# [['11' '12' '13']
# ['21' '22' '23']
# ['31' '32' '33']]
print(matrix.dtype) #ndarray的类型
# <U2
print(matrix.astype(float)) #ndarray的类型 转换
# [[ 0. 1. 2. 3.]
# [ 10. 11. 12. 13.]
# [ 20. 21. 22. 23.]
# [ 30. 31. 32. 33.]]
print(matrix.astype(float).dtype) #ndarray的类型 转换之后
# float64
'''
矩阵初始化:
'''
print(numpy.zeros((2,3))) #快速编造全0矩阵 (常用于矩阵初始化)
# [[ 0. 0. 0.]
# [ 0. 0. 0.]]
print(numpy.ones((2,3),dtype=numpy.int32)) #快速编造全1矩阵 (常用于矩阵初始化)
# [[1 1 1]
# [1 1 1]]
print(numpy.eye(3)) #单位方阵
# [[ 1. 0. 0.]
# [ 0. 1. 0.]
# [ 0. 0. 1.]]
print(numpy.eye(2,3)) #单位矩阵
# [[ 1. 0. 0.]
# [ 0. 1. 0.]]
print(numpy.arange(12)) #快速编造数列
# [0 1 2 3 4 5 6 7 8 9 10 11]
print(numpy.arange(12).reshape(3,4)) #重新排列矩阵
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(numpy.arange(1,22,3)) #快速编造数列 起始值1 依次加3 直至小于22
# [ 1 4 7 10 13 16 19]
print(numpy.random.random((2))) #随机向量 0至1之间
# [ 0.77478872 0.36609742]
print(numpy.random.random((2,3))) #随机矩阵 0至1之间
# [[ 0.72243154 0.33383428 0.11111886]
# [ 0.85122432 0.68508064 0.51619949]]
print(numpy.linspace(0, 2, 10)) #在0至2之间,取10个数,平均的
# [ 0. 0.22222222 0.44444444 0.66666667 0.88888889 1.11111111 1.33333333 1.55555556 1.77777778 2. ]
'''
矩阵运算:
'''
matrixA = numpy.array([[1,2,3],
[4,5,6],
[7,8,9]])
matrixB = numpy.array([[-1,-2,-3],
[-4,-5,-6],
[-7,-8,-9]])
print(matrixA + matrixB) #矩阵相加
# [[0 0 0]
# [0 0 0]
# [0 0 0]]
print(matrixA * 2) #矩阵数乘
# [[ 2 4 6]
# [ 8 10 12]
# [14 16 18]]
print(matrixA * matrixB) #矩阵对应元素相乘(姑且叫做点乘),新矩阵的元素,是原来两个矩阵的对应元素相乘
# [[ -1 -4 -9]
# [-16 -25 -36]
# [-49 -64 -81]]
print(matrixA.dot(matrixB)) #矩阵相乘 行列相乘 或者 numpy.dot(matrixA,matrixB)
# [[ -30 -36 -42]
# [ -66 -81 -96]
# [-102 -126 -150]]
print(matrixA.T) #转置
# [[1 4 7]
# [2 5 8]
# [3 6 9]]
print(numpy.vstack((matrixA,matrixB))) #行拼接
# [[ 1 2 3]
# [ 4 5 6]
# [ 7 8 9]
# [-1 -2 -3]
# [-4 -5 -6]
# [-7 -8 -9]]
print(numpy.hstack((matrixA,matrixB))) #列拼接
# [[ 1 2 3 -1 -2 -3]
# [ 4 5 6 -4 -5 -6]
# [ 7 8 9 -7 -8 -9]]
print(numpy.vsplit(matrixA,3)) #行切分
# [array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])]
print(numpy.vsplit(matrixA,(0,1))) #行切分 在第0列切一刀 在第1列切一刀,爱切几刀就几刀
# [array([], shape=(0, 3), dtype=int32),
# array([[1, 2, 3]]),
# array([[4, 5, 6],
# [7, 8, 9]])]
print(numpy.hsplit(matrixA,3)) #列切分
# [array([[1],
# [4],
# [7]]),
# array([[2],
# [5],
# [8]]),
# array([[3],
# [6],
# [9]])]
matrixC = matrixA.view() #浅层复制,视图,其实matrixA和matrixC都是共享同一份数据,不推荐使用view
matrixC = matrixA.copy() #深层复制,整整的数据拷贝,matrixA和matrixC是两份数据,互不干扰
print(matrixC)
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
matrixC = numpy.tile(matrixA,(2,3)) #复制,深层的,把矩阵按行按列各复制几次
print(matrixC)
# [[1 2 3 1 2 3 1 2 3]
# [4 5 6 4 5 6 4 5 6]
# [7 8 9 7 8 9 7 8 9]
# [1 2 3 1 2 3 1 2 3]
# [4 5 6 4 5 6 4 5 6]
# [7 8 9 7 8 9 7 8 9]]
matrixD = numpy.array([[1,-2,-3],
[4,-5,-6],
[7,-8,-9]])
print(matrixD.argmax(axis=1)) #按行查找,最大元素在该行中的索引
# [0 0 0]
print(matrixD.argmax(axis=0)) #按列查找,最大元素在该行中的索引
# [2 0 0]
matrixE = numpy.array([[1,-2,-3],
[9,3,4],
[7,-8,-9]])
print(numpy.sort(matrixE, axis=1)) #排序,按行,升序
# [[-3 -2 1]
# [ 3 4 9]
# [-9 -8 7]]
print(numpy.argsort(matrixE, axis=1)) #排序,按行,升序,返回坐标矩阵
# [[2 1 0]
# [1 2 0]
# [2 1 0]]
print(numpy.sort(matrixE, axis=0)) #排序,按列,升序
# [[ 1 -8 -9]
# [ 7 -2 -3]
# [ 9 3 4]]
print(numpy.argsort(matrixE, axis=0)) #排序,按列,升序,返回坐标矩阵
# [[0 2 2]
# [2 0 0]
# [1 1 1]]