【numpy】np.linspace() 函数

4 篇文章 0 订阅
def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

编写如下代码:

#!/usr/bin/python27
import numpy as np

x1 = np.linspace(1,10)
x2 = np.linspace(1,10,num = 10)
x3 = np.linspace(1,10,num = 10,retstep = True)
x4 = np.linspace(2,10,num = 10,endpoint = False)

print(x1)
print(x2)
print(x3)
print(x4)
print('---------------------------')
print("length of x1 is %d" % len(x1))
print("length of x2 is %d" % len(x2))
print("length of x2 is %d" % len(x3))
print("length of x2 is %d" % len(x4))

代码执行结果如下:

[ 1.          1.18367347  1.36734694  1.55102041  1.73469388  1.91836735
  2.10204082  2.28571429  2.46938776  2.65306122  2.83673469  3.02040816
  3.20408163  3.3877551   3.57142857  3.75510204  3.93877551  4.12244898
  4.30612245  4.48979592  4.67346939  4.85714286  5.04081633  5.2244898
  5.40816327  5.59183673  5.7755102   5.95918367  6.14285714  6.32653061
  6.51020408  6.69387755  6.87755102  7.06122449  7.24489796  7.42857143
  7.6122449   7.79591837  7.97959184  8.16326531  8.34693878  8.53061224
  8.71428571  8.89795918  9.08163265  9.26530612  9.44897959  9.63265306
  9.81632653 10.        ]
[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]
(array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]), 1.0)
[2.  2.8 3.6 4.4 5.2 6.  6.8 7.6 8.4 9.2]
---------------------------
length of x1 is 50
length of x2 is 10
length of x2 is 2
length of x2 is 10

参数说明:

  • start:scalar
    • 起始点
  • stop:scalar
    • 终止点
  • num : int, optional
    • Number of samples to generate. Default is 50. Must be non-negative.
    • 默认50,生成start和stop之间50个等差间隔的元素
  • endpoint : bool, optional
    • If True, stop is the last sample. Otherwise, it is not included. Default is True.
    • 生成等差间隔的元素,但是不包含stop,即间隔为 (stop - start)/num
  • retstep : bool, optional
    • If True, return (samples, step), where step is the spacing between samples.
    • 返回一个(array,num)元组,array是结果数组,num是间隔大小
  • dtype : dtype, optional
    • The type of the output array. If dtype is not given, infer the data type from the other input arguments.
    • 输出数组的类型。如果没有给出dtype,则从其他输入参数推断数据类型。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云中鲸

谢赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值