I get the following error:

TypeError Traceback (most recent call last)C:\Users\levanim\Desktop\Levani Predictive\cosinesimilarity1.py in <module>()39 40 for i in meowmix_nearest_neighbors.index:---> 41 top_ten = pd.DataFrame(similarity_matrix.ix[i,]).sort([i], ascending=False[1:6]).index.values42 meowmix_nearest_neighbors.ix[i,:] = top_ten43 TypeError: 'bool' object is not subscriptable 

for the following code. I'm new to Python and can't quite put my finger on how I have to change the syntax(if its a syntax python 3 problem). Anybody encounter this? I think it's to do with the ascending=False[1:6] portion and have spent some time banging my head against the wall. Hoping it's a simple fix but don't know enough

import numpy as npimport pandas as pdfrom scipy.spatial.distance import cosineenrollments = pd.read_csv(r'C:\Users\levanim\Desktop\Levani Predictive\smallsample.csv')meowmix = enrollments.fillna(0)meowmix.ix[0:5,0:5]def getCosine(x,y) :cosine = np.sum(x*y) / (np.sqrt(np.sum(x*x)) * np.sqrt(np.sum(y*y)))return cosineprint("done creating cosine function")similarity_matrix = pd.DataFrame(index=meowmix.columns, columns=meowmix.columns)similarity_matrix = similarity_matrix.fillna(np.nan)similarity_matrix.ix[0:5,0:5]print("done creating a matrix placeholder")for i in similarity_matrix.columns:for j in similarity_matrix.columns:similarity_matrix.ix[i,j] = getCosine(meowmix[i].values, meowmix[j].values)print("done looping through each column and filling in placeholder with cosine similarities")meowmix_nearest_neighbors = pd.DataFrame(index=meowmix.columns,columns=['top_'+str(i+1) for i in range(5)])meowmix_nearest_neighbors = meowmix_nearest_neighbors.fillna(np.nan)print("done creating a nearest neighbor placeholder for each item")for i in meowmix_nearest_neighbors.index:top_ten = pd.DataFrame(similarity_matrix.ix[i,]).sort([i], ascending=False[1:6]).index.valuesmeowmix_nearest_neighbors.ix[i,:] = top_tenprint("done creating the top 5 neighbors for each item")meowmix_nearest_neighbors.head()
3

Best Answer


Instead of

 top_ten = pd.DataFrame(similarity_matrix.ix[i,]).sort([i], ascending=False[1:6]).index.values

use

 top_ten = pd.DataFrame(similarity_matrix.ix[i,]).sort([i], ascending=False), [1:6]).index.values

(i. e. insert ), just after the False.)

False is the value of the sort() method parameter with meaning "not in ascending order", i. e. requiring the descending one. So you need to terminate the sort() method parameter list with ) and then delimit the 1st parameter of the DataFrame constructor from the 2nd one with , .

[1:6] is the second parameter of the DataFrame constructor (the index to use for resulting frame)

If you are getting the 'bool' object is not subscriptable error in Python, it means you are trying to access an element of a boolean object as if it were a list or dictionary. In Python, boolean objects, which can have a value of either True or False, cannot be subscripted or indexed. This error typically occurs when you mistakenly treat a boolean variable as a collection type.

To fix this error, you need to review your code and identify where you are trying to subscript a boolean object. Once you locate the problematic line, you can modify it to use the boolean value directly without subscripting.

For example, if you have a boolean variable named 'myBool' and you mistakenly try to access its elements like 'myBool[0]', you will encounter the 'bool' object is not subscriptable error. To resolve this, simply use the boolean variable directly in your code without subscripting it.

It is important to understand the difference between boolean objects and collection types like lists or dictionaries. Boolean objects represent a single value, either True or False, while collection types can hold multiple values. Treating a boolean object as a collection type will result in the 'bool' object is not subscriptable error.

In conclusion, the 'bool' object is not subscriptable error occurs when you try to subscript a boolean object in Python. To fix this error, review your code and remove any attempts to subscript a boolean variable. Remember that boolean objects cannot be indexed or subscripted because they represent a single value, not a collection of values.

Yeah, you can't do False[1:6] - False is a boolean, meaning it can only be one of two things (False or True)

Just change it to False and your problem will be solved.

the [1:6] construct is for working with lists. So if you had, for example:

theList = [ "a","b","c","d","e","f","g","h","i","j","k","l" ] print theList # (prints the whole list)print theList[1] # "b" print theList[1:6] # ['b', 'c', 'd', 'e', 'f']

In python, this is called "slicing", and can be quite useful.

You can also do things like:

print theList[6:] # everything in the list after "f" print theList[:6] # everything in the list before "f", but including f

I encourage you to play with this using Jupyter Notebook - and of course, read the documentation