Splitting columns of a numpy array easily - python

How can I split an array's columns into three arrays x, y, z without manually writing each of the [:,0],[:,1],[:,2] separately?
Example
# Create example np array
import numpy as np
data = np.array([[1,2,3],[4,5,6],[7,8,9]])
Now data is
[[1 2 3]
[4 5 6]
[7 8 9]]
What I want to do:
x, y, z = data[:,0], data[:,1], data[:,2] ## Help me here!
print(x)
Wanted output:
array([1, 4, 7])

Transpose, then unpack:
>>> x, y, z = data.T
>>> x
array([1, 4, 7])

You don't need to slice it.
>>> import numpy as np
>>> data = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> x, y, z = data.T
>>> x
array([1, 4, 7])
>>> y
array([2, 5, 8])
>>> z
array([3, 6, 9])

Related

Python matrix row shifting(Also column)

Is there any way that I can shift specific row in python? (It would be nice if numpy is used).
I want
[[1,2],
[3,4]]
to be
[[1,2],
[4,3]].
Also for the column would be nice!
[[1,2],
[3,4]]
to be
[[1,4],
[3,2]]
.
Thank you.
np.roll is your friend.
>>> import numpy as np
>>> x = np.array([[1,2],[3,4]])
>>> x
array([[1, 2],
[3, 4]])
>>> x[1]
array([3, 4])
>>> np.roll(x[1],1)
array([4, 3])
>>> x[1] = np.roll(x[1],1)
>>> x
array([[1, 2],
[4, 3]])
>>> x[1] = np.roll(x[1],1)
>>> x[:,1]
array([2, 4])
>>> x[:,1] = np.roll(x[:,1],1)
>>> x
array([[1, 4],
[3, 2]])
>>>
For the row shifting you can do:
import numpy as np
matrix = np.random.rand(5, 5)
row_no = 2
matrix[row_no, :] = np.array([matrix[row_no, -1]] + list(matrix[row_no, :-1]))
and similarly for column shifting you can simply switch the dimension orders.
import numpy as np
matrix = np.random.rand(5, 5)
col_no = 2
matrix[:, col_no] = np.array([matrix[-1, col_no]] + list(matrix[:-1, col_no]))

Inconsistent Numpy array aliasing behavior

The following behavior is expected and is what I get. This is consistent with how aliasing works for native Python objects like lists.
>>> x = np.array([1, 2, 3])
>>> y = x
>>> x
array([1, 2, 3])
>>> y
array([1, 2, 3])
>>> x = x + np.array([2, 3, 4])
>>> x
array([3, 5, 7])
>>> y
array([1, 2, 3])
But the following behavior is unexpected by changing x = x + np.array([2, 3, 4]) to x += np.array([2, 3, 4])
>>> x += np.array([2, 3, 4])
>>> x
array([3, 5, 7])
>>> y
array([3, 5, 7])
The Numpy version is 1.16.4 on my machine. Is this a bug or feature? If it is a feature how x = x + np.array([2, 3, 4]) differs from x += np.array([2, 3, 4])
Your line y = x doesn't create a copy of the array; it simply tells y to point to the same data as x, which you can see if you look at their ids:
x = np.array([1,2,3])
y = x
print(id(x), id(y))
(140644627505280, 140644627505280)
x = x + np.array([2, 3, 4]) will do a reassignment of x to a new id, while x += np.array([2, 3, 4]) will modify it in place. Thus, the += will also modify y, while x = x + ... won't.
x += np.array([2, 3, 4])
print(id(x))
print(x, y)
x = x + np.array([2, 3, 4])
print(id(x))
print(x, y)
140644627505280
[3 5 7] [3 5 7]
140644627175744
[ 5 8 11] [3 5 7]

Appending arrays with NumPy

Using NumPy, I want to create an n-by-2 array, by starting with an empty array, and adding on some 1-by-2 arrays.
Here's what I have tried so far:
x = np.array([1, 2])
y = np.array([3, 4])
z = np.array([])
z = np.append(z, x)
z = np.append(z, y)
However, this gives me:
z = [1, 2, 3, 4]
What I want is:
z = [[1, 2], [3, 4]]
How can I achieve this?
import numpy as np
x = np.array([1, 2])
y = np.array([3, 4])
z = np.append([x],[y], axis=0)
print(z)
>>> [[1 2]
[3 4]]
No need to create the array before appending, axis=0 will allow you to append row wise.
The previous works if z is not an array already. From then on you specify z as the original array and append the other array as such:
t = np.array([5, 6])
z = np.append(z,[t], axis=0)
print(z)
[[1 2]
[3 4]
[5 6]]
You can simply use np.array :
>>> np.array((x,y))
array([[1, 2],
[3, 4]])

Condition on numpy arrays

I have two arrays with the same number of elements
X = [1,2,3,4,5,6,7,8,9]
Y = [10,4,3,7,7,3,1,8,98]
I would like to keep the elements of X and Y such as 2<X<7. How can I do?
Ok it works well with
Y = Y[np.logical_and(X>2, X<5)]
X = X[np.logical_and(X>2, X<5)]
Thanks a lot!
You can use numpy.logical_and:
>>> X = np.array([1,2,3,4,5,6,7,8,9])
>>> X[np.logical_and(X>2, X<7)]
array([3, 4, 5, 6])
you can use a loop and if ,and also you can use set() for keep the deferent indexes :
>>> X = [1,2,3,4,5,6,7,8,9]
>>> Y = [10,4,3,7,7,3,1,8,98]
>>> X=[i for i in X if 2<i<7]
>>> Y=[i for i in Y if 2<i<7]
>>> X
[3, 4, 5, 6]
>>> Y
[4, 3, 3]
>>> set(Y)
set([3, 4])

Scipy interpolate 2D with one value

I would like to interpolate X, Y, Z in Python and return the interpolated results X, Y with z. For example
from scipy.interpolate import interp2d
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
z = [1, 2, 3, 4, 5]
i = interp2d(x, y, z)
i(1, 2)
Out: array([ 1.5])
So, here, the interp2d object takes X and Y to get Z. I would like to give Z to get X and Y. I think I might need a spline function.

Categories