CS231python课程——numpy

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013317445/article/details/84556747

Numpy

数组Arrays

import numpy as np
a=np.array([1,2,3])
print(type(a))
print(a.shape)
print(a[0],a[1],a[2])
<type 'numpy.ndarray'>
(3L,)
(1, 2, 3)
b=np.array([[1,2,3],
            [9,0,6]])#注意有一个大[]
print(b.shape)
print(b[0],b[1])
(2L, 3L)
(array([1, 2, 3]), array([9, 0, 6]))
print(b[0,0],b[0,1])
(1, 2)

其它创建数组的方法

c=np.zeros((2,3))
print(c)
[[0. 0. 0.]
 [0. 0. 0.]]
d=np.ones((1,10))
print(d)
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
e=np.full((3,3),2)
print(e)
[[2 2 2]
 [2 2 2]
 [2 2 2]]
#单位矩阵
f=np.eye(4)
print(f)
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

生成随机一个4*2的数组

#生成随机浮点数  0-1之间  size:样本的大小
g=np.random.random(size=(4,2))
print(g)
[[0.44471268 0.67466063]
 [0.05796339 0.20853947]
 [0.88246722 0.58151158]
 [0.81634659 0.31795433]]
#生成随机整数
#randint(low, high=None, size=None, dtype=’l’) 
gInt=np.random.randint(10,20,size=(4,2))
print(gInt)
[[18 18]
 [18 11]
 [16 14]
 [12 17]]
#高斯分布/正态分布随机数
#normal(loc=0.0, scale=1.0, size=None) 
#loc:均值,scale:标准差,size:抽取样本的size
n1= np.random.normal(1.7,0.1, 10)
print(n1)
n2=np.random.normal(1.7,0.1, (4,2))
print(n2)
[1.73745478 1.81127892 1.63386844 1.77307303 1.57168221 1.68772298
 1.71955854 1.81566671 1.71896083 1.51836231]
[[1.61877448 1.49208483]
 [1.64712027 1.72809354]
 [1.74291505 1.67063753]
 [1.57154457 1.71569964]]
#标准正态分布 N(0,1)
n2=np.random.randn(2,4)
n2
array([[ 1.20168678,  0.4254389 , -0.19798632,  2.14897648],
       [ 0.74364044, -0.05988914,  0.82132666,  0.40078756]])
n22=np.random.randn(2,4,3)  #参数d0,d1,d2...dn 维度
n22
array([[[-0.14915425, -1.67944095,  1.30268024],
        [-0.20010639,  1.91225408, -1.05935465],
        [-1.32296067, -0.10023931,  0.84715779],
        [-0.00499243,  0.30338752,  1.48260821]],

       [[ 0.38468581,  0.88247802,  0.27390488],
        [-1.19540403,  0.21729268, -0.52203668],
        [ 0.28635075,  0.69627528,  0.22776306],
        [-0.41877602,  0.78666712, -1.42378401]]])

访问数组

切片:必须为每个维度(行,列)指定好切片

aa=np.array([[1,2,3,4], 
             [5,6,7,8], 
             [9,10,11,12]])
[5 6 7 8]
[[1 2 3 4]
 [5 6 7 8]]

取某几行

#要第1行
row_r1=aa[0, :]
print(row_r1, row_r1.shape)
#要第2行
row_r2=aa[1, :]
print(row_r2)
#要前两行
row=aa[0:2, :]
print(row)
print(row.shape)
(array([1, 2, 3, 4]), (4L,))
[5 6 7 8]
[[1 2 3 4]
 [5 6 7 8]]
(2L, 4L)

要某几列

#要第1列
column_c1=aa[:, 0]
print(column_c1)
#要第3列
column_c3=aa[:, 2]
print(column_c3)
#要倒数第一列
column_cd1=aa[:, -1]
print(column_cd1)
#要第2到第3列
column=aa[:, 1:3]
print(column)
[1 5 9]
[ 3  7 11]
[ 4  8 12]
[[ 2  3]
 [ 6  7]
 [10 11]]
#要前两行的第2到第3列
aa1=aa[:2, 1:3]
print(aa1)
[[2 3]
 [6 7]]
#布尔数组访问 选取数组中满足某些条件的元素
print(aa>3)
print(aa[aa>3])
bb=np.array([[3,8,4,9],
            [6,6,6,6],
            [8,8,1,2]])
print(bb>6)
print(bb[bb>6])
print(set(bb[bb>6]))
[[False False False  True]
 [ True  True  True  True]
 [ True  True  True  True]]
[ 4  5  6  7  8  9 10 11 12]
[[False  True False  True]
 [False False False False]
 [ True  True False False]]
[8 9 8 8]
set([8, 9])

查看数据类型


print(type(bb))
bb.dtype
<type 'numpy.ndarray'>





dtype('int32')

数组计算

x=np.array([[1,2],
            [3,4]], dtype= np.float64)#强制各元素为float64类型
y=np.array([[5,6],
            [7,8]], dtype= np.float64)
print(x+y)
[[ 6.  8.]
 [10. 12.]]
print(x/y)
[[0.2        0.33333333]
 [0.42857143 0.5       ]]
print(x*y)#注意:和矩阵乘法规则不一样
[[ 5. 12.]
 [21. 32.]]

如果要进行矩阵乘法,在Numpy中可以使用dot来进行:

x.dot(y)
#或者np.dot(x,y)
array([[19., 22.],
       [43., 50.]])
print(np.sqrt(x))
[[1.         1.41421356]
 [1.73205081 2.        ]]

Numpy提供了很多计算数组的函数,其中最常用的一个是sum:

x
array([[1., 2.],
       [3., 4.]])
np.sum(x) #数组所有元素之和
10.0
np.sum(x, axis=0)#数组每列column元素之和
array([4., 6.])
np.sum(x, axis=1)#数组每行row元素之和
array([3., 7.])

转置:

x.T
array([[1., 3.],
       [2., 4.]])

np.tile()通过第二个参数便可指定在各个轴上的复制倍数

vector=np.array([1,0,1])
np.tile(vector, (3,1)) #行上复制3倍 1行变3行
array([[1, 0, 1],
       [1, 0, 1],
       [1, 0, 1]])

Numpy广播机制:

x=np.array([[2,0,2],
            [9,9,6]])
x+vector
array([[ 3,  0,  3],
       [10,  9,  7]])
vector=np.array([1,0,1])
print(vector.T)#?????????????????????????????????为什么下面那个可以转置 这个向量就不行
print(x.T)
[1 0 1]
[[2 9]
 [0 9]
 [2 6]]
np.reshape(vector, (3,1)) (1,3)变(3,1)这个reshape可以把一维向量转置
array([[1],
       [0],
       [1]])

猜你喜欢

转载自blog.csdn.net/u013317445/article/details/84556747