在NumPy中返回指定区间内的等间距数字
要在指定区间内返回等间距的数字,请使用Python NumPy中的**numpy.linspace()**方法。第一个参数是“**start**”,即序列的起始值;第二个参数是“**end**”,即序列的结束值;第三个参数是**num**,即要生成的样本数量。
除非将endpoint设置为False,否则stop是序列的结束值。在这种情况下,序列包含除最后一个num + 1个等间距样本之外的所有样本,因此stop被排除在外。请注意,当endpoint为False时,步长会发生变化。
dtype是输出数组的类型。如果未给出dtype,则数据类型将从start和stop推断。推断出的dtype永远不会是整数;即使参数会生成整数数组,也会选择浮点数。结果中存储样本的轴。仅当start或stop为数组状时才相关。默认情况下(0),样本将沿着插入到开头的新的轴。使用-1在末尾获取轴。
步骤
首先,导入所需的库:
import numpy as np
要在指定区间内返回等间距的数字,请使用Python NumPy中的numpy.linspace()方法:
arr = np.linspace(10, 20)
print("Array...
", arr)获取类型:
print("
Type...
", arr.dtype)获取维度:
print("
Dimensions...
",arr.ndim)获取形状:
print("
Shape...
",arr.shape)获取元素数量:
print("
Number of elements...
",arr.size)示例
import numpy as np
# To return evenly spaced numbers over a specified interval, use the numpy.linspace() method in Python Numpy
# The 1st parameter is the "start" i.e. the start of the sequence
# The 2nd parameter is the "end" i.e. the end of the sequence
# The 3rd parameter is the num i.s the number of samples to generate. Default is 50.
arr = np.linspace(10, 20)
print("Array...
", arr)
# Get the type
print("
Type...
", arr.dtype)
# Get the dimensions
print("
Dimensions...
",arr.ndim)
# Get the shape
print("
Shape...
",arr.shape)
# Get the number of elements
print("
Number of elements...
",arr.size)输出
Array... [10. 10.20408163 10.40816327 10.6122449 10.81632653 11.02040816 11.2244898 11.42857143 11.63265306 11.83673469 12.04081633 12.24489796 12.44897959 12.65306122 12.85714286 13.06122449 13.26530612 13.46938776 13.67346939 13.87755102 14.08163265 14.28571429 14.48979592 14.69387755 14.89795918 15.10204082 15.30612245 15.51020408 15.71428571 15.91836735 16.12244898 16.32653061 16.53061224 16.73469388 16.93877551 17.14285714 17.34693878 17.55102041 17.75510204 17.95918367 18.16326531 18.36734694 18.57142857 18.7755102 18.97959184 19.18367347 19.3877551 19.59183673 19.79591837 20. ] Type... float64 Dimensions... 1 Shape... (50,) Number of elements... 50
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP