Python Numpy - Reading elements of a Vector Numpy Array as numerical values rather than as Numpy Arrays
<p>Suppose I have a Numpy Array that has dimensions of nx1 (n rows, 1 column). My usage of this is for implementing 3D vectors as 3x1 Matrices using Numpy, but the application can be extended for nx1 Vector Matrices:</p>

<pre><code>In [0]: import numpy as np

In [1]: foo = np.array([ ['a11'], ['a21'], ['a31'], ..., ['an1'] ])
</code></pre>

<p>I want to be able to access the values of the array by dereferencing one value.</p>

<pre><code>In [2]: foo[0]
Out[2]: 'a11'

In [3]: foo[n]
Out[3]: 'an1'
</code></pre>

<p>However, by the general formatting of Numpy Arrays, a Vector array would be considered a 2D Array and would require 2 values to dereference it: I would have to use <code>foo[0][0]</code> or <code>foo[0][n]</code> to get the same values. I could use <code>np.transpose</code> to Transpose the Vector into one row, but the syntax continues to produce a 2D Numpy Array that requires 2 values to dereference: hence</p>

<pre><code>In [4]: np.transpose(foo)[0] == foo[0][0]
Out[4]: array([ True, False, False], dtype=bool)

In [5]: np.transpose(foo)[0][0] == foo[0][0]
Out[5]: True
</code></pre>

<p>This would nullify any advantage that transposing would provide. How can I access the elements of a Vector Numpy Array using only one Dereferencing Value?</p>

<p>Your <code>foo</code> is a 2d array of strings:</p>

<pre><code>In [354]: foo = np.array([ ['a11'], ['a21'], ['a31'], ['an1'] ])
In [355]: foo.shape
Out[355]: (4, 1)
In [356]: foo[0]   # selects a 'row'
Out[356]: 
array(['a11'], 
      dtype='&lt;U3')
In [357]: foo[0,:]    # or more explicitly with the column :
Out[357]: 
array(['a11'], 
      dtype='&lt;U3')
In [358]: foo[:,0]     # selects a column, results in a 1d array
Out[358]: 
array(['a11', 'a21', 'a31', 'an1'], 
      dtype='&lt;U3')
In [359]: foo[0,0]     # select an element
Out[359]: 'a11'
</code></pre>

<p>Transpose is still 2d, just switching rows and columns:</p>

<pre><code>In [360]: foo.T
Out[360]: 
array([['a11', 'a21', 'a31', 'an1']], 
      dtype='&lt;U3')
In [361]: _.shape
Out[361]: (1, 4)      
</code></pre>

<p>Ravel (or flatten) turns it into a 1d array, which can be accessed with just one index.</p>

<pre><code>In [362]: foo.ravel()[1]
Out[362]: 'a21'
</code></pre>

<p>Your talk about matrix multiplication and such suggests that your array really isn't of strings, but that 'a21' represents an array, or is number.  So is it really a 2d numeric array, or maybe a 3d array?</p>

python arrays numpy vector dereference