0
votes

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

1
Use the weights directly. Don't do float(weights). - Moses Koledoye
Please update your answer with more detail. Explain what error you're getting (in the question body) and explain what output you're expecting. - Soviut
I've added the edit with the full error. I'm trying to find the minmaxscaler value for $200,000.00. There are a wide range of salaries. Some salaries are unknown. Those are replaced with "NaN". - Phillip Tillotson

1 Answers

0
votes
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()]