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中自定义函数的教程
Apr 27 Python
python中迭代器(iterator)用法实例分析
Apr 29 Python
python实现批量下载新浪博客的方法
Jun 15 Python
Python多线程、异步+多进程爬虫实现代码
Feb 17 Python
Python2中文处理纪要的实现方法
Mar 10 Python
利用ctypes获取numpy数组的指针方法
Feb 12 Python
Anaconda3+tensorflow2.0.0+PyCharm安装与环境搭建(图文)
Feb 18 Python
python3通过qq邮箱发送邮件以及附件
May 20 Python
Python如何使用ElementTree解析xml
Oct 12 Python
Pycharm操作Git及GitHub的步骤详解
Oct 27 Python
只用40行Python代码就能写出pdf转word小工具
May 31 Python
python中__slots__节约内存的具体做法
Jul 04 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
PHP技术开发技巧分享
2010/03/23 PHP
服务器上配置PHP运行环境教程
2015/02/12 PHP
php截取指定2个字符之间字符串的方法
2015/04/15 PHP
php 中htmlentities导致中文无法查询问题
2018/09/10 PHP
Javascript中的常见排序算法
2007/03/27 Javascript
JQuery1.4+ Ajax IE8 内存泄漏问题
2010/10/15 Javascript
用jQuery中的ajax分页实现代码
2011/09/20 Javascript
jQuery实现可收缩展开的级联菜单实例代码
2013/11/27 Javascript
jquery批量设置属性readonly和disabled的方法
2014/01/24 Javascript
Document.location.href和.replace的区别示例介绍
2014/03/04 Javascript
javascript生成img标签的3种实现方法(对象、方法、html)
2015/12/25 Javascript
利用Jquery实现几款漂亮实用的时间轴(附示例代码)
2017/02/15 Javascript
JavaScript实现的数字与字符串转换功能示例
2017/08/23 Javascript
防止页面url缓存中ajax中post请求的处理方法
2017/10/10 Javascript
浅析vue 函数配置项watch及函数 $watch 源码分享
2018/11/22 Javascript
JS判断数组里是否有重复元素的方法小结
2019/05/21 Javascript
Vue登录拦截 登录后继续跳转指定页面的操作
2020/08/04 Javascript
python求素数示例分享
2014/02/16 Python
归纳整理Python中的控制流语句的知识点
2015/04/14 Python
教你利用Python玩转histogram直方图的五种方法
2018/07/30 Python
Python面向对象类编写细节分析【类,方法,继承,超类,接口等】
2019/01/05 Python
将pip源更换到国内镜像的详细步骤
2019/04/07 Python
Python利用requests模块下载图片实例代码
2019/08/12 Python
解决python中的幂函数、指数函数问题
2019/11/25 Python
Python类中的装饰器在当前类中的声明与调用详解
2020/04/15 Python
使用Python防止SQL注入攻击的实现示例
2020/05/21 Python
教师自我评价范例
2013/09/24 职场文书
端午节活动策划方案
2014/03/09 职场文书
安全宣传标语
2014/06/10 职场文书
2014最新房贷收入证明范本
2014/09/12 职场文书
乡党政领导班子群众路线教育实践活动个人对照检查材料
2014/09/20 职场文书
工程竣工验收申请报告
2015/05/15 职场文书
2015个人年度工作总结范文
2015/05/28 职场文书
班干部学习委员竞选稿
2015/11/20 职场文书
2016猴年开门红标语口号
2015/12/26 职场文书
POST提交数据常见的四种方式
2022/01/18 HTML / CSS