当前位置: 首页 > news >正文

Numpy数值计算基础

 1 # -*- coding: utf-8 -*-
 2 
 3 ###############################################################################
 4 #######################            正文代码             #######################
 5 ###############################################################################
 6 
 7 # 代码 2-30
 8 import numpy as np  #导入NumPy库
 9 matr1 = np.mat("1 2 3;4 5 6;7 8 9") #使用分号隔开数据
10 print('创建的矩阵为:',matr1)
11 
12 matr2 = np.matrix([[123],[456],[789]])
13 print('创建的矩阵为:',matr2)
14 
15 
16 # 代码 2-31
17 arr1 = np.eye(3)
18 print('创建的数组1为:',arr1)
19 
20 arr2 = 3*arr1
21 print('创建的数组2为:',arr2)
22 
23 print('创建的矩阵为:',np.bmat("arr1 arr2; arr1 arr2"))
24 
25 
26 # 代码 2-32
27 matr1 = np.mat("1 2 3;4 5 6;7 8 9")  #创建矩阵
28 print('创建的矩阵为:',matr1)
29 
30 matr2 = matr1*3  #矩阵与数相乘
31 print('创建的矩阵为:',matr2)
32 print('矩阵相加结果为:',matr1+matr2)  #矩阵相加
33 print('矩阵相减结果为:',matr1-matr2)  #矩阵相减
34 print('矩阵相乘结果为:',matr1*matr2)  #矩阵相乘
35 print('矩阵对应元素相乘结果为:',np.multiply(matr1,matr2))
36 
37 # 代码 2-33
38 '''
39 print('矩阵转置结果为:',matr1.T)  #转置
40 print('矩阵共轭转置结果为:',matr1.H)  #共轭转置(实数的共轭就是其本身)
41 print('矩阵的逆矩阵结果为:',matr1.I)  #逆矩阵
42 print('矩阵的二维数组结果为:',matr1.A)  #返回二维数组的视图
43 '''
44 # 代码 2-34
45 x = np.array([1,2,3])
46 y = np.array([4,5,6])
47 print('数组相加结果为:',x + y)  #数组相加
48 print('数组相减结果为:',x - y)  #数组相减
49 print('数组相乘结果为:',x * y)  #数组相乘
50 print('数组相除结果为:',x / y)  #数组相除
51 print('数组幂运算结果为:',x ** y)  #数组幂运算
52 
53 # 代码 2-35
54 x = np.array([1,3,5])
55 y = np.array([2,3,4])
56 print('数组比较结果为:',x < y)
57 print('数组比较结果为:',x > y)
58 print('数组比较结果为:',x == y)
59 print('数组比较结果为:',x >= y)
60 print('数组比较结果为:',x <= y)
61 print('数组比较结果为:',x != y)
62 
63 # 代码 2-36
64 print('数组逻辑运算结果为:',np.all(x == y))  #np.all()表示逻辑and
65 print('数组逻辑运算结果为:',np.any(x == y))  #np.any()表示逻辑or
66 
67 # 代码 2-37
68 arr1 = np.array([[0,0,0],[1,1,1],[2,2,2],[3,3,3]])
69 print('创建的数组1为:',arr1)
70 print('数组1的shape为:',arr1.shape)
71 arr2 = np.array([1,2,3])
72 print('创建的数组2为:',arr2)
73 print('数组2的shape为:',arr2.shape)
74 print('数组相加结果为:',arr1 + arr2)
75 
76 # 代码 2-38
77 arr1 = np.array([[0,0,0],[1,1,1],[2,2,2],[3,3,3]])
78 print('创建的数组1为:',arr1)
79 print('数组1的shape为:',arr1.shape)
80 
81 arr2 = np.array([1,2,3,4]).reshape((4,1))
82 print('创建的数组2为:',arr2)
83 print('数组2的shape为:',arr2.shape)
84 print('数组相加结果为:',arr1 + arr2)

 

转载于:https://www.cnblogs.com/779084229yyt/p/9442715.html

相关文章:

  • MYSQL设置查询内存表大小
  • 聊聊rocketmq的FileAppender
  • JS高级
  • MongoDB4.0构建分布式分片群集
  • (六)Hibernate的二级缓存
  • 用 PreparedStatement 向 SqlServer 中一次性插入多条记录
  • redis的高级特性
  • 新版ExTiX 诞生,基于 Ubuntu 的桌面 Linux 发行
  • 移动端测试===从安卓手机截图到桌面的几行代码
  • redis系列:通过日志案例学习string命令
  • Go 与 Scala
  • 互联网健康教育品牌百诺名医汇获深圳汇投资本3000万元Pre-A轮融资
  • 教你快速入门ES6
  • tranform知多少
  • Android酷炫实用的开源框架(UI框架)
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • GraphQL学习过程应该是这样的
  • Javascript基础之Array数组API
  • PermissionScope Swift4 兼容问题
  • quasar-framework cnodejs社区
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • SwizzleMethod 黑魔法
  • WePY 在小程序性能调优上做出的探究
  • 老板让我十分钟上手nx-admin
  • 区块链将重新定义世界
  • 小程序button引导用户授权
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 怎样选择前端框架
  • Python 之网络式编程
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • #我与Java虚拟机的故事#连载12:一本书带我深入Java领域
  • (1)(1.9) MSP (version 4.2)
  • (windows2012共享文件夹和防火墙设置
  • (强烈推荐)移动端音视频从零到上手(上)
  • (一)python发送HTTP 请求的两种方式(get和post )
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转)MVC3 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
  • (转)创业家杂志:UCWEB天使第一步
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .net core webapi Startup 注入ConfigurePrimaryHttpMessageHandler
  • .NET 反射 Reflect
  • .net中调用windows performance记录性能信息
  • :如何用SQL脚本保存存储过程返回的结果集
  • @require_PUTNameError: name ‘require_PUT‘ is not defined 解决方法
  • [1525]字符统计2 (哈希)SDUT
  • [202209]mysql8.0 双主集群搭建 亲测可用
  • [Android]RecyclerView添加HeaderView出现宽度问题
  • [Avalon] Avalon中的Conditional Formatting.
  • [BT]BUUCTF刷题第9天(3.27)
  • [BZOJ5125]小Q的书架(决策单调性+分治DP+树状数组)
  • [C#]winform使用引导APSF和梯度自适应卷积增强夜间雾图像的可见性算法实现夜间雾霾图像的可见度增强
  • [Excel] vlookup函数
  • [go] 策略模式
  • [HXPCTF 2021]includer‘s revenge