numpy重新學習系列(10)---如何用np.arange生成均勻間隔分佈的array

'''
numpy.arange

numpy.arange([start, ]stop, [step, ]dtype=None)
Return evenly spaced values within a given interval.

Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding stop). For integer arguments the function is equivalent to the Python built-in range function, but returns an ndarray rather than a list.

When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use numpy.linspace for these cases.

Parameters
start  number, optional
Start of interval. The interval includes this value. The default start value is 0.
stop  number
End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out.
step  number, optional
Spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default step size is 1. If step is specified as a position argument, start must also be given.
dtype dtype
The type of the output array. If dtype is not given, infer the data type from the other input arguments.
Returns
arangend array
Array of evenly spaced values.
For floating point arguments, the length of the result is ceil((stop - start)/step). Because of floating point overflow, this rule may result in the last element of out being greater than stop.
'''

 # 作用是返回均勻分佈的array,從開始到結束的數字,按照step的間隔
# 1. 生成規則是按照從start的數字開始,但是不包含stop的數字
# 2. 如果setp是整數的話,用法將會和range一樣,只是一個返回的是python的list類型,一個返回的是array
# 3. 如果step是非整數的話,返回的結果經常是不穩定的,最好是使用numpy.linspace代替
#### 參數說明
# start 可選參數,默認是0,開始的數字
# stop  必選參數,結束的數字,一般情況下不包括這個數字,如果step是float的類型的話,最後結束的時候,四捨五入可能會超過這個數字
# step  可選參數,默認是1 間隔的數字,兩個數字之差,如果step作爲位置參數的話,start也必須給出。
# dtype 可選參數,返回的是數據的類型,如果沒有指定,將會推斷給出。

import numpy as np
# 只使用stop這個必要參數
print(np.arange(10))
# 使用前兩個參數
print(np.arange(1,8))
# 使用前三個參數
print(np.arange(1,8,3))
# 使用數據類型參數
print(np.arange(1,8,2,"float"))

'''

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

'''

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章