enumerate(),flatnonzero(),argsort(),array_split(),numpy.random.choice()
2018-02-13 09:37
429 查看
1. enumerate()函数
是python的内置枚举函数对于一个可迭代的(iterable)/可遍历的对象(如列表、字符串),enumerate将其组成一个索引序列,利用它可以同时获得索引和值enumerate多用于在for循环中得到计数
使用:[python] view plain copylist1 = ["这", "是", "一个", "测试"]
for index, item in enumerate(list1):
print index, item
[python] view plain copy>>>
0 这
1 是
2 一个
3 测试
2.flatnonzero()
该函数输入一个矩阵,返回扁平化后矩阵中非零元素的位置(index)这是官方文档给出的用法,非常正规,输入一个矩阵,返回了其中非零元素的位置[python] view plain copy>>> x = np.arange(-2, 3)
[python] view plain copy>>> x
array([-2, -1, 0, 1, 2])
>>> np.flatnonzero(x)
array([0, 1, 3, 4])
特别地,用来返回某个特定元素的位置对向量元素的判断d==3返回了一个和向量等长的由0/1组成的矩阵,然后调用函数,返回的位置,就是对应要找的元素的位置。[python] view plain copyd = np.array([1,2,3,4,4,3,5,3,6])
find_d = np.flatnonzero(d == 3)
print find_d
>>>
[2,5,7]
3. argsort()函数
argsort函数返回的是数组值从小到大的索引值[python] view plain copy>>> x = np.array([3, 1, 2])>>> np.argsort(x) #按升序排列
array([1, 2, 0])
>>> np.argsort(-x) #按降序排列
array([0, 2, 1])
[python] view plain copy>>> x[np.argsort(x)] #通过索引值排序后的数组
array([1, 2, 3])
>>> x[np.argsort(-x)]
array([3, 2, 1])
[python] view plain copy另一种方式实现按降序排序:
>>> a = x[np.argsort(x)]
>>> a
array([1, 2, 3])
>>> a[::-1]
array([3, 2, 1])
当然,如果想排序不需要这样,可以直接使用sort()和sorted()函数:sort()改变原列表[python] view plain copy>>> a = [1,2,1,4,3,5]
>>> a.sort()
>>> a
[1, 1, 2, 3, 4, 5]
sorted()不改变原列表[python] view plain copy>>> a = [1,2,1,4,3,5]
>>> sorted(a)
[1, 1, 2, 3, 4, 5]
>>> a
[1, 2, 1, 4, 3, 5]
4. array_split()函数
将列表拆分为几份首先是spilt()使用:[python] view plain copy>>> x = np.arange(9.0)>>> np.split(x, 3)
[array([ 0., 1., 2.]), array([ 3., 4., 5.]), array([ 6., 7., 8.])]
[python] view plain copy>>> x = np.arange(8.0)
>>> np.split(x, [3, 5, 6, 10])
[array([ 0., 1., 2.]), array([ 3., 4.]), array([ 5.]), array([ 6., 7.]), array([], dtype=float64)]
array_split():
功能与split一样,唯一的区别是 array_split() 允许不均等地拆分原列表:[python] view plain copy<span style="font-size:14px;">>>> x = np.arange(8.0)
>>> np.array_split(x, 3)
[array([ 0., 1., 2.]), array([ 3., 4., 5.]), array([ 6., 7.])]</span>
numpy.random.choice
numpy.random.choice(a, size=None, replace=True, p=None)Generates a random sample from a given 1-D arrayNew in version 1.7.0.Parameters: | a : 1-D array-like or int(左闭右开区间)If an ndarray, a random sample is generated from its elements. If an int, the random sample is generated as if a was np.arange(n)size : int or tuple of ints, optionalOutput shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.replace : boolean, optionalWhether the sample is with or without replacement(有无重复元素)p : 1-D array-like, optional(各个元素出现概率)The probabilities associated with each entry in a. If not given the sample assumes a uniform distribution over all entries in a. |
---|---|
Returns: | samples : 1-D ndarray, shape (size,)The generated random samples |
Raises: | ValueErrorIf a is an int and less than zero, if a or p are not 1-dimensional, if a is an array-like of size 0, if p is not a vector of probabilities, if a and p have different lengths, or if replace=False and the sample size is greater than the population size |
相关文章推荐
- numpy中的random.choice()函数
- numpy.random.choice 中replace 的含义
- numpy.random.choice
- [numpy]split()和array_split()
- numpy.random.choice和zeros的用法
- [numpy]random.choice()随机选取内容
- [numpy]split()和array_split()
- [numpy]random.choice()随机选取内容
- Whats the best way to split an array in ruby into multiple smaller arrays of random size
- numpy.random.seed()方法的作用
- 算法练习(11) —— Split Array Largest Sum
- Numpy的array的布尔型索引与赋值
- numpy的random模块
- leetcode 659. Split Array into Consecutive Subsequences 分割连续序列 + 贪心算法
- leetcode 410 Split Array Largest Sum
- numpy、pandas包List, Dict, Array, Series, DataFrame 相互转换
- ValueError: setting an array element with a sequence. tensorflow numpy
- numpy中matrix 和 array的区别
- NumPy提供了一些使用占位符创建数组的函数#split_img.py
- 关于Java中使用split()方法时报ArrayIndexOutOfBoundsException数组越界异常解决方法