Python之numpy

numpy: numeric python

创建数组:

1.array()函数

python 复制代码
numpy.array(object,dtype=None,copy=True,order='k',sobok=false,ndmin=0)

解释:

object:任何具有数组接口方法的对象

dtype:数据类型

copy:布尔型,可选项,默认值为True

2.访问数组对象的性质

ndarray类的重要对象属性:

ndim:数组维度

shape:数组各维度大小的形状元组

size:数组元素总个数

dtype:数组中元素的数据类型

python 复制代码
In [1]: list1 = [1,2,3]

In [2]: list2 = [[1,2,3],[4,5,6]]

In [3]: import numpy as np

In [4]: arr1 = np.arrary(list1)

In [5]: arr1 = np.array(list1)

In [6]: arr2 = np.array(list2)

In [7]: print(arr1)
[1 2 3]

In [8]: print(arr2)
[[1 2 3]
 [4 5 6]]

In [9]: arr1.ndim
Out[9]: 1

In [10]: arr2.ndim
Out[10]: 2

In [11]: arr1.shape
Out[11]: (3,)

In [12]: arr2.shape
Out[12]: (2, 3)

In [13]: arr1.size
Out[13]: 3

In [14]: arr2.size
Out[14]: 6

In [15]: arr1.dtype
Out[15]: dtype('int64')

In [16]: arr2.dtype
Out[16]: dtype('int64')

3.创建常用数组

numpy.eye用于创建一个对角矩阵,对角线上的元素可以是任何值,矩阵可以是方阵也可以不是方阵

numpy.indentity用于生成一个单位矩阵

python 复制代码
In [17]: np.__version__
Out[17]: '2.2.3'

In [18]: np.zeros(5)
Out[18]: array([0., 0., 0., 0., 0.])

In [19]: np.ones(6)
Out[19]: array([1., 1., 1., 1., 1., 1.])

In [20]: np.eye(4)
Out[20]:
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])

In [21]: np.identity(4)
Out[21]:
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])

In [22]: np.identity(3)
Out[22]:
array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

In [23]: np.eye(2,3)
Out[23]:
array([[1., 0., 0.],
       [0., 1., 0.]])

4.创建等差数列

python 复制代码
numpy.arange([start,]stop[,step,]dtype=None,*,like=None)
python 复制代码
numpy.linspace(start,stop,num,endpoint=True,retstep=False,dtype=None,axis=0)
python 复制代码
In [24]: np.linspace(3,20,3)
Out[24]: array([ 3. , 11.5, 20. ])

In [25]: np.arange(3,20,3)
Out[25]: array([ 3,  6,  9, 12, 15, 18])
相关推荐
威哥爱编程3 分钟前
模拟面试智能体开发实战 12:每篇都能跑不等于接起来能跑,端到端联调收官把十一块拼图合成一个能用的智能体
python·agent·harmonyos
威哥爱编程8 分钟前
模拟面试智能体开发实战 11:报告只躺在后端怎么复盘,把总评搬上手机还要适配大小屏
python·agent·harmonyos
2601_967987371 小时前
Python 实现行为驱动开发 (BDD) 自动化测试详解
自动化测试·python·bdd·behave·gherkin
Evand J1 小时前
【MATLAB例程】基于IMM-CKF的AOA+测距融合的二维移动目标跟踪定位
开发语言·matlab·目标跟踪
数字融合2 小时前
透明化视频驱动化工生产动态三维重建管控技术
大数据·人工智能·python
JianZhen✓2 小时前
IM 聊天列表为什么总“消息乱跳“:有序插入、时间轴派生、本地截断、已读回执
开发语言·前端·php
FungLeo2 小时前
FengCMS · PHP+MySQL 升级实录· 代码审计怎么闭环:REVIEW到 CLOSURE 四件套,12 个域90 篇修复文档跟到底
开发语言·mysql·php·代码审计·fengcms
屿华AI3 小时前
AUTOSAR 实战-诊断工具 | DTC/Event/FiM 故障报告自动化:DEM 诊断数据采集与解析
python·测试工具·自动化·汽车
七夜zippoe3 小时前
多轮对话管理:Session/记忆在 Java 后端怎么存(存储设计实战)
java·开发语言·ai·session·记忆
贝壳6903 小时前
9.13.12 那三个新功能,我一个一个跑了一遍:两个真香,一个有前提
python