4.numpy包的使用

NumPy包的使用(上)

1. ndarray

ndarray 是NumPy中array类,用于处理多维数组,也成为array,
但是ndarray和array.array是不一样的。这个需要进行区分。

2. ndarray对象属性

ndarray对象有下面几个属性:
ndim: ndarray的轴的个数,或者是维度
shape: ndarray的形状,如果是二维的话,比如如果是2行三列的话,那么shape=(2,3)
size: ndarray中所有的元素的个数,同时也是shape的每个元素的乘积
dtype: 元素的类型。
itemsize:  元素的大小

3. 如何创建array

  • 可以通过list或者是tuple进行创建,使用np.array函数
a = np.array([1,2,3])       # array自动推导出dtype=np.int64
b = np.array((1.0,2.0,3.0)) # array会自动推导出dtype=np.float64    
c = np.array(((1,2),(3,4))) #创建二维数组
d = np.array((1,2), dtype=complex) # 当然也可以通过dtype指定相应的类型
  • 如果只知道shape,该怎么创建数组
    这样有个好处是省了数组扩容的开销
e = np.zeros((2,3)) # 创建大小为(2,3)的数组,元素全为0,dtype=np.float64默认
f = np.ones((2,2), dtype=np.int32) #当然也可以制定数组的dtype
g = np.empty((2,4)) # 创建空数组,这样的话元素随机初始化
  • numpy有个函数为arange和python build-in的range相似,但是返回的是array
a = np.arange(10, 25, 5) # 10,15,20, 包左不包右
b = np.arange(1.0, 1.2, 0.1) # 1.0,1.1
c = np.linspace(0,1,3) # 0, 0.5 如果是float通常建议使用该函数,可以指定个数

4. 如何print ndarray

#最后一维是从左往右边,剩下的都是从上到下
a = np.arange(24).reshape(2, 3, 3)
[[[ 0  1  2  3]  #最后一维,从左到右
  [ 4  5  6  7]  #倒数第二维到倒数第一维是从上到下
  [ 8  9 10 11]] #依次类推

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]]

5. numpy四则运算

  • 四则运算在array上是elementwise的是每个element进行运算
a = np.array((1, 2, 3, 4))
b = np.arange(4)  # [0, 1, 2, 3]
c = a - b  # [1, 1, 1, 1]
d = a**2 # [2, 4, 6, 8]
a < 3  # True, True, False, False
# array中对应元素相乘和矩阵相乘
a = np.array([[1, 2], [3, 4]])
b = np.array([[1, 1], [1, 1]])
c = a * b #对应相乘
d = a.dot(b) #矩阵相乘
# += *=是原地(in place)进行修改变量
a = np.array([1, 2])
a *= 2 # 是直接对a进行处理
  • 常用函数sum
    通常一些函数是不用指定shape的如sum, 当然也可以指定
    这里解释下轴的概念,可以使用下面这张图,轴=0的时候表示行,从上到下,轴=1的时候表示列,从左到右。
    在这里插入图片描述
a = np.array([[1, 2], [3, 4]])
a.sum() # 10
a.sum(axis=0) # 3, 7
a.sum(axis=1) # 4, 6

6.universal 函数

universal函数包括np.exp, np.sqrt, 返回一个array

B = np.range(3)
np.exp(b) # array([0. , 1. , 1.41421356])

7. 一维数组和高维数组索引,切边和遍历

# 一位数组索引
a = np.arange(10) ** 3

print(a)
# 索引第三个位置
print(a[2])
# 切片 包左不包右
print(a[0:2])
# 可以进行赋值,同样包左不包右
a[0:4:2] = 100 #和a[:4:2]等价
print(a)
b = np.arange(24).reshape(4,6)
print(b)
#二位数组索引
print(b[0,1])
#索引一行, 如果索引缺省则为:
print(b[1, :])
print(b[1])
# 遍历,相当于遍历一行, 如果是三维数组则按照第一个axis进行面遍历。
for i in b:
    print(i)

8. 操纵shape

c = np.arange(6).reshape(2,3)
print(c) # [[0, 1, 2], [3, 4, 5]] 

# 可以通过ravel进行flatten,但是不会改变c
b = c.ravel() # [0, 1, 2, 3, 4, 5]
print(b)
print(c)      #没有改变

# reshape和T,都不改变value,但是会返回值
print(c.reshape(3,2)) # 等价与c.reshape(3, -1)
print(c.T)

9. 是否copy array(重要)

不会copy的情况

c = np.arange(6).reshape(2,3)
print(c) # [[0, 1, 2], [3, 4, 5]] 

# 可以通过ravel进行flatten,但是不会改变c
b = c.ravel() # [0, 1, 2, 3, 4, 5]
print(b)
print(c)      #没有改变

# reshape和T,都不会改变value,但是会返回值
print(c.reshape(3,2)) # 等价与c.reshape(3, -1)
print(c.T)

view and shallow copy

# view() 创建一个对象但是共用数据, 并且切片相当于返回view
d = np.arange(6).reshape(2, 3) #[[0, 1, 2], [3, 4, 5]]
e = d.view()  # d和e共用一样的数据
e = e.reshape(1, 6)
print(d)
print(e) # [0, 1, 2, 3, 4, 5]
#但是如果改数据的话,就会同时改变
e[0, 5] = 10
print(d) #[[0, 1, 2], [3, 4, 10]] 
print(e) #[0, 1, 2, 3, 4, 10]

深拷贝copy

a = np.array([1, 2])
b = a.copy()
b[0, 1] = 5
print(a) # [1, 2]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章