はじめに
NumPyのlinspace()は、startからstopの間を指定の個数で等間隔に区切った配列を生成する関数である。
numpy.linspace
(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
引数には、start,stopに加えて、num, endpoint, retstep, dtype, axisがある。ここではその詳細を説明する。
num(要素数)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np
data_1 = np.linspace(0,10,11)
data_1
#array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
start=0, stop=10, num=11となる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_2 = np.linspace(0,10,101)
data_2
'''
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ,
1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1,
2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2,
3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4. , 4.1, 4.2, 4.3,
4.4, 4.5, 4.6, 4.7, 4.8, 4.9, 5. , 5.1, 5.2, 5.3, 5.4,
5.5, 5.6, 5.7, 5.8, 5.9, 6. , 6.1, 6.2, 6.3, 6.4, 6.5,
6.6, 6.7, 6.8, 6.9, 7. , 7.1, 7.2, 7.3, 7.4, 7.5, 7.6,
7.7, 7.8, 7.9, 8. , 8.1, 8.2, 8.3, 8.4, 8.5, 8.6, 8.7,
8.8, 8.9, 9. , 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 9.7, 9.8,
9.9, 10. ])
'''
numを増やすことで配列の間隔を狭めることができる。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_3 = np.linspace(0,10)
data_3
'''
array([ 0. , 0.20408163, 0.40816327, 0.6122449 , 0.81632653,
1.02040816, 1.2244898 , 1.42857143, 1.63265306, 1.83673469,
2.04081633, 2.24489796, 2.44897959, 2.65306122, 2.85714286,
3.06122449, 3.26530612, 3.46938776, 3.67346939, 3.87755102,
4.08163265, 4.28571429, 4.48979592, 4.69387755, 4.89795918,
5.10204082, 5.30612245, 5.51020408, 5.71428571, 5.91836735,
6.12244898, 6.32653061, 6.53061224, 6.73469388, 6.93877551,
7.14285714, 7.34693878, 7.55102041, 7.75510204, 7.95918367,
8.16326531, 8.36734694, 8.57142857, 8.7755102 , 8.97959184,
9.18367347, 9.3877551 , 9.59183673, 9.79591837, 10. ])
'''
data_4 = np.linspace(0,10,50)
(data_3 == data_4).all()
#True
defaultではnum=50となる。
endpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_5 = np.linspace(0,10,11,endpoint=True)
data_5
#array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
data_6 = np.linspace(0,10,11,endpoint=False)
data_6, (10-0)/11
(array([0. , 0.90909091, 1.81818182, 2.72727273, 3.63636364,
4.54545455, 5.45454545, 6.36363636, 7.27272727, 8.18181818,
9.09090909]), 0.9090909090909091)
endpointでstopの値を配列に含めるかを設定できる。デフォルトはTrueなのでstopは含まれる。
endpoint=Falseとすると、stopの値が含まれなくなる。そのため、間隔は(stop-start)/numとなる。
retstep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_7 = np.linspace(0,10,11,retstep=True)
data_7
#(array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0)
data_7[1]
#1.0
retstep=Trueとすることで等差数列とその差を一度に得ることができる。
data_7[1]のようにすることでその差を得ることができる。
dtype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_8 = np.linspace(0,10,11,dtype=int)
data_8.dtype
#dtype('int64')
data_1.dtype
dtype('float64')
dtype=intとすることでint型のlinspace配列が得られる。指定しない場合はfloat型となる。
axis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data_10 = np.linspace([0,10],[10,20],11)# axis=0
data_10
'''
array([[ 0., 10.],
[ 1., 11.],
[ 2., 12.],
[ 3., 13.],
[ 4., 14.],
[ 5., 15.],
[ 6., 16.],
[ 7., 17.],
[ 8., 18.],
[ 9., 19.],
[10., 20.]])
'''
data_11 = np.linspace([0,10],[10,20],11, axis=-1)
data_11
'''
array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.],
[10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20.]])
'''
配列にlinspace()を適用することもできる。axisで配列を伸ばす方向を指定できる。デフォルトではaxis=0。axis=-1とすることで生成する配列の形状を変化させることができる。
参考
numpy.linspace — NumPy v2.2 Manual
コメント