python 中 常用到的 numpy 函数 整理
2015-06-28 09:58
801 查看
1. 创建二维数组 array() :
set = array([[1., 2, ],[3., 4.],[5., 6.],[7., 9.]])
求 数组的 行数:
>>> set.shape[0]
4
求 数组的列数:
>>> set.shape[1]
>>> set.shape
(4, 2)
>>> set.dtype
dtype('float64')
2 empty() 函数:
[python] view
plaincopy
>>> a = empty([2, 2])
>>> a
array([[ 2.01269048e-313, 4.44659081e-323],
[ 5.03965339e+223, 6.48588014e-310]])
>>> b = empty([2, 2], dtype = int)
>>> b
array([[19988563, 36534944],
[ 2460004, 2460004]])
>>> c = empty([2, 2], dtype = int, order = 'C')
>>> c
array([[19988581, 36534944],
[ 2460004, 2460004]])
>>> d = empty([2, 2], dtype = int ,order = 'F')
>>> d
array([[19857521, 2460004],
[36534944, 2460004]])
>>>
最后一个参数,返回数组在内存中的存放顺序,
C代表C语言风格, row major
F代表····,column major
3. eye()
[python] view
plaincopy
>>> e1 = eye(2, 3, 0, dtype = int)
>>> e1
array([[1, 0, 0],
[0, 1, 0]])
>>> e2 = eye(3)
>>> e2
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> e3 = eye(3, dtype = int)
>>> e3
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
>>> e4 = eye(3, 1, dtype = int)
>>> e4
array([[1],
[0],
[0]])
>>> e5 = eye(3, k = 1, dtype = int)
>>> e5
array([[0, 1, 0],
[0, 0, 1],
[0, 0, 0]])
>>> e6 = eye(3, k = -1, dtype = int)
>>> e6
array([[0, 0, 0],
[1, 0, 0],
[0, 1, 0]])
>>>
第一个参数N = 列数
第二个参数 M = 行数,省略代表M = N
第三个参数 k 代表对角线位置, = 0 代表主对角线, +1就向右上方偏移1, -1 就向左下角偏移1
第四个参数表示类型 dtype 默认为 float 类型
4 。 创建 方阵 identity()
[python] view
plaincopy
>>> i1 = identity(3)
>>> i1
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> i2 = identity(3, dtype = int)
>>> i2
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
>>>
只有两个参数,第一个表示 行(列)数,第二个表示类型(默认为float)类型
5. 生成一个元素全为1的数组
[python] view
plaincopy
>>> o1 = ones(3)
>>> o1
array([ 1., 1., 1.])
>>> o1.shape
(3,)
要指定完整的shape(完整的行数和列数)的话:
[python] view
plaincopy
>>> o4 = ones( (2, 3), dtype = int)
>>> o4
array([[1, 1, 1],
[1, 1, 1]])
>>>
6. zeros() 全是0 的矩阵
[python] view
plaincopy
<pre code_snippet_id="82520" snippet_file_name="blog_20131126_6_5493221" name="code" class="python">>>> from numpy import *
>>> z1 = zeros(3)
>>> z1
array([ 0., 0., 0.])
>>> z1.shape
(3,)
>>> z2 = zeros((2, 3), dtype = int)
>>> z2
array([[0, 0, 0],
[0, 0, 0]])
>>> z2.shape
(2, 3)
>>> s = (3, 2)
z4 = zeros(s)
>>> z4
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
>>> z4.shape
(3, 2)</pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_12_2832072" name="code" class="python"></pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_12_2832072" name="code" class="python"></pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_9_956672" name="code" class="python">ones_like() zeros_like()</pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_12_2832072" name="code" class="python"></pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_11_8513673" name="code" class="python"><pre code_snippet_id="82520" snippet_file_name="blog_20131126_11_8513673" name="code" class="python">>>> from numpy import *
>>> s = (3, 2)
>>> a = array(s)
>>> a
array([3, 2])
>>> a.shape
(2,)
>>> z = zeros(s, dtype = int)
>>> zz = zeros_like(z)
>>> zz
array([[0, 0],
[0, 0],
[0, 0]])
>>> ooo = ones_like(z)
>>> ooo = ones_like(z)
>>> ooo
array([[1, 1],
[1, 1],
[1, 1]])
>>> </pre><br>
<br>
<p></p>
<pre></pre>
<pre code_snippet_id="82520" snippet_file_name="blog_20131126_12_2832072" name="code" class="python"></pre>
<pre></pre>
<p></p>
</pre>
set = array([[1., 2, ],[3., 4.],[5., 6.],[7., 9.]])
求 数组的 行数:
>>> set.shape[0]
4
求 数组的列数:
>>> set.shape[1]
>>> set.shape
(4, 2)
>>> set.dtype
dtype('float64')
记得 >>> from numpy import *
2 empty() 函数:[python] view
plaincopy
>>> a = empty([2, 2])
>>> a
array([[ 2.01269048e-313, 4.44659081e-323],
[ 5.03965339e+223, 6.48588014e-310]])
>>> b = empty([2, 2], dtype = int)
>>> b
array([[19988563, 36534944],
[ 2460004, 2460004]])
>>> c = empty([2, 2], dtype = int, order = 'C')
>>> c
array([[19988581, 36534944],
[ 2460004, 2460004]])
>>> d = empty([2, 2], dtype = int ,order = 'F')
>>> d
array([[19857521, 2460004],
[36534944, 2460004]])
>>>
最后一个参数,返回数组在内存中的存放顺序,
C代表C语言风格, row major
F代表····,column major
3. eye()
[python] view
plaincopy
>>> e1 = eye(2, 3, 0, dtype = int)
>>> e1
array([[1, 0, 0],
[0, 1, 0]])
>>> e2 = eye(3)
>>> e2
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> e3 = eye(3, dtype = int)
>>> e3
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
>>> e4 = eye(3, 1, dtype = int)
>>> e4
array([[1],
[0],
[0]])
>>> e5 = eye(3, k = 1, dtype = int)
>>> e5
array([[0, 1, 0],
[0, 0, 1],
[0, 0, 0]])
>>> e6 = eye(3, k = -1, dtype = int)
>>> e6
array([[0, 0, 0],
[1, 0, 0],
[0, 1, 0]])
>>>
第一个参数N = 列数
第二个参数 M = 行数,省略代表M = N
第三个参数 k 代表对角线位置, = 0 代表主对角线, +1就向右上方偏移1, -1 就向左下角偏移1
第四个参数表示类型 dtype 默认为 float 类型
4 。 创建 方阵 identity()
[python] view
plaincopy
>>> i1 = identity(3)
>>> i1
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> i2 = identity(3, dtype = int)
>>> i2
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1]])
>>>
只有两个参数,第一个表示 行(列)数,第二个表示类型(默认为float)类型
5. 生成一个元素全为1的数组
[python] view
plaincopy
>>> o1 = ones(3)
>>> o1
array([ 1., 1., 1.])
>>> o1.shape
(3,)
要指定完整的shape(完整的行数和列数)的话:
[python] view
plaincopy
>>> o4 = ones( (2, 3), dtype = int)
>>> o4
array([[1, 1, 1],
[1, 1, 1]])
>>>
6. zeros() 全是0 的矩阵
[python] view
plaincopy
<pre code_snippet_id="82520" snippet_file_name="blog_20131126_6_5493221" name="code" class="python">>>> from numpy import *
>>> z1 = zeros(3)
>>> z1
array([ 0., 0., 0.])
>>> z1.shape
(3,)
>>> z2 = zeros((2, 3), dtype = int)
>>> z2
array([[0, 0, 0],
[0, 0, 0]])
>>> z2.shape
(2, 3)
>>> s = (3, 2)
z4 = zeros(s)
>>> z4
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.]])
>>> z4.shape
(3, 2)</pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_12_2832072" name="code" class="python"></pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_12_2832072" name="code" class="python"></pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_9_956672" name="code" class="python">ones_like() zeros_like()</pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_12_2832072" name="code" class="python"></pre><pre code_snippet_id="82520" snippet_file_name="blog_20131126_11_8513673" name="code" class="python"><pre code_snippet_id="82520" snippet_file_name="blog_20131126_11_8513673" name="code" class="python">>>> from numpy import *
>>> s = (3, 2)
>>> a = array(s)
>>> a
array([3, 2])
>>> a.shape
(2,)
>>> z = zeros(s, dtype = int)
>>> zz = zeros_like(z)
>>> zz
array([[0, 0],
[0, 0],
[0, 0]])
>>> ooo = ones_like(z)
>>> ooo = ones_like(z)
>>> ooo
array([[1, 1],
[1, 1],
[1, 1]])
>>> </pre><br>
<br>
<p></p>
<pre></pre>
<pre code_snippet_id="82520" snippet_file_name="blog_20131126_12_2832072" name="code" class="python"></pre>
<pre></pre>
<p></p>
</pre>
相关文章推荐
- Python学习笔记(未完)
- Python 3.5 终于将 Type Hint(类型提示)官方化
- Python 3.5 终于将 Type Hint(类型提示)官方化
- Python学习笔记 - 字符串和编码
- Python学习笔记 - 字符串和编码
- Python游戏之Trivia游戏
- 分析用Python脚本关闭文件操作的机制
- python实现搜索指定目录下文件及文件内搜索指定关键词的方法
- python中getaddrinfo()基本用法实例分析
- python查找指定具有相同内容文件的方法
- python自动zip压缩目录的方法
- python实现根据主机名字获得所有ip地址的方法
- python实现将html表格转换成CSV文件的方法
- Python实现将绝对URL替换成相对URL的方法
- python实现自动登录人人网并采集信息的方法
- python实现的简单RPG游戏流程实例
- 基于进程内通讯的python聊天室实现方法
- python实现简单ftp客户端的方法
- python实现的用于搜索文件并进行内容替换的类实例
- python实现矩阵乘法的方法