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])
相关推荐
AI攻城狮2 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽2 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健17 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞19 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽21 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程1 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪1 天前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook1 天前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田2 天前
使用 pkgutil 实现动态插件系统
python