- 教程
- 介绍
- 环境
- Ndarray 对象(Ndarray Object)
- 数据类型
- 数组属性(Array Attributes)
- 阵列创建例程(Array Creation Routines)
- 来自现有数据的数组(Array from Existing Data)
- 数值范围中的数组(Array From Numerical Ranges)
- 数值范围中的数组(Array From Numerical Ranges)
- 数值范围中的数组(Array From Numerical Ranges)
- 广播(Broadcasting)
- 迭代数组(Iterating Over Array)
- 数组操作(Array Manipulation)
- Binary 运算符
- 字符串函数(String Functions)
- 数学函数(Mathematical Functions)
- 算术运算(Arithmetic Operations)
- 统计函数(Statistical Functions)
- 统计函数(Statistical Functions)
- 字节交换(Byte Swapping)
- 副本和视图(Copies & Views)
- 矩阵库(Matrix Library)
- 线性代数(Linear Algebra)
- Matplotlib(Matplotlib)
- 使用Matplotlib的直方图(Histogram Using Matplotlib)
- I/O with NumPy
- 有用的资源
- reshape
- flat
- flatten
- ravel
- transpose
- ndarray.T
- rollaxis
- swapaxes
- broadcast
- broadcast_to
- expand_dims
- squeeze
- concatenate
- stack
- hstack
- vstack
- split
- hsplit
- vsplit
- resize
- append
- insert
- delete
- unique
- bitwise_and
- bitwise_or
- invert
- left_shift
- right_shift
- add()
- multiply()
- center()
- capitalize()
- title()
- lower()
- upper()
- split()
- splitlines()
- strip()
- join()
- replace()
- decode()
- encode()
- dot
- vdot
- inner
- matmul
- determinant
- solve
- inv
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
unique
此函数返回输入数组中的唯一元素数组。 该函数可以返回唯一值数组的数组和关联索引数组。 索引的性质取决于函数调用中返回参数的类型。
numpy.unique(arr, return_index, return_inverse, return_counts)
Where,
Sr.No. | 参数和描述 |
---|---|
1 | arr 输入数组。 如果不是一维阵列,将会变平 |
2 | return_index 如果为True,则返回输入数组中元素的索引 |
3 | return_inverse 如果为True,则返回唯一数组的索引,该索引可用于重建输入数组 |
4 | return_counts 如果为True,则返回唯一数组中的元素出现在原始数组中的次数 |
例子 (Example)
import numpy as np
a = np.array([5,2,6,2,7,5,6,8,2,9])
print 'First array:'
print a
print '\n'
print 'Unique values of first array:'
u = np.unique(a)
print u
print '\n'
print 'Unique array and Indices array:'
u,indices = np.unique(a, return_index = True)
print indices
print '\n'
print 'We can see each number corresponds to index in original array:'
print a
print '\n'
print 'Indices of unique array:'
u,indices = np.unique(a,return_inverse = True)
print u
print '\n'
print 'Indices are:'
print indices
print '\n'
print 'Reconstruct the original array using indices:'
print u[indices]
print '\n'
print 'Return the count of repetitions of unique elements:'
u,indices = np.unique(a,return_counts = True)
print u
print indices
其输出如下 -
First array:
[5 2 6 2 7 5 6 8 2 9]
Unique values of first array:
[2 5 6 7 8 9]
Unique array and Indices array:
[1 0 2 4 7 9]
We can see each number corresponds to index in original array:
[5 2 6 2 7 5 6 8 2 9]
Indices of unique array:
[2 5 6 7 8 9]
Indices are:
[1 0 2 0 3 1 2 4 0 5]
Reconstruct the original array using indices:
[5 2 6 2 7 5 6 8 2 9]
Return the count of repetitions of unique elements:
[2 5 6 7 8 9]
[3 2 2 1 1 1]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论