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


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. linux上安装字体

    安装字体命令: yum install wqy-microhei-fonts wqy-zenhei-fonts   安装完字体的存放目录:/usr/share/fonts   默认会在fonts目录下 ...

  2. Linux mysql 联表查询

    在rhce考试题中,第21.22题为数据库查询题 题目: 在system1上创建一个Maria DB数据库,名为Contacts,要求: 数据库应该包含来自数据库users.mdb的内容,数据库只能被 ...

  3. ucore-lab1-练习5report

    实验5--实现函数调用堆栈跟踪函数 需要完成kdebug.c中函数print_stackframe的实现,可以通过函数print_stackframe来跟踪函数调用堆栈中记录的返回地址. 一.函数堆栈 ...

  4. HDU 6301 Distinct Values

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6301 多校contest1 题目大意是有一个长度为N的数组,给出M个"事实",每个 ...

  5. select2插件设置选中值并显示的问题

    在select2中,要想设置指定值为选中状态并显示: $("#select2_Id").val("XXXXX").select2() 或者 var obj= $ ...

  6. Python local error

    原来在python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,在修改之前对该变量的引用自然就会出现没定义这样的错误了,如果确定要引用全局变量,并且要对它修改,必须加上global关 ...

  7. 微软Office Online服务安装部署(二)

    现在准备配置Client 1.进入到桌面后,打开powershell 输入: Add-WindowsFeature Web-Server,Web-Mgmt-Tools,Web-Mgmt-Console ...

  8. Head First Servlets & JSP 学习笔记 第十三章 —— 过滤器的威力

    过滤器可能是最强大的Web应用开发工具了! 与Servlet非常类似,过滤器就是Java组件,请求发送到Servlet之前,可以用过滤器截获和处理请求:另外Servlet结束工作之后,但在响应发回给客 ...

  9. 小豆包的学习之旅:占用概率栅格地图和cost-map

    接下来将制图和定位问题分别进行介绍.这两个问题可以视为SLAM过程中两个相互联系的子问题,但是也可以视为两个单独的问题.虽然说SLAM问题是鸡和蛋的问题,但是在实际处理过程中总是有先后的.为了简化问题 ...

  10. MySQL开发——【数据库、数据表的基本操作】

    启动MySQL服务器端 CMD启动MySQL服务器端 net start(启动)|stop(停止)|restart(重启)服务名称(mysql) 连接MySQL服务器端 CMD连接MySQL服务器端 ...