how to convert 2d list to 2d numpy array? - python

I have a 2D list something like
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
and I want to convert it to a 2d numpy array. Can we do it without allocating memory like
numpy.zeros((3,3))
and then storing values to it?

Just pass the list to np.array:
a = np.array(a)
You can also take this opportunity to set the dtype if the default is not what you desire.
a = np.array(a, dtype=...)

just use following code
c = np.matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
Then it will give you
you can check shape and dimension of matrix by using following code
c.shape
c.ndim

np.array() is even more powerful than what unutbu said above.
You also could use it to convert a list of np arrays to a higher dimention array, the following is a simple example:
aArray=np.array([1,1,1])
bArray=np.array([2,2,2])
aList=[aArray, bArray]
xArray=np.array(aList)
xArray's shape is (2,3), it's a standard np array. This operation avoids a loop programming.

I am using large data sets exported to a python file in the form
XVals1 = [.........]
XVals2 = [.........]
Each list is of identical length. I use
>>> a1 = np.array(SV.XVals1)
>>> a2 = np.array(SV.XVals2)
Then
>>> A = np.matrix([a1,a2])

Related

How to index a numpy array of dimension N with a 1-dimensional array of shape (N,)

I would like to index an array of dimension N using an array of size (N,).
For example, let us consider a case where N is 2.
import numpy as np
foo = np.arange(9).reshape(3,3)
bar = np.array((2,1))
>>> foo
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>>bar
array([2, 1])
>>>foo[bar[0],bar[1]]
7
This works fine. However, with this method, I would need to write N times bar[i], which is not a nice solution if N is high.
The following command does not give the result that I need:
>>>foo[bar]
array([[6, 7, 8],
[3, 4, 5]])
What could I do to get the result that I want in a nice and concise way?
I think you can turn bar into tuple:
foo[tuple(bar)]
# 7

Sorting an array with respect to an another array

I am using some datas in my program were I have some sorting issue which takes longer time for me. So I have mentioned an example situation here for which I would like to get a solution.
import numpy as np
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
B = np.array([[4,5,6,7],[7,8,9,4],[1,2,3,2]])
# Need to apply some sort function
C = sort(B[:,0:3] to be sorted with respect to A)
print(C)
I have two numpy arrays were I would like the first 3 columns of array B to be sorted with respect to array A.
And I want the output of C as
[[1,2,3,2],[4,5,6,7],[7,8,9,4]]
Is there any numpy method or any other python libraries which could do this.
Looking forward for some answers
Regards
Aadithya
This only works when using the first column :
_, indexer, _ = np.intersect1d(B[:,:1], A[:,:1], return_indices=True)
B[indexer]
array([[1, 2, 3, 2],
[4, 5, 6, 7],
[7, 8, 9, 4]])
Apparently, the above solution works only if the values are unique (Thanks #AadithyaSaathya.
If we are to use all of A, we could use itertools' product function :
from itertools import product
indexer = [B[0]
for A,B
in
product(enumerate(A), enumerate(B[:,:3]))
if np.all(np.equal(A[-1], B[-1]))]
B[indexer]
array([[1, 2, 3, 2],
[4, 5, 6, 7],
[7, 8, 9, 4]])

Slicing Numpy Array by 2 index arrays

If I have a set of indices stored in two Numpy arrays, my goal is to slice a given input array based on corresponding indices in those index arrays. For eg.
index_arr1 = np.asarray([2,3,4])
index_arr2 = np.asarray([5,5,6])
input_arr = np.asarray([1,2,3,4,4,5,7,2])
The output to my code should be [[3,4,4],[4,4],[4,5]] which is basically [input_arr[2:5], input_arr[3:5], input_arr[4:6]]
Can anybody suggest a way to solve this problem using numpy functions and avoiding any for loops to be as efficient as possible.
Do you mean:
[input_arr[x:y] for x,y in zip(index_arr1, index_arr2)]
Output:
[array([3, 4, 4]), array([4, 4]), array([4, 5])]
Or if you really want list of lists:
[[input_arr[x:y].tolist() for x,y in zip(index_arr1, index_arr2)]
Output:
[[3, 4, 4], [4, 4], [4, 5]]

How to create a 2d numpy ndarray using two list comprehensions

I tried to create a 2D numpy ndarray using the following code:
temp = np.array([[np.mean(w2v[word]) for word in docs if word in w2v] for docs in X[:5]])
temp has a shape of (5,) instead of expected (5,x).
Also temps's data structure is like: array([list([.....],...)])
It seems that the inner list is not converted to ndarray.
Your missing np.array in there, it should be:
temp = np.array([np.array([np.mean(w2v[word]) for word in docs if word in w2v] for docs in X[:5])])
Running example:
bob
Out[70]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
tmp = np.array([np.array([x for x in Y]) for Y in bob])
tmp
Out[72]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

Append a 1d array to a 2d array in Numpy Python

I have a numpy 2D array [[1,2,3]].
I need to append a numpy 1D array,( say [4,5,6]) to it, so that it becomes [[1,2,3], [4,5,6]]
This is easily possible using lists, where you just call append on the 2D list.
But how do you do it in Numpy arrays?
np.concatenate and np.append dont work. they convert the array to 1D for some reason.
Thanks!
You want vstack:
In [45]: a = np.array([[1,2,3]])
In [46]: l = [4,5,6]
In [47]: np.vstack([a,l])
Out[47]:
array([[1, 2, 3],
[4, 5, 6]])
You can stack multiple rows on the condition that The arrays must have the same shape along all but the first axis.
In [53]: np.vstack([a,[[4,5,6], [7,8,9]]])
Out[53]:
array([[1, 2, 3],
[4, 5, 6],
[4, 5, 6],
[7, 8, 9]])
Try this:
np.concatenate(([a],[b]),axis=0)
when
a = np.array([1,2,3])
b = np.array([4,5,6])
then result should be:
array([[1, 2, 3],
[4, 5, 6]])

Categories