randomuser
randomuser

Reputation: 298

Accessing matrix elements

I got used to working in IDLE, but got a recommendation to use PyCharm instead.

As I'm just getting used to it, I have a question on accessing elements of a matrix.

I'm getting different string inputs from the user and filling the matrix with them;

from numpy import *

row=int(input())
col=int(input())

m=range(row*col)
m=reshape(m,(row,col))

for i in range(0,row):
    for j in range(0,col):
        el=int(input())
        m[i][j]=el

What PyCharm is telling me:

Class 'ndarray' does not define 'getitem', so the '[]' operator cannot be used on its instances less... This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

Could anyone please explain to me how I can change/fix this?

Upvotes: 0

Views: 466

Answers (3)

Nothing
Nothing

Reputation: 512

Try this :

from numpy import np
row=int(input())
col=int(input())
m = []
for i in range(0,row):
    for j in range(0,col):
        el=int(input())
        m.append(el)
m=np.reshape(m,(row,col))

Upvotes: 1

hpaulj
hpaulj

Reputation: 231355

Your code works, in IDLE, and ipython:

In [178]: row=3
     ...: col=2
     ...: 
     ...: m=range(row*col)
     ...: m=np.reshape(m,(row,col))
     ...: 
     ...: for i in range(0,row):
     ...:     for j in range(0,col):
     ...:         el=int(input())
     ...:         m[i][j]=el
     ...: 
1
2
3
4
5
6
In [179]: m
Out[179]: 
array([[1, 2],
       [3, 4],
       [5, 6]])

If pycharm objects, it's because it's looking at style as much as syntax. I don't use it, so can't say how well it handles numpy constructs.

I would use np.arange(row*col).reshape(row,col) to generate m, but your use of range and reshape works as well. Actually since you are filling in all values from user input, m = np.zeros((row,col),int) works just as well.

Iterative input like that is slow and clumsy, but at least you aren't trying to use np.append.

The double indexing might be a problem for pycharm.

m[i][j] = e1    # works
m[i, j] = e1    # better

I have no idea why pycharm is complaining that `'ndarray' does not define 'getitem'.

In [184]: m.__getitem__
Out[184]: <method-wrapper '__getitem__' of numpy.ndarray object at 0x7fe7e883e350>

It may just be that pycharm is objecting to [i][j], but for rather convoluted reasons. I'd try the [i,j] syntax and see if the complaint goes away.

Upvotes: 1

rayryeng
rayryeng

Reputation: 104474

You are using the vanilla range object in Python. It looks like you want to use the numpy.arange operation instead which creates a NumPy array for you. range does not give you what numpy.arange does.

You need to change that statement to use arange:

m=arange(row*col)

The reason why PyCharm is probably giving you that error is because you are implicitly converting from one type to another. This is due to their type hinting mechanism so it's good practice to produce the expected output immediately when you produce the array with arange.

However, this is quite superfluous as you are creating a row x col 2D array that is linearly increasing, but you end up replacing all of the elements anyway. Consider pre-allocating the matrix with numpy.zeros of the desired size instead:

m = zeros((row, col))

Also, it is very bad practice to do this: from numpy import *. This is because you may be importing other packages that could share the same function names between packages. Unless you absolutely know that doing the above style of import won't provide any conflicts, I would not recommend you do this. Instead, import NumPy with the alias np as many people do so:

import numpy as np

After, call any NumPy functions by accessing np:

import numpy as np

row=int(input())
col=int(input())

m=np.range(row*col)
m=np.reshape(m,(row,col))
# or:
# m = np.zeros((row, col))

for i in range(0,row):
    for j in range(0,col):
        el=int(input())
        m[i][j]=el

Upvotes: 2

Related Questions