Numpyにおけるデータの取得と格納、及び関数呼び出し

ndarrayにおける色んな参照方法をやってみた

```python
import numpy as np
print(np.__version__)
data = np.array([[1,2,3,4,5], [10,20,30,40,50], [100,200,300,400,500]])
print(data)
```

    1.19.2
    [[  1   2   3   4   5]
     [ 10  20  30  40  50]
     [100 200 300 400 500]]
    

## 取得


```python
d = data[1]
print(d)
```

    [10 20 30 40 50]
    


```python
d = data[[1]]
print(d)
```

    [[10 20 30 40 50]]
    


```python
d = data[:,2]
print(d)
```

    [  3  30 300]
    


```python
d = data[:,2:3]
print(d)
```

    [[  3]
     [ 30]
     [300]]
    


```python
d = data[1,2:4]
print(d)
```

    [30 40]
    


```python
d = data[1:2,2:4]
print(d)
```

    [[30 40]]
    


```python
d = data[1,2]
print(d)
```

    30
    


```python
d = data[1,2:3]
print(d)
```

    [30]
    


```python
d = data[1:2,2:3]
print(d)
```

    [[30]]
    


```python
d = data[[0,1,2],1]
print(d)
```

    [  2  20 200]
    


```python
d = data[1,[1,2,3]]
print(d)
```

    [20 30 40]
    


```python
d = data[[1,2],[2,4]]
print(d)
```

    [ 30 500]
    

```
d = data[[0,1],[1,2,3]]
print(d)

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-14-d9878e643f58> in <module>
----> 1 d = data[[0,1],[1,2,3]]
      2 print(d)

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,) 
```
 


```python
d = data[np.ix_([0, 1], [1,2,3])]
print(d)
```

    [[ 2  3  4]
     [20 30 40]]
    

# 代入


```python
data1 = data.copy()
data1[1] = 1000
print(data1)
```

    [[   1    2    3    4    5]
     [1000 1000 1000 1000 1000]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
data1[[1]] = 1000
print(data1)
```

    [[   1    2    3    4    5]
     [1000 1000 1000 1000 1000]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
data1[:,2] = 1000
print(data1)

```

    [[   1    2 1000    4    5]
     [  10   20 1000   40   50]
     [ 100  200 1000  400  500]]
    


```python
data1 = data.copy()
data1[:,2:3] = 1000
print(data1)
```

    [[   1    2 1000    4    5]
     [  10   20 1000   40   50]
     [ 100  200 1000  400  500]]
    


```python
data1 = data.copy()
data1[1,2:4] = 1000
print(data1)
```

    [[   1    2    3    4    5]
     [  10   20 1000 1000   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
data1[1:2,2:4] = 1000
print(data1)
```

    [[   1    2    3    4    5]
     [  10   20 1000 1000   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
data1[1,2] = 1000
print(data1)

```

    [[   1    2    3    4    5]
     [  10   20 1000   40   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
data1[1,2:3] = 1000
print(data1)
```

    [[   1    2    3    4    5]
     [  10   20 1000   40   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
data1[1:2,2:3] = 1000
print(data1)
```

    [[   1    2    3    4    5]
     [  10   20 1000   40   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
data1[[0,1,2],1] = 1000
print(data1)
```

    [[   1 1000    3    4    5]
     [  10 1000   30   40   50]
     [ 100 1000  300  400  500]]
    


```python
data1 = data.copy()
data1[1,[1,2,3]] = 1000
print(data1)
```

    [[   1    2    3    4    5]
     [  10 1000 1000 1000   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
data1[np.ix_([0, 1], [1,2,3])] = 1000
print(data1)
```

    [[   1 1000 1000 1000    5]
     [  10 1000 1000 1000   50]
     [ 100  200  300  400  500]]
    

## 関数呼び出し


```python
def hoge(d):
    print(d)
    d[0] = 2000
    print(d)
```


```python
data1 = data.copy()
hoge(data1[1])
print(data1)
```

    [10 20 30 40 50]
    [2000   20   30   40   50]
    [[   1    2    3    4    5]
     [2000   20   30   40   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
hoge(data1[[1]])
print(data1)
```

    [[10 20 30 40 50]]
    [[2000 2000 2000 2000 2000]]
    [[  1   2   3   4   5]
     [ 10  20  30  40  50]
     [100 200 300 400 500]]
    


```python
data1 = data.copy()
hoge(data1[:,2])
print(data1)
```

    [  3  30 300]
    [2000   30  300]
    [[   1    2 2000    4    5]
     [  10   20   30   40   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
hoge(data1[:,2:3])
print(data1)

```

    [[  3]
     [ 30]
     [300]]
    [[2000]
     [  30]
     [ 300]]
    [[   1    2 2000    4    5]
     [  10   20   30   40   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
hoge(data1[1,2:4])
print(data1)
```

    [30 40]
    [2000   40]
    [[   1    2    3    4    5]
     [  10   20 2000   40   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
hoge(data1[1:2,2:4])
print(data1)
```

    [[30 40]]
    [[2000 2000]]
    [[   1    2    3    4    5]
     [  10   20 2000 2000   50]
     [ 100  200  300  400  500]]
    

```
data1 = data.copy()
hoge(data1[1,2])
print(data1)

30
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-65-07c98b5a0381> in <module>
      1 data1 = data.copy()
----> 2 hoge(data1[1,2])
      3 print(data1)

<ipython-input-58-7cd04c1a5a75> in hoge(d)
      1 def hoge(d):
      2     print(d)
----> 3     d[0] = 2000
      4     print(d)

TypeError: 'numpy.int32' object does not support item assignment
```



```python
data1 = data.copy()
hoge(data1[1,2:3])
print(data1)
```

    [30]
    [2000]
    [[   1    2    3    4    5]
     [  10   20 2000   40   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
hoge(data1[1:2,2:3])
print(data1)
```

    [[30]]
    [[2000]]
    [[   1    2    3    4    5]
     [  10   20 2000   40   50]
     [ 100  200  300  400  500]]
    


```python
data1 = data.copy()
hoge(data1[[0,1,2],1])
print(data1)
```

    [  2  20 200]
    [2000   20  200]
    [[  1   2   3   4   5]
     [ 10  20  30  40  50]
     [100 200 300 400 500]]
    


```python
data1 = data.copy()
hoge(data1[1,[1,2,3]])
print(data1)

```

    [20 30 40]
    [2000   30   40]
    [[  1   2   3   4   5]
     [ 10  20  30  40  50]
     [100 200 300 400 500]]
    


```python
data1 = data.copy()
hoge(data1[np.ix_([0, 1], [1,2,3])])
print(data1)
```

    [[ 2  3  4]
     [20 30 40]]
    [[2000 2000 2000]
     [  20   30   40]]
    [[  1   2   3   4   5]
     [ 10  20  30  40  50]
     [100 200 300 400 500]]