Why isn't flatten working? I have looked at example code and I am doing exactly what they are doing in the example. I've even copied their code and ran it but the array still doesn't come out as a flattened array.
I don't know if it matters but I am running Python 3.7.4.
code:
import numpy as np
array1 = np.array([[1, 2, 3, 2, 5, 8], [9, 5, 1, 7, 5, 3]])
array1.flatten()
print(array1)
output:
[[1 2 3 2 5 8]
[9 5 1 7 5 3]]
desired output:
[1 2 3 2 5 8 9 5 1 7 5 3]
array1.flatten() returns the flattened array but does not change in place. Try equating it back should work.
Code:
import numpy as np
array1 = np.array([[1, 2, 3, 2, 5, 8], [9, 5, 1, 7, 5, 3]])
array1 = array1.flatten()
print(array1)
You have to assign the array1.flatten() to a variable, so something like this could work array2 = array1.flatten().
Related
I had to make a matrix using numpy.array method. How can I now update every third element of my matrix? I have made a for loop for the problem but that is not the optimal solution. Is there a way to avoid loops? For example if I have this matrix:
matrix = np.array([[1,2,3,4],
[5,6,7,8],
[4,7,6,9]])
is there a way to add 1 to every third element and get this matrix:
[[2,2,3,5],[5,6,8,8],[4,8,6,9]]
Solution:
matrix = np.ascontiguousarray(matrix)
matrix.ravel()[::3] += 1
Why does the ascontiguousarray is needed? Because matrix may not be c-contiguous (for example matrix may have fortran-order - column major). It that case ravel returns a copy instead of a view so a simple inplace operation matrix.ravel()[::3] += 1 will not work as expected.
Example 1
import numpy as np
arr = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[4, 7, 6, 9]])
arr.ravel()[::3] += 1
print(arr)
Works as expected:
[[2 2 3 5]
[5 6 8 8]
[4 8 6 9]]
Example 2
But with fortran-order
import numpy as np
arr = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[4, 7, 6, 9]])
arr = np.asfortranarray(arr)
arr.ravel()[::3] += 1
print(arr)
produces:
[[1 2 3 4]
[5 6 7 8]
[4 7 6 9]]
Example 3
Will work as expected in both cases
import numpy as np
arr = np.array([
[1, 2, 3, 4],
[5, 6, 7, 8],
[4, 7, 6, 9]])
# arr = np.asfortranarray(arr)
arr = np.ascontiguousarray(arr)
arr.ravel()[::3] += 1
print(arr)
I would like to expand a list of arrays into a single array, so for example:
a = [array([1,2,3]), array([4,5,6]), array([7,8,9,])]
To become:
a = [array([1,2,3,4,5,6,7,8,9])]
How do I do this?
One option is to convert list to np.array and then flatten inside the list:
>>> import numpy as np
>>> arr = [np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9,])]
>>> [np.array(arr).flatten()]
[array([1, 2, 3, 4, 5, 6, 7, 8, 9])]
Try using
list.extend
It will work
Maybe you want this
from numpy import array
k=[array([1,2,3]), array([4,5,6]), array([7,8,9,])]
l=[]
for i in range(len(k)):
l.extend(k[i])
print(array(l))
Output:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
You can use reshape of Numpy to do it:-
a=[[1,2,3],[3,4,5],[6,7,8]]
print("Before:" , a)
import numpy as np
a=np.reshape(a,9)
print("After:",a)
The output:
Before: [[1, 2, 3], [3, 4, 5], [6, 7, 8]]
After: [1 2 3 3 4 5 6 7 8]
Hope this is what you want.
This question already has answers here:
Numpy random choice to produce a 2D-array with all unique values
(3 answers)
Closed 4 years ago.
I need to create a N x M numpy array of randomly shuffled numbers 0 to N. That is to say that each row of the matrix has exactly one of each numbers 0 to N in a random order with no repeating elements and every number is represented.
The best I've been able to come up with so far is (for N = 10 and M = 5)
import random
import numpy as np
a = np.array([random.sample(range(10), 10) for x in range(5)])
which gives me
[[5 9 1 3 8 2 6 4 0 7]
[4 8 5 2 9 3 7 6 0 1]
[8 4 6 7 9 2 0 5 1 3]
[3 5 4 9 2 0 6 7 1 8]
[6 0 4 7 3 2 1 8 5 9]]
My current solution does work, but I'd like to only have one random library if possible (I'm using numpy elsewhere so a numpy only solution would be ideal). I'm assuming numpy has an easier way to do this, but I'm currently missing it.
If the issue is simply to only use one random library (as you said in your comments), you could just use numpy.random.choice with the argument replace=False, which ensures each element is not repeated:
import numpy as np
a = np.stack([np.random.choice(range(10), 10, replace=False) for _ in range(5)])
>>> a
array([[0, 9, 3, 5, 8, 7, 1, 4, 6, 2],
[5, 6, 8, 3, 0, 4, 7, 9, 2, 1],
[7, 4, 9, 5, 0, 1, 6, 8, 3, 2],
[9, 0, 3, 8, 5, 7, 6, 1, 4, 2],
[5, 6, 0, 1, 3, 4, 9, 8, 7, 2]])
I have a numpy array created such as:
x = np.array([[1,2,3,4],[5,6,7,8]])
y = np.asarray([x])
which prints out
x=[[1 2 3 4]
[5 6 7 8]]
y=[[[1 2 3 4]
[5 6 7 8]]]
What I would like is an array such as
[0 [[1 2 3 4]
[5 6 7 8]]]
What's the easiest way to go about this?
Thanks!
To do what you're asking, just use the phrase
labeledArray = [0, x]
This way, you will get a standard list with 0 as the first element and a Numpy array as the second element.
However, in practice, you are probably trying to label for the purpose of later recall. In that case, I'd recommend you use a dictionary, as it is less confusing to keep track of:
myArrays = {}
myArrays[0] = x
Which can be used as follows:
>>> myArrays
{0: array([[1, 2, 3, 4],
[5, 6, 7, 8]])}
>>> myArrays[0]
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
I want to concatenate numpy matrices that have different shapes in order to get an array with dimension=3.
example :
A= [[2 1 3 4]
[2 4 0 6]
[9 5 7 4]]
B= [[7 2 8 4]
[8 6 8 6]]
and result what I need should be like that:
C=[[[2 1 3 4]
[2 4 0 6]
[9 5 7 4]]
[[7 2 8 4]
[8 6 8 6]]]
Thanks for help
If I understand your question correctly, a 3dim numpy array is probably not the way to represent your data, because there's no definitive shape.
A 3dim numpy array should have a shape of the form N1 x N2 x N3, whereas in your case each "2dim row" has a different shape.
Alternatives would be to keep your data in lists (or a list of arrays), or to use masked arrays, if that happens to be reasonable in you case.
You can only convert to a 3D np.ndarray in a useful manner if A.shape == B.shape. In that case all you need to do is e.g. C = np.array([A, B]).
import numpy as np
A = np.array([[2, 1, 3, 4],
[9, 5, 7, 4]])
B = np.array([[7, 2, 8, 4],
[8, 6, 8, 6]])
C = np.array([A, B])
print C
Because A and B have different sizes (# of rows), the best you can do make an array of shape (2,) and dtype object. Or at least that's what a simple construction gives you:
In [9]: np.array([A,B])
Out[9]:
array([array([[2, 1, 3, 4],
[2, 4, 0, 6],
[9, 5, 7, 4]]),
array([[7, 2, 8, 4],
[8, 6, 8, 6]])], dtype=object)
But constructing an array like this doesn't help much. Just use the list [A,B].
np.vstack([A,B]) produces a (5,4) array.
np.array([A[:2,:],B]) gives a (2,2,4) array. Or you could pad B so they are both (3,4).
So one way or other you need to redefine your problem.