📜  Python中的 numpy.arange()

📅  最后修改于: 2022-05-13 01:55:49.436000             🧑  作者: Mango

Python中的 numpy.arange()

arange([start,] stop[, step,][, dtype]) :根据间隔返回一个具有均匀间隔元素的数组。提到的间隔是半开的,即[开始,停止)

参数 :

start : [optional] start of interval range. By default start = 0
stop  : end of interval range
step  : [optional] step size of interval. By default step size = 1,  
For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. 
dtype : type of output array

返回:

Array of evenly spaced values.
Length of array being generated  = Ceil((Stop - Start) / Step) 

例子:

Python3
# Python Programming illustrating
# numpy.arange method
 
import numpy as geek
 
print("A\n", geek.arange(4).reshape(2, 2), "\n")
print("A\n", geek.arange(4, 10), "\n")
print("A\n", geek.arange(4, 20, 3), "\n")


Python3
# Python Programming illustrating
# numpy.arange method
 
import numpy as np
 
# Printing all numbers from 1 to
# 2 in steps of 0.1
print(np.arange(1, 2, 0.1))


输出 :

A
 [[0 1]
 [2 3]]

A
 [4 5 6 7 8 9]

A
 [ 4  7 10 13 16 19]

笔记:

  • 这些 NumPy-Python 程序不会在 onlineID 上运行,因此请在您的系统上运行它们来探索它们。
  • numpy.arange() 相对于普通内置 range()函数的优势在于它允许我们生成非整数的数字序列。

例子:

Python3

# Python Programming illustrating
# numpy.arange method
 
import numpy as np
 
# Printing all numbers from 1 to
# 2 in steps of 0.1
print(np.arange(1, 2, 0.1))

输出:

[1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9]

如果你尝试使用 range()函数,你会得到一个 TypeError。