Python numpy.linspace实用方法

官方文档: numpy.linspace(start, stop, num = 50, endpoint = True, retstep = False, dtype = None)

  • 在指定的间隔内返回均匀间隔的数字;
  • 返回num均匀间隔的样本, 在[start,stop]区间内计算;
  • 可以选择排除间隔的终点.

Parameters

  • start: scalar(标量), 序列的起始点
  • stop: scalar, 依据endpoint会有变化, endpoint为True, 则包含显示, 当为False, 不包含(生成序列相当于原始num上加1按endpoint = True生成, 结果只显示第一个到倒数第二个)
  • num: int, optional(可选), 生成样本数量, 必须是非负数
  • endpoint: bool, optional, 如果是真,则包括stop,如果为False,则没有stop
  • retstep: bool, optional
  • dtype: dtype, optional

Returns

  • samples : ndarray
  • step : float, optional

Examples

import numpy as np
np.linspace(-10, 10, 11)
# array([-10.,  -8.,  -6.,  -4.,  -2.,   0.,   2.,   4.,   6.,   8.,  10.])

import matplotlib.pyplot as plt
num = 11
y = np.zeros(num)
endpoint_T = np.linspace(-10, 10, num, endpoint = True)
endpoint_F = np.linspace(-10, 10, num, endpoint = False)

plt.plot(endpoint_T, y, 'o')
plt.plot(endpoint_F, y + 0.5, 'o')
plt.ylim([-0.5, 1])
xticks(np.linspace(-10, 10, num, endpoint = True))
plt.show()

# 设置dtype
lin_dtype = np.linspace(-10, 10, 11, dtype = np.int)
lin_dtype
# array([-10,  -8,  -6,  -4,  -2,   0,   2,   4,   6,   8,  10])
print(lin_dtype.dtype) # int64
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,143评论 0 13
  • numpy.random.randint Return random integers fromlow(inclu...
    onepedalo阅读 4,892评论 0 1
  • # NumPy ## The Basics NumPy 主要面向的对象是齐次多维数组。在NumPy中维度(dime...
    昨年今日阅读 1,911评论 0 1
  • Scipy.org -> Docs -> NumPy v1.15 Manual -> NumPy Refere...
    meowwzzz阅读 3,434评论 0 0
  • 在毕业十年之际,我再也忍不住对你的思念,找到了合适的机会,终于咱们要见面了。 我记得很清楚,还没回到老家我就对行程...
    穆正月阅读 1,202评论 0 4