numpy 使用详解

numpy.arange([start, ]stop, [step, ]dtype=None)

  • 返回数值均匀分布的数组
>>> np.arange(3)
array([0, 1, 2])
>>> np.arange(3,7)
array([3, 4, 5, 6])
>>> np.arange(3,7,2)
array([3, 5]


numpy.reshape(a, newshape, order=‘C‘)

ndarray.reshape(shape, order=‘C‘)

  • 返回形状调整后的数组,原数组不变
  • newshape 中可以有一个维度为-1,表明这个维度的大小会根据数组长度和其他维度大小进行推断
>>> a = np.array([[1,2,3], [4,5,6]])
>>> np.reshape(a, 6)
array([1, 2, 3, 4, 5, 6])>>> np.reshape(a, (3,-1))       # the unspecified value is inferred to be 2
array([[1, 2],
       [3, 4],
       [5, 6]])


numpy.transpose(a, axes=None)

ndarray.transpose(*axes)

numpy.ndarray.T

  • 返回转置后的数组,原数组不变
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>> x = np.arange(24).reshape((2,3,4))
>>> x.shape
(2,3,4)
>>> x.transpose(1,0,2).shape
(3,2,4)


ndarray.astype(dtype, order=‘K‘, casting=‘unsafe‘, subok=True, copy=True)

  • 更改数组的数据类型
>>> x = np.array([1, 2, 2.5])
>>> x
array([ 1. ,  2. ,  2.5])
>>> x.astype(int)
array([1, 2, 2])


numpy.concatenate((a1, a2, ...), axis=0)

  • 拼接数组
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
       [3, 4, 6]])...>>> x = [np.arange(5) for i in range(5)]>>> x[array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])]>>> np.concatenate(x)array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4])


numpy.flatnonzero(a)

  • Return indices that are non-zero in the flattened version of a
>>> x = np.arange(-2, 3)
>>> x
array([-2, -1,  0,  1,  2])
>>> np.flatnonzero(x)
array([0, 1, 3, 4])


numpy.random.choice(a, size=None, replace=True, p=None)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.choice.html#numpy.random.choice

  • 返回随机数
>>> np.random.choice(5, 3)
array([0, 3, 4])
>>> np.random.choice(5, 3, p=[0.1, 0, 0.3, 0.6, 0])
array([3, 3, 0])
>>> np.random.choice(5, 3, replace=False)
array([3,1,0])...>>> a = np.arange(5)>>> np.random.choice(a,10)array([3, 4, 2, 0, 3, 2, 4, 2, 0, 2])


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

https://docs.scipy.org/doc/numpy/reference/generated/numpy.argsort.html#numpy.argsort

  • 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.
>>> x = np.array([3, 1, 2])
>>> np.argsort(x)
array([1, 2, 0])

>>> 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]])


numpy.argmax(a, axis=None, out=None)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.argmax.html#numpy.argmax

  • Returns the indices of the maximum values along an axis.
>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.argmax(a)
5
>>> np.argmax(a, axis=0)
array([1, 1, 1])
>>> np.argmax(a, axis=1)
array([2, 2])


numpy.bincount(x, weights=None, minlength=0)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html#numpy.bincount

  • Count number of occurrences of each value in array of non-negative ints.
>>> np.bincount(np.arange(5))
array([1, 1, 1, 1, 1])
>>> np.bincount(np.array([0, 1, 1, 3, 2, 1, 7]))
array([1, 3, 1, 1, 0, 0, 0, 1])


numpy.sum(a, axis=None, dtype=None, out=None, keepdims=<class numpy._globals._NoValue>)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html#numpy.sum


时间: 2024-08-07 06:58:11

numpy 使用详解的相关文章

Numpy用法详解

1.NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组.用于对整组数据进行快速运算的标准数学函数(无需编写循环).用于读写磁盘数据的工具以及用于操作内存映射文件的工具.线性代数.随机数生成以及傅里叶变换功能.用于集成C.C++.Fortran等语言编写的代码的工具. 首先要导入numpy库:import numpy as np A NumPy函数和属性: 表2.1.A.1 NumPy类型 表2.1.

Heapsort 堆排序算法详解(Java实现)

Heapsort (堆排序)是最经典的排序算法之一,在google或者百度中搜一下可以搜到很多非常详细的解析.同样好的排序算法还有quicksort(快速排序)和merge sort(归并排序),选择对这个算法进行分析主要是因为它用到了一个非常有意思的算法技巧:数据结构 - 堆.而且堆排其实是一个看起来复杂其实并不复杂的排序算法,个人认为heapsort在机器学习中也有重要作用.这里重新详解下关于Heapsort的方方面面,也是为了自己巩固一下这方面知识,有可能和其他的文章有不同的入手点,如有错

人脸验证算法Joint Bayesian详解及实现(Python版)

人脸验证算法Joint Bayesian详解及实现(Python版) Tags: JointBayesian DeepLearning Python 本博客仅为作者记录笔记之用,不免有很多细节不对之处. 还望各位看官能够见谅,欢迎批评指正. 博客虽水,然亦博主之苦劳也. 如对代码有兴趣的请移步我的 Github. 如需转载,请附上本文链接,不甚感激!  http://blog.csdn.net/cyh_24/article/details/49059475 Bayesian Face Revis

DeepLearning tutorial(3)MLP多层感知机原理简介+代码详解

DeepLearning tutorial(3)MLP多层感知机原理简介+代码详解 @author:wepon @blog:http://blog.csdn.net/u012162613/article/details/43221829 本文介绍多层感知机算法,特别是详细解读其代码实现,基于python theano,代码来自:Multilayer Perceptron,如果你想详细了解多层感知机算法,可以参考:UFLDL教程,或者参考本文第一部分的算法简介. 经详细注释的代码:放在我的gith

DeepLearning tutorial(4)CNN卷积神经网络原理简介+代码详解

DeepLearning tutorial(4)CNN卷积神经网络原理简介+代码详解 @author:wepon @blog:http://blog.csdn.net/u012162613/article/details/43225445 本文介绍多层感知机算法,特别是详细解读其代码实现,基于python theano,代码来自:Convolutional Neural Networks (LeNet).经详细注释的代码和原始代码:放在我的github地址上,可下载. 一.CNN卷积神经网络原理

DeepLearning tutorial(1)Softmax回归原理简介+代码详解

DeepLearning tutorial(1)Softmax回归原理简介+代码详解 @author:wepon @blog:http://blog.csdn.net/u012162613/article/details/43157801 本文介绍Softmax回归算法,特别是详细解读其代码实现,基于python theano,代码来自:Classifying MNIST digits using Logistic Regression,参考UFLDL. 一.Softmax回归简介 关于算法的详

AdaBoost算法详解与实战

[原创]Liu_LongPo 转载请注明出处 [CSDN]http://blog.csdn.net/llp1992 AdaBoost算法是基于单层决策树等弱分类算法的强学习分类算法.单层决策树算法也是一种分类算法,但是其分类效果较差,只根据一个特征进行数据划分,因此单层决策树算法被称为弱分类算法:而AdaBoost算法通过将多个弱分类算法串行训练而成,属于强分类算法. AdaBoost算法是boosting算法的一种,它所串联的弱分类器一般都是一致的,而且它训练是的关注点在于被之前分类器分错的数

Linux 包管理工具 pip 使用详解

1. pip install 安装软件 [[email protected] ~]$ pip install somepackage [...] Successfully installed somepackage 参数:--upgrade 升级某包 2. pip show 查看已安装的软件(以matplotlib为例子) [[email protected] ~]$ pip show matplotlib --- Metadata-Version: 1.1 Name: matplotlib V

机器学习经典算法详解及Python实现--CART分类决策树、回归树和模型树

摘要: Classification And Regression Tree(CART)是一种很重要的机器学习算法,既可以用于创建分类树(Classification Tree),也可以用于创建回归树(Regression Tree),本文介绍了CART用于离散标签分类决策和连续特征回归时的原理.决策树创建过程分析了信息混乱度度量Gini指数.连续和离散特征的特殊处理.连续和离散特征共存时函数的特殊处理和后剪枝:用于回归时则介绍了回归树和模型树的原理.适用场景和创建过程.个人认为,回归树和模型树