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])
相关推荐
向上的车轮13 分钟前
基于go语言的云原生TodoList Demo 项目,验证云原生核心特性
开发语言·云原生·golang
The Chosen One98514 分钟前
C++ : AVL树-详解
开发语言·c++
PH_modest25 分钟前
【Qt跬步积累】—— 初识Qt
开发语言·qt
君名余曰正则26 分钟前
机器学习实操项目01——Numpy入门(基本操作、数组形状操作、复制与试图、多种索引技巧、线性代数)
线性代数·机器学习·numpy
怀旧,1 小时前
【C++】18. 红⿊树实现
开发语言·c++
多恩Stone1 小时前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc
xiaopengbc1 小时前
在 Python 中实现观察者模式的具体步骤是什么?
开发语言·python·观察者模式
Python大数据分析@1 小时前
python用selenium怎么规避检测?
开发语言·python·selenium·网络爬虫
ThreeAu.1 小时前
Miniconda3搭建Selenium的python虚拟环境全攻略
开发语言·python·selenium·minicoda·python环境配置