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


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. Python设计模式 - 基础 - 七大基本原则

    提倡使用设计模式,主要出发点就是实现代码复用,增加代码的扩展性和可维护性.如何设计出简洁.易懂.灵活.优美的代码结构的确是一门学问,透彻理解并践行如下七大原则通常都能取得基本满意的结果: - 单一职责 ...

  2. sqlserver2017 重装过程中出现“无法找到数据库引擎启动句柄”错误的解决办法

    sqlserver数据库引擎修改账号名,详情参考:http://blog.51cto.com/djclouds/2089047?utm_source=oschina-app 在SQL Server安装 ...

  3. 用rekit创建react项目

    第一步  先进入github.com 然后搜索rekit 往下滑 1 . 先全局安装 npm install -g rekit 2 . 进入自己想要创建项目文件的目录输入 rekit create / ...

  4. 373. Find K Pairs with Smallest Sums 找出求和和最小的k组数

    [抄题]: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. D ...

  5. 394. Decode String 解码icc字符串3[i2[c]]

    [抄题]: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], ...

  6. [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  7. linux 下安装arm-linux-gnueabi交叉编译器

    1,开发平台 虚拟机:vm12 系统:ubuntu14.04 LTS  32位 2.准备ARM交叉编译工具包 编译uboot和linux kernel都需要ARM交叉工具链支持,这里使用Linaro提 ...

  8. grafana add custom dashboard

    grafana-dashboard-json prometheus-operator helm 中的grafana dashboard 扩展的时候,需要转换下载(https://grafana.com ...

  9. cpp 区块链模拟示例(四) 区块链工作量证明

    本文主要在之前的区块链原形上添加了工作量证明,并且为后继的交易功能做好准备. 上一个章节我们已经创建了区块链的基本原形,但是区块的哈希计算和加入太过于简单,如果按照这种速度添加区块那么区块链估计一个小 ...

  10. 主机性能监控之wmi 获取进程信息

    标 题: 主机性能监控之wmi 获取进程信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990499.html 欢迎转帖 请保持文本完整并注明出处 仅 ...