numpy基础——numpy.argsort

本文详细介绍了 numpy 中的 argsort 函数,该函数能够返回数组排序后的索引,并且可以根据需求选择不同的排序算法。适用于一维及多维数组的排序需求。

numpy.argsort

numpy.argsort(a, axis=-1, kind=’quicksort’, order=None)

Returns the indices that would sort an array.
返回排序数组的索引。


Perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as a that index data along the given axis in sorted order.
使用kind关键字指定的算法沿给定轴进行间接排序。 它按照排序的顺序返回与给定轴的索引数据相同形状的索引数组。


Parameters:

a : array_like
Array to sort.

axis : int or None, optional
Axis along which to sort. The default is -1 (the last axis). If None, the flattened array is used.

kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, optional
Sorting algorithm.

order : str or list of str, optional
When a is an array with fields defined, this argument specifies which fields to compare first, second, etc. A single field can be specified as a string, and not all fields need be specified, but unspecified fields will still be used, in the order in which they come up in the dtype, to break ties.

Returns:

index_array : ndarray, int
Array of indices that sort a along the specified axis. If a is one-dimensional, a[index_array] yields a sorted a.


Examples

One dimensional array:

返回的结果为 输入数组 元素从小到大 的索引

>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])

Two-dimensional array:

>>> x = np.array([[0, 3], [2, 2]])

>>> x
array([[0, 3],
       [2, 2]])

>>> np.argsort(x, axis=0)
array([[0, 1],
       [1, 0]])

>>> np.argsort(x, axis=1)
array([[0, 1],
       [0, 1]])
### 特征向量的维度分析 在给定 `normTrainFaceMat` 的形状为 \( M \times N \),其中 \( M \) 表示样本数量,\( N \) 表示每个样本的特征数。通过调用 `numpy.transpose(normTrainFaceMat)` 方法,原始矩阵被转置,其形状变为 \( N \times M \)[^1]。 对于 PCA(主成分分析),通常会计算协方差矩阵并求解该矩阵的特征值和特征向量。假设我们已经得到了前 \( k \) 个最大的特征值对应的特征向量组成的矩阵 `topk_evecs`,那么它的形状应为 \( N \times k \)[^2]。这意味着每列是一个特征向量,共有 \( k \) 列。 ### numpy.transpose(normTrainFaceMat) 当执行 `numpy.transpose(normTrainFaceMat)` 后,原矩阵中的每一行变成了新矩阵的一列,因此如果原来的矩阵大小是 \( M \times N \),则经过转置后的矩阵大小将是 \( N \times M \)[^1]。 ### topk_evecs 的维度 如前所述,在完成降维操作之后得到的 `topk_evecs` 是由最重要的 \( k \) 个特征向量构成的一个子集矩阵。由于这些特征向量是从具有 \( N \) 维度的空间中提取出来的,所以此矩阵的高度仍然是 \( N \),宽度取决于选取了多少个主要方向来表示数据——也就是 \( k \) 值。最终结果就是 `topk_evecs` 将会有 \( N \times k \) 这样的尺寸[^2]。 ### Eigenface 计算过程 Eigenfaces 技术的核心思想基于PCA技术应用于面部图像识别领域。以下是具体步骤: 1. **标准化训练脸部图片集合**:首先将所有的脸像调整到相同的像素分辨率,并将其展平成一维向量形式存入矩阵 `trainFaceMat` 中。 2. **中心化处理**:减去平均人脸向量以消除偏移影响,形成新的矩阵 `normTrainFaceMat`[ ^3 ]. 3. **构建协方差矩阵**:利用公式 \( C = \frac{1}{M}A^{T}A \), 其中 A 即为中心化的 face matrix (`normTrainFaceMat`) ,从而获得反映整体变化模式的相关性结构的信息。 4. **求取特征值与特征向量**:对上述所得协方差矩阵做谱分解(Spectral Decomposition),找出对应最大几个特征值下的正交基底作为投影轴线;这一步骤产生的正是所谓的 eigenfaces 或者说 principal components(PCs). 5. **选择Top K PCs 构建映射空间**:挑选出贡献率较高的若干个PC组合起来创建低维嵌入空间(topK_EVecs)用于后续分类任务或者重建目的。 6. **测试阶段应用**:对待测未知样本人物同样按照前述流程预处理后再投射至这个已建立好的特征面上比较距离远近实现匹配判断等功能。 ```python import numpy as np # Example of creating an eigenface-like structure with random data. def create_random_data(): normTrainFaceMat = np.random.rand(100, 1024) # Assume we have 100 faces each flattened to 1024 pixels cov_matrix = np.cov(np.transpose(normTrainFaceMat)) evals, evecs = np.linalg.eig(cov_matrix) # Selecting the first 'k' eigenvectors corresponding to largest eigenvalues k = 10 idx = evals.argsort()[::-1][:k] topk_evecs = evecs[:,idx] create_random_data() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值