Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Python Help

I am getting an error in Python
XAll = pd.read_excel("./studentnew.xlsx", sheet_name = 0)

XAll = XAll[np.where( XAll[:,-1] < 2.0 )]

It keeps on giving me the error like:

res = cache.get(item)

TypeError: unhashable type: 'slice'

I don't know where is the problem. Please help me.

3 responses

Not really sure what you are trying to do? However, a couple of things. I find it very important to always be aware of the object types. In this case

XAll = pd.read_excel("./studentnew.xlsx", sheet_name = 0)

XAll will be a pandas dataframe since the 'read_excel' method generally returns a dataframe (look at the docs http://pandas.pydata.org/pandas-docs/version/0.22/generated/pandas.read_excel.html). One can check this by entering

type(XAll)

Why is this important? Since XAll is a pandas dataframe, one can't really select using the bracket notation (ie XAll[something] won't work) See the documentation http://pandas.pydata.org/pandas-docs/version/0.15/indexing.html. The bracket notation is typically used for series and numpy arrays. So, the error starts with

XAll[:,-1] 

Try just that and you will see the error.

Also, typically use pandas methods when working with pandas dataframes and numpy methods when working with numpy arrays. Often no need to mix the two. It works and there is nothing really wrong as long as you keep track of what you are doing.

Again, not sure what you are trying to accomplish but it may be as simple as this.

df.where(df<2.0)

See attached notebook.

def compute_classes(XAll, YAll, PERC_TRAIN):

n = XAll.shape[0]  
m = XAll.shape[1]

indI = np.arange(n)  
indTrain = np.random.permutation(n)[0:int(PERC_TRAIN*n)]  
indTest = np.setdiff1d(indI, indTrain)  
nTrain = indTrain.size  
nTest = indTest.size  
indJ = np.arange(m)


phat = np.mean(YAll[indTrain])

model = ConcreteModel()

model.indI = Set(initialize=indI.tolist())  
model.indTrain = Set(initialize=indTrain.tolist())  
model.indTest = Set(initialize=indTest.tolist())  
model.indJ = Set(initialize=indJ.tolist())

model.X = Var( model.indI, within=Binary )



def obj_rule(model):  
    z = ( sum( YAll[i]*model.X[i] for i in model.indTrain )/nTrain ) + ( ( sum( (1-model.X[i]) * sum( ( XAll[i,j] - (sum(XAll[k,j]*model.X[k] for k in model.indI))/(sum(model.X[k] for k in model.indI)) )**2 for j in model.indJ ) for i in model.indI ) ) / sum( (1-model.X[i]) for i in model.indI) ) - ( ( sum( model.X[i] * sum( ( XAll[i,j] - (sum(XAll[k,j]*model.X[k] for k in model.indI))/(sum(model.X[k] for k in model.indI)) )**2 for j in model.indJ ) for i in model.indI ) ) / sum(model.X[i] for i in model.indI) )  

    return z


def const_rule0(model):  
    z = ( ( sum( (1-model.X[i]) * sum( ( XAll[i,j] - (sum(XAll[k,j]*model.X[k] for k in model.indI))/(sum(model.X[k] for k in model.indI)) )**2 for j in model.indJ ) for i in model.indI ) ) / sum((1-model.X[i]) for i in model.indI) ) >=  \  

( ( sum( model.X[i] * sum( ( XAll[i,j] - (sum(XAll[k,j]model.X[k] for k in model.indI))/(sum(model.X[k] for k in model.indI)) )*2 for j in model.indJ ) for i in model.indI ) ) / sum(model.X[i] for i in model.indI) )
return z

def const_rule1(model):  
    z = sum(model.X[i] for i in model.indI) <= np.ceil( n*phat + 1.96 * np.sqrt(phat)*np.sqrt(1-phat)*np.sqrt(n) )  
    return z

def const_rule2(model):  
    z = sum(model.X[i] for i in model.indI) >= np.floor( n*phat - 1.96 * np.sqrt(phat)*np.sqrt(1-phat)*np.sqrt(n) )  
    return z  



model.value = Objective(rule=obj_rule , sense = maximize )

model.constr0 = Constraint(rule = const_rule0 )

model.constr1 = Constraint(rule = const_rule1 )  
model.constr2 = Constraint(rule = const_rule2 )

solver = SolverFactory('ipopt')  
solver.solve(model)  

model.X.display()

model.pprint()

YhatAll = np.int64(1 - np.array(list(model.X.get_values().values())))

******************************************************************************

Error = np.count_nonzero(np.abs(np.int64(YAll[indTest]) - YhatAll[indTest])) / np.float64(nTest) 

A = np.zeros(shape=(2,2), dtype=np.float64)  
for i in np.arange(nTest):  
    if np.int64(YAll[indTest[i]]) == 1 and np.int64(YhatAll[indTest[i]]) == 1:  
        A[1,1] += 1  
    elif np.int64(YAll[indTest[i]]) == 0 and np.int64(YhatAll[indTest[i]]) == 0:  
        A[0,0] += 1  
    elif np.int64(YAll[indTest[i]]) == 1 and np.int64(YhatAll[indTest[i]]) == 0:  
        A[1,0] += 1  
    elif np.int64(YAll[indTest[i]]) == 0 and np.int64(YhatAll[indTest[i]]) == 1:  
        A[0,1] += 1

sens = A[1,1] / np.sum(A[1,:])  
spec = A[0,0] / np.sum(A[0,:])  

return (sens, spec)

***************************************************************************************************************

open the file

XAll = pd.read_excel("./studentnew.xlsx", sheet_name = 0)

________________

XAll = XAll[np.where( XAll[:,-1] < 2.0 )]

________________

YAll = XAll[0:1000,-1]
XAll = XAll[0:1000,0:-1]

This is what I am trying to do. Please help, why I am getting an error.