简体   繁体   English

sklearn knn预测错误:float()参数必须是字符串或数字,而不是'dict'

[英]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.

相关问题 错误:“TypeError: float() argument must be a string or a number, not 'NoneType'”,但是没有'NoneType'? - Error: "TypeError: float() argument must be a string or a number, not 'NoneType'", but there is no 'NoneType'? Python 错误:TypeError:float() 参数必须是字符串或实数,而不是“列表” - Python error: TypeError: float() argument must be a string or a real number, not 'list' 类型错误:float() 参数必须是字符串或数字,而不是“模块” - TypeError: float() argument must be a string or a number, not 'module' TypeError:float() 参数必须是字符串或数字,而不是“PolyCollection” - TypeError: float() argument must be a string or a number, not 'PolyCollection' 错误:尝试在多边形中寻找点时,float()参数必须为字符串或数字,而不是“多边形” - Error: float() argument must be a string or a number, not 'Polygon' when trying to find points in polygons 类型错误:float() 参数必须是字符串或数字,而不是“datetime.timedelta” - TypeError: float() argument must be a string or a number, not 'datetime.timedelta' Python-TypeError:float()参数必须是字符串或数字,而不是&#39;list - Python - TypeError: float() argument must be a string or a number, not 'list python 错误:参数必须是字符串或数字,而不是 function - python error: argument must be a string or a number, not function 使用 knn 算法时(无法将字符串转换为浮点数)错误 - (could not convert string to float) error while using knn algorithm 类型错误:float() 参数必须是字符串或数字,而不是“元组” ValueError:使用序列设置数组元素 - TypeError: float() argument must be a string or a number, not 'tuple' ValueError: setting an array element with a sequence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM