擅长:python、mysql、java
<p>如果您有一个字符串或其他对象的向量,并且希望给它分类标签,那么可以使用<code>Factor</code>类(在<code>pandas</code>命名空间中可用):</p>
<pre><code>In [1]: s = Series(['single', 'touching', 'nuclei', 'dusts', 'touching', 'single', 'nuclei'])
In [2]: s
Out[2]:
0 single
1 touching
2 nuclei
3 dusts
4 touching
5 single
6 nuclei
Name: None, Length: 7
In [4]: Factor(s)
Out[4]:
Factor:
array([single, touching, nuclei, dusts, touching, single, nuclei], dtype=object)
Levels (4): [dusts nuclei single touching]
</code></pre>
<p>因子具有属性<code>labels</code>和<code>levels</code>:</p>
<pre><code>In [7]: f = Factor(s)
In [8]: f.labels
Out[8]: array([2, 3, 1, 0, 3, 2, 1], dtype=int32)
In [9]: f.levels
Out[9]: Index([dusts, nuclei, single, touching], dtype=object)
</code></pre>
<p>这是针对一维向量的,所以不确定它是否可以立即应用到您的问题上,但请看一看。</p>
<p>顺便说一句,我建议你在statsmodels和/或scikit learn邮件列表上提出这些问题,因为我们大多数人都不经常使用。</p>