1

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?

2
  • X is almost certainly not a list of lists, but probably a numpy ndarray. Commented Apr 2, 2016 at 12:03
  • @DSM X was generated using "np.random.uniform". Sorry, as I said, I am new to python. Commented Apr 2, 2016 at 12:08

1 Answer 1

2

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')
Sign up to request clarification or add additional context in comments.

2 Comments

Actually Y[t] is also a list. That means, Y itself is a list-of-lists. So, Y[t] would be [2,4,5,6,8,...]. What would be the output of X[:,Y[t]] is that case? Possible guess ! The output can be... m[ : , [0,1] ] => [ 'a' , 'c' , 'f' , 'b' , 'd' , 'g' ] Am I right?
@HemendraSharma m[:, [0,1]] would be asking for "all rows of m, in column 0 and column 1", I added the trace in my answer to explain

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.