python category字符串拼接_python – 从大熊猫数据框转换为数组的字符串(category)到数组...

我正在尝试与

previous question非常相似的事情,但我遇到错误.

我有一个包含特征和标签的熊猫数据框,我需要做一些转换,将功能和标签变量发送到机器学习对象中:

import pandas

import milk

from scikits.statsmodels.tools import categorical

那我有

trainedData=bigdata[bigdata['meta']<15]

untrained=bigdata[bigdata['meta']>=15]

#print trainedData

#extract two columns from trainedData

#convert to numpy array

features=trainedData.ix[:,['ratio','area']].as_matrix(['ratio','area'])

un_features=untrained.ix[:,['ratio','area']].as_matrix(['ratio','area'])

print 'features'

print features[:5]

##label is a string:single, touching,nuclei,dust

print 'labels'

labels=trainedData.ix[:,['type']].as_matrix(['type'])

print labels[:5]

#convert single to 0, touching to 1, nuclei to 2, dusts to 3

#

tmp=categorical(labels,drop=True)

targets=categorical(labels,drop=True).argmax(1)

print targets

输出控制台首先产生:

features

[[ 0.38846334 0.97681855]

[ 3.8318634 0.5724734 ]

[ 0.67710876 1.01816444]

[ 1.12024943 0.91508699]

[ 7.51749674 1.00156707]]

labels

[[single]

[touching]

[single]

[single]

[nuclei]]

我遇到了以下错误:

Traceback (most recent call last):

File "/home/claire/Applications/ProjetPython/projet particule et objet/karyotyper/DAPI-Trainer02-MILK.py", line 83, in

tmp=categorical(labels,drop=True)

File "/usr/local/lib/python2.6/dist-packages/scikits.statsmodels-0.3.0rc1-py2.6.egg/scikits/statsmodels/tools/tools.py", line 206, in categorical

tmp_dummy = (tmp_arr[:,None]==data).astype(float)

AttributeError: 'bool' object has no attribute 'astype'

可以将数据帧中的类别变量’type’转换为int吗? ‘type’可以取值’single’,’touch’,’nuclei’,’dusts’,我需要使用int值(如0,1,2,3)进行转换.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值