| unique(self) | Return the ``Categorical`` which ``categories`` and ``codes`` are | unique.
时间: 2023-10-17 16:07:06 浏览: 36
`unique(self)` is a method of the `Categorical` class in pandas library. It returns a new `Categorical` object containing only the unique categories and codes of the original `Categorical` object.
For example, consider a `Categorical` object with the following categories and codes:
```
categories = ['a', 'b', 'c', 'a']
codes = [0, 1, 2, 0]
cat = pd.Categorical(categories, codes)
```
Calling `unique()` on `cat` will return a new `Categorical` object with unique categories and codes:
```
pd.unique(cat)
```
Output:
```
['a', 'b', 'c']
Categories (3, object): ['a', 'b', 'c']
```
相关问题
torch.distributions.Categorical
torch.distributions.categorical是PyTorch中的一个概率分布模块,用于生成分类分布。
该模块包含了一个Categorical类,可以用来创建分类分布对象。分类分布用于生成从一组离散概率分布中选择的随机样本。Categorical类的构造函数需要一个1-D张量probs,其中每个元素都是该类别被选中的概率。可以使用这个类的sample()方法生成从这个分布中采样的值,或者使用log_prob()方法计算一个或多个给定值的对数概率。
例如,以下代码创建了一个包含3个类别的分类分布,其中第一个类别的概率为0.5,第二个和第三个类别的概率分别为0.25:
```python
import torch
probs = torch.tensor([0.5, 0.25, 0.25])
categorical_dist = torch.distributions.categorical.Categorical(probs=probs)
```
可以使用sample()方法从分类分布中生成一个样本:
```python
sample = categorical_dist.sample()
```
可以使用log_prob()方法计算样本的对数概率:
```python
log_prob = categorical_dist.log_prob(sample)
```
这里的样本是一个从分类分布中随机生成的整数,它的值介于0和2之间,对应于分布中的三个类别。
torch.distributions.categorical
torch.distributions.categorical是PyTorch中的一个概率分布模块,用于生成分类分布。
该模块包含了一个Categorical类,可以用来创建分类分布对象。分类分布用于生成从一组离散概率分布中选择的随机样本。Categorical类的构造函数需要一个1-D张量probs,其中每个元素都是该类别被选中的概率。可以使用这个类的sample()方法生成从这个分布中采样的值,或者使用log_prob()方法计算一个或多个给定值的对数概率。
例如,以下代码创建了一个包含3个类别的分类分布,其中第一个类别的概率为0.5,第二个和第三个类别的概率分别为0.25:
```python
import torch
probs = torch.tensor([0.5, 0.25, 0.25])
categorical_dist = torch.distributions.categorical.Categorical(probs=probs)
```
可以使用sample()方法从分类分布中生成一个样本:
```python
sample = categorical_dist.sample()
```
可以使用log_prob()方法计算样本的对数概率:
```python
log_prob = categorical_dist.log_prob(sample)
```
这里的样本是一个从分类分布中随机生成的整数,它的值介于0和2之间,对应于分布中的三个类别。