前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 中 常用到的 numpy 函数 整理

python 中 常用到的 numpy 函数 整理

作者头像
bear_fish
发布2018-09-19 15:57:24
5570
发布2018-09-19 15:57:24
举报

http://blog.csdn.net/timidsmile/article/details/16963699

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')

记得 >>> from numpy import * 

2  empty() 函数:

[python] view plain copy

  1. >>> a = empty([2, 2])  
  2. >>> a  
  3. array([[  2.01269048e-313,   4.44659081e-323],  
  4.        [  5.03965339e+223,   6.48588014e-310]])  
  5. >>> b = empty([2, 2], dtype = int)  
  6. >>> b  
  7. array([[19988563, 36534944],  
  8.        [ 2460004,  2460004]])  
  9. >>> c = empty([2, 2], dtype = int, order = 'C')  
  10. >>> c  
  11. array([[19988581, 36534944],  
  12.        [ 2460004,  2460004]])  
  13. >>> d = empty([2, 2], dtype = int ,order = 'F')  
  14. >>> d  
  15. array([[19857521,  2460004],  
  16.        [36534944,  2460004]])  
  17. >>>   

最后一个参数,返回数组在内存中的存放顺序,

C代表C语言风格, row major

F代表····,column  major

3.  eye()

[python] view plain copy

  1. >>> e1 = eye(2, 3, 0, dtype = int)  
  2. >>> e1  
  3. array([[1, 0, 0],  
  4.        [0, 1, 0]])  
  5. >>> e2 = eye(3)  
  6. >>> e2  
  7. array([[ 1.,  0.,  0.],  
  8.        [ 0.,  1.,  0.],  
  9.        [ 0.,  0.,  1.]])  
  10. >>> e3 = eye(3, dtype = int)  
  11. >>> e3  
  12. array([[1, 0, 0],  
  13.        [0, 1, 0],  
  14.        [0, 0, 1]])  
  15. >>> e4 = eye(3, 1, dtype = int)  
  16. >>> e4  
  17. array([[1],  
  18.        [0],  
  19.        [0]])  
  20. >>> e5 = eye(3, k = 1, dtype = int)  
  21. >>> e5  
  22. array([[0, 1, 0],  
  23.        [0, 0, 1],  
  24.        [0, 0, 0]])  
  25. >>> e6 = eye(3, k = -1, dtype = int)  
  26. >>> e6  
  27. array([[0, 0, 0],  
  28.        [1, 0, 0],  
  29.        [0, 1, 0]])  
  30. >>>   

第一个参数N = 列数

第二个参数 M = 行数,省略代表M = N 

第三个参数 k 代表对角线位置, = 0 代表主对角线, +1就向右上方偏移1, -1 就向左下角偏移1

第四个参数表示类型 dtype 默认为 float 类型

4 。 创建 方阵 identity()

[python] view plain copy

  1. >>> i1 = identity(3)  
  2. >>> i1  
  3. array([[ 1.,  0.,  0.],  
  4.        [ 0.,  1.,  0.],  
  5.        [ 0.,  0.,  1.]])  
  6. >>> i2 = identity(3, dtype = int)  
  7. >>> i2  
  8. array([[1, 0, 0],  
  9.        [0, 1, 0],  
  10.        [0, 0, 1]])  
  11. >>>   

只有两个参数,第一个表示 行(列)数,第二个表示类型(默认为float)类型

5.  生成一个元素全为1的数组

[python] view plain copy

  1. >>> o1 = ones(3)  
  2. >>> o1  
  3. array([ 1.,  1.,  1.])  
  4. >>> o1.shape  
  5. (3,)  

要指定完整的shape(完整的行数和列数)的话:

[python] view plain copy

  1. >>> o4 = ones( (2, 3), dtype = int)  
  2. >>> o4  
  3. array([[1, 1, 1],  
  4.        [1, 1, 1]])  
  5. >>>   

6. zeros() 全是0 的矩阵

[python] view plain copy

  1. <pre code_snippet_id="82520" snippet_file_name="blog_20131126_6_5493221" name="code" class="python">>>> from numpy import *  
  2. >>> z1 = zeros(3)  
  3. >>> z1  
  4. array([ 0.,  0.,  0.])  
  5. >>> z1.shape  
  6. (3,)  
  7. >>> z2 = zeros((2, 3), dtype = int)  
  8. >>> z2  
  9. array([[0, 0, 0],  
  10.        [0, 0, 0]])  
  11. >>> z2.shape  
  12. (2, 3)  
  13. >>> s = (3, 2)  
  14. z4 = zeros(s)  
  15. >>> z4  
  16. array([[ 0.,  0.],  
  17.        [ 0.,  0.],  
  18.        [ 0.,  0.]])  
  19. >>> z4.shape  
  20. (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 *  
  21. >>> s = (3, 2)  
  22. >>> a = array(s)  
  23. >>> a  
  24. array([3, 2])  
  25. >>> a.shape  
  26. (2,)  
  27. >>> z = zeros(s, dtype = int)  
  28. >>> zz = zeros_like(z)  
  29. >>> zz  
  30. array([[0, 0],  
  31.        [0, 0],  
  32.        [0, 0]])  
  33. >>> ooo = ones_like(z)  
  34. >>> ooo = ones_like(z)  
  35. >>> ooo  
  36. array([[1, 1],  
  37.        [1, 1],  
  38.        [1, 1]])  
  39. >>> </pre><br>  
  40. <br>  
  41. <p></p>  
  42. <pre></pre>  
  43. <pre code_snippet_id="82520" snippet_file_name="blog_20131126_12_2832072" name="code" class="python"></pre>  
  44. <pre></pre>  
  45. <p></p>  
  46. </pre>  
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年08月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 记得 >>> from numpy import * 
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档