python 获取等间隔的数组实例

yipeiwu_com6年前Python基础

可以使用numpy中的linspace函数

np.linspace(start, stop, num, endpoint, retstep, dtype)
#start和stop为起始和终止位置,均为标量
#num为包括start和stop的间隔点总数,默认为50
#endpoint为bool值,为False时将会去掉最后一个点计算间隔
#restep为bool值,为True时会同时返回数据列表和间隔值
#dtype默认为输入变量的类型,给定类型后将会把生成的数组类型转为目标类型
np.linspace(1,3,num=4)
Out[17]: array([1.    , 1.66666667, 2.33333333, 3.    ])

np.linspace(1,3,num=4,endpoint=False)
Out[18]: array([1. , 1.5, 2. , 2.5])

np.linspace(1,3,num=4,endpoint=False,retstep=True)
Out[19]: (array([1. , 1.5, 2. , 2.5]), 0.5)

np.linspace(1,3,num=4,endpoint=False,retstep=True,dtype=float)
Out[20]: (array([1. , 1.5, 2. , 2.5]), 0.5)

np.linspace(1,3,num=4,endpoint=False,retstep=True,dtype=int)
Out[21]: (array([1, 1, 2, 2]), 0.5)

以上这篇python 获取等间隔的数组实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python url 参数修改方法

基于python 3.5,python 2.7 与python3.4 的urllib不同,是urlparse >>> from urllib import pars...

tensorflow更改变量的值实例

如下所示: from __future__ import print_function,division import tensorflow as tf #create a Var...

Python中的with...as用法介绍

这个语法是用来代替传统的try...finally语法的。 复制代码 代码如下: with EXPRESSION [ as VARIABLE] WITH-BLOCK 基本思想是wi...

python读取txt文件,去掉空格计算每行长度的方法

如下所示: # -*- coding: utf-8 -*- file2 = open("source.txt", 'r') file1 = open("target.txt",...

python opencv 图像尺寸变换方法

利用Python OpenCV中的 cv.Resize(源,目标,变换方法)就可以实现变换为想要的尺寸了 源文件:就不用说了 目标:你可以对图像进行倍数的放大和缩小 也可以直接的输入尺寸...