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])
相关推荐
aqi005 小时前
15天学会AI应用开发(八)使用向量数据库实现RAG功能
人工智能·python·大模型·ai编程·ai应用
Csvn6 小时前
`functools.lru_cache` —— 一行代码搞定缓存加速
后端·python
金銀銅鐵1 天前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup111 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi001 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent