Numpy学习笔记2-创建array的几种方式
2015-05-31 22:05
741 查看
创建array的几种方式:
1、通过python的标准类型转换(如:lists,tuples)
2、通过Munpy固有的一些函数创建对象(如:arange,zones等)
3、通过从磁盘读取数组,或者标准的格式
4、通过字节数组,通过使用字符串或者缓冲区
5、使用特定的库函数,例如:random
下面是常用的几种:
其中:
np的dtype类型可以被当做函数类使用,用来将python的数据转换成array类型。
函数
其它函数array,
zeros, zeros_like, ones, ones_like, empty, empty_like, arange, linspace, rand, randn, fromfunction, fromfile参考手册:Array creation routines
>>>
arange( 10,30,5
)
array([10,15,20,25])
>>>
arange( 0,2,0.3
) #
it accepts float arguments
array([0.
, 0.3,0.6,0.9,1.2,1.5,1.8])
当
linspace
返回指定范围指定间隔的数组。官方文档
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
参数retstep指定是否返回步进长度,如果为True则返回元组(样本数据,步进长度)
1、通过python的标准类型转换(如:lists,tuples)
2、通过Munpy固有的一些函数创建对象(如:arange,zones等)
3、通过从磁盘读取数组,或者标准的格式
4、通过字节数组,通过使用字符串或者缓冲区
5、使用特定的库函数,例如:random
下面是常用的几种:
>>> import numpy as np >>> x = np.float32(1.0) >>> x 1.0 >>> y = np.int_([1,2,4]) >>> y array([1, 2, 4]) >>> z = np.arange(3, dtype=np.uint8) >>> z array([0, 1, 2], dtype=uint8)
其中:
np的dtype类型可以被当做函数类使用,用来将python的数据转换成array类型。
numpy.arange
numpy.arange([start, ]stop, [step, ]dtype=None)
函数 ones创建一个全1的数组,函数
empty创建一个内容随机并且依赖与内存状态的数组。默认创建的数组类型(dtype)都是float64。
其它函数array,
zeros, zeros_like, ones, ones_like, empty, empty_like, arange, linspace, rand, randn, fromfunction, fromfile参考手册:Array creation routines
>>>
arange( 10,30,5
)
array([10,15,20,25])
>>>
arange( 0,2,0.3
) #
it accepts float arguments
array([0.
, 0.3,0.6,0.9,1.2,1.5,1.8])
当
arange使用浮点数参数时,由于有限的浮点数精度,通常无法预测获得的元素个数。因此,最好使用函数
linspace去接收我们想要的元素个数来代替用range来指定步长。
linspace
返回指定范围指定间隔的数组。官方文档
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
参数retstep指定是否返回步进长度,如果为True则返回元组(样本数据,步进长度)
相关文章推荐
- Python模块学习 ---- thread 多线程处理
- 转载:python爬虫实践之模拟登录
- Numpy学习笔记1-基本类型
- python unicode
- Python Web运行环境搭建
- 多线程
- Python图像处理(6):分离土壤与植物
- .T in numpy
- Python 2.7.x 和 3.x 版本的重要区别
- python re 模块 findall 函数用法简述
- estimated parameters that ends with underscore in scikit
- reshape in numpy
- Python 可变对象与不可变对象
- python程序打包成exe方案
- Scikit Source Code Rea 4000 ding(2015.05.31)
- Pool进程池创建大量子进程,进程间通信
- Python高级编程–正则表达式(习题)
- Python基础:10函数参数
- 翻译《Writing Idiomatic Python》(五):类、上下文管理器、生成器
- Python Django 的 django templatedoesnotexist