TypeError:“布尔"对象不可下标Python 3 [英] TypeError: 'bool' object is not subscriptable Python 3

查看:217
本文介绍了TypeError:“布尔"对象不可下标Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

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 

用于以下代码.我是Python的新手,无法完全掌握如何更改语法(如果它是python 3的语法问题).有人遇到这个吗?我认为这与ascending = False [1:6]部分有关,并花了一些时间将我的头撞在墙上.希望这是一个简单的解决方法,但了解不够

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 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()

推荐答案

代替

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

使用

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

(即,在False之后插入),.)

Falsesort()方法参数的值,含义为不按升序",即i. e.要求降序.因此,您需要使用)终止sort()方法参数列表,然后使用,DataFrame构造函数的 1st参数 2nd one 分隔开.

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]是DataFrame构造函数的第二个参数(用于结果帧的索引)

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

这篇关于TypeError:“布尔"对象不可下标Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆