我收到以下错误:
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问题)。有人遇到过吗?我认为这与升序=假[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()
答案 0 :(得分:2)
而不是
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
。)
False
是sort()
方法参数的值,其含义为“不按递增顺序”,i。即需要降序一个。因此,您需要使用sort()
终止)
方法参数列表,然后从第二个1>中划分DataFrame
构造函数的第一个参数 em> ,
。
[1:6]
是DataFrame构造函数的第二个参数(用于结果框架的索引)
答案 1 :(得分:0)
是的,你不能False[1:6]
- False
是bool
ean,这意味着它只能是两件事之一(False
或{{ 1}})
只需将其更改为True
即可解决您的问题。
False
构造用于处理[1:6]
s。所以如果你有,例如:
list
在python中,这被称为&#34;切片&#34;,并且非常有用。
您还可以执行以下操作:
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']
我鼓励您使用Jupyter Notebook进行播放 - 当然,请阅读documentation