python 模块学习--Numpy
2015-08-24 20:30
731 查看
Numpy是Python的一个科学计算库,提供了矩阵运算的功能。安装方法可以直接使用pip install numpy命令,也可以在http://sourceforge.net/projects/numpy/files/NumPy/上下载与python相应版本的exe文件。
这里就记录下在学习和使用Numpy中所用过的一些函数方法,随时进行补充。
numpy的官方文档:http://docs.scipy.org/doc/numpy/reference/
这是生成一维数组,如果是要生成多维数组,则再使用reshape函数,如下图:
这里就生成了一个5*2的数组
使用numpy.array
可以使用list列表或者tuple元组作为参数生成一维或多维数组
另外,还可以指定数据类型,如numpy.int32,numpy.int16,numpy.float64等等。
使用numpy.zeros,numpy.ones,numpy.eye函数可以生成特定的矩阵
使用mat函数可以将数组转化为矩阵
数组的属性
如上图所示,
ndim:表示数组的维度
shape:表示数组的行数和列数
size:表示数组的元素个数
dtype:表示数组的数据类型
itemsize:表示每个元素所占的字节数
tile函数可以用于扩充数组元素
tile(A,repl)可以用于扩充数组的元素,A表示需要扩充的矩阵或数组,而repl参数如果是一个数,表示对A中元素的重复次数,如果repl = (x,y),则y表示对A中元素重复的次数,而x是对进行y次重复得到的数组所要扩充的次数,下图是官方的例子
这里也参考[numpy中argsort函数用法]这篇文章的介绍。
(http://www.aichengxu.com/view/15541)
以下分别是一维数组和二维数组的使用例子:
另外,对于二维数组还可以设置axis参数,当axis= 0 表示按列排序,而axis=1表示按行排序。
另外说到排序,还有两个常用的函数sort和sorted,详细内容请看:http://maoersong.blog.163.com/blog/static/171557351201424105925681/?newFollowBlog
(1)切割索引
(2)整数索引
(3)布尔值索引
这里就记录下在学习和使用Numpy中所用过的一些函数方法,随时进行补充。
numpy的官方文档:http://docs.scipy.org/doc/numpy/reference/
1.导入Numpy
导入方法如下:2.多维数组
使用numpy.arange这是生成一维数组,如果是要生成多维数组,则再使用reshape函数,如下图:
这里就生成了一个5*2的数组
使用numpy.array
可以使用list列表或者tuple元组作为参数生成一维或多维数组
另外,还可以指定数据类型,如numpy.int32,numpy.int16,numpy.float64等等。
使用numpy.zeros,numpy.ones,numpy.eye函数可以生成特定的矩阵
使用mat函数可以将数组转化为矩阵
数组的属性
如上图所示,
ndim:表示数组的维度
shape:表示数组的行数和列数
size:表示数组的元素个数
dtype:表示数组的数据类型
itemsize:表示每个元素所占的字节数
tile函数可以用于扩充数组元素
tile(A,repl)可以用于扩充数组的元素,A表示需要扩充的矩阵或数组,而repl参数如果是一个数,表示对A中元素的重复次数,如果repl = (x,y),则y表示对A中元素重复的次数,而x是对进行y次重复得到的数组所要扩充的次数,下图是官方的例子
3.numpy的argsort函数用法
通过help函数获取argsort函数的介绍,从介绍可以得知其返回的是数组值从小到大排序的索引值这里也参考[numpy中argsort函数用法]这篇文章的介绍。
(http://www.aichengxu.com/view/15541)
以下分别是一维数组和二维数组的使用例子:
另外,对于二维数组还可以设置axis参数,当axis= 0 表示按列排序,而axis=1表示按行排序。
另外说到排序,还有两个常用的函数sort和sorted,详细内容请看:http://maoersong.blog.163.com/blog/static/171557351201424105925681/?newFollowBlog
4.对多维数组的索引
对多维数组的索引有以下几种方法:
(1)切割索引
import numpy as np # Slicing indexing # Create the following rank 2 array with shape (3,4) # [[1 2 3 4] # [5 6 7 8] # [9 10 11 12]] a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]]) # Use slicing to pull out the subarray consisting of the first 2 rows # and columns 1 and 2; b is the following array of shape (2,2): # [[2 3] # [6 7]] b = a[:2, 1:3] # A slice of an array is a view into the same data, so modifying it # will modify the original array print a[0,1] # Prints "2" b[0, 0] = 55 # b[0, 0] is the same piece of data as a[0, 1] print a[0, 1] # Prints "55"
(2)整数索引
# Integer array indexing # The returned array will have shape(3,) print a[[0, 1 , 2], [2, 3, 2]] # Prints "[ 3 8 11]" # The above example of integer array indexing is equivalent to this: print np.array([a[0, 2], a[1, 3], a[2, 2]]) # Prints "[ 3 8 11]" # When using integer array indexing, you can reuse the same # element from the source array: print a[[0, 0], [1, 1]] # Prints "[2 2]" # Equivalent to the previous integer array indexing example print np.array([a[0, 1], a[0,1]]) # Prints "[2 2]"
(3)布尔值索引
# Boolean array indexing bool_idx = (a > 3) # Find the elements of a that are bigger than 3; # this returns a numpy array of Boolean of the same # shape as a, where each slot of bool_idx tells # whether that element of a is > 2 print bool_idx # Prints "[[False False False True]" # [ True True True True] # [ True True True True]] # We use boolean array indexing to construct a rank 1 array # consisiting of the elements of a corresponding to the True values # of bool_idx print a[bool_idx] # Prints "[ 4 5 6 7 8 9 10 11 12]" # We can do all of the above in a single concise statement: print a[a > 3] # Prints "[ 4 5 6 7 8 9 10 11 12]"
相关文章推荐
- python实现简单爬虫功能
- python常用模块
- Python中文件对象的访问模式
- python_学习笔记0824
- Python自然语言处理学习笔记之信息提取步骤&分块(chunking)
- Python写Excel文件
- Python学习笔记:概要
- python嵌套dict的深度遍历合并
- 在VS中安装Python
- python ftplib模块
- python中的read()、readline()与readlines()
- python 使用ioctl() 获得网卡IP 和MAC地址
- python字符串format函数学习
- python-virtualenv安装方法
- 【Python】[IO编程]文件读写,StringIO和BytesIO,操作文件和目录,序列化
- python没有switch,可以用字典来替代
- python闭包以及装饰器
- 分享python的几个学习资源
- WINDOWS安装python3.X遇到的错误解决方法
- 人生苦短,我学python -- 入门篇