python 模块学习--Numpy
来源:互联网 发布:早晨 句子 知乎 编辑:程序博客网 时间:2024/05/05 20:01
Numpy是Python的一个科学计算库,提供了矩阵运算的功能。安装方法可以直接使用pip install numpy命令,也可以在http://sourceforge.net/projects/numpy/files/NumPy/上下载与python相应版本的exe文件。
这里就记录下在学习和使用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 arrayprint 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 exampleprint np.array([a[0, 1], a[0,1]]) # Prints "[2 2]"
(3)布尔值索引
# Boolean array indexingbool_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 > 2print 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_idxprint 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 模块学习--Numpy
- python numpy 模块学习
- python系列之numpy模块学习
- python学习(八)----numpy模块相关函数
- Python模块之NumPy
- python安装numpy模块
- Python--numpy模块
- python numpy模块/库
- python 安装 numpy模块
- python numpy模块
- python的numpy模块
- python--初级学习之numpy模块的安装
- Python里安装NumPy模块
- python模块安装:numpy、pylab
- Python笔记之NUMPY模块
- win7安装 Python numpy 模块
- Python 外部模块numpy安装
- python 安装 numpy,scipy模块
- 在Xcode中玩转Git
- UE3 材质函数
- HTML
- mysql 创建用户分配只读权限
- 利用jdk6中Annotation将XML与对象之间互相转化(一)
- python 模块学习--Numpy
- ckeditor批量上传图片(下)
- spring Quartz多个定时任务的配置
- poj 3259 Wormholes
- Android Studio百度地图(二)
- 基础入门(1)
- c++primer之函数(函数基础和参数传递)
- Hash
- 每天进步一点点——load data infile的相关优化