Python笔记:详解使用内置函数创建ndarray

Python--ndarray 常用属性详解 1. 描述维度的属性--shape 1.1 改变原始数组的维度 1.2 视图变维 1.3 复制变维 2. 数组的索引操作--index 2.1 一维数组的索引切片 2.2 多维数组的切片 2.2.1 二维数组的切片 2.2.2 三维数组的切片 3. ndarray 掩码 3.1 布尔掩码 3.2 索引掩码 4. ndarray 的属性... 阅读详情

引入

import numpy as np

在此引入一次,下面直接使用 np

使用 np.zeros(shape) 创建

X = np.zeros((3,4))
print()
print('X = \n', X) 
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

输出:

X =
[[ 0. 0. 0. 0.]
 [ 0. 0. 0. 0.]
 [ 0. 0. 0. 0.]]

X has dimensions: (3, 4)
X is an object of type: class 'numpy.ndarray'
The elements in X are of type: float64
  • np.zeros(shape) 会创建一个全是 0 并且为给定形状的 ndarray
  • 同 np.array() 一样,内置函数也可以创建指定类型的 ndarray, 参考前文

使用 np.ones(shape) 创建

X = np.ones((3,2), dtype = np.int64) # 此处指定类型
print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

输出:

X = 
 [[1 1]
 [1 1]
 [1 1]]

X has dimensions: (3, 2)
X is an object of type: <class 'numpy.ndarray'>
The elements in X are of type: int64
  • 此处我们创建了一个具有指定形状的 ndarray,其中的元素全是 1

使用 np.full(shape, constant value) 创建

X = np.full((2,3), 5) 
print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

输出:

X =
[[5 5 5]
 [5 5 5]]

X has dimensions: (2, 3)
X is an object of type: class 'numpy.ndarray'
The elements in X are of type: int64
  • 第一个参数是你要创建的 ndarray 的形状,第二个参数是你要向数组中填充的常数值。
  • 常数值这里如果你填写5.0,那么类型自动变为 float64 类型
  • 当然,还可再加第三个参数,比如: dtype = np.int64 , 来更改数据类型

使用 np.eye(N) 创建 单位矩阵

X = np.eye(5)

print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

输出:

X =
[[ 1. 0. 0. 0. 0.]
 [ 0. 1. 0. 0. 0.]
 [ 0. 0. 1. 0. 0.]
 [ 0. 0. 0. 1. 0.]
 [ 0. 0. 0. 0. 1.]]

X has dimensions: (5, 5)
X is an object of type: class 'numpy.ndarray'
The elements in X are of type: float64
  • 单位矩阵是主对角线上全是 1,其他位置全是 0 的方形矩阵。
  • 线性代数中的基本数组是单位矩阵。

使用 np.diag() 创建 对角矩阵

X = np.diag([10,20,30,50])
print()
print('X = \n', X)
print()

输出为:

X =
[[10 0 0 0]
 [ 0 20 0 0]
 [ 0 0 30 0]
 [ 0 0 0 50]]

使用 np.arange(start,stop,step) 创建在给定区间内值均匀分布的 ndarray

x = np.arange(1,14,3)
print()
print('x = ', x)
print()
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype)

输出:

x = [ 1 4 7 10 13]

x has dimensions: (5,)
x is an object of type: class 'numpy.ndarray'
The elements in x are of type: int64
  • 可以传入一个参数、两个参数或三个参数。
  • 如果只传入一个参数,np.arange(N) 将创建一个秩为 1 的 ndarray,其中包含从 0 到 N - 1 的连续整数。
  • 注意,如果我希望数组具有介于 0 到 9 之间的整数,则需要将 N 设为 10,而不是将 N 设为 9。
  • 均匀分布的数字将包括 start 数字,但是不包括 stop 数字。
  • step 表示两个相邻值之间的差。允许间隔为非整数,例如 0.3,但是由于浮点数精度有限,输出通常不一致。
  • 因此,如果需要非整数间隔,通常建议使用函数 np.linspace(start, stop, N)。

使用 np.linspace(start,stop,step) 创建在给定区间内值均匀分布的 ndarray

x = np.linspace(0,25,10)
print()
print('x = \n', x)
print()
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype)

输出:

x = 
 [ 0.          2.77777778  5.55555556  8.33333333 11.11111111 13.88888889
 16.66666667 19.44444444 22.22222222 25.        ]

x has dimensions: (10,)
x is an object of type: <class 'numpy.ndarray'>
The elements in x are of type: float64
  • np.linspace(start, stop, N) 函数返回 N 个在闭区间 [start, stop] 内均匀分布的数字, 即 start 和 stop 值都包括在内。
  • 也可以不包含区间的结束点(就像 np.arange() 函数一样),方法是在 np.linspace() 函数中将关键字 endpoint 设为 False 。
x = np.linspace(0,25,10, endpoint = False)
    

使用 np.reshape(ndarray, new_shape) 创建秩为 2 的任何形状 ndarray

x = np.arange(20)
print()
print('Original x = ', x)
print()
x = np.reshape(x, (4,5))
print()
print('Reshaped x = \n', x)
print()
print('x has dimensions:', x.shape)
print('x is an object of type:', type(x))
print('The elements in x are of type:', x.dtype)

输出:

Original x = [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]

Reshaped x =
[[ 0 1 2 3 4]
 [ 5 6 7 8 9]
 [10 11 12 13 14]
 [15 16 17 18 19]]

x has dimensions: (4, 5)
x is an object of type: class 'numpy.ndarray'
The elements in x are of type: int64
  • 我们将 np.arange() 和 np.linspace() 这些函数与 np.reshape() 函数相结合,创建秩为 2 的任何形状 ndarray。
  • np.reshape(ndarray, new_shape) 函数会将给定 ndarray 转换为指定的 new_shape。注意:new_shape 应该与给定 ndarray 中的元素数量保持一致。
  • 某些函数还可以当做方法使用, 比如我们用一行代码实现上述示例中的相同结果: Y = np.arange(20).reshape(4, 5)
  • 注意,new_shape 应该与 ndarray 中的元素数量保持一致。

同样,我们也可以使用 reshape() 与 np.linspace() 创建秩为 2 的数组。

X = np.linspace(0,50,10, endpoint=False).reshape(5,2)
print()
print('X = \n', X)
print()
print('X has dimensions:', X.shape)
print('X is an object of type:', type(X))
print('The elements in X are of type:', X.dtype)

输出:

X =
 [[ 0. 5.]
 [ 10. 15.]
 [ 20. 25.]
 [ 30. 35.]
 [ 40. 45.]]

X has dimensions: (5, 2)
X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64

创建随机 ndarray

  • 使用 np.random.random(shape) 函数创建具有给定形状的 ndarray, 包含位于半开区间 [0.0, 1.0) 内的随机浮点数。

    X = np.random.random((3,3))
    print()
    print('X = \n', X)
    print()
    print('X has dimensions:', X.shape)
    print('X is an object of type:', type(X))
    print('The elements in x are of type:', X.dtype)
    
    

    输出:

    X =
    [[ 0.12379926 0.52943854 0.3443525 ]
     [ 0.11169547 0.82123909 0.52864397]
     [ 0.58244133 0.21980803 0.69026858]]
    
    X has dimensions: (3, 3)
    X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64
    
    
  • 使用np.random.randint(start, stop, size = shape)创建由特定区间内的随机整数构成的 ndarray, 其中包含在半开区间 [start, stop) 内的随机整数。

   X = np.random.randint(4,15,size=(3,2))
   print()
   print('X = \n', X)
   print()
   print('X has dimensions:', X.shape)
   print('X is an object of type:', type(X))
   print('The elements in X are of type:', X.dtype)

输出:

    X =
   	[[ 7 11]
   	 [ 9 11]
   	 [ 6 7]]
   	
   X has dimensions: (3, 2)
   X is an object of type: class 'numpy.ndarray' The elements in X are of type: int64
   
  • 使用 np.random.normal(mean, standard deviation, size=shape) 创建由满足特定统计学特性的随机数字组成的 ndarray

    X = np.random.normal(0, 0.1, size=(1000,1000))
    print()
    print('X = \n', X)
    print()
    print('X has dimensions:', X.shape)
    print('X is an object of type:', type(X))
    print('The elements in X are of type:', X.dtype)
    print('The elements in X have a mean of:', X.mean())
    print('The maximum value in X is:', X.max())
    print('The minimum value in X is:', X.min())
    print('X has', (X < 0).sum(), 'negative numbers')
    print('X has', (X > 0).sum(), 'positive numbers')
    

    输出:

    X =
    [[ 0.04218614 0.03247225 -0.02936003 ..., 0.01586796 -0.05599115 -0.03630946]
     [ 0.13879995 -0.01583122 -0.16599967 ..., 0.01859617 -0.08241612 0.09684025]
     [ 0.14422252 -0.11635985 -0.04550231 ..., -0.09748604 -0.09350044 0.02514799]
     ...,
     [-0.10472516 -0.04643974 0.08856722 ..., -0.02096011 -0.02946155 0.12930844]
     [-0.26596955 0.0829783 0.11032549 ..., -0.14492074 -0.00113646 -0.03566034]
     [-0.12044482 0.20355356 0.13637195 ..., 0.06047196 -0.04170031 -0.04957684]]
    
    X has dimensions: (1000, 1000)
    X is an object of type: class 'numpy.ndarray' The elements in X are of type: float64
    The elements in X have a mean of: -0.000121576684405
    The maximum value in X is: 0.476673923106
    The minimum value in X is: -0.499114224706 X 具有 500562 个负数 X 具有 499438 个正数
    

    说明:

    • 创建了包含从正态高斯分布(具有给定均值和标准差)中抽样的随机数字。即:一个 1,000 x 1,000 ndarray,其中包含从正态分布(均值为 0,标准差为 0.1)中随机抽样的浮点数。
    • 可以看出,ndarray 中的随机数字的平均值接近 0,X 中的最大值和最小值与 0(平均值)保持对称,正数和负数的数量很接近。
NumPy模块核心数据结构:ndarray详解 文章目录引入:什么是NumPy?什么是ndarray?(课前须知)一、ndarray的创建:(一)常用函数:array、zeros、ones、empty:(二) 其他创建方式(函数):arange、linspace、logspace、random二、ndarray对象属性(一)基本属性(二)NumPy基本数据类型(较常用)(三)ndarray修改形状三、NummPy基本操作(一)数组与标量、数组之... 阅读详情

相关推荐

NumPy数据分析基础:ndarray属性查看、创建及输出各类操作详解

作为数据分析三巨头Pandas、matplotlib、NumPy之一,必然要给足面子单独拿出来讲解一波。NumPy应用场景十分宽泛,Pandas很多函数转换后也都是NumPy数组类型的数据结构。在机器学习、深度学习以及一些数据处理操作中使用的频率甚至比Pandas都高。而且NumPy功能强大,使用起来也十分便捷,支持多种复杂操作。平时我的Pandas以及一些机器学习的文章都有用到NumPy,但是博客内容并没有详细解答NumPy的操作也没有记录有关NumPy操作的一些具体函数解答。

master_hunter的博客 4558

python_numpy库_ndarray的创建

ndarray是NumPy中表示数组的重要类型。

weixin_73002278的博客 838

光学系统设计:波段、光源、光纤与探测器匹配指南

在光学测量和光纤通信系统中,波段、光源、光纤和探测器的匹配关系是系统设计的核心基础。波段指电磁波谱中的特定波长范围,光源负责产生特定波长的光信号,光纤作为传输介质需要与波长匹配以降低损耗,探测器则负责将光信号转换为电信号。理解各组件的光谱特性对于系统性能至关重要,例如在850nm短波长多模光纤系统中,VCSEL激光器与PIN光电二极管的组合能提供最佳性价比。典型应用场景包括生物医学成像、光纤通信和气体传感,其中光谱重叠优化、模场直径匹配和偏振特性协调是关键技术要点。通过实测数据对比不同组合的传输性能,可以为

weixin_33812433的博客 339

利用内置函数生成数据

4.1合计函数1 COUNT------- SELECT COUTN(*) FROM TEAMSTATS WHERE HITS/AS2 SUM --- 返回一列中所有数值之和  SELECT SUM(SINGLES) TOTAL_SINGLES FROM TEAMSTATS;3 AVG --- 计算一列的平均值 SELECT AVG(SO) “AVE_STRIKE_OUTS” FROM TEAMS

qxjavajava的专栏 1106

Django 前后端分离实战:网络设备运维系统 - 从登录认证到业务模块

这篇文章分享了Django+Vue前后端分离开发网络设备运维系统的实战经验,重点介绍了登录认证、全局布局和设备管理三大模块的实现。文章包含以下核心内容: 登录模块实现:包括表单验证、Token认证、记住密码功能,以及美观的UI设计(渐变背景、毛玻璃效果) 全局布局架构:采用侧边栏+顶部导航的响应式设计,支持菜单权限控制和多设备适配 业务功能开发:详细展示了设备分类和部门管理的CRUD功能实现,包括列表展示、搜索过滤、分页等功能 常见问题解决:提供了5个开发中常见问题的解决方案,如Vue Router警告、C

qq_16766705的博客 526

从零到飞:在Ubuntu 22.04上为你的无人机搭建PX4-Autopilot开发环境

本文详细指导在Ubuntu 22.04上搭建PX4-Autopilot开发环境,涵盖系统依赖安装、源码获取、编译目标选择及Gazebo仿真测试。通过优化构建过程和调试技巧,帮助开发者快速为无人机部署稳定的飞控系统,实现从仿真到实机的平滑过渡。

weixin_26878995的博客 490

Python 内置数据类型 02----列表

1. 列表 list 简介 Python 中的 list 类型,类似于数组,但是比数组功能更加强大。列表可以看作一排容器,这些容器按照下标 0、1、2.....排列,每个单独容器可以存放字符串、数值、布尔值、也可以存放列表、元组等。 1.1 使用 [] 直接创建列表 1.2 使用 list 函数创建列表

一分耕耘一分收获 6673

MobaXterm-SSH远程连接Ubuntu

我们常常在公司的服务器上面的虚拟机进行交叉编译,所以才需要我们用远程终端工具MobaXterm去接入虚拟机。然后把我们在window用VScode编辑好的代码拖拽上传到服务器,等待服务器编译完把生成的可执行文件从服务器拖拽下来,烧录到板子运行。

weixin_50257954的博客 3478

【python】python内置函数——range()创建一个整数列表

python内置函数——range()介绍

sinat_41752325的博客 4973

QGis软件 —— 9、QGis - 由点绘制热力图来模拟人流量(绘制点方式、excel导入数据方式)

QGis软件 —— 9、QGis - 由点绘制热力图来模拟人流量(绘制点方式、excel导入数据方式)

1472

ANSYS——“There is at least 1 small equation solver pivot term”问题的解决办法

ANSYS——“There is at least 1 small equation solver pivot term”问题的解决办法

python_AI_fans的博客 1万+

1.创建列表

首先,创建列表可以可以直接用方括号语法 test_list = [1, 2, 3] print(test_list) # 输出为[1, 2, 3] 除了以上的方法,Python还提供了一个内置的list()函数来创建列表,list()函数可用于将元组,区间(range)等对象转换为列表 a_tuple = ('tgh', 20, -1.2) # 将元组转换成列表 a_list = list(a...

NZGMTCAH的博客 2473

机器人环境仿真软件:Webots_(9).路径规划与导航算法仿真实现

通过仿真,我们可以验证路径规划算法的有效性和鲁棒性,同时优化机器人在不同环境中的导航性能。动态窗口法是一种局部路径规划算法,通过在机器人当前速度空间中选择一个最优的速度窗口,使机器人在动态环境中安全地移动。算法的代价函数通常包括两个部分:从起始点到当前节点的实际代价(g-cost)和从当前节点到目标节点的估计代价(h-cost)。在World Editor中,选择“Insert”菜单,添加“Robot”对象,如“E-puck”或“Thymio”。Dijkstra算法的实现与A*算法类似,但不使用启发函数。

kkchenjj的博客 2008

详解 Numpy.ndarray

NumPy支持比Python更多种类的数字类型,有5种基本数字类型:布尔值(bool)、整数(int)、无符号整数(uint)、浮点(float)、复数(complex) 1、np.array 数组 2、np.ndarray N维数组 一个 ndarray是具有相同类型和大小的项目的多维容器。 尺寸和数组中的项目的数量是由它的shape定义, 它是由N个非负整数组成的tuple(元组),用于指定每个维度的大小。 不同的是,ndarrays可以共享相同的数据, 因此在一个ndarray中进行的更改可能在另一个

绿色羽毛 5万+

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Python高阶——Numpy创建ndarray

Python高阶——Numpy创建ndarrayPython高阶——Numpy创建ndarray如何导入NumpyPython 列表和 NumPy 阵列有什么区别用np.array() 创建一个 ndarray对象创建数组1、np.empty()创建指定形状、数据类型,且没有初始化的数组2、np.zeros()创建一个全0数组3、np.ones()创建一个全1数组4、numpy.asarray ()从已有数组创建数组1、列表转ndarray:元组转ndarray元组列表转ndarray元组的元组转ndarr

sinat_42247418的博客 9258

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

「Python数据科学」一文掌握NumPy创建ndarray的常用方法

本文分别介绍了基于Python原生的序列创建NumPy数组、基于已存在的NumPy数组创建新的NumPy数组、NumPy内部函数创建特定数组、从磁盘数据创建NumPy数组和使用其他Python数据科学库的库函数创建NumPy数组的方法。

南宫理的日知录 1362

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

python中ndarray是什么意思_一起认识Python-ndarray

1.1 认识ndarray1、同构数据多维容器所有元素必须是相同类型的维数、形状、数据类型2、面向数组的编程和思维方式用简洁的数组表达式替代循环写法用数组表达式代替循环的做法,通常被称为矢量化。1.1.1 数据类型1.1.2 ndarray属性ndarray.ndim:数组的维数(即数组轴的个数)。ndarray.shape:数组的维数维度,返回一个元组,这个...

weixin_39528366的博客 1592

ndarray数组创建方法和转换

ndarray数组创建方法1、根据python中列表、元组中创建2、使用NumPy中函数创建ndarray数组3、使用NumPy中其他函数创建ndarray数组4、ndarray数组变换 1、根据python中列表、元组中创建 x = np.array(list\tuple) 例如:x = np.array([1,2,4,6]) y = np.array([5,4],[2,3],[2,8]) z = np.array((1,2,5,4)) 可以使用dtype指定数据元素类型

m0_46672151的博客 2137

python ndarray与pandas series相互转换,ndarray与dataframe相互转换

用python做科技计算时,经常需要类型转换,以下是常用类型转换 一、ndarray 转换为 series 1、如果ndarray是二维数组,如下 array([[1], [2], [3]]) 需要通过map结合lamdba import numpy as np import pandas as pd data = np.array([1, 2, 3])...

lovelife110的博客 5万+

ndarray数组的创建方法

ndarray数组的创建方法 本文参考自中国慕课网中北京理工大学教授讲授的《Python数据分析与展示》。 1、从Python中的列表、元祖等类型创建ndarray数组 “x=np.array(list/tuple,dtype=? )” 当np.array()不指定dtype时,numpy将根据数据情况关联一个dtype类型。 import numpy as np x=np.array([0,1,...

会飞的哼哧的博客 1596

NumPy基础之array创建ndarray多维数组

NumPy(Numerical Python)是一个python库,提供多维数组对象及其派生对象,以及用于数组快速操作的各种API。它运行速度快,用于数值计算,是python中科学计算的基础包。

sinat_34735632的博客 1354

python ndarray find_Python中Numpy ndarray的使用详解

python: numpy的ndarray和array有什么区别?为什么小编以为小编够坚强了,可还是敌不过你的执着你的冷酷。What is the difference between ndarray and array in Numpy? And where can I find the implementations in the numpy source code? (Numpy中ndarr...

weixin_39898854的博客 384

Python-numpy中ndarray对象创建,数据类型,基本属性

Python-numpy中ndarray对象创建,数据类型,基本属性

weixin_48668114的博客 1332

ndarray 的创建及属性

在调用 zeros() 函数生成多维数组时,要注意传入的是一个元组,元组的各个元素指明了多维数组各个维度的大小。上面使用 arange() 函数生成的是一维数组,我们可以在生成一维数组后对其进行重塑,这样便可以得到多维数组。在上面的例子中,当我们明确指定元素类型时,虽然元素的类型为整型,但是由于明确指定了浮点型,所以得到的元素类型为浮点型。在上面的代码中,调用 reshape() 函数对生成的一维数组进行重塑,便得到一个 3 行 4 列的二维数组。由于我们创建的是一维数组,所以代码中得到的维度数为 1。

shiwei0813的博客 1197
上一篇: Python笔记:详解使用Python列表创建ndarray
下一篇: Python笔记:操作ndarray元素:访问、删除、插入
Wang's Blog
博客等级 码龄12年 7440粉丝 2427原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang's Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值