numpy十大函数

reshape

1
a.reshape(2,4)

resize

1
a.resize(2,4)

transpose

1
b = a.transpose(1,0,2)

concatenate

1
2
3
4
5
6
7
8
9
10
11
>>> from numpy import *
>>> x = array([[1,2],[3,4]])
>>> y = array([[5,6],[7,8]])
>>> concatenate((x,y)) # default is axis=0
array([[1, 2],
[3, 4],
[5, 6],
[7, 8]])
>>> concatenate((x,y),axis=1)
array([[1, 2, 5, 6],
[3, 4, 7, 8]])

hstack

1
2
3
4
5
6
>>> from numpy import *
>>> a =array([[1],[2]]) # 2x1 array
>>> b = array([[3,4],[5,6]]) # 2x2 array
>>> hstack((a,b,a)) # only the 2nd dimension of the arrays is allowed to be different
array([[1, 3, 4, 1],
[2, 5, 6, 2]])

vstack

1
2
3
4
5
6
7
8
>>> from numpy import *
>>> a =array([1,2])
>>> b = array([[3,4],[5,6]])
>>> vstack((a,b,a)) # only the first dimension of the arrays is allowed to be different
array([[1, 2],
[3, 4],
[5, 6],
[1, 2]])

dtype

1
x_train = np.array([], dtype=np.float32)

unsqueeze

1
test_x = Variable(torch.unsqueeze(test_data.test_data, dim=1), volatile=True).type(torch.FloatTensor)[:2000]/255.   # shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)

zeros

1
2
3
4
5
6
np.zeros([20, 18])
```

### squeeze, expand_dims
```python
y = np.expand_dims(x, axis=0)

a[np.newaxis, :]

np.tile(a, [2,2])

np.append()

1
2
3

>>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
请作者喝一杯咖啡☕️