python -- numpy 基本数据类型,算术运算,组合,分割 函数

本文详细介绍NumPy数组的基础概念,包括数组的创建、属性、数据类型及常见操作。同时介绍了数组的索引、切片和迭代,形状操作以及数组的组合与分割方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0 NumPy数组

NumPy数组:NumPy数组是一个多维数组对象,称为ndarray。其由两部分组成:
实际的数据
描述这些数据的元数据

NumPy数组属性:
ndim(纬数,x,y 2),shape(纬度,2*3),reshape(纬度),size:元素个数,dtype:元素数据类型,itemsize:所有元素的字节大小
创建数组:
使用array函数, a = array( [2,3,4] ), b = array( [ (1.5,2,3), (4,5,6) ] )  
可以在创建时显式指定数组中元素的类型c = array( [ [1,2], [3,4] ], dtype=complex)
d = zeros((3,4))  
ones( (2,3,4), dtype=int16 ) #手动指定数组中元素类型
empty((2,3)) 
full((2,3),8)
NumPy提供一个类似arange的函数返回一个数列形式的数组:
arange(10, 30, 5)
array([10, 15, 20, 25])
arange(0,2,0.5)
array([ 0. , 0.5, 1. , 1.5])
a = array([1,2,3,4])
a2 = array([1,2,3,4],[1,2,3,4],[1,2,3,4])

np.ones((2,3))
np.zeeros((2,4))
np.full((2,2),8)

1 NumPy中的基本数据类型
名称 描述
bool 用一个字节存储的布尔类型(True或False)
inti 由所在平台决定其大小的整数(一般为int32或int64)
int8 一个字节大小,-128 至 127
int16 整数,-32768 至 32767
int32 整数,-2 ** 31 至 2 ** 32 -1
int64 整数,-2 ** 63 至 2 ** 63 - 1
uint8 无符号整数,0 至 255
uint16 无符号整数,0 至 65535
uint32 无符号整数,0 至 2 ** 32 - 1
uint64 无符号整数,0 至 2 ** 64 - 1
float16 半精度浮点数:16位,正负号1位,指数5位,精度10位
float32 单精度浮点数:32位,正负号1位,指数8位,精度23位
float64或float 双精度浮点数:64位,正负号1位,指数11位,精度52位
complex64 复数,分别用两个32位浮点数表示实部和虚部
complex128或complex 复数,分别用两个64位浮点数表示实部和虚部

输出数组

2 NumPy数组2

数组的操作:数组的算术运算是按元素逐个运算。数组运算后将创建包含运算结果的新数组,有些操作符如+=和*=用来更改已存在数组而不创建一个新的数组。
基本运算:+,-,*./ 按元素逐个计算
索引切片和迭代:和列表和其它Python序列一样,一维数组可以进行索引、切片和迭代操作。 a[2],a[2:5], a[: :-1] # 反转a
a[:6:2]= -1000 # 等同于a[0:6:2]= -1000,从开始到第6个位置,每隔一个元素将其赋值为-1000
for i in a:
print i**(1/3.)

多维数组可以每个轴有一个索引。这些索引由一个逗号分割的元组给出。 b[0:5, 1]

形状shape操作
更改数组的形状: a.ravel() # 平坦化数组


3 自定义结构数组
student= dtype({'names':['name', 'age', 'weight'], 'formats':['S32', 'i','f']}, align = True)
a= array([(“Zhang”, 32, 65.5), (“Wang”, 24, 55.2)], dtype =student)

组合函数: 2 * a
水平组合:hstack((a, b)) ,也可通过concatenate函数并指定相应的轴来获得这一效果:concatenate((a, b), axis=1)
垂直组合: vstack((a, b))
深度组合: dstack((a, b)) 数组的第三个轴(即深度)上组合
行组合:row_stack((one, two)),每一行进行组合
列组合:column_stack((oned,two))
分割数组:在NumPy中,分割数组的函数有hsplit、vsplit、dsplit和split。可将数组分割成相同大小的子数组,或指定原数组分割的位置

水平分割:hsplit(a, 3),split(a, 3, axis=1)
垂直分割:vsplit(a, 3) ,也可通过split函数并指定轴为1来获得这样的效果:split(a, 3, axis=0)
面向深度的分割:dsplit(c, 3)

复制和镜像View
完全不复制:
简单的赋值,而不复制数组对象或它们的数据。
视图view和浅复制:
c = a.view() 切片数组返回它的一个视图,不同的数组对象分享同一个数据。视图方法创造一个新的数组对象指向同一数据。
深复制:
d = a.copy() 这个复制方法完全复制数组和它的数据。

更多资料请参考:

http://blog.youkuaiyun.com/sunny2038/article/details/9023797

转载于:https://www.cnblogs.com/csj007523/p/7391008.html

Rebuild started: Project: Project *** Using Compiler 'V6.22', folder: 'E:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值