Numpyにおけるデータの取得と格納、及び関数呼び出し
ndarrayにおける色んな参照方法をやってみた
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]]
取得
d = data[1]
print(d)
[10 20 30 40 50]
d = data[[1]]
print(d)
[[10 20 30 40 50]]
d = data[:,2]
print(d)
[ 3 30 300]
d = data[:,2:3]
print(d)
[[ 3]
[ 30]
[300]]
d = data[1,2:4]
print(d)
[30 40]
d = data[1:2,2:4]
print(d)
[[30 40]]
d = data[1,2]
print(d)
30
d = data[1,2:3]
print(d)
[30]
d = data[1:2,2:3]
print(d)
[[30]]
d = data[[0,1,2],1]
print(d)
[ 2 20 200]
d = data[1,[1,2,3]]
print(d)
[20 30 40]
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,)
d = data[np.ix_([0, 1], [1,2,3])]
print(d)
[[ 2 3 4]
[20 30 40]]
代入
data1 = data.copy()
data1[1] = 1000
print(data1)
[[ 1 2 3 4 5]
[1000 1000 1000 1000 1000]
[ 100 200 300 400 500]]
data1 = data.copy()
data1[[1]] = 1000
print(data1)
[[ 1 2 3 4 5]
[1000 1000 1000 1000 1000]
[ 100 200 300 400 500]]
data1 = data.copy()
data1[:,2] = 1000
print(data1)
[[ 1 2 1000 4 5]
[ 10 20 1000 40 50]
[ 100 200 1000 400 500]]
data1 = data.copy()
data1[:,2:3] = 1000
print(data1)
[[ 1 2 1000 4 5]
[ 10 20 1000 40 50]
[ 100 200 1000 400 500]]
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]]
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]]
data1 = data.copy()
data1[1,2] = 1000
print(data1)
[[ 1 2 3 4 5]
[ 10 20 1000 40 50]
[ 100 200 300 400 500]]
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]]
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]]
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]]
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]]
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]]
関数呼び出し
def hoge(d):
print(d)
d[0] = 2000
print(d)
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]]
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]]
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]]
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]]
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]]
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
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]]
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]]
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]]
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]]
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]]
data1 = data.copy()
data1[1] = 1000
print(data1)
[[ 1 2 3 4 5]
[1000 1000 1000 1000 1000]
[ 100 200 300 400 500]]
data1 = data.copy()
data1[[1]] = 1000
print(data1)
[[ 1 2 3 4 5]
[1000 1000 1000 1000 1000]
[ 100 200 300 400 500]]
data1 = data.copy()
data1[:,2] = 1000
print(data1)
[[ 1 2 1000 4 5]
[ 10 20 1000 40 50]
[ 100 200 1000 400 500]]
data1 = data.copy()
data1[:,2:3] = 1000
print(data1)
[[ 1 2 1000 4 5]
[ 10 20 1000 40 50]
[ 100 200 1000 400 500]]
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]]
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]]
data1 = data.copy()
data1[1,2] = 1000
print(data1)
[[ 1 2 3 4 5]
[ 10 20 1000 40 50]
[ 100 200 300 400 500]]
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]]
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]]
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]]
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]]
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]]
def hoge(d):
print(d)
d[0] = 2000
print(d)
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]]
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]]
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]]
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]]
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]]
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
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]]
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]]
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]]
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]]
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]]
Author And Source
この問題について(Numpyにおけるデータの取得と格納、及び関数呼び出し), 我々は、より多くの情報をここで見つけました https://qiita.com/ShimantoAkira/items/d659e407faf5c5628900著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .