人工智能学习笔记——科学计算库Numpy

import numpy

创建向量:vector = numpy.array([1,2,3,4])

创建矩阵:matrix = numpy.array([[1,2,3],[4,5,6]])

打印维度(行列数):print(vector.shape)

                               print(matrix.shape):(2, 3)

打印类型:print(marix.dtype):int64

读取文本:world_alchohol = numpy.genfromtxt("world_alcohol.txt",delimiter=",",dtype =str,skip_header = 1 )

                 delimiter:分隔符

                 dtype:传入的类型

                 skip_header = 1:跳过第一行   

索引取值:value = world_alchohol[1,4]#第二行第五列

                 allValue = world_alchohol[:,4]#所有行的第五列

                 print(vector[0:3])

                 print(marix[:,0:3])

                 print(marix[0:2,0:3])


vector = numpy.array([1,2,3,4,5])

vector == 5

array([False, False, False, False,  True])

equalFive = (vector == 5)

print(vector[equalFive])

[5]

marix = numpy.array([
    [1,2,3],
    [4,5,6],
    [7,8,9]
])
marix == 5

array([[False, False, False],
       [False,  True, False],
       [False, False, False]])


&与    |或

类型转换:vector = numpy.array(["1","2","3"])
                 print(vector.dtype)
                 vector = vector.astype(float)
                 print(vector.dtype)

    <U1
    float64
最值:vector = numpy.array([1,2,3,4,5])
          minValue = vector.min()
          maxValue = vector.max()
          print(minValue)
          print(maxValue)

    1
    5
行列求和:marix = numpy.array([
                    [1,2,3],

                    [4,5,6],
                    [7,8,9]

                ])
                columnSum = marix.sum(axis=0)#列求和
                rawSum = marix.sum(axis=1)#行求和
                print(columnSum)
                print(rawSum)
        [12 15 18]
        [ 6 15 24]

import numpy as np

构造数列:print(np.arange(15))

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

构造切片数列:np.arange(2,10,2)

array([2, 4, 6, 8])

构造矩阵:marix = np.arange(15).reshape(3,5)

                print(marix)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
矩阵的维度(二维、三维):marix.ndim

矩阵元素的个数:marix.size

构造零矩阵:np.zeros((3,4))

array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
构造单位矩阵:np.ones((2,3,4),dtype=np.int16)
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]], dtype=int16)
构造随机数矩阵:np.random.random((2,3))
array([[0.36243571, 0.10834852, 0.50913561],
       [0.19129293, 0.01765437, 0.18629496]])
平均切片:from numpy import pi
                np.linspace(0,2*pi,10)
array([[0.36243571, 0.10834852, 0.50913561],
       [0.19129293, 0.01765437, 0.18629496]])
矩阵内积(对应相乘):A = np.array([[1,2],[3,4]])
                                    B = np.array([[5,6],[7,8]])
                                    print(A*B)
[[ 5 12]
 [21 32]]
矩阵外积:A = np.array([[1,2],[3,4]])
                B = np.array([[5,6],[7,8]])
                print(A.dot(B))
                print(np.dot(A,B))
[[19 22]
 [43 50]]
[[19 22]
 [43 50]]

数学运算:np.exp()

                 np.sqrt()

向下取整:print(np.floor(1.01))

向上取整:print(np.ceil(1.01))

向零取整:print(np.fix(1.8))

矩阵转向量:print(marix)
                    print(marix.ravel())

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
垂直、水平拼接矩阵:print(np.vstack((A,B)))
                                  print(np.hstack((A,B)))
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
[[1 2 5 6]
 [3 4 7 8]]
矩阵水平平均切割:marix = np.arange(24).reshape(2,12)
                                print(marix)
                                print(np.hsplit(marix,2))
[[ 0  1  2  3  4  5  6  7  8  9 10 11]
 [12 13 14 15 16 17 18 19 20 21 22 23]]
[array([[ 0,  1,  2,  3,  4,  5],
       [12, 13, 14, 15, 16, 17]]), 
 array([[ 6,  7,  8,  9, 10, 11],
       [18, 19, 20, 21, 22, 23]])]
矩阵水平任意切割:print(np.hsplit(marix,(3,5)))
[array([[ 0,  1,  2],
       [12, 13, 14]]), 
 array([[ 3,  4],
       [15, 16]]), 
 array([[ 5,  6,  7,  8,  9, 10, 11],
       [17, 18, 19, 20, 21, 22, 23]])]
矩阵垂直平均切割:print(np.vsplit(marix,2))
[array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11]]), array([[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])]

复制:

a = np.array([1,2,3,4])
b = a.copy()#复制初始化值
print(b is a)
b[0] = 10
print(a)
print(b)

False
[1 2 3 4]
[10  2  3  4]
a = np.array([1,2,3,4])
b = a
print(b is a)
b[0] = 10
print(a)
print(b)
True
[10  2  3  4]
[10  2  3  4]

矩阵行(列)的最大值索引:

data = np.sin(np.arange(20)).reshape(5,4)
print(data)
ind = data.argmax(axis = 0)#axis = 0列axis = 1行
print(ind)
data_max = data[ind,range(data.shape[1])]#data.shape[1]列数
print(data_max)

[[ 0.          0.84147098  0.90929743  0.14112001]
 [-0.7568025  -0.95892427 -0.2794155   0.6569866 ]
 [ 0.98935825  0.41211849 -0.54402111 -0.99999021]
 [-0.53657292  0.42016704  0.99060736  0.65028784]
 [-0.28790332 -0.96139749 -0.75098725  0.14987721]]
[2 0 3 1]
[0.98935825 0.84147098 0.99060736 0.6569866 ]

矩阵复制扩展:

a = np.arange(0,40,10)
print(a)
b = np.tile(a,(3,2))
print(b)

[ 0 10 20 30]
[[ 0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30]
 [ 0 10 20 30  0 10 20 30]]

排序:

marix = np.array([[1,4,3],[4,2,9],[7,6,8]])
print(marix)
print(np.sort(marix,axis=1))#行排序

[[1 4 3]
 [4 2 9]
 [7 6 8]]
[[1 3 4]
 [2 4 9]
 [6 7 8]]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值