QNA > C > Can Anybody Elaborate The Use Of C_ In Numpy?

Can anybody elaborate the use of c_ in numpy?

Say we have:

  1. a = array([[1, 2, 3], 
  2. [4, 5, 6]]) 

The zeroth item along the first axis is:

  1. >>> a[0,:] 
  2. array([1, 2, 3]) 

So, the first axis is rows.

The zeroth item along the second axis is:

  1. >>> a[:,0] 
  2. array([1, 4]) 

So, the second axis is columns.

If we also have:

  1. b = array([[7, 8, 9], 
  2. [10, 11, 12]]) 

We can concatenate them along the first axis:

  1. >>> r_[a, b]  
  2. array([[ 1, 2, 3], 
  3. [ 4, 5, 6], 
  4. [ 7, 8, 9], 
  5. [10, 11, 12]]) 

So, stack them on top of each other.

Or along the second axis:

  1. >>> c_[a, b] 
  2. array([[ 1, 2, 3, 7, 8, 9], 
  3. [ 4, 5, 6, 10, 11, 12]]) 

So, put them next to each other.

È utile, come dice la documentazione, per costruire gli array - per esempio se avete un ciclo `for` o qualcosa che costruisce un array colonna per colonna, l'helper `c_` sarà utile. Idem se lo costruisci riga per riga, con `r_`.

P.S. Spero che sia chiaro da dove vengono i nomi `r_` e `c_` :D

Di Odeen Similton

Come fare un array in Python :: Qual è lo scopo di "end" in Python?
Link utili