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

Numpy 入门(四)Basic reductions

本帖最后由 hj170520 于 2020-6-10 11:31 编辑

入门代码(四)
# 6. Basic reductions

## Computing sums

```
>> x = np.array()
>> np.sum(x)
10
>> x.sum()
10
```

### Sum by rows and by columns:

```
>> x = np.array([, ])
>> x
array([,
       ])
>> x.sum(axis=0)   # columns (first dimension)
array()
>> x[:, 0].sum(), x[:, 1].sum()
(3, 3)
>> x.sum(axis=1)   # rows (second dimension)
array()
>> x.sum(), x.sum()
(2, 4)
```


###Same idea in higher dimensions:

```
>> x = np.random.rand(2,2,2)
>> x.sum(axis=2)
1.345648389754668
>> x.sum()
1.345648389754668
```

# Other reductions

> — works the same way (and take axis=)
>

## Extrema:

```
>> x = np.array()
>> x.min()
1
>> x.max()
3
>> x.argmin()# index of minimum
0
>> x.argmax()# index of maximum
1
```

## Logical operations:

```
>> np.all()
False
>> np.any()
True
```

### Note Can be used for array comparisons:

```
>> a = np.zeros((100, 100))
>> np.any(a != 0)

>> np.all(a == a)


>> a = np.array()
>> b = np.array()
>> c = np.array()
>> ((a <= b) & (b <= c)).all()
True
```

## Statistics:

```
>> x = np.array()
>> y = np.array([, ])
>> x.mean()
1.75
>> np.median(x)
1.5
>> np.median(y, axis=-1) # last axis
array()
>> x.std()          # full population standard dev.
0.82915619758885
```

> Exercise: Reductions
>
> Given there is a sum, what other function might you expect to see?
> What is the difference between sum and cumsum?

### Worked Example: data statistics

> Data in populations.txt describes the populations of hares and lynxes (and carrots) in northern Canada during 20 years.
>

```
>> !cat data/populations.txt

>> data = np.loadtxt('data/populations.txt')
>> year, hares, lynxes, carrots = data.T# trick: columns to variables

>> from matplotlib import pyplot as plt
>> plt.axes()
>> plt.plot(year, hares, year, lynxes, year, carrots)
>> plt.legend(('Hare', 'Lynx', 'Carrot'), loc=(1.05, 0.5))
>> plt.show()
```


```
>> populations = data[:, 1:]
>> populations.mean(axis=0) # <-- The mean populations over time:
array()
>> populations.std(axis=0) # <-- The sample standard deviations:
array()
>> np.argmax(populations, axis=1)# <-- Which species has the highest population each year?:
array()
```

### Worked Example: diffusion using a random walk algorithm



> Let us consider a simple 1D random walk process: at each time step a walker jumps right or left with equal probability.



> We are interested in finding the typical distance from the origin of a random walker after t left or right jumps?
We are going to simulate many “walkers” to find this law, and we are going to do so using array computing tricks: we are going to create a 2D array with the “stories” (each walker has a story) in one direction, and the time in the other:

```
>> n_stories = 1000 # number of walkers
>> t_max = 200      # time during which we follow the walker

>> t = np.arange(t_max)
>> steps = 2 * np.random.randint(0, 1 + 1, (n_stories, t_max)) - 1 # +1 because the high value is exclusive
>> np.unique(steps) # Verification: all steps are 1 or -1
array([-1,1])
>> positions = np.cumsum(steps, axis=1) # axis = 1: dimension of time
>> sq_distance = positions**2

>> mean_sq_distance = np.mean(sq_distance, axis=0)

>> plt.figure(figsize=(4, 3))

>> plt.plot(t, np.sqrt(mean_sq_distance), 'g.', t, np.sqrt(t), 'y-')

>> plt.xlabel(r"$t$")

>> plt.ylabel(r"$\sqrt{\langle (\delta x)^2 \rangle}$")

>> plt.tight_layout() # provide sufficient space for labels
# ply.show()# <-- We find a well-known result in physics: the RMS distance grows as the square root of the time!
```


hj170520 发表于 2020-6-9 23:11

本帖最后由 hj170520 于 2020-6-9 23:12 编辑




```
populations.txt :
# year      hare      lynx      carrot
1900      30e3      4e3      48300
1901      47.2e3      6.1e3      48200
1902      70.2e3      9.8e3      41500
1903      77.4e3      35.2e3      38200
1904      36.3e3      59.4e3      40600
1905      20.6e3      41.7e3      39800
1906      18.1e3      19e3      38600
1907      21.4e3      13e3      42300
1908      22e3      8.3e3      44500
1909      25.4e3      9.1e3      42100
1910      27.1e3      7.4e3      46000
1911      40.3e3      8e3      46800
1912      57e3      12.3e3      43800
1913      76.6e3      19.5e3      40900
1914      52.3e3      45.7e3      39400
1915      19.5e3      51.1e3      39000
1916      11.2e3      29.7e3      36700
1917      7.6e3      15.8e3      41800
1918      14.6e3      9.7e3      43300
1919      16.2e3      10.1e3      41300
1920      24.7e3      8.6e3      47300
```

OO2OO 发表于 2020-6-10 08:25

学习中,感谢总结分享

skyward 发表于 2020-6-10 09:32

上一篇还没看。。。

ymhld 发表于 2020-6-10 09:53

看着挺好,能给翻译一下?

hj170520 发表于 2020-6-10 10:10

ymhld 发表于 2020-6-10 09:53
看着挺好,能给翻译一下?

可以先适应着看,看着看着后面速度就提上来了。
代码结合英文看才是最大的提升,很多代码都是英文简写。{:301_986:}
页: [1]
查看完整版本: Numpy 入门(四)Basic reductions