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

Python 3.x sklearn knn预测错误:float()参数必须是字符串或数字,而不是';dict';,python-3.x,scikit-learn,knn,Python 3.x,Scikit Learn,Knn,我正试图通过使用以下代码向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, #

我正试图通过使用以下代码向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])
但也不管用


我应该如何解决此类型错误?我觉得解决这个问题很容易,但不知怎么的,我还是能把脑袋绕过去

编程新手,不到三个月就开始学习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)
#我从编码本上查到了数字
cma=462
年龄组=9
性别=1
年龄imm=6
immstat=1
pob=21
nol=4
cip2011=7
hdgree=12

超过50K=1#我要预测的是,1代表>50K,0因为它需要一个浮点数的numpy数组,你要传递一个dict,你需要将你的输入数据转换成一个仅包含这些值的numpy数组。参见这里的示例:非常感谢你的回复。然后我尝试了inputData=np.ndarray(shape=(1,10))inputData=[462,12,1,6,1,21,4,7,12,1],它成功了:)虽然我对这个预测不满意,因为它说我赚的钱比你发布的答案要少,记住要接受它,但在答案的左上角会有一个空的勾号
# 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)