Python中numpy模块常见用法demo实例小结


Posted in Python onMarch 16, 2019

本文实例总结了Python中numpy模块常见用法。分享给大家供大家参考,具体如下:

import numpy as np
arr = np.array([[1,2,3], [2,3,4]])
print(arr)
print(type(arr))
print('number of dim:', arr.ndim)
print('shape:', arr.shape)
print('size:', arr.size)

[[1 2 3]
 [2 3 4]]
number of dim: 2
shape: (2, 3)
size: 6

a32 = np.array([1,23,456], dtype=np.int)
print(a32.dtype)
a64 = np.array([1,23,456], dtype=np.int64)
print(a64.dtype)
f64 = np.array([1,23,456], dtype=np.float)
print(f64.dtype)

int32
int64
float64

z = np.zeros((3, 4))
print(z)
print(z.dtype)
print()
one = np.ones((3, 4), dtype=int)
print(one)
print(one.dtype)
print()
emt = np.empty((3, 4), dtype=int)
print(emt)
print(emt.dtype)
print()
ran = np.arange(12).reshape((3,4))
print(ran)
print(ran.dtype)
print()
li = np.linspace(1, 10, 6).reshape(2, 3)
print(li)
print(li.dtype)

[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
float64
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
int32
[[          0  1072693248  1717986918  1074161254]
 [ 1717986918  1074947686 -1717986918  1075419545]
 [ 1717986918  1075865190           0  1076101120]]
int32
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
int32
[[ 1.   2.8  4.6]
 [ 6.4  8.2 10. ]]
float64

a = np.array([10,20,30,40])
b = np.arange(4)
print(a)
print(b)
print()
print(a+b)
print(a-b)
print(a*b)
print()
print(a**b)
print()
print(10*np.sin(a))
print()
print(b<3)
print()

[10 20 30 40]
[0 1 2 3]
[10 21 32 43]
[10 19 28 37]
[  0  20  60 120]
[    1    20   900 64000]
[-5.44021111  9.12945251 -9.88031624  7.4511316 ]
[ True  True  True False]

a = np.array([[1,2], [3,4]])
b = np.arange(4).reshape(2, 2)
print(a)
print(b)
print()
print(a * b)
print(np.dot(a, b)) #矩阵乘法,或下面:
print(a.dot(b))
print()

[[1 2]
 [3 4]]
[[0 1]
 [2 3]]
[[ 0  2]
 [ 6 12]]
[[ 4  7]
 [ 8 15]]
[[ 4  7]
 [ 8 15]]

a = np.random.random((2, 4))
print(a)
print(np.sum(a))
print(np.min(a))
print(np.max(a))
print()
print(np.sum(a, axis=1)) #返回每一行的和。 axis=1代表行
print(np.min(a, axis=0)) #返回每一列的最小值。 axis=0代表列
print(np.mean(a, axis=1)) #返回每一行的平均值

[[0.04456704 0.99481679 0.96599561 0.48590905]
 [0.56512852 0.62887714 0.78829115 0.32759434]]
4.8011796551183945
0.04456704487406293
0.9948167913629338
[2.4912885  2.30989116]
[0.04456704 0.62887714 0.78829115 0.32759434]
[0.62282212 0.57747279]

A = np.arange(2, 14).reshape(3, 4)
print(A)
print(np.argmin(A)) #最小索引
print(np.argmax(A)) #最大索引
print()
print(A.mean())
print(np.median(A)) #中位数
print(A.cumsum()) #累加值
print(np.diff(A)) #相邻差值
print()

[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
0
11
7.5
7.5
[ 2  5  9 14 20 27 35 44 54 65 77 90]
[[1 1 1]
 [1 1 1]
 [1 1 1]]
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int32), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int32))

A = np.array([[1,0], [0,3]])
print(A)
print(A.nonzero()) #分别输出非零元素的行和列值
print(np.sort(A)) #逐行排序后的矩阵
print(np.sort(A, axis=0)) #逐列排序的矩阵
print(np.sort(A).nonzero())
print()
B = np.arange(14, 2, -1).reshape(3, 4)
print(B)
print(B.transpose()) #转置
print((B.T).dot(B)) #转置
print()
print(np.clip(B, 5, 9)) #B中将范围限定,大于9的数都为9,小于5的都为5,之间的数不变
print()

[[1 0]
 [0 3]]
(array([0, 1], dtype=int32), array([0, 1], dtype=int32))
[[0 1]
 [0 3]]
[[0 0]
 [1 3]]
(array([0, 1], dtype=int32), array([1, 1], dtype=int32))
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
[[332 302 272 242]
 [302 275 248 221]
 [272 248 224 200]
 [242 221 200 179]]
[[9 9 9 9]
 [9 9 8 7]
 [6 5 5 5]]

A = np.arange(3, 7)
print(A)
print(A[2])
print()
B = np.arange(3, 15).reshape(3, 4)
print(B)
print(B[2])
print(B[2][1])
print(B[2, 1])
print()
print(B[2, 2:])
print(B[1:, 2:])
print()
for row in B:
  print(row)
print()
for col in B.T:
  print(col)
print()
print(B.flatten())
for elm in B.flat:
  print(elm)

[3 4 5 6]
5
[[ 3  4  5  6]
 [ 7  8  9 10]
 [11 12 13 14]]
[11 12 13 14]
12
12
[13 14]
[[ 9 10]
 [13 14]]
[3 4 5 6]
[ 7  8  9 10]
[11 12 13 14]
[ 3  7 11]
[ 4  8 12]
[ 5  9 13]
[ 6 10 14]
[ 3  4  5  6  7  8  9 10 11 12 13 14]
3
4
5
6
7
8
9
10
11
12
13
14

#矩阵合并
A = np.array([1,1,1])
B = np.array([2,2,2])
C = np.vstack((A, B, A, B))
print(C)
print(A.shape, (A.T).shape)
print(C.shape)
print()
D = np.hstack((A, B))
print(D)
print()
print(A[np.newaxis, :])
print(A[:, np.newaxis])
print(np.hstack((A[:, np.newaxis], B[:, np.newaxis])))
print()
print(np.stack((A,B), axis=0))
print(np.stack((A,B), axis=1))
#print(np.concatenate((A,B,B,A), axis=0))
#print(np.concatenate((A,B,B,A), axis=1))

[[1 1 1]
 [2 2 2]
 [1 1 1]
 [2 2 2]]
(3,) (3,)
(4, 3)
[1 1 1 2 2 2]
[[1 1 1]]
[[1]
 [1]
 [1]]
[[1 2]
 [1 2]
 [1 2]]
[[1 1 1]
 [2 2 2]]
[[1 2]
 [1 2]
 [1 2]]

A = np.arange(12).reshape(3, 4)
print(A)
print(np.split(A, 2, axis=1))
print(np.split(A, 3, axis=0))
print()
print(np.array_split(A, 3, axis=1)) #不等分割
print()
print(np.hsplit(A, 2))
print(np.vsplit(A, 1))

[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2],
       [ 6],
       [10]]), array([[ 3],
       [ 7],
       [11]])]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
[array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])]

A = np.arange(4)
B = A
C = B
D = A.copy()
print(A, B, C, D)
A[0] = 5
print(A, B, C, D)
print(id(A), id(B), id(C), id(D)) #id返回指针的值(内存地址)
print()

[0 1 2 3] [0 1 2 3] [0 1 2 3] [0 1 2 3]
[5 1 2 3] [5 1 2 3] [5 1 2 3] [0 1 2 3]
172730832 172730832 172730832 172730792

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python显示进度条的方法
Sep 20 Python
python实现Decorator模式实例代码
Feb 09 Python
Python语言快速上手学习方法
Dec 14 Python
pyqt5之将textBrowser的内容写入txt文档的方法
Jun 21 Python
python获取当前文件路径以及父文件路径的方法
Jul 10 Python
python plotly绘制直方图实例详解
Jul 22 Python
python GUI库图形界面开发之PyQt5单行文本框控件QLineEdit详细使用方法与实例
Feb 27 Python
jupyter notebook 多行输出实例
Apr 09 Python
学会python自动收发邮件 代替你问候女友
May 20 Python
pycharm 对代码做静态检查操作
Jun 09 Python
在python下实现word2vec词向量训练与加载实例
Jun 09 Python
Django如何创作一个简单的最小程序
May 12 Python
Python常见的pandas用法demo示例
Mar 16 #Python
详解python中list的使用
Mar 15 #Python
详解Python_shutil模块
Mar 15 #Python
python批量修改文件夹及其子文件夹下的文件内容
Mar 15 #Python
使用PyQtGraph绘制精美的股票行情K线图的示例代码
Mar 14 #Python
详解Django+uwsgi+Nginx上线最佳实战
Mar 14 #Python
TensorFlow卷积神经网络之使用训练好的模型识别猫狗图片
Mar 14 #Python
You might like
当海贼王变成JOJO风
2020/03/02 日漫
php设计模式 Facade(外观模式)
2011/06/26 PHP
sql注入与转义的php函数代码
2013/06/17 PHP
codeigniter中view通过循环显示数组数据的方法
2015/03/20 PHP
PHP实现基于图的深度优先遍历输出1,2,3...n的全排列功能
2017/11/10 PHP
php将字符串转换为数组实例讲解
2020/05/05 PHP
jquery pagination插件实现无刷新分页代码
2009/10/13 Javascript
JQuery UI DatePicker中z-index默认为1的解决办法
2010/09/28 Javascript
javascript textContent与innerText的异同分析
2010/10/22 Javascript
jQuery学习笔记[1] jQuery中的DOM操作
2010/12/03 Javascript
深入浅出分析javaScript中this用法
2015/05/09 Javascript
输入法的回车与消息发送快捷键回车的冲突解决方法
2016/08/09 Javascript
IE8兼容Jquery.validate.js的问题
2016/12/01 Javascript
Bootstrap风格的WPF样式
2016/12/07 Javascript
基于js 本地存储(详解)
2017/08/16 Javascript
使用JS和canvas实现gif动图的停止和播放代码
2017/09/01 Javascript
Vue可自定义tab组件用法实例
2019/10/24 Javascript
vue 路由懒加载中给 Webpack Chunks 命名的方法
2020/04/24 Javascript
如何搜索查找并解决Django相关的问题
2014/06/30 Python
Python中列表(list)操作方法汇总
2014/08/18 Python
Python下线程之间的共享和释放示例
2015/05/04 Python
Python中遍历字典过程中更改元素导致异常的解决方法
2016/05/12 Python
python3使用PyMysql连接mysql数据库实例
2017/02/07 Python
python 内置函数filter
2017/06/01 Python
Python3 操作符重载方法示例
2017/11/23 Python
对Django中static(静态)文件详解以及{% static %}标签的使用方法
2019/07/28 Python
python Jupyter运行时间实例过程解析
2019/12/13 Python
双向RNN:bidirectional_dynamic_rnn()函数的使用详解
2020/01/20 Python
HTML5 在canvas中绘制矩形附效果图
2014/06/23 HTML / CSS
详解如何用HTML5 Canvas API控制图片的缩放变换
2016/03/22 HTML / CSS
鼓励运动员的广播稿
2014/02/08 职场文书
社会学专业求职信
2014/07/17 职场文书
护士长2014年终工作总结
2014/11/11 职场文书
python基础入门之普通操作与函数(三)
2021/06/13 Python
Go 语言中 20 个占位符的整理
2021/10/16 Golang
Python面试不修改数组找出重复的数字
2022/05/20 Python