numpy.ndarray相关

Slicing and Indexing(切片与索引)

  • The basic slice syntax is i:j:k where i is the starting index, j is
    the stopping index, and k is the step ( k!=0). This selects the m
    elements (in the corresponding dimension) with index values i,i + k,
    …, i + (m - 1) k where and q and r are the quotient and remainder
    obtained by dividing j - i by k: j - i = q k + r, so that i + (m - 1)
    k < j
  • Negative i and j are interpreted as n + i and n + j where n is the number of elements in the corresponding dimension. Negative k makes stepping go towards smaller indices.
  • :代表选中该轴的所有数据
    -
arr = np.array([(2,3),(5,4),(9,6),(4,7),(8,1),(7,2)])

#获取第一列
fisrt_col = arr[:, 0]
#获取第二列
second_col = arr[:, 1]
ndarray是Python中的一种多维数组,可以用来存储和操作大量的数值数据。它具有许多有用的属性和方法,可以让用户更容易地处理数据。`numpy.ndarray` 是 NumPy 库中的一个类,用于表示 N 维数组(即多维数组)。它是 NumPy 的核心数据结构之一,提供了许多高效的操作,例如数组索引、切片、算术运算、数学函数等等。 在使用 `numpy.ndarray` 时,通常需要先导入 NumPy 库,然后通过 `numpy.array()` 函数将 Python 列表或元组转换成 N 维数组,例如: ```python import numpy as np # 从列表创建一个一维数组 a = np.array([1, 2, 3]) print(a) # 输出:[1 2 3] # 从元组创建一个二维数组 b = np.array([(1, 2, 3), (4, 5, 6)]) print(b) # 输出: # [[1 2 3] # [4 5 6]] ``` 可以通过 `ndarray.shape` 属性获取数组的维度信息,通过 `ndarray.dtype` 属性获取数组元素的数据类型,例如: ```python # 获取数组的形状 print(a.shape) # 输出:(3,) print(b.shape) # 输出:(2, 3) # 获取数组元素的数据类型 print(a.dtype) # 输出:int64 print(b.dtype) # 输出:int64 ``` 还可以使用各种方法对数组进行操作,例如: ```python # 数组加法 c = a + b print(c) # 输出: # [[2 4 6] # [5 7 9]] # 数组乘法 d = a * b print(d) # 输出: # [[ 1 4 9] # [ 4 10 18]] # 数组转置 e = b.T print(e) # 输出: # [[1 4] # [2 5] # [3 6]] ``` 这些只是 `numpy.ndarray` 类提供的一些基本功能,NumPy 库还有更多高级的特性和函数可以探索。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值