gpt4 book ai didi

python - dtype 对象不支持 unique 的 axis 参数

转载 作者:行者123 更新时间:2023-11-30 22:05:25 25 4
gpt4 key购买 nike

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

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

虽然我收到这样的错误:

TypeError: The axis argument to unique is not supported for dtype object

如何解决这个问题?

样本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]

最佳答案

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

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)

输出:

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

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

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

为此,您的数组必须不同

关于python - dtype 对象不支持 unique 的 axis 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53020609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com