I am trying to change a two dimensional array to one dimensional, my code is like this:
x = np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])
x = x.flatten()
however, I found that flatten function works well on
x = np.array([[1, 2], [3, 4]])
but it does not work on
x = np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])
could anyone help me to change
np.array([[1, 2, 4], [3, 4], [1,2,3,4,5,6,7]])
to
np.array([[1, 2, 4, 3, 4, 1,2,3,4,5,6,7])
thank you
In this C# Program, we are reading the elements of the 2-Dimensional matrix. Using for loop assign the value of 'a[i,j]' variable to b[] array variable. Increment the value of base index 'k' variable. Print the value of one dimensional array.
I write this method for swapping two elements in a 2D array: public void swap(Object[][] array, int a, int b) { Object temp; temp = array[a]; array[a] = array[b]; array[b] = temp; // Error, Why? }
Convert a 1D array to a 2D Numpy array using numpy. Here, we are using np. reshape to convert a 1D array to 2 D array. You can divide the number of elements in your array by ncols.
You can try using concatenate
(numpy documentation):
flatten_x = np.concatenate(x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With