numpy常用用法总结
numpy 简介
numpy的存在使得python拥有强大的矩阵计算能力,不亚于matlab。
官方文档(https://docs.scipy.org/doc/numpy-dev/user/quickstart.html)
各种用法介绍
首先是numpy中的数据类型,ndarray类型,和标准库中的array.array并不一样。
ndarray的一些属性
ndarray.ndim
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.
ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be (n,m). The length of the shape tuple is therefore the rank, or number of dimensions, ndim.
ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.
ndarray.dtype
an object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.
ndarray.itemsize
the size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.
ndarray.data
the buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.
ndarray的创建
>>> import numpy as np
>>> a = np.array([2,3,4])
>>> a
array([2, 3, 4])
>>> a.dtype
dtype('int64')
>>> b = np.array([1.2, 3.5, 5.1])
>>> b.dtype
dtype('float64')
二维的数组
>>> b = np.array([(1.5,2,3), (4,5,6)])
>>> b
array([[ 1.5, 2. , 3. ],
[ 4. , 5. , 6. ]])
创建时指定类型
>>> c = np.array( [ [1,2], [3,4] ], dtype=complex )
>>> c
array([[ 1.+0.j, 2.+0.j],
[ 3.+0.j, 4.+0.j]])
创建一些特殊的矩阵
>>> np.zeros( (3,4) )
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
>>> np.ones( (2,3,4), dtype=np.int16 ) # dtype can also be specified
array([[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]],
[[ 1, 1, 1, 1],
[ 1, 1, 1, 1],
[ 1, 1, 1, 1]]], dtype=int16)
>>> np.empty( (2,3) ) # uninitialized, output may vary
array([[ 3.73603959e-262, 6.02658058e-154, 6.55490914e-260],
[ 5.30498948e-313, 3.14673309e-307, 1.00000000e+000]])
创建一些有特定规律的矩阵
>>> np.arange( 10, 30, 5 )
array([10, 15, 20, 25])
>>> np.arange( 0, 2, 0.3 ) # it accepts float arguments
array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
>>> from numpy import pi
>>> np.linspace( 0, 2, 9 ) # 9 numbers from 0 to 2
array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])
>>> x = np.linspace( 0, 2*pi, 100 ) # useful to evaluate function at lots of points
>>> f = np.sin(x)
一些基本的运算
加减乘除三角函数逻辑运算
>>> a = np.array( [20,30,40,50] )
>>> b = np.arange( 4 )
>>> b
array([0, 1, 2, 3])
>>> c = a-b
>>> c
array([20, 29, 38, 47])
>>> b**2
array([0, 1, 4, 9])
>>> 10*np.sin(a)
array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854])
>>> a<35
array([ True, True, False, False], dtype=bool)
矩阵运算
matlab中有.* ,./等等
但是在numpy中,如果使用+,-,×,/优先执行的是各个点之间的加减乘除法
如果两个矩阵(方阵)可既以元素之间对于运算,又能执行矩阵运算会优先执行元素之间的运算
>>> import numpy as np
>>> A = np.arange(10,20)
>>> B = np.arange(20,30)
>>> A + B
array([30, 32, 34, 36, 38, 40, 42, 44, 46, 48])
>>> A * B
array([200, 231, 264, 299, 336, 375, 416, 459, 504, 551])
>>> A / B
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> B / A
array([2, 1, 1, 1, 1, 1, 1, 1, 1, 1])
如果需要执行矩阵运算,一般就是矩阵的乘法运算
>>> A = np.array([1,1,1,1])
>>> B = np.array([2,2,2,2])
>>> A.reshape(2,2)
array([[1, 1],
[1, 1]])
>>> B.reshape(2,2)
array([[2, 2],
[2, 2]])
>>> A * B
array([2, 2, 2, 2])
>>> np.dot(A,B)
8
>>> A.dot(B)
8
一些常用的全局函数
>>> B = np.arange(3)
>>> B
array([0, 1, 2])
>>> np.exp(B)
array([ 1. , 2.71828183, 7.3890561 ])
>>> np.sqrt(B)
array([ 0. , 1. , 1.41421356])
>>> C = np.array([2., -1., 4.])
>>> np.add(B, C)
array([ 2., 0., 6.])
矩阵的索引分片遍历
>>> a = np.arange(10)**3
>>> a
array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729])
>>> a[2]
8
>>> a[2:5]
array([ 8, 27, 64])
>>> a[:6:2] = -1000 # equivalent to a[0:6:2] = -1000; from start to position 6, exclusive, set every 2nd element to -1000
>>> a
array([-1000, 1, -1000, 27, -1000, 125, 216, 343, 512, 729])
>>> a[ : :-1] # reversed a
array([ 729, 512, 343, 216, 125, -1000, 27, -1000, 1, -1000])
>>> for i in a:
... print(i**(1/3.))
...
nan
1.0
nan
3.0
nan
5.0
6.0
7.0
8.0
9.0
矩阵的遍历
>>> import numpy as np
>>> b = np.arange(16).reshape(4, 4)
>>> for row in b:
... print(row)
...
[0 1 2 3]
[4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
>>> for node in b.flat:
... print(node)
...
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
矩阵的特殊运算
改变矩阵形状--reshape
>>> a = np.floor(10 * np.random.random((3,4)))
>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
>>> a.ravel()
array([ 6., 5., 1., 5., 5., 5., 8., 9., 5., 5., 9., 7.])
>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
resize和reshape的区别
resize会改变原来的矩阵,reshape并不会
>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
>>> a.reshape(2,-1)
array([[ 6., 5., 1., 5., 5., 5.],
[ 8., 9., 5., 5., 9., 7.]])
>>> a
array([[ 6., 5., 1., 5.],
[ 5., 5., 8., 9.],
[ 5., 5., 9., 7.]])
>>> a.resize(2,6)
>>> a
array([[ 6., 5., 1., 5., 5., 5.],
[ 8., 9., 5., 5., 9., 7.]])
矩阵的合并
>>> a = np.floor(10*np.random.random((2,2)))
>>> a
array([[ 8., 8.],
[ 0., 0.]])
>>> b = np.floor(10*np.random.random((2,2)))
>>> b
array([[ 1., 8.],
[ 0., 4.]])
>>> np.vstack((a,b))
array([[ 8., 8.],
[ 0., 0.],
[ 1., 8.],
[ 0., 4.]])
>>> np.hstack((a,b))
array([[ 8., 8., 1., 8.],
[ 0., 0., 0., 4.]])
numpy常用用法总结的更多相关文章
- 数据分析-numpy的用法
一.jupyter notebook 两种安装和启动的方式: 第一种方式: 命令行安装:pip install jupyter 启动:cmd 中输入 jupyter notebook 缺点:必须手动去 ...
- centos的vi常用用法
centos的vi常用用法 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的 ...
- MySql与SqlServer的一些常用用法的差别
MySql与SqlServer的一些常用用法的差别 本文为转载 本文将主要列出MySql与SqlServer不同的地方,且以常用的存储过程的相关内容为主. 1. 标识符限定符 SqlServer [] ...
- [转]ssh常用用法小结
ssh常用用法小结 1.连接到远程主机: 命令格式 : ssh name@remoteserver 或者 ssh remoteserver -l name 说明:以上两种方式都可以远程登录到远程主机, ...
- 【三支火把】---一份程序看懂C程序printf()的几种常用用法
闲来继续巩固我的学习之路,今天略微整理了一下,C程序中Printf()的一些常用用法,虽然自己以前好像会,但是不够系统,今天大致整理了一些,先贴上来看看,以后在看到其他,继续补充,希望能帮到一些像我一 ...
- grep参数说明及常用用法
grep参数说明及常用用法 趁着午休的时间把自己经常使用的一些grep命令整理一下. 方便以后查看. 后续会逐步把awk/sed/find等常用的命令理一理. 增强下记忆. 也算是对得起自己了. ^^ ...
- ssh常用用法小结
ssh常用用法小结 1.连接到远程主机: 命令格式 : ssh name@remoteserver 或者 ssh remoteserver -l name 说明:以上两种方式都可以远程登录到远程主机, ...
- C# Linq基本常用用法
1.什么是Linq? Lanaguage Interated Query(语言集成查询),Linq 是集成C# 和VB这些语言中用于提供数据查询能力的一个新特性. 这里只介绍两种基本常用用法. 学习方 ...
- Java集合中迭代器的常用用法
该例子展示了一个Java集合中迭代器的常用用法public class LinkedListTest { public static void main(String[] args) { List&l ...
随机推荐
- Jquery点击本身,修改出本身之外的其他同级元素的样式
1.引用Jquyer库 2.Jquery代码: <script type="text/javascript"> $(function () { slidColor('d ...
- 【C#】 知乎用户网络爬虫
目的 由一个种子用户出发,抓取相关的关注者和被关注者,然后再延伸开抓取更多的相关用户,以便后期进行数据分析. 拓扑图 开发工具 编程语言:C# 数据库:SqlServer 2008 R2 程序架构 流 ...
- Semaphore
信号量玩的也是aqs的state. package com.hust.grid.leesf.semaphore; import java.util.concurrent.Semaphore; clas ...
- 【leetcode】Simplify Path
题目简述: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/& ...
- 【leetcode】Min Stack -- python版
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- Windows 8.1安装 Vmware10
之前在windows 8上安装的Vmware 9.0,已经激活了用的蛮好,可是自从上次自动更新系统到windows 8.1后,启动虚拟机时提示要激活 使用各种激活码与注册机都无效,就算注册表信息丢失但 ...
- featherview模板引擎
1.判断语法 <?php if(isset($value['fromVR']) && !empty($value['fromVR'])) {?> <s class=& ...
- [转]PHP语言的数据库操作函数的理解
就我接触到的R语言以及对数据库的操作来说,基本的操作其实也就是CRUD(Create, Read, Update, Delete). 习惯了之后,对PHP中的MYSQLI操作函数感觉很不适应,查询或者 ...
- 纯CSS完成tab实现5种不同切换对应内容效果
很常用的一款特效纯CSS完成tab实现5种不同切换对应内容效果 实例预览 下载地址 实例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...
- CocoaPods pod install/pod update更新慢的问题
CocoaPods pod install/pod update 最近使用CocoaPods来添加第三方类库,无论是执行pod install还是pod update都卡在了Analyzing d ...