Home > OS >  Any ideas on why my syntax is wrong for np.identity?
Any ideas on why my syntax is wrong for np.identity?

Time:07-24

Don't know if i'm missing something super obvious but i keep getting a syntax error from this one line

lambda = np.ones((D,D)) - np.identity(D))

where

classpriors = np.array([0.65,0.35])
L = len(classpriors)
D = np.array(range(L))

I can't figure out what's wrong with the lambda line -- any ideas?

CodePudding user response:

lambda is a reserved keyword in Python; you can't use it as an identifier. (You can find the full list of keywords here.)

Lambda is fine, lambda_ is fine, lamb is fine, even λ is fine (though a bit of a chore to type):

>>> λ = 5
>>> λ * 2
10

CodePudding user response:

I think what you maybe try to archive is this, having the matrices constructed by D.size?

import numpy as np
classpriors = np.array([0.65,0.35])
L = len(classpriors)
D = np.array(range(L))

lam = np.ones((D.size,D.size)) - np.identity(D.size)

Anyways, creating arrays with sizes given as arrays like in your version, does not make sense.

  • Related