I have a list for example:
x = [1,2,3,4,5]
and I want to convert it into a matrix that looks like so:
mat = [ 1 1 1
2 2 2
3 3 3
4 4 4
5 5 5 ]
so each column in the matrix is the list.
is there an easy way doing so with numpy or just regular python?
thank you
Maybe you need to repeat i.e
n = 3 # No of repetition
np.repeat(np.array(x),n).reshape(-1,n)
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]])
without any modules
a = [1, 2, 3, 4, 5]
n = 3
b = [[x] * n for x in a]
print(b)
Let's use np.tile:
import numpy as np
arr = np.array(x)
np.tile(arr,3).reshape(5,3, order='F')
Output:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]])
Another array option:
In [248]: np.stack([a]*3,axis=1)
Out[248]:
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5]])
You need to be careful with list replication:
In [250]: b=[a]*3
In [251]: b[0][0]=30
In [252]: b
Out[252]: [[30, 2, 3, 4, 5], [30, 2, 3, 4, 5], [30, 2, 3, 4, 5]]
# a is also changed
It replicates the pointer, not the values. The array stack makes a copy.
np.array will join those lists along a new first axis. stack does something similar (but using np.concatenate), and allows us to join them on a new 2nd axis.
In [255]: np.array(b)
Out[255]:
array([[30, 2, 3, 4, 5],
[30, 2, 3, 4, 5],
[30, 2, 3, 4, 5]])
Related
Matrix is like
[0, 1, 2]
[1, 2, 3]
[2, 3, 4]
For clarification, it's not just to create one such matrix but many other different matrices like this.
[0, 1, 2, 3]
[1, 2, 3, 4]
[2, 3, 4, 5]
You can use a sliding_window_view
from numpy.lib.stride_tricks import sliding_window_view as swv
cols = 4
rows = 3
out = swv(np.arange(cols+rows-1), cols).copy()
NB. because this is a view, you need .copy() to make it a mutable array, it's not necessary if a read-only object is sufficient (e.g., for display or indexing).
Output:
array([[0, 1, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5]])
Output with cols = 3 ; rows = 5:
array([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6]])
alternative: broadcasting:
cols = 4
rows = 3
out = np.arange(rows)[:,None] + np.arange(cols)
Output:
array([[0, 1, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5]])
L = 3
np.array([
np.array(range(L)) + j
for j in range(L)
])
or a bit of optimization:
L = 3
a = np.array(range(L))
np.array([
a + j
for j in range(L)
])
You can easily create a matrix like that using broadcasting, for instance
>>> np.arange(3)[:, None] + np.arange(4)
array([[0, 1, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5]])
Let's say I have data structured in a 2D array like this:
[[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
The first column denotes a third dimension, so I want to convert this to the following 3D array:
[[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]]
Is there a built-in numpy function to do this?
You can try code below:
import numpy as np
array = np.array([[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]])
array = np.delete(array, 0, 1)
array.reshape(2,3,-1)
Output
array([[[3, 4, 6],
[4, 8, 2],
[3, 2, 9]],
[[2, 4, 8],
[4, 9, 1],
[2, 9, 3]]])
However, this code can be used when you are aware of the array's shape. But if you are sure that the number of columns in the array is a multiple of 3, you can simply use code below to show the array in the desired format.
array.reshape(array.shape[0]//3,3,-3)
Use numpy array slicing with reshape function.
import numpy as np
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# convert the list to numpy array
arr = np.array(arr)
# remove first column from numpy array
arr = arr[:,1:]
# reshape the remaining array to desired shape
arr = arr.reshape(len(arr)//3,3,-1)
print(arr)
Output:
[[[3 4 6]
[4 8 2]
[3 2 9]]
[[2 4 8]
[4 9 1]
[2 9 3]]]
You list a non numpy array. I am unsure if you are just suggesting numpy as a means to get a non numpy result, or you are actually looking for a numpy array as result. If you don't actually need numpy, you could do something like this:
arr = [[1, 3, 4, 6],
[1, 4, 8, 2],
[1, 3, 2, 9],
[2, 2, 4, 8],
[2, 4, 9, 1],
[2, 2, 9, 3]]
# Length of the 3rd and 2nd dimension.
nz = arr[-1][0] + (arr[0][0]==0)
ny = int(len(arr)/nz)
res = [[arr[ny*z_idx+y_idx][1:] for y_idx in range(ny)] for z_idx in range(nz)]
OUTPUT:
[[[3, 4, 6], [4, 8, 2], [3, 2, 9]], [[2, 4, 8], [4, 9, 1], [2, 9, 3]]]
Note that the calculation of nz takes into account that the 3rd dimension index in your array is either 0-based (as python is per default) or 1-based (as you show in your example).
Let's say I have this numpy matrix:
>>> mat = np.matrix([[3,4,5,2,1], [1,2,7,6,5], [8,9,4,5,2]])
>>> mat
matrix([[3, 4, 5, 2, 1],
[1, 2, 7, 6, 5],
[8, 9, 4, 5, 2]])
Now let's say I have some indexes in this form:
>>> ind = np.matrix([[0,2,3], [0,4,2], [3,1,2]])
>>> ind
matrix([[0, 2, 3],
[0, 4, 2],
[3, 1, 2]])
What I would like to do is to get three values from each row of the matrix, specifically values at columns 0, 2, and 3 for the first row, values at columns 0, 4 and 2 for the second row, etc. This is the expected output:
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
I've tried using np.take but it doesn't seem to work. Any suggestion?
This is take_along_axis.
>>> np.take_along_axis(mat, ind, axis=1)
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
This will do it: mat[np.arange(3).reshape(-1, 1), ind]
In [245]: mat[np.arange(3).reshape(-1, 1), ind]
Out[245]:
matrix([[3, 5, 2],
[1, 5, 7],
[5, 9, 4]])
(but take_along_axis in #user3483203's answer is simpler).
if this is given on a homework assignment:
import numpy as np
room_matrix = \
np.array(
[[6, 3, 4, 1],
[5, 2, 3, 2],
[8, 3, 6, 2],
[5, 1, 3, 1],
[10, 4, 7, 2]])
and the task is:
write an expression that retrieves the following submatrix from room_matrix:
array([[2,3],
[3,6]])
I have done this so far:
a=room_matrix[1,1:3]
b=room_matrix[2,1:3]
then I print "a" and "b" and the output is:
[2 3]
[3 6]
but I want them to be executed as an actual subarray like so:
array([[2,3],
[3,6]])
Can I concatenate "a" and "b"? Or is there another way to extract a sub array so that the output actually shows it as an array, and not just me printing two splices? I hope this makes sense. Thank you.
You needn't do that in two lines. Numpy allows you to splice within a single statement, like this:
room_matrix[1:3, 1:3]
#will slice rows starting from 1 to 2 (row numbers start at 0), likewise for columns
How about this:
In [1]: import numpy as np
In [2]: room_matrix = \
...: np.array(
...: [[6, 3, 4, 1],
...: [5, 2, 3, 2],
...: [8, 3, 6, 2],
...: [5, 1, 3, 1],
...: [10, 4, 7, 2]])
In [3]: room_matrix
Out[3]:
array([[ 6, 3, 4, 1],
[ 5, 2, 3, 2],
[ 8, 3, 6, 2],
[ 5, 1, 3, 1],
[10, 4, 7, 2]])
In [4]: room_matrix[1:3, 1:3]
Out[4]:
array([[2, 3],
[3, 6]])
The question has already been answered, so just throwing it out there, but, indeed, you could use np.vstack to "concatenate" your a and b matrices to get the desired result:
In [1]: import numpy as np
In [2]: room_matrix = \
...: np.array(
...: [[6, 3, 4, 1],
...: [5, 2, 3, 2],
...: [8, 3, 6, 2],
...: [5, 1, 3, 1],
...: [10, 4, 7, 2]])
In [3]: a=room_matrix[1,1:3]
In [4]: b=room_matrix[2,1:3]
In [5]: np.vstack((a,b))
Out[5]:
array([[2, 3],
[3, 6]])
I have a matrix of the following form:
a=[[1 2 3 4]
[2 5 7 6]
[5 4 2 1]]
(for example).
Firstly, what are each of the elements called? ie. what type of object is [1 3 4] (a list without the commas). Secondly, how would I go about turning each of these 'things' into a list, so that the matrix reads:
b=[[1, 2, 3, 4]
[2, 5, 7, 6]
[5, 4, 2, 1]]
?
I started with a list of lists and then used insert to replace each list with a matrix of that list and it turned it into a. However, it was necessary for me to do so as I needed to keep lists in order and multiply by a matrix.
Thank you in advance for any help!
The object is a numpy.matrix and is converted to a list simply as:
as_list = a.tolist()
As thefourtheye says, it's probably a Numpy ndarray.
Following the insight from earlier answers, this quick demo might answer your question
>>>import numpy as np
>>> a = np.array([[1, 2, 3, 4], [2, 5, 7, 6], [5, 4, 2, 1]])
>>> a
array([[1, 2, 3, 4],
[2, 5, 7, 6],
[5, 4, 2, 1]])
>>> print(a)
[[1 2 3 4]
[2 5 7 6]
[5 4 2 1]]
>>> b = list(a)
>>> print(b)
[array([1, 2, 3, 4]), array([2, 5, 7, 6]), array([5, 4, 2, 1])]
>>> b = [list(e) for e in a]
>>> b
[[1, 2, 3, 4], [2, 5, 7, 6], [5, 4, 2, 1]]
Thus '[1 3 4]' is called a numpy array.
-- revised for matrix --
>>> a = np.matrix([[1, 2, 3, 4], [2, 5, 7, 6], [5, 4, 2, 1]])
>>> a
matrix([[1, 2, 3, 4],
[2, 5, 7, 6],
[5, 4, 2, 1]])
>>>
>>> print(a)
[[1 2 3 4]
[2 5 7 6]
[5 4 2 1]]
>>> a.tolist()
[[1, 2, 3, 4], [2, 5, 7, 6], [5, 4, 2, 1]]