NumPy | 07 create an array from a range of values

numpy.arange *****

numpy package using arange function to create and return a numerical range ndarray objects, functions in the following format:

numpy.arange(start, stop, step, dtype)

The start and stop step and the step to set the specified range to generate a ndarray.

parameter description
start Start value, default is0
stop Termination value (no)
step Step, default1
dtype Return ndarraydata type, if one is not provided use of the type of input data.

 

Example 1:  generating an array of 0 to 5:

import numpy as np
 
x = np.arange(5)  
print (x)

Output:

[0  1  2 3 4]

 

Example 2: Bit Set return type float

import numpy as np
 
# Set the DTYPE 
X = np.arange (. 5, DTYPE = a float )  
 Print (X)

Output:

[0.  1.  2. 3. 4.]

 

Example 3: set the start value and the end value of the step size

import numpy as np

x = np.arange(10,20,2)  
print (x)

Output:

[10  12  14 16 18]

numpy.linspace

numpy.linspace function to create a one-dimensional array, the array is composed of an arithmetic sequence, the following format:

np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
parameter description
start Sequence start value
stop Termination sequence of values, if endpointis true, the value is included in the number of column
num Steps quantity of sample to be generated, the default is50
endpoint This value is ture, the number of the column contains the stopvalues, whereas it does not, the default is True.
retstep If True, the resulting array will appear in the pitch, the converse is not displayed.
dtype ndarray Data types

 

Example 1: set to a start point, end point 10, the number of columns 10

import numpy as np

a = np.linspace(1,10,10)
print(a)

The output is:

[ 1.  2. 3. 4. 5. 6. 7. 8. 9. 10.]

 

Example 2: Set all elements of an arithmetic sequence

import numpy as np

a = np.linspace(1,1,10)
print(a)

The output is:

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

 

Example 3: The endpoint is set to false, not including the stop value

import numpy as np
 
a = np.linspace(10, 20,  5, endpoint =  False)  
print(a)

The output is:

[10. 12. 14. 16. 18.]

If the endpoint is set to true, it will contain 20.

import numpy as np

a = np.linspace(10, 20, 5, endpoint = True)
print(a)

The output is:

[10.  12.5 15.  17.5 20. ]

 

Example 4: disposing interval

import numpy as np

a =np.linspace(1,10,10,retstep= True)
print(a)

# Expand Examples 
B = np.linspace (1,10,10) .reshape ([10,1 ])
 Print (B)

The output is:

(array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0) [[ 1.] [ 2.] [ 3.] [ 4.] [ 5.] [ 6.] [ 7.] [ 8.] [ 9.] [10.]]

numpy.logspace

numpy.logspace 函数用于创建一个于等比数列。格式如下:

np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)
参数 描述
start 序列的起始值为:base ** start
stop 序列的终止值为:base ** stop。如果endpointtrue,该值包含于数列中
num 要生成的等步长的样本数量,默认为50
endpoint 该值为 ture 时,数列中中包含stop值,反之不包含,默认是True。
base 对数 log 的底数。
dtype ndarray 的数据类型

实例1

import numpy as np

# 默认底数是 10
a = np.logspace(1.0,  2.0, num =  10)  
print (a)

输出结果为:

[ 10.           12.91549665 16.68100537 21.5443469 27.82559402 35.93813664 46.41588834 59.94842503 77.42636827 100. ]

 

实例2: 将对数的底数设置为 2 

import numpy as np

a = np.logspace(0,9,10,base=2)
print (a)

输出如下:

[  1.   2. 4. 8. 16. 32. 64. 128. 256. 512.]

Guess you like

Origin www.cnblogs.com/Summer-skr--blog/p/11688949.html