类型错误:'bool' 对象不可下标 Python 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44337101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:56:30  来源:igfitidea点击:

TypeError: 'bool' object is not subscriptable Python 3

pythonpython-3.xpandasnumpy

提问by meowmixplzdeliver

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.values
     42     meowmix_nearest_neighbors.ix[i,:] = top_ten
     43 
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

对于以下代码。我是 Python 的新手,不能完全确定我必须如何更改语法(如果是语法 python 3 问题)。有人遇到这个吗?我认为这与上升=False[1:6] 部分有关,并且花了一些时间将我的头撞在墙上。希望这是一个简单的修复,但还不够了解

import numpy as np
import pandas as pd
from scipy.spatial.distance import cosine


enrollments = 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 cosine

print("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.values
    meowmix_nearest_neighbors.ix[i,:] = top_ten

print("done creating the top 5 neighbors for each item")

meowmix_nearest_neighbors.head()

回答by MarianD

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.之后插入)

Falseis the value of the sort()method parameter with meaning "not in ascending order", i. e. requiring the descendingone. So you need to terminate the sort()method parameter list with )and then delimit the 1st parameterof the DataFrameconstructor from the 2nd onewith ,.

Falsesort()方法参数的值,意思是“不按升序排列”,即需要降序。所以,你需要终止sort()与方法的参数列表),然后划定的第一个参数的的DataFrame从构造第二个一个,

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

[1:6]是DataFrame 构造函数的第二个参数(用于结果帧的索引)

回答by rm-vanda

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

是的,你不能做False[1:6]-False是一个boolean,这意味着它只能是两件事之一(FalseTrue

Just change it to Falseand your problem will be solved.

只需将其更改为False,您的问题就会得到解决。

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

[1:6]构造用于处理lists。所以如果你有,例如:

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.

在 Python 中,这称为“切片”,并且非常有用。

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

我鼓励你使用Jupyter Notebook来玩这个——当然,阅读文档