[英]sklearn knn prediction error: float() argument must be a string or a number, not 'dict'
I'm trying to feed a record to knn.predict() to make a prediction by using the following code: 我正在尝试将记录提供给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])
But it showed 但它表明
TypeError: float() argument must be a string or a number, not 'dict'
TypeError:float()参数必须是字符串或数字,而不是'dict'
I tried making it into list of tuples like: 我试着把它变成元组列表,如:
person_features= [('cma',462), ('agegrp',9), ('sex',1), ('ageimm',6), ('immstat',1), ('pob',21), ('nol',4), ('cip2011',7), ('hdgree',12)])
But didnt work either. 但也没有工作。
What should I do to solve this type error? 我该怎么做才能解决这种类型错误? I feel like the solution is easy, but somehow I just could wrap my head around it.
我觉得这个解决方案很简单,但不知怎的,我只能把它包裹起来。
New to programming and just started to learn Python less than three month. 编程新手,刚开始学习Python不到三个月。 So bear with me for my amateur questions and answer!
所以请耐心等待我的业余问题和答案!
# 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)
So it was pretty straightforward afterall. 所以这毕竟是非常简单的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.