Rodolfo
Rodolfo

Reputation: 31

Remove elements from numpy array by value

I am analyzing a network using networkx. The networkx is stored in pandas dataframe, to create the graph I use a set of data q stored in numpy array 5053x5053, then assign the serial numbers (taken from a file stored in numpy array serials) to index and columns :

Import networkx as nx
Import pandas as pd

network = pd.DataFrame (data = q, index = serials, columns = serials)

G = nx.from_numpy_matrix(network.values)

then i relabel the nodes:

G = nx.relabel_nodes(G, dict(enumerate(network.columns)))

I have a subgraph given by green products, and I have stored their serial numbers in another file imported like a numpy array (named greens). I need to have a new array to work on the two subgraph green and no green removing all the elements of green from serials.

Is there a function in numpy or should I use dicitonaries(how?)?

EDIT: Here's a sample. Serial is a numpy array with 5053 values:

[ 10110  10190  10210 ... 970500 970600 999999]

Greens is a numpy array with 255 values:

[840410 840510 841410 ... 903210 460120 843680]

I would like to have a new array with 5053-255 values removing from serial the elements of greens. Example: Import numpy as np

a = np.array([1,5,7,9,45,52,59])
b = np.array ([1,9,52])

The output I would like to get is

c = np.array([5,7,45,59])

Upvotes: 0

Views: 214

Answers (1)

Dishin H Goyani
Dishin H Goyani

Reputation: 7723

Use np.setdiff1d - Return the unique values in a that are not in b.

a = np.array([1,5,7,9,45,52,59])
b = np.array ([1,9,52])

c = np.setdiff1d(a,b)
c
array([ 5,  7, 45, 59])

Upvotes: 2

Related Questions