代码运行时有错误呢?请高手帮忙解决
3个回答
和你的python版本有关吧,Sofa竞赛3 的官方代码还是python2的,如果你在用python3的话,不需要decode了,因为python3的str没有decode的这个属性了
SofaSofa数据科学社区DS面试题库 DS面经
您好!在程序上具体怎么修改呢?谢谢!见谅,来自python小白的发问
-
scdmxy
2018-12-09 19:22
共三处涉及:
第一处:
# 统计每个字在男生、女生名字中出现的总次数
lists_male = map(lambda x: x.encode('utf-8'), names_male.decode('utf-8'))
counts_male = Counter(lists_male)
lists_female = map(lambda x: x.encode('utf-8'), names_female.decode('utf-8'))
counts_female = Counter(lists_female)
第二处:
# 得到训练集中每个人的每个字的词频(Term Frequency,通常简称TF)
train_encoded = []
for i in range(len(train)):
name = train.at[i, 'name']
chs = map(lambda x: x.encode('utf-8'), name.decode('utf-8'))
row = [0., 0., 0., 0, train.at[i, 'gender']]
for j in range(len(chs)):
row[2* j] = counts_female[chs[j]] * 1. / f_cnt
row[2* j + 1] = counts_male[chs[j]] * 1. / m_cnt
train_encoded.append(row)
第三处:
# 得到测试集中每个人的每个字的词频(Term Frequency,通常简称TF)
test_encoded = []
for i in range(len(test)):
name = test.at[i, 'name']
chs = map(lambda x: x.encode('utf-8'), name.decode('utf-8'))
row = [0., 0., 0., 0.,]
for j in range(len(chs)):
try:
row[2 * j] = counts_female[chs[j]] * 1. / f_cnt
except:
pass
try:
row[2 * j + 1] = counts_male[chs[j]] * 1. / m_cnt
except:
pass
test_encoded.append(row)
-
scdmxy
2018-12-09 19:28
共三处涉及您说的这个问题:
第一处:
第二处:
第三处:
谢谢!
试试直接去掉.decode('utf-8')
-
GuoLinhui
2018-12-11 16:20
你好!只有第一处删除后可以运行,后两处都不行,怎么回事呢?感谢!
-
scdmxy
2018-12-11 23:52