我正试图将一条记录馈送到knn.predict(),以便使用以下代码进行预测:
person_features = {
'cma': 462, # Metropolitan area
'agegrp': 9, # Age Group
'sex': 1,
'ageimm': 6, # Age group at immigration
'immstat': 1, # Immigrant status
'pob': 21, # Other Eastern Asia
'nol': 4, # First languages
'cip2011': 7, # Major field of study: Mathematics, computer and information sciences
'hdgree': 12, # Hightest Education
}
prediction = knn.predict(person_features)
labels={True: '>50K', False: '<=50K'}
print(labels[prediction])
但它显示
TypeError:foat()参数必须是字符串或数字,而不是"dict"
我试着把它做成元组列表,比如:
person_features= [('cma',462), ('agegrp',9), ('sex',1), ('ageimm',6), ('immstat',1), ('pob',21), ('nol',4), ('cip2011',7), ('hdgree',12)])
但也没用。
我应该怎么做才能解决这种类型的错误?我觉得这个解决方案很简单,但不知怎么的,我可以把我的头脑放在它周围。
编程新手,刚开始学习Python不到三个月。请耐心等待我的业余问答!
# I looked up the numbers from the coding book
cma = 462
agegrp = 9
sex = 1
ageimm = 6
immstat = 1
pob = 21
nol = 4
cip2011 =7
hdgree = 12
MoreThan50K = 1 # what I am going to predict, 1 for >50K, 0 for <50K
person_features = [cma, agegrp, sex, ageimm, immstat, pob, nol, cip2011, hdgree, MoreThan50K]
prediction = knn.predict(person_features)
所以这毕竟很简单。