hj170520 发表于 2020-6-9 22:55

Numpy 入门(五)Broadcasting & Array shape manipulation & Sorting data

入门代码(五)

# 7. Broadcasting

```
>> a = np.tile(np.arange(0, 40, 10), (3, 1)).T
>> a
array([[ 0,0,0],
       ,
       ,
       ])
>> b = np.array()
>> a + b
array([[ 0,1,2],
       ,
       ,
       ])
>> a = np.ones((4, 5))
>> a = 2# we assign an array of dimension 0 to an array of dimension 1
>> a
array([,
       ,
       ,
       ])
```

## A useful trick:

```
>> a = np.arange(0, 40, 10)
>> a.shape
(4,)
>> a = a[:, np.newaxis]# adds a new axis -> 2D array
>> a.shape
(4, 1)
>> a
array([[ 0],
       ,
       ,
       ])
>> a + b
array([[ 0,1,2],
       ,
       ,
       ])
```

### Worked Example: Broadcasting

```
>> mileposts = np.array([0, 198, 303, 736, 871, 1175, 1475, 1544,
       1913, 2448])
>> distance_array = np.abs(mileposts - mileposts[:, np.newaxis])
>> distance_array
array([[   0,198,303,736,871, 1175, 1475, 1544, 1913, 2448],
       [ 198,    0,105,538,673,977, 1277, 1346, 1715, 2250],
       [ 303,105,    0,433,568,872, 1172, 1241, 1610, 2145],
       [ 736,538,433,    0,135,439,739,808, 1177, 1712],
       [ 871,673,568,135,    0,304,604,673, 1042, 1577],
       ,
       ,
       ,
       ,
       ])
>> x, y = np.arange(5), np.arange(5)[:, np.newaxis]
>> distance = np.sqrt(x ** 2 + y ** 2)
>> distance
array([,
       ,
       ,
       ,
       ])
>> plt.pcolor(distance)   
>> plt.colorbar()
```



> Remark : the numpy.ogrid() function allows to directly create vectors x and y of the previous example, with two “significant dimensions”:

```
>> x, y = np.ogrid
>> x, y
(array([,
      ,
      ,
      ,
      ]),
array([]))
>> x.shape, y.shape
((5, 1), (1, 5))
>> distance = np.sqrt(x ** 2 + y ** 2)

>> x, y = np.mgrid
>> x
>> y
array([,
       ,
       ,
       ])
```

# 8. Array shape manipulation

## Flattening

```
>> a = np.array([, ])
>> a.ravel()
array()
>> a.T
array([,
       ,
       ])
>> a.T.ravel()
array()
```

## Reshaping

```
>> a.shape
(2, 3)
>> b = a.ravel()
>> b = b.reshape((2, 3))
>> b
array([,
       ])
>> a.reshape((2, -1))    # unspecified (-1) value is inferred
array([,
       ])
```

> ndarray.reshape may return a view (cf help(np.reshape))), or copy

```
>> b = 99
>> a
array([,
       [ 4,5,6]])
>> a = np.zeros((3,2))
>> b = a.T.reshape(3*2)
>> b = 9
>> a
array([,
       ,
       ])
```

## Adding a dimension

```
>> z = np.array()
>> z
array()
>> z[:, np.newaxis]
array([,
       ,
       ])
>> z
array([])
```

## Dimension shuffling

```
>> a = np.arange(4*3*2).reshape(4, 3, 2)
>> a.shape
(4, 3, 2)
>> a
5
>> b = a.transpose(1, 2, 0)
>> b.shape
(3, 2, 4)
>> b
5
>> b = -1
>> a
-1
```

## Resizing

```
>> a = np.arange(4)
>> a.resize((8,))
>> a
array()
>> b = a# a.resize((4,))   
```


> Exercise: Shape manipulations
>
> Look at the docstring for reshape, especially the notes section which has some more information about copies and views.
> Use flatten as an alternative to ravel. What is the difference? (Hint: check which one returns a view and which a copy)
> Experiment with transpose for dimension shuffling.

# 9. Sorting data

```
>> a = np.array([, ])
>> b = np.sort(a, axis=1)
>> b
array([,
       ])
>> a.sort(axis=1)
>> a
array([,
       ])
>> a = np.array()
>> j = np.argsort(a)
>> j
>> a
array()

>> a = np.array()
>> j_max = np.argmax(a)
>> j_min = np.argmin(a)
>> j_max, j_min
(0, 2)
```

> Exercise: Sorting
>
> Try both in-place and out-of-place sorting.
> Try creating arrays with different dtypes and sorting them.
> Use all or array_equal to check the results.
> Look at np.random.shuffle for a way to create sortable input quicker.
> Combine ravel, sort and reshape.
> Look at the axis keyword for sort and rewrite the previous exercise.

沐雨红尘 发表于 2020-6-9 23:28

页: [1]
查看完整版本: Numpy 入门(五)Broadcasting & Array shape manipulation & Sorting data