您可以使用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)
输出:
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)