简体   繁体   中英

TypeError: only length-1 arrays can be converted to Python scalar

Why am I getting this error in the following code? I'm using Spyder 3.1.4, and I am running this through IPython console.

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

edit: full error:

['477' '6615' '63744' ..., 'NaN' 'NaN' 'NaN'] Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/ptillotson/Documents/Python Scripts/ud120-projects-master/k_means/k_means_cluster.py', wdir='C:/Users/ptillotson/Documents/Python Scripts/ud120-projects-master/k_means')

File "C:\\ProgramData\\Anaconda2\\lib\\site-packages\\spyder\\utils\\site\\sitecustomize.py", line 880, in runfile execfile(filename, namespace)

File "C:\\ProgramData\\Anaconda2\\lib\\site-packages\\spyder\\utils\\site\\sitecustomize.py", line 87, in execfile exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/ptillotson/Documents/Python Scripts/ud120-projects-master/k_means/k_means_cluster.py", line 89, in rescaled_weights = scaler.fit_transform(float(weights))

TypeError: only length-1 arrays can be converted to Python scalars

list_stock.append(value["salary"])

You wanted float(value["salary"]) there.

Also, better to phrase it as list_stock = [float(v['salary']) for v in data_dict.values()]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM