博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
numpy 使用详解
阅读量:5331 次
发布时间:2019-06-14

本文共 3690 字,大约阅读时间需要 12 分钟。

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 2array([[1, 2],       [3, 4],       [5, 6]])

 

numpy.transpose(a, axes=None)

ndarray.transpose(*axes)

numpy.ndarray.T

  • 返回转置后的数组,原数组不变
>>> a = np.array([[1, 2], [3, 4]])>>> aarray([[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])>>> xarray([ 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)>>> xarray([-2, -1,  0,  1,  2])>>> np.flatnonzero(x)array([0, 1, 3, 4])

 

 


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

  •  返回随机数
>>> 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)

  • 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]])>>> xarray([[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)

  • Returns the indices of the maximum values along an axis.
>>> a = np.arange(6).reshape(2,3)>>> aarray([[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)

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


numpy.array_split

>>> x = np.arange(8.0)>>> np.array_split(x, 3)    [array([ 0.,  1.,  2.]), array([ 3.,  4.,  5.]), array([ 6.,  7.])]

 


 

numpy.hstack

Take a sequence of arrays and stack them horizontally to make a single array.

Equivalent to np.concatenate(tup, axis=1) if tup contains arrays that are at least 2-dimensional.

>>> a = np.array((1,2,3))>>> b = np.array((2,3,4))>>> np.hstack((a,b))array([1, 2, 3, 2, 3, 4])>>> a = np.array([[1],[2],[3]])>>> b = np.array([[2],[3],[4]])>>> np.hstack((a,b))array([[1, 2],       [2, 3],       [3, 4]])

 

转载于:https://www.cnblogs.com/irran/p/7794396.html

你可能感兴趣的文章
HDU 1712 ACboy needs your help (分组背包模版题)
查看>>
共享内存
查看>>
从零开始学JavaWeb
查看>>
Tomcat源码浅析
查看>>
Codeforces Round #256 (Div. 2) Multiplication Table
查看>>
计算三球交点坐标的快速算法
查看>>
HDU 1269 迷宫城堡
查看>>
my_ls-ailh
查看>>
Extjs介绍(二)
查看>>
jQuery中$.ajax知识点总结
查看>>
微信小程序开发7-JavaScript脚本
查看>>
leetcode-78-子集
查看>>
LINUX进程小结
查看>>
公告会看门道:四个不同的厨师和史蒂夫·乔布斯
查看>>
HDU 1983 BFS&amp;&amp;DFS
查看>>
c++开源项目汇总
查看>>
python yield返回多个值
查看>>
每日站立会议及今日份任务
查看>>
R12 付款过程请求-功能和技术信息 (文档 ID 1537521.1)
查看>>
洛谷 4364 [九省联考2018]IIIDX
查看>>