liubov
liubov

Reputation: 11

Minor of matrix

I'm writing in function for computing minor of matrix

def minor(arr,i,j):
    return arr[np.array(range(i)+range(i+1,arr.shape[0]))[:,np.newaxis],  
               np.array(range(j)+range(j+1,arr.shape[1]))]

And then apply it to an array which I initialized:

for row in values_float:
    for item in row:
        am[p][k] = item

But I'm getting an error:

AttributeError: 'list' object has no attribute 'shape'

Does anybody know why I got it?

Upvotes: 1

Views: 3688

Answers (4)

stackPusher
stackPusher

Reputation: 6512

Is arr a 2-dimensional array? If you dont have numpy you could compute the minor like so:

def minor(arr, i, j):
    minor = [row[:j] + row[j+1:] for row in (arr[:i] + arr[i+1:])]
    return minor

Upvotes: 0

Michael F
Michael F

Reputation: 40869

.shape is an attribute of numpy arrays, while you apply it to a Python list. You can replace arr.shape[0] (and arr.shape[1], respectively) with the dimension of the list that you are interested in (i or j, in your case).

Alternatively, you can initialise a numpy array from your values_float list, as such:

am = numpy.array(values_float, dtype=float)

Upvotes: 1

cachorrocadi
cachorrocadi

Reputation: 29

I think the best way to initialize an array in numpy is with numpy.ndarray or numpy.zeros instead of how you did and what you're initializing a list.

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html

Upvotes: 1

kiriloff
kiriloff

Reputation: 26333

A possible source for this error, in general:

in your class MyClass, in __init__(), you define an attribute attr for the instances of your class. In some method, you want to access this attribute. You call this method on a object of type MyClass, say myObj. But instead of doing myObj.attr, you are calling MyClass.attr. Your class has no attribute. Instances of the class do.

In your case, shape is an attribute of a numpy arrays, whereas your input object is a list.

Upvotes: 0

Related Questions