dtype obj不支持unique的轴参数

2024-06-16 08:26:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试按列获取唯一计数,但我的数组有分类变量(dtype对象)

val, count = np.unique(x, axis=1, return_counts=True)

虽然我遇到了这样的错误:

^{pr2}$

我怎么解决这个问题?在

样品x:

array([[' Private', ' HS-grad', ' Divorced'],
       [' Private', ' 11th', ' Married-civ-spouse'],
       [' Private', ' Bachelors', ' Married-civ-spouse'],
       [' Private', ' Masters', ' Married-civ-spouse'],
       [' Private', ' 9th', ' Married-spouse-absent'],
       [' Self-emp-not-inc', ' HS-grad', ' Married-civ-spouse'],
       [' Private', ' Masters', ' Never-married'],
       [' Private', ' Bachelors', ' Married-civ-spouse'],
       [' Private', ' Some-college', ' Married-civ-spouse']], dtype=object)

需要以下计数:

for x_T in x.T:
    val, count = np.unique(x_T, return_counts=True)
    print (val,count)


[' Private' ' Self-emp-not-inc'] [8 1]
[' 11th' ' 9th' ' Bachelors' ' HS-grad' ' Masters' ' Some-college'] [1 1 2 2 2 1]
[' Divorced' ' Married-civ-spouse' ' Married-spouse-absent'
 ' Never-married'] [1 6 1 1]

Tags: returncountnpvalprivate计数hsunique
1条回答
网友
1楼 · 发布于 2024-06-16 08:26:30

您可以使用Itemfreq event,尽管它的输出看起来不像您的,但它提供了所需的计数:

import numpy as np
from scipy.stats import itemfreq

x = np. array([[' Private', ' HS-grad', ' Divorced'],
       [' Private', ' 11th', ' Married-civ-spouse'],
       [' Private', ' Bachelors', ' Married-civ-spouse'],
       [' Private', ' Masters', ' Married-civ-spouse'],
       [' Private', ' 9th', ' Married-spouse-absent'],
       [' Self-emp-not-inc', ' HS-grad', ' Married-civ-spouse'],
       [' Private', ' Masters', ' Never-married'],
       [' Private', ' Bachelors', ' Married-civ-spouse'],
       [' Private', ' Some-college', ' Married-civ-spouse']], dtype=object)

itemfreq(x)

输出:

^{pr2}$

否则,您可以尝试指定其他数据类型,例如:

val, count = np.unique(x.astype("<U22"), axis=1, return_counts=True)

但是你的阵型必须不同

相关问题 更多 >