为什么我在以下代码中收到此错误?我正在使用Spyder 3.1.4,我通过IPython控制台运行它 .
list_stock = []
for key,value in data_dict.iteritems():
list_stock.append(value["salary"])
sorted_list_stock = sorted(list_stock)
scaler = MinMaxScaler()
weights = np.array(sorted_list_stock)
print weights
rescaled_weights = scaler.fit_transform(float(weights))
print rescaled_weights
编辑:完整错误:
['477''6615''63744'......,'NaN''NaN''NaN'] Traceback(最近一次电话 Session ):
文件“”,第1行,在runfile中('C:/ Users / ptillotson / Documents / Python Scripts / ud120-projects-master / k_means / k_means_cluster.py',wdir ='C:/ Users / ptillotson / Documents / Python Scripts / ud120项目主/ k_means')
文件“C:\ ProgramData \ Anaconda2 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,第880行,在runfile execfile(filename,namespace)中
文件“C:\ ProgramData \ Anaconda2 \ lib \ site-packages \ spyder \ utils \ site \ sitecustomize.py”,第87行,在execfile exec中(compile(scripttext,filename,'exec'),glob,loc)
文件“C:/ Users / ptillotson / Documents / Python Scripts / ud120-projects-master / k_means / k_means_cluster.py”,第89行,rescaled_weights = scaler.fit_transform(float(weights))
TypeError:只能将length-1数组转换为Python标量
1 回答
你想要
float(value["salary"])
.另外,最好将其短语为
list_stock = [float(v['salary']) for v in data_dict.values()]