0
votes

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:

In [0]: import numpy as np

In [1]: foo = np.array([ ['a11'], ['a21'], ['a31'], ..., ['an1'] ])

I want to be able to access the values of the array by dereferencing one value.

In [2]: foo[0]
Out[2]: 'a11'

In [3]: foo[n]
Out[3]: 'an1'

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 foo[0][0] or foo[0][n] to get the same values. I could use np.transpose to Transpose the Vector into one row, but the syntax continues to produce a 2D Numpy Array that requires 2 values to dereference: hence

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

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?

2
You can write foo = np.array(['a11', 'a21', 'a31', ..., 'an1'] ). Then foo[0] 'a11'. Numpy has true n-dimensional arrays, and n can be 1.Warren Weckesser
Also, in the 2-d case, you can write foo[0,0].Warren Weckesser
I like the 2nd suggestion you made, but the first one takes away from the original formatting of Vector Matrices as I will be using them in Matrix Multiplication. I suppose I can always Transpose 1D Arrays whenever I perform said Mux operations, but it doesn't seem appealing to have to do transpositions like that.S. Gamgee
" I suppose I can always Transpose 1D Arrays ..." Actually, you can't. :) A transpose interchanges two dimensions. A 1-d array has only one dimensional, so a transpose doesn't do anything.Warren Weckesser
Huh, true fact that. Just tried it out. Seems legit. Thanks for the tip. I wouldn't have figured that out until things hit the fan.S. Gamgee

2 Answers

0
votes

You could use the numpy.ndarray.tolist() function:

In [1]: foo = np.array([ ['a11'], ['a21'], ['a31'] ])
In [2]: foo.tolist()
Out[2]: [['an1'], ['a2n'], ['a3n']]

In [3]: foo.tolist()[0]
Out[3]: ['an1']
0
votes

Your foo is a 2d array of strings:

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='<U3')
In [357]: foo[0,:]    # or more explicitly with the column :
Out[357]: 
array(['a11'], 
      dtype='<U3')
In [358]: foo[:,0]     # selects a column, results in a 1d array
Out[358]: 
array(['a11', 'a21', 'a31', 'an1'], 
      dtype='<U3')
In [359]: foo[0,0]     # select an element
Out[359]: 'a11'

Transpose is still 2d, just switching rows and columns:

In [360]: foo.T
Out[360]: 
array([['a11', 'a21', 'a31', 'an1']], 
      dtype='<U3')
In [361]: _.shape
Out[361]: (1, 4)      

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

In [362]: foo.ravel()[1]
Out[362]: 'a21'

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?