---恢复内容开始---


Numpy_pratice In [2]:
n = 10
L = [i for i in range(n)]
In [3]: L * 2
Out[3]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [4]: import numpy as np
X = np.arange(1,16).reshape(3,5)
X
Out[4]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
In [5]: X+1
Out[5]:
array([[ 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16]])
In [6]: X//2
Out[6]:
array([[0, 1, 1, 2, 2],
[3, 3, 4, 4, 5],
[5, 6, 6, 7, 7]])
In [7]: X%2
Out[7]:
array([[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1]])
In [8]: 1/X
Out[8]:
array([[1. , 0.5 , 0.33333333, 0.25 , 0.2 ],
[0.16666667, 0.14285714, 0.125 , 0.11111111, 0.1 ],
[0.09090909, 0.08333333, 0.07692308, 0.07142857, 0.06666667]])
In [9]: np.abs(X)
Out[9]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
In [10]: np.sin(X)
Out[10]:
array([[ 0.84147098, 0.90929743, 0.14112001, -0.7568025 , -0.95892427],
[-0.2794155 , 0.6569866 , 0.98935825, 0.41211849, -0.54402111],
[-0.99999021, -0.53657292, 0.42016704, 0.99060736, 0.65028784]])
In [11]: np.log2(X)
Out[11]:
array([[0. , 1. , 1.5849625 , 2. , 2.32192809],
[2.5849625 , 2.80735492, 3. , 3.169925 , 3.32192809],
[3.45943162, 3.5849625 , 3.70043972, 3.80735492, 3.9068906 ]])
In [12]: np.exp(X)
Out[12]:
array([[2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 5.45981500e+01,
1.48413159e+02],
[4.03428793e+02, 1.09663316e+03, 2.98095799e+03, 8.10308393e+03,
2.20264658e+04],
[5.98741417e+04, 1.62754791e+05, 4.42413392e+05, 1.20260428e+06,
3.26901737e+06]])
In [13]: np.exp2(X) # 指定幂,结果同下述power(2,x)
Out[13]:
array([[2.0000e+00, 4.0000e+00, 8.0000e+00, 1.6000e+01, 3.2000e+01],
[6.4000e+01, 1.2800e+02, 2.5600e+02, 5.1200e+02, 1.0240e+03],
[2.0480e+03, 4.0960e+03, 8.1920e+03, 1.6384e+04, 3.2768e+04]])
In [14]: np.power(2,X)
Out[14]:
array([[ 2, 4, 8, 16, 32],
[ 64, 128, 256, 512, 1024],
[ 2048, 4096, 8192, 16384, 32768]])
In [15]: 2**X
Out[15]:
array([[ 2, 4, 8, 16, 32],
[ 64, 128, 256, 512, 1024],
[ 2048, 4096, 8192, 16384, 32768]])
In [16]: np.log2(X)
Out[16]:
array([[0. , 1. , 1.5849625 , 2. , 2.32192809],
[2.5849625 , 2.80735492, 3. , 3.169925 , 3.32192809],
[3.45943162, 3.5849625 , 3.70043972, 3.80735492, 3.9068906 ]])
In [17]: np.log10(X)
Out[17]:
array([[0. , 0.30103 , 0.47712125, 0.60205999, 0.69897 ],
[0.77815125, 0.84509804, 0.90308999, 0.95424251, 1. ],
[1.04139269, 1.07918125, 1.11394335, 1.14612804, 1.17609126]])
矩阵运算
In [18]: A = np.arange(4).reshape(2,2)
A
Out[18]:
array([[0, 1],
[2, 3]])
In [19]: B = np.full((2,2),10)
B
Out[19]:
array([[10, 10],
[10, 10]])
In [20]: A+B
Out[20]:
array([[10, 11],
[12, 13]])
In [21]: A*B
Out[21]:
array([[ 0, 10],
[20, 30]])
In [22]: A.dot(B)
Out[22]:
array([[10, 10],
[50, 50]])
In [23]: A.T
Out[23]:
array([[0, 2],
[1, 3]])
向量和矩阵运算
In [24]: V = np.array([1,2])
V
Out[24]:
array([1, 2])
In [25]: V+A
Out[25]:
array([[1, 3],
[3, 5]])
In [26]: np.vstack([V]*A.shape[0])
Out[26]:
array([[1, 2],
[1, 2]])
In [27]: np.vstack([V]*A.shape[0]) + A
Out[27]:
array([[1, 3],
[3, 5]])
In [28]: np.tile(V,(2,1)) # 堆叠,行叠成2次,列叠成1次
Out[28]:
array([[1, 2],
[1, 2]])
In [29]: np.tile(V,A.shape)
Out[29]:
array([[1, 2, 1, 2],
[1, 2, 1, 2]])
In [30]: np.tile(V,(2,1)) + A
Out[30]:
array([[1, 3],
[3, 5]])
In [31]: V.dot(A)
Out[31]:
array([4, 7])
In [32]: A.dot(V)
Out[32]:
array([2, 8])
矩阵的逆
In [33]: A
Out[33]:
array([[0, 1],
[2, 3]])
In [34]: np.linalg.inv(A)
Out[34]:
array([[-1.5, 0.5],
[ 1. , 0. ]])
In [35]: invA = np.linalg.inv(A)
In [36]: A.dot(invA)
Out[36]:
array([[1., 0.],
[0., 1.]])
In [37]: invA.dot(A)
Out[37]:
array([[1., 0.],
[0., 1.]])
In [38]: X
Out[38]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
In [39]: invX = np.linalg.inv(X)
invX
---------------------------------------------------------------------------
LinAlgError Traceback (most recent call last)
<ipython-input-39-870393757875> in <module>
----> 1 invX = np.linalg.inv(X)
2 invX ~/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py in inv(a)
525 a, wrap = _makearray(a)
526 _assertRankAtLeast2(a)
--> 527 _assertNdSquareness(a)
528 t, result_t = _commonType(a)
529 ~/anaconda3/lib/python3.7/site-packages/numpy/linalg/linalg.py in _assertNdSquareness(*arrays)
213 m, n = a.shape[-2:]
214 if m != n:
--> 215 raise LinAlgError('Last 2 dimensions of the array must be square')
216
217 def _assertFinite(*arrays): LinAlgError: Last 2 dimensions of the array must be square 矩阵的伪逆
In [40]: invpX = np.linalg.pinv(X)
invpX
Out[40]:
array([[-2.46666667e-01, -6.66666667e-02, 1.13333333e-01],
[-1.33333333e-01, -3.33333333e-02, 6.66666667e-02],
[-2.00000000e-02, -2.51534904e-17, 2.00000000e-02],
[ 9.33333333e-02, 3.33333333e-02, -2.66666667e-02],
[ 2.06666667e-01, 6.66666667e-02, -7.33333333e-02]])
In [41]: X.dot(invpX)
Out[41]:
array([[ 0.83333333, 0.33333333, -0.16666667],
[ 0.33333333, 0.33333333, 0.33333333],
[-0.16666667, 0.33333333, 0.83333333]])
In [42]: invpX.dot(X)
Out[42]:
array([[ 6.00000000e-01, 4.00000000e-01, 2.00000000e-01,
-1.33226763e-15, -2.00000000e-01],
[ 4.00000000e-01, 3.00000000e-01, 2.00000000e-01,
1.00000000e-01, -7.63278329e-16],
[ 2.00000000e-01, 2.00000000e-01, 2.00000000e-01,
2.00000000e-01, 2.00000000e-01],
[ 1.73472348e-16, 1.00000000e-01, 2.00000000e-01,
3.00000000e-01, 4.00000000e-01],
[-2.00000000e-01, 4.99600361e-16, 2.00000000e-01,
4.00000000e-01, 6.00000000e-01]])
矩阵的伪逆又被称为“广义逆矩阵”,有兴趣的同学可以翻看线性教材课本查看更多额广义逆矩阵相关的性质。中文wiki链接: https://zh.wikipedia.org/wiki/%E5%B9%BF%E4%B9%89%E9%80%86%E9%98%B5
Numpy 中的比较和Fancy Indexing
In [43]: X
Out[43]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
In [44]: x = np.arange(16)
x
Out[44]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
In [45]: ind = [3, 5, 7]
x[ind]
Out[45]:
array([3, 5, 7])
In [46]: ind = np.array([[0, 2], [1, 3],[5,7]])
x[ind]
Out[46]:
array([[0, 2],
[1, 3],
[5, 7]])
In [47]: row = np.array([0, 1, 2])
col = np.array([1, 2, 3])
X[row, col]
Out[47]:
array([ 2, 8, 14])
In [48]: X[[0,1,2],[1,2,3]]
Out[48]:
array([ 2, 8, 14])
In [49]: X[[1,2],[2,3]] # X里面只能放两个坐标点,放3个报错,需要用到上面的方法,把行列都分开存放
Out[49]:
array([ 8, 14])
In [50]: X[row]
Out[50]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
In [51]: X[-1,row] # -1表示取最后一行
Out[51]:
array([11, 12, 13])
In [53]: X[:,row] # :表示取所有行
Out[53]:
array([[ 1, 2, 3],
[ 6, 7, 8],
[11, 12, 13]])
In [54]: chs = [True, False, True, True, False]
In [55]: X[:,chs] # 取1,3,4列
Out[55]:
array([[ 1, 3, 4],
[ 6, 8, 9],
[11, 13, 14]])
numpy.array 的比较
In [56]: x
Out[56]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
In [57]: x <= 6
Out[57]:
array([ True, True, True, True, True, True, True, False, False,
False, False, False, False, False, False, False])
In [58]: x != 3
Out[58]:
array([ True, True, True, False, True, True, True, True, True,
True, True, True, True, True, True, True])
In [59]: X < 6
Out[59]:
array([[ True, True, True, True, True],
[False, False, False, False, False],
[False, False, False, False, False]])
使用 numpy.array 的比较结果
In [60]: np.count_nonzero( x <= 3) # 统计小于等于3的数量--有4个满足,所以是4个true,true用表示1,所以结果为4
Out[60]:
4
In [65]: np.sum(x <= 3) # 统计小于等于3的数量
Out[65]:
4
In [66]: X
Out[66]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
In [67]: np.sum(X % 2 == 0, axis=0) # 沿着行的方向  即每列偶数的数量
Out[67]:
array([1, 2, 1, 2, 1])
In [71]: np.sum(X % 2 == 0, axis=1) # 沿着列的方向  即每行偶数的数量
Out[71]:
array([2, 3, 2])
In [91]: np.any(x == 0) # any 查找所有值,只要有一个满足条件 为true,则返回True
Out[91]:
True
In [92]: np.any(x < 0)
Out[92]:
False
In [93]: np.all(x<0) # all 查找所有值,只要有一个不满足条件 为false,则返回False
Out[93]:
False
In [94]: np.all(x>=0)
Out[94]:
True
In [95]: np.all(X>0) # 整体判断,只返回一个布尔值
Out[95]:
True
In [96]: np.all(X>3,axis=1) # 沿着列的方向, 即每行的值是否满足条件,有一个不满足,则返回false
Out[96]:
array([False, True, True])
In [97]: np.all(X>3,axis=0) # 沿着行的方向, 即每列的值是否满足条件,有一个不满足,则返回false
Out[97]:
array([False, False, False, True, True])
In [98]: np.any(X>14) # 整体判断,只返回一个布尔值
Out[98]:
True
In [99]: np.any(X>14,axis=0)
Out[99]:
array([False, False, False, False, True])
In [100]: np.any(X>14,axis=1)
Out[100]:
array([False, False, True])
In [101]: np.sum((x>3)&(x<10)) # 查询3<x<10的数量
Out[101]:
6
In [102]: np.sum((x%2==0)|(x>10)) # 查询x为偶数或大于10的数量
Out[102]:
11
In [103]: np.sum(~(x==0)) # 查询x中不等于0的值的数量
Out[103]:
15
In [104]: x[x<5] # 返回满足条件的数组
Out[104]:
array([0, 1, 2, 3, 4])
In [105]: x[x%2==0]
Out[105]:
array([ 0, 2, 4, 6, 8, 10, 12, 14])
In [106]: X
Out[106]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15]])
In [107]: X[X[:,-1]%3==0,:] # 返回最后一列中能被3整除的行
Out[107]:
array([[11, 12, 13, 14, 15]])
In [108]: X[X[:,-1]%3==0]
Out[108]:
array([[11, 12, 13, 14, 15]])
In [109]: X[:,X[-1,:]%3==0] # 返回最后一行中能被3整除的列
Out[109]:
array([[ 2, 5],
[ 7, 10],
[12, 15]])
In [110]: X[-1,:]%3==0
Out[110]:
array([False, True, False, False, True])

---恢复内容结束---

Jupyter中python3之numpy练习的更多相关文章

  1. Python3:numpy模块中的argsort()函数

    Python3:numpy模块中的argsort()函数   argsort函数是Numpy模块中的函数: >>> import numpy >>> help(nu ...

  2. 其它课程中的python---3、numpy总结(非常全)

    其它课程中的python---3.numpy总结(非常全) 一.总结 一句话总结: 学习方式应该是:听课+总结:-->找总结博客+再总结 需要始终记住:凭借,继承,复用 1.numpy的主要功能 ...

  3. 在jupyter中配置c++内核

    安装 xeus-cling conda install xeus-cling -c conda-forg xeus-cling 是一个用于编译解释于C++的Jupyter内核目前,支持Mac与Linu ...

  4. Linux中python3,django,redis以及mariab的安装

    1. Linux中python3,django,redis以及mariab的安装 2. CentOS下编译安装python3 编译安装python3.6的步骤 1.下载python3源码包 wget ...

  5. MacOS环境中 python3 部署

    MacOS环境中 python3 部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.在MacOS安装Python3.6 1>.打开python关于MacOS版本的官方网 ...

  6. 在 jupyter 中添加菜单和自动完成功能

    在 jupyter 中添加菜单和自动完成功能 参考文档http://www.360doc.com/content/17/1103/14/1489589_700569828.shtmlhttp://to ...

  7. Jupyter 中的 10 个魔法函数

    1)%pwd 于显示Jupyter当前的工作空间. 2)%hist 显示当前Jupyter中,所有运行过的历史代码. 3)%who 显示当前Jupyter环境中的所有变量或名称. 4)%reset 删 ...

  8. Mac下安装lightgb并在jupyter中使用

    1.先安装cmake和gcc brew install cmake brew install gcc 2.下载后确定自己的gcc版本 cd /usr/local/opt/gcc/lib/gcc/ 看到 ...

  9. jupyter中那些神奇的第三方拓展魔术命令

    1 简介 无论是jupyter notebook还是jupyter lab,都可以使用ipython中的众多自带魔术命令来实现丰富的辅助功能,诸如%time之类的. 这些都已经是老生常谈的知识没什么好 ...

随机推荐

  1. EL表达式与标签库

    https://blog.csdn.net/panhaigang123/article/details/78428567

  2. python的int方法实现数据类型转换

    int方法默认以十进制来实现数据类型的转换: 举例: str1=" #给定的内容最好是纯数字,当然也可以是数字再掺杂点别的,最好别掺杂,因为会报错 print(type(str1),str) ...

  3. React Context(一):隐式传递数据

    一 Context概述 Context provides a way to pass data through the component tree without having to pass pr ...

  4. canvas(一) 基本线条绘制

    var dom = document.getElementById('canvasItem'), ctx = dom.getContext('2d'); //坐标位置默认基于 浏览器窗口(0,0),此 ...

  5. Quartz.Net进阶之三:SimpleTrigger详述

    以前都是将所有的内容放在一篇文章里,就会导致文章很长,对于学习的人来说,有时候这也是一个障碍.所以,以后我的写作习惯,我就会把我写的文章缩短,但是内容不会少,内容更集中.这样,学习起来也不会很累,很容 ...

  6. 操作系统学习笔记(三) windows内存管理

    //系统物理页面是由 (Page Frame Number Database )简称PFN数据库来进行管理,实际上是一个数组,每个物理页面都对应一个PFN项. 进程的地址空间是通过VAD(Virtua ...

  7. ----一个roadmap----

    在课上了解到了学web前端三个基础,HTML.CSS.JS 从HTML开始,逐步学习CSS.JS 大致写了一个roadmap(应该是roadmap喔 就是这样,以后可能会来更错或者更新 对没错我来更错 ...

  8. Something of HTTP

    学习发现所需且所欠知识: 参考:  1.一堆博客   2.HTTP图解(链接奉上,自取)提取码: n6jq http简介 http返回状态码 http方法(点击查看) GET POST PATCH H ...

  9. 14.2.4HTML5约束API验证

    <body> <form> <!-- required属性在提交表单时不能空着 这个属性适用于<input> <textarea> <sel ...

  10. 20154305 齐帅 PC平台逆向破解

    Exp1 PC平台逆向破解 一.实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时 ...