科学計算ライブラリ-Numpy
11146 ワード
Numpy(科学計算ライブラリ)
NumPyシステムはPythonのオープンソースの数値計算拡張であり,このツールは大型マトリクスの格納と処理に利用できる.
1.データ構造
2.基本操作
3.マトリックス属性
4.マトリクス操作
5.基本関数
6.関数のいくつかのコピー方法
まとめ:1.ファイルを読み込むgenfromtxt(“test.txt”,delimiter=",",dtype=str) 2.行列を作成するarray 3.次元vectorを表示します.shape 4.データ型vectorを表示します.dtype 5.配列スライスvector[0:3]6.配列に要素10 print(vector==10)が含まれているかどうかを確認します.タイプ変換astype 8.最小値を探すmin() 9.行ごとにmatrixを求める.sum(axis=1) 10.列ごとにmatrixを求める.sum(axis=0) 11.次元変換reshape(3,5) 12.次元a.ndim 13を表示する.初期化マトリクス、3行4列、値はすべて0.np.zeros ((3,4)) 14.初期化マトリクス、2つの3行4列、値はすべて1.np.ones((2,3,4),dtype=np.int32) 15.始点10、終点50、差5.np.arange(10,50,5) 16.乱数を生成する、[-1,1]区間から2行4列npを生成する.random.random((2,4)) 17.xからyまでの平均z個数linspace(x,y,z)18をとる.マトリクス対応位置乗算A*B 19.行列乗算A.dot(B)np.dot(A,B) 20.exp:平方sqrt:ルート番号21.多次元配列を1次元配列a.ravel()22に変換する.aをprint(a.T)23に転置.垂直接合:np.vstack((a,b))横接合:np.hstack((a,b)) 24.垂直分割:np.hsplit((a,b))横分割:np.hsplit(a,3) 25.コピー方法:=、view、copy 26.検索操作:axis=0列で検索、axis=1行で検索27.拡張操作:np.tile(a,(2,3)) 28.ソート:np.sort(a,axis=1)最小数のインデックスを求める:np.argsort(a)
NumPyシステムはPythonのオープンソースの数値計算拡張であり,このツールは大型マトリクスの格納と処理に利用できる.
1.データ構造
#1.1
test = numpy.genfromtxt("test.txt",delimiter=",",dtype=str)
//delimiter:# ,
//dtype: 。
print(type(test))
print(test)
print(help(numpy.genfromtxt))
numpy.genfromtxt() 。 :
numpy.genfromtxt(fname, dtype=, delimiter=None, skip_header=0, skip_footer=0)
fname
dtype ,dtype=str / int
delimiter , , :delimiter=',';
skip_header , , 0 , 1 2 1 2
world_alcohol = numpy.genfromtxt("world_alcohol.txt",delimiter=",", dtype=str, skip_header=1)
print(world_alcohol)
[['1986' 'Western Pacific' 'Viet Nam' 'Wine' '0']
['1986' 'Americas' 'Uruguay' 'Other' '0.5']
['1985' 'Africa' "Cte d'Ivoire" 'Wine' '1.62']
...
['1987' 'Africa' 'Malawi' 'Other' '0.75']
['1989' 'Americas' 'Bahamas' 'Wine' '1.5']
['1985' 'Africa' 'Malawi' 'Spirits' '0.31']]
#1.2
import numpy
vector = numpy.array([5,10,15,20])
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print (vector)
print (matrix)
[ 5 10 15 20]
[[ 5 10 15]
[20 25 30]
[35 40 45]]
#1.3
vector = numpy.array([5,10,15,20])
print (vector.shape)
matrix = numpy.array([[5,10,15],[20,25,30],[35,40,45]])
print (matrix.shape)
(4,)
(3, 3)
#1.4
# ,
vector = numpy.array([1,2,3,4])
print (vector)
print (vector.dtype)
[1 2 3 4]
dtype('int64')
#1.5
other_1986 = world_alcohol[1,4]
third_country = world_alcohol[2,2]
print (other_1986)
print (third_country)
0.5
Cte d'Ivoire
#1.6
vector = numpy.array([5,10,15,20])
print(vector[0:3])
[ 5 10 15]
#1.7
matix = numpy.array([
[5,10,15],
[10,15,20],
[15,20,25],
])
print(matix[:,1])
print("---------")
print(matix[:,0:2])
print("---------")
print(matix[0:1,0:2])
print("---------")
print(matix[0:2,0:2])
[10 15 20]
---------
[[ 5 10]
[10 15]
[15 20]]
---------
[[ 5 10]
---------
[[ 5 10]
[10 15]]
2.基本操作
#2.1
# 10
import numpy
vector = numpy.array([5,10,15,20])
print(vector == 10)
array([False, True, False, False])
#2.2
# 25
matix = numpy.array([
[5,10,15],
[10,15,20],
[15,20,25],
])
print(matix == 25)
array([[False, False, False],
[False, False, False],
[False, False, True]])
#2.3
# False
vector = numpy.array([5,10,15,20])
equal_to_ten = (vector == 10)
print (equal_to_ten)
print (vector[equal_to_ten])
[False True False False]
[10]
#2.4
vector = numpy.array([5,10,15,20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
equal_to_ten_or_five = (vector == 10) | (vector == 5)
print (equal_to_ten_and_five)
print (equal_to_ten_or_five)
vector[equal_to_ten_or_five] = 50
print (vector)
[False False False False]
[ True True False False]
[50 50 15 20]
#2.5
vector = numpy.array(["1","2","3"])
print (vector.dtype)
print (vector)
print ("--------")
vector = vector.astype(float)
print (vector.dtype)
print (vector)
#2.6
vector = numpy.array([5,10,15,20])
vector.min()
print (vector.min())
5
#2.7
#2.8
matrix = numpy.array([
[5,10,15],
[20,25,30],
[35,40,45]
])
#
print (matrix.sum(axis=1))
#
print (matrix.sum(axis=0))
[ 30 75 120]
[60 75 90]
3.マトリックス属性
import numpy as np
print (np.arange(15))
#
a = np.arange(15).reshape(3,5)
print ("------")
print (a)
print ("------")
#
print (a.shape)
print ("------")
#
print (a.ndim)
print ("------")
#
print (a.dtype.name)
print ("------")
#
print (a.size)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
------
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]
------
(3, 5)
------
2
------
int64
------
15
4.マトリクス操作
#4.1 ,3 4 , 0
np.zeros ((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
#4.2 ,2 3 4 , 1
np.ones((2,3,4),dtype=np.int32)
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]], dtype=int32)
#4.3
# 10, 50, 5
np.arange(10,50,5)
array([10, 15, 20, 25, 30, 35, 40, 45])
#4.4 ,
np.arange(20).reshape(4,5)
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19]])
#4.5 [-1,1] , 2 4
np.random.random((2,4))
array([[0.96736023, 0.13918327, 0.92569963, 0.86135961],
[0.6545531 , 0.80578378, 0.44185247, 0.13230089]])
#4.6 0 2*pi 10
from numpy import pi
np.linspace(0,2*pi,10)
array([0. , 0.6981317 , 1.3962634 , 2.0943951 , 2.7925268 ,
3.4906585 , 4.1887902 , 4.88692191, 5.58505361, 6.28318531])
#4.7
A = np.array( [[1,1],[0,1]] )
B = np.array( [[2,0],[3,4]] )
print(A)
print("-----------")
print(B)
print("-----------")
#
print(A*B)
print("-----------")
#
print(A.dot(B))
print("-----------")
#
print(np.dot(A,B))
[[1 1]
[0 1]]
-----------
[[2 0]
[3 4]]
-----------
[[2 0]
[0 4]]
-----------
[[5 4]
[3 4]]
-----------
[[5 4]
[3 4]]
5.基本関数
# 5.1
# exp: sqrt:
import numpy as np
B = np.arange(3)
print(B)
print(np.exp(B))
print(np.sqrt(B))
[0 1 2]
[1. 2.71828183 7.3890561 ]
[0. 1. 1.41421356]
# 5.2
#ravel:
a = np.floor(10*np.random.random((3,4)))
print(a)
print("--------")
print(a.ravel())
print("--------")
a.shape = (6,2)
print(a)
print("-------")
# a
print(a.T)
[[2. 7. 8. 5.]
[1. 0. 1. 2.]
[0. 6. 6. 6.]]
--------
[2. 7. 8. 5. 1. 0. 1. 2. 0. 6. 6. 6.]
--------
[[2. 7.]
[8. 5.]
[1. 0.]
[1. 2.]
[0. 6.]
[6. 6.]]
-------
[[2. 8. 1. 1. 0. 6.]
[7. 5. 0. 2. 6. 6.]]
#5.3
#horizontal ,vertocal 。
import numpy as np
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print("-----")
print(b)
print("-----")
#
print(np.vstack((a,b)))
print("-----")
#
print(np.hstack((a,b)))
[[6. 0.]
[5. 6.]]
-----
[[6. 8.]
[9. 4.]]
-----
[[6. 0.]
[5. 6.]
[6. 8.]
[9. 4.]]
-----
[[6. 0. 6. 8.]
[5. 6. 9. 4.]]
#5.4
a = np.floor(10*np.random.random((2,12)))
print(a)
print("------")
print(np.hsplit(a,3))
print("------")
# 3 4
print(np.hsplit(a,(3,4)))
a = np.floor(10*np.random.random((12,2)))
print("------")
print(a)
np.vsplit(a,3)
[[4. 4. 6. 3. 5. 4. 4. 3. 8. 6. 3. 7.]
[9. 7. 6. 3. 8. 2. 0. 2. 3. 5. 3. 6.]]
------
[array([[4., 4., 6., 3.],
[9., 7., 6., 3.]]), array([[5., 4., 4., 3.],
[8., 2., 0., 2.]]), array([[8., 6., 3., 7.],
[3., 5., 3., 6.]])]
------
[array([[4., 4., 6.],
[9., 7., 6.]]), array([[3.],
[3.]]), array([[5., 4., 4., 3., 8., 6., 3., 7.],
[8., 2., 0., 2., 3., 5., 3., 6.]])]
------
[[7. 7.]
[7. 9.]
[3. 8.]
[2. 4.]
[0. 8.]
[1. 1.]
[1. 7.]
[7. 6.]
[6. 2.]
[3. 7.]
[0. 6.]
[6. 1.]]
[array([[7., 7.],
[7., 9.],
[3., 8.],
[2., 4.]]), array([[0., 8.],
[1., 1.],
[1., 7.],
[7., 6.]]), array([[6., 2.],
[3., 7.],
[0., 6.],
[6., 1.]])]
6.関数のいくつかのコピー方法
6.1 =
# a b ,
a = np.arange(12)
b = a
print (b is a)
b.shape = (3,4)
print(a.shape)
print(id(a))
print(id(b))
True
(3, 4)
4432047680
4432047680
#6.2 view
#a c ,
c = a.view()
print (c is a)
c.shape = 2,6
print (a.shape)
c[0,4] = 1234
print (a)
print ("------")
print (c)
print (id(a))
print (id(c))
False
(3, 4)
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
------
[[ 0 1 2 3 1234 5]
[ 6 7 8 9 10 11]]
4432065840
4432066240
#6.3 copy
#copy ,a d
d = a.copy()
d is a
d[0,0] = 9999
print(d)
print("------")
print(a)
[[9999 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
------
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
#6.4
import numpy as np
data = np.sin(np.arange(20)).reshape(5,4)
print(data)
#axis = 0 ,axis = 1
ind = data.argmax(axis = 0)
print(ind)
data_max = data[ind,range(data.shape[1])]
print(data_max)
[[ 0. 0.84147098 0.90929743 0.14112001]
[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
[-0.53657292 0.42016704 0.99060736 0.65028784]
[-0.28790332 -0.96139749 -0.75098725 0.14987721]]
[2 0 3 1]
[0.98935825 0.84147098 0.99060736 0.6569866 ]
#6.5 tile
a = np.arange(0,40,10)
print(a)
b = np.tile(a,(2,3))
print (b)
[ 0 10 20 30]
[[ 0 10 20 30 0 10 20 30 0 10 20 30]
[ 0 10 20 30 0 10 20 30 0 10 20 30]]
#6.6 sort
a = np.array([[4,3,5],[1,3,5]])
print(a)
b = np.sort(a,axis = 1)
print ("------b")
print (b)
a.sort(axis = 1)
print ("------a")
print (a)
a = np.array([4,3,1,2])
# ,
j = np.argsort(a)
print ("------")
print(j)
print ("------")
#
print(a[j])
[[4 3 5]
[1 3 5]]
------b
[[3 4 5]
[1 3 5]]
------a
[[3 4 5]
[1 3 5]]
------
[2 3 1 0]
------
[1 2 3 4]
まとめ:1.ファイルを読み込むgenfromtxt(“test.txt”,delimiter=",",dtype=str) 2.行列を作成するarray 3.次元vectorを表示します.shape 4.データ型vectorを表示します.dtype 5.配列スライスvector[0:3]6.配列に要素10 print(vector==10)が含まれているかどうかを確認します.タイプ変換astype 8.最小値を探すmin() 9.行ごとにmatrixを求める.sum(axis=1) 10.列ごとにmatrixを求める.sum(axis=0) 11.次元変換reshape(3,5) 12.次元a.ndim 13を表示する.初期化マトリクス、3行4列、値はすべて0.np.zeros ((3,4)) 14.初期化マトリクス、2つの3行4列、値はすべて1.np.ones((2,3,4),dtype=np.int32) 15.始点10、終点50、差5.np.arange(10,50,5) 16.乱数を生成する、[-1,1]区間から2行4列npを生成する.random.random((2,4)) 17.xからyまでの平均z個数linspace(x,y,z)18をとる.マトリクス対応位置乗算A*B 19.行列乗算A.dot(B)np.dot(A,B) 20.exp:平方sqrt:ルート番号21.多次元配列を1次元配列a.ravel()22に変換する.aをprint(a.T)23に転置.垂直接合:np.vstack((a,b))横接合:np.hstack((a,b)) 24.垂直分割:np.hsplit((a,b))横分割:np.hsplit(a,3) 25.コピー方法:=、view、copy 26.検索操作:axis=0列で検索、axis=1行で検索27.拡張操作:np.tile(a,(2,3)) 28.ソート:np.sort(a,axis=1)最小数のインデックスを求める:np.argsort(a)