numpy学習ノート1
8630 ワード
python numpy基本操作
numpy arrayはndarrayと呼ばれ、numpyのndarrayはpythonのarrayとは異なる.python array.arrayは1次元配列でnp.arrayは多次元配列であり,各次元を軸(axes)と呼ぶ.
1、np.Arrayコンストラクタ
(1)、npを利用する.array関数はnpを作成する.array
numpy.array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0)
注意:npのようなシーケンスを括弧を省略する直接挿入することはできない.array(2,3,4)
ここには[[],[]が挿入されています.
(2)、プレースホルダによる配列の構築
zeros関数は初期値が0の配列を構築します
「`python zero=np.zeros((3,4)##はプレースホルダであり空間のみを表すため、zerosの中にはzero 2=np.zeros(3)#np.zerosで構成されたものが配列であるため、この関数で一次元配列print(zero)print(zero 2)print(zero 2)を構築することができる
ones関数は初期値が1の配列を構築する
Empty関数は初期値のランダムな配列を構築し、デフォルトタイプはfloat 64です.
(3)、乱数関数randomにより所与の大きさの配列を生成する
ここにはnpがたくさんあります.randomライブラリの関数はnp.randomはnumpyのrandomライブラリなので、ほとんどの関数はndarrayフォーマットを操作しているので、詳しくは列挙しません.
(4)、arange関数を用いて等差配列を作成しarrayを直接生成する
2、np.Array形状操作
(1)、numpy.reshape関数
numpy.reshape(a, newshape, order=’C’)
a:reshapeが必要な配列
Newshape:(2,3)、すなわち2行3列のような変換が必要です.(2,-1)の場合は出力2行を示し,列数は配列要素の個数と与えられた行数から算出される.
order:Cスタイル「`python reshape=np.arange(6).reshape((3,2))#reshapeの行列は、元の配列サイズと同じprint(reshape)reshape 2=np.reshape(2,3))print(reshape 2)reshape 3=np.reshape(reshape 2,(3,-1))print(reshape 3)
注:reshapeの後、reshapeの後、連続スライスの有無にかかわらず、元の配列とメモリを共有することに注意してください.
(3)、深いコピー
copy関数は新しいオブジェクトを生成します
numpy arrayはndarrayと呼ばれ、numpyのndarrayはpythonのarrayとは異なる.python array.arrayは1次元配列でnp.arrayは多次元配列であり,各次元を軸(axes)と呼ぶ.
1、np.Arrayコンストラクタ
(1)、npを利用する.array関数はnpを作成する.array
numpy.array(object, dtype=None, copy=True, order=’K’, subok=False, ndmin=0)
import numpy as np
a = np.array([2,3,4])
print(a)
print(type(a))
print(a.dtype)
print(a.shape)
print('a[0]is ',a[0])
#print('a[0][1]is',a[0][1]) is wrong
[2 3 4]
int32
(3,)
a[0]is 2
注意:npのようなシーケンスを括弧を省略する直接挿入することはできない.array(2,3,4)
#
b = np.array([[1,2,3],[4,5,6]],dtype = np.int16)
print(b)
print('b shape is',b.shape)
print('b[0] is',b[0])
print('b[0]type is',type(b[0]))
print('b[0] dtype is',b[0].dtype)
[[1 2 3]
[4 5 6]]
b shape is (2, 3)
b[0] is [1 2 3]
b[0]type is
b[0] dtype is int16
ここには[[],[]が挿入されています.
# range list
c= np.array( range(0,100) )
print(c)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99]
(2)、プレースホルダによる配列の構築
zeros関数は初期値が0の配列を構築します
「`python zero=np.zeros((3,4)##はプレースホルダであり空間のみを表すため、zerosの中にはzero 2=np.zeros(3)#np.zerosで構成されたものが配列であるため、この関数で一次元配列print(zero)print(zero 2)print(zero 2)を構築することができる
```
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[ 0. 0. 0.]
ones関数は初期値が1の配列を構築する
one = np.ones( (2,4),dtype = int)
print(one)
[[1 1 1 1]
[1 1 1 1]]
Empty関数は初期値のランダムな配列を構築し、デフォルトタイプはfloat 64です.
empty = np.empty((2,5))
print(empty)
[[ 2.12199579e-314 0.00000000e+000 2.12199579e-314 1.42418987e-306
1.50200295e-307]
[ 4.67296746e-307 1.69121096e-306 1.29062568e-306 1.42419938e-306
7.56603881e-307]]
(3)、乱数関数randomにより所与の大きさの配列を生成する
ここにはnpがたくさんあります.randomライブラリの関数はnp.randomはnumpyのrandomライブラリなので、ほとんどの関数はndarrayフォーマットを操作しているので、詳しくは列挙しません.
rand = 10*np.random.random((2,4))
print(rand)
randint = np.floor(rand)
print(randint)
randi = np.random.randint(3,high = 6,size=10) # 3, 6,size 10
print(randi)
randi2 = np.random.randint(5,size=(2,4)) # high , 5.
print(randi2)
[[ 9.69936167 4.35667075 6.81937385 8.76402435]
[ 2.95693062 1.50591608 5.02003945 5.43058465]]
[[ 9. 4. 6. 8.]
[ 2. 1. 5. 5.]]
[5 3 5 3 5 5 5 3 3 3]
[[1 3 2 3]
[3 1 0 4]]
(4)、arange関数を用いて等差配列を作成しarrayを直接生成する
arange1 = np.arange(6) # 0-6
print(arange1)
arange = np.arange(10,30,5)
print(arange)
print(arange.dtype)
print(arange.size)
[0 1 2 3 4 5]
[10 15 20 25]
int32
4
2、np.Array形状操作
(1)、numpy.reshape関数
numpy.reshape(a, newshape, order=’C’)
a:reshapeが必要な配列
Newshape:(2,3)、すなわち2行3列のような変換が必要です.(2,-1)の場合は出力2行を示し,列数は配列要素の個数と与えられた行数から算出される.
order:Cスタイル「`python reshape=np.arange(6).reshape((3,2))#reshapeの行列は、元の配列サイズと同じprint(reshape)reshape 2=np.reshape(2,3))print(reshape 2)reshape 3=np.reshape(reshape 2,(3,-1))print(reshape 3)
```
[[0 1]
[2 3]
[4 5]]
[[0 1 2]
[3 4 5]]
[[0 1]
[2 3]
[4 5]]
注:reshapeの後、reshapeの後、連続スライスの有無にかかわらず、元の配列とメモリを共有することに注意してください.
```
a is v ? False
v is [ 0 1 199 3 4 5 6 7 8 9 10 11]
a is [[ 0 1 199]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
v is [ 0 1 199 3 4 5 6 7 8 9 10 11]
s = v[1:5] # ,s a , ,
s[2] = 9999
print('v is ',v)
v is [ 0 1 199 9999 4 5 6 7 8 9 10 11]
(3)、深いコピー
copy関数は新しいオブジェクトを生成します
co = np.arange(12)
co1 = co.copy()
co1[2] = 999 # co1 co
co1.shape = (3,4) # co1 co
print('co is',co)
print('co1 is',co1)
co is [ 0 1 2 3 4 5 6 7 8 9 10 11]
co1 is [[ 0 1 999 3]
[ 4 5 6 7]
[ 8 9 10 11]]