Hemendra Sharma
Hemendra Sharma

Reputation: 1060

What does the syntax ndarray[:,list[i]] means in python?

I was looking at a tutorial on the web for Python. I don't know anything about Python, so I searched and could not find the answer.

There is some code like this:

s = np.tanh(self.X[:,Y[t]])

Where, X is ndarray and Y is a List of lists (where each list is integer-type), np is a numpy object, and tanh is the hyperbolic tangent.

What does this syntax mean?

Upvotes: 1

Views: 127

Answers (1)

bakkal
bakkal

Reputation: 55448

In the context of numpy, it can e.g. allow to access the columns, so e.g. in your example X[:, Y[t]], it allows you to access the column of X, indexed by the value in Y[t].

The : basically says "all rows" and the Y[t] specifies the column index.

Here's a simple example to see it in action:

In [1]: import numpy as np

In [3]: m = np.array([['a', 'b'], ['c', 'd'], ['f', 'g']])

In [4]: m[:, 0]
Out[4]: 
array(['a', 'c', 'f'], 
      dtype='|S1')

In [5]: m[:, 1]
Out[5]: 
array(['b', 'd', 'g'], 
      dtype='|S1')

What if the "column index" is a list?

If you use m[:, some_list], the : colon would ask for all rows, and then the columns would be the column indices in some_list, in that order

so e.g. if we want all rows, and the columns [1, 0] (in that order), here's what you get:

In [53]: m[:, [1, 0]]
Out[53]: 
array([['b', 'a'],
       ['d', 'c'],
       ['g', 'f']], 
      dtype='|S1')

Upvotes: 2

Related Questions