How to get the difference between two sparse matrices? - python

I have two sparse matrix, and want to get the difference between the two sparse matrix.
import pandas as pd
import numpy as np
from scipy.sparse import csr_matrix,find
row = np.array([0, 0, 1, 2, 2, 2])
col = np.array([0, 2, 2, 0, 1, 2])
data = np.array([1, 2, 3, 4, 5, 6])
sparse_matrix = csr_matrix((data, (row, col)), shape=(3, 3))
print(sparse_matrix.toarray())
row2= np.array([0, 1, 1, 0, 1])
col2= np.array([0, 0, 1, 2, 2])
data2= np.array([1, 4, 5, 2, 6])
sparse_matrix2 =csr_matrix((data2,(row2, col2)),shape=(2,3))
print(sparse_matrix2.toarray())
Output:
[[1 0 2]
[0 0 3]
[4 5 6]]
[[1 0 2]
[4 5 6]]
Expected:
I want to get a sparse matrix, and it's dense matrix is shown as follow.
[[0 0 3]]

Related

how to append a 1d numpy array to a 2d numpy array python

I would like to append an array [3, 3, 3] to an array [[1, 1, 1], [2, 2, 2]], so that it becomes [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
Here is my code:
import numpy as np
arr1 = np.array([[1, 1, 1],
[2, 2, 2]])
arr2 = np.append(arr1, [3, 3, 3])
print (arr2)
instead of printing [[1, 1, 1], [2, 2, 2], [3, 3, 3]],
it prints [1, 1, 1, 2, 2, 2, 3, 3, 3].
I am quite new to numpy and I do not understand why the 2d array suddenly becomes 1d.
You can use the useful numpy's standard method of vstack.
Here is my code.
Initialize 2-dimensional numpy array
initial_array = np.array([
[1, 1, 1],
[2, 2, 2]
])
define the array to append to initiali array
new_array = np.array([3, 3, 3])
append the new array to initial array as row
result = np.vstack((initial_array, new_array))
this is the result
print(result)
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]])
You can read more details at here.
Hope this answer to be helpful for you. Thanks.
import numpy as np
arr1 = np.array([[1, 1, 1],
[2, 2, 2]])
arr2 = np.append(arr1, [[3, 3, 3]], axis=0)
print (arr2)
Output:
[[1 1 1]
[2 2 2]
[3 3 3]]
Use numpy.vstack:
arr2 = np.vstack((arr1, [3,3,3]))
>>> arr2
[[1 1 1]
[2 2 2]
[3 3 3]]

Swapping columns in a numpy array by a given indexs

I am trying to change column position of a matrix by a given indexs of array
import numpy as np
t = np.array([[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4],
[0, 1, 2, 3, 4]])
indexs = np.array([3, 4, 2, 1, 0])
check = [False for i in range(len(indexs))]
for i in range(len(indexs)):
check[i] = True
if (i != indexs[i] and check[indexs[i]] == False):
check[indexs[i]] = True
t[:, [i, indexs[i]]] = t[:, [indexs[i], i]]
print(t)
The result I want:
[[3 4 2 1 0]
[3 4 2 1 0]
[3 4 2 1 0]
[3 4 2 1 0]]
I want to return an array whose column positions is the same as indexs but I can't.
How can I achieve that?
Just index the array along the dimension you want:
t[:, indexs]
if you transpose the matrix it's easy
transposed = t.T
result = np.array([transposed[i] for i in indexs])
result = result.T
array([[3, 4, 2, 1, 0],
[3, 4, 2, 1, 0],
[3, 4, 2, 1, 0],
[3, 4, 2, 1, 0]])

Python, Numpy: How to change two vectors at once

I'm trying to change two vectors of a NumPy matrix at once, but I'm losing one vector components:
import numpy as np
data = np.array([[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]])
last = data[:, -1]
print(last)
data[:, 1:] = data[:, :-1]
data[:, 0] = last
print(data)
Gives this result:
[4 4 4 4]
[[3 1 2 3]
[3 1 2 3]
[3 1 2 3]
[3 1 2 3]]
But I want to maintain the 4s in the first column. Is there any form to accomplish that?
Use:
>>> np.roll(data, 1, 1)
array([[4, 1, 2, 3],
[4, 1, 2, 3],
[4, 1, 2, 3],
[4, 1, 2, 3]])

numpy indexing operations for 3D matrix

Is there an elegant/quick way to reproduce this without the for loops? I'm looking to have a 3D matrix of values, and and 2D matrix that gives the indices for which to copy the 3rd dimensions' values while creating a new 3D matrix of the same shape. Here is an implementation with a lot of loops.
np.random.seed(0)
x = np.random.randint(5, size=(2, 3, 4))
y = np.random.randint(x.shape[1], size=(3, 4))
z = np.zeros((2, 3, 4))
for i in range(x.shape[0]):
for j in range(x.shape[1]):
z[i, j, :] = x[i, y[i, j], :]
This puzzled me for a bit, until I realized you aren't using all of y. y is (3,4), but you are indexing over (2,3):
In [28]: x[np.arange(2)[:,None], y[:2,:3],:]
Out[28]:
array([[[4, 0, 0, 4],
[4, 0, 3, 3],
[3, 1, 3, 2]],
[[3, 0, 3, 0],
[2, 1, 0, 1],
[1, 0, 1, 4]]])
We could use all of y with:
In [32]: x[np.arange(2)[:,None,None],y,np.arange(4)]
Out[32]:
array([[[4, 0, 3, 2],
[4, 0, 3, 2],
[3, 0, 0, 3]],
[[3, 1, 1, 4],
[3, 1, 1, 4],
[1, 1, 3, 1]]])
the 3 indexes broadcast to (2,3,4). But the selection is different from your z.

Applying a mask to an multidimensional array

I want to do this in a proper way:
data = np.array(data)
data =[
[1, 1, 2, 1],
[0, 1, 3, 2],
[0, 2, 3, 2],
[2, 4, 3, 1],
[0, 2, 1, 4],
[3, 1, 4, 1]]
this should become (delete the lines that start with 0):
[1, 1, 2, 1]
[2, 4, 3, 1]
[3, 1, 4, 1]
So far I did it like this:
lines = []
for i in range(0, len(data[0])):
if data[0,i] != 0:
lines.append(data[:,i])
lines = np.array(lines)
Then I found this fine method:
mask = 1 <= data[0,:]
and now I want to apply that mask to that array. This Mask reads: [True, False, False, True, False, True]. How do I do that?
Why not just:
[ar for ar in data if ar[0] != 0]
This assumes that arrays are not empty.
I presume you have a numpy array based on the data[0,:] and data[0,i] you have in your question and you mean data[:, 0] :
import numpy as np
data = np.array([
[1, 1, 2, 1],
[0, 1, 3, 2],
[0, 2, 3, 2],
[2, 4, 3, 1],
[0, 2, 1, 4],
[3, 1, 4, 1]])
data = data[data[:,0] != 0]
print(data)
Output:
[[1 1 2 1]
[2 4 3 1]
[3 1 4 1]]
data[0,:] is the first row [1 1 2 1] not the first column
Using List comprehension
In [56]: [elem for elem in data if elem[0] !=0]
Out[56]: [[1, 1, 2, 1], [2, 4, 3, 1], [3, 1, 4, 1]]

Categories