ndarray.shape && np.copy && enumerate && np.concatenate && // && append
1 . numpy.ndarray.shape是返回一个数组维度的元组。 (2,)与(2,1)的区别如下:
-
ndarray.shape:数组的维度。为一个表示数组在每个维度上大小的整数元组。例如二维数组中,表示数组的“行数”和“列数”。
-
ndarray.shape返回一个元组,这个元组的长度就是维度的数目,即ndim属性。
一般情况下:
[1,2]的shape值(2,),意思是一维数组,数组中有2个元素。
[[1],[2]]的shape值是(2,1),意思是一个二维数组,每行有1个元素。
[[1,2]]的shape值是(1,2),意思是一个二维数组,每行有2个元素。
2 . np.copy
import numpy as np
x = np.array([1, 2, 3])
y = x
z = np.copy(x)
x[0] = 10
x[0] == y[0] #True
x[0] == z[0] #False
3 . enumerate
import numpy as np
seq = ['one', 'two', 'three']
for i, element in enumerate(seq):
print (i, element)
>>0 one
>>1 two
>>2 three
4 . // :向下取整。 1//2 = 0
5 . append
a = [1,2,3]
a.append(4)
print(a)
>>[1, 2, 3, 4]
6. np.concatenate
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
np.concatenate((a, b), axis=0) # python中axis = 0,纵向操作。操作方向的维度要一致,不然会报错
>>array([[1, 2],
[3, 4],
[5, 6]])
7. np.random.shuffle(a)和np.random.permutation(a)
两者都是打乱数组/列表 a的顺序 ,前者是永久操作,后者是暂时操作