title: How to load in graph from networkx into PyTorch geometric and set node features and labels?
body: <p><strong>Goal:</strong> I am trying to import a graph <strong>FROM</strong> networkx into PyTorch geometric and <strong>set labels and node features</strong>.</p>
  <p>(This is in Python)</p>
  <p><strong>Question(s)</strong>:</p>
  <ol>
  <li>How do I do this [the conversion from networkx to PyTorch geometric]? (presumably by using the <code>from_networkx</code> function)</li>
  <li><strong>How do I transfer over node features and labels?</strong> (more important question)</li>
  </ol>
  <p>I have seen some other/previous posts with this question but they weren't answered (correct me if I am wrong).</p>
  <p><strong>Attempt</strong>: (I have just used an unrealistic example below, as I cannot post anything real on here)</p>
  <p>Let us imagine we are trying to do a graph learning task (e.g. node classification) on a group of cars (not very realistic as I said). That is, we have a group of cars, an adjacency matrix, and some features  (e.g. price at the end of the year). We want to predict the node label (i.e. brand of the car).</p>
  <p>I will be using the following adjacency matrix: (apologies, cannot use latex to format this)</p>
  <p>A = [(0, 1, 0, 1, 1), (1, 0, 1, 1, 0), (0, 1, 0, 0, 1), (1, 1, 0, 0, 0), (1, 0, 1, 0, 0)]</p>
  <p>Here is the code (for Google Colab environment):</p>
  <pre><code>import pandas as pd
  import numpy as np
  import matplotlib.pyplot as plt
  import networkx as nx
  from torch_geometric.utils.convert import to_networkx, from_networkx
  import torch
  
  !pip install torch-scatter torch-sparse torch-cluster torch-spline-conv torch-geometric -f https://data.pyg.org/whl/torch-1.10.0+cpu.html
  
  # Make the networkx graph
  G = nx.Graph()
  
  # Add some cars (just do 4 for now)
  G.add_nodes_from([
        (1, {'Brand': 'Ford'}),
        (2, {'Brand': 'Audi'}),
        (3, {'Brand': 'BMW'}),
        (4, {'Brand': 'Peugot'}),
        (5, {'Brand': 'Lexus'}),
  ])
  
  # Add some edges
  G.add_edges_from([
                    (1, 2), (1, 4), (1, 5),
                    (2, 3), (2, 4),
                    (3, 2), (3, 5), 
                    (4, 1), (4, 2),
                    (5, 1), (5, 3)
  ])
  
  # Convert the graph into PyTorch geometric
  pyg_graph = from_networkx(G)
  </code></pre>
  <p>So this correctly converts the networkx graph to PyTorch Geometric. However, I still don't know how to properly set the labels.</p>
  <p>The brand values for each node have been converted and are stored within:</p>
  <pre><code>pyg_graph.Brand
  </code></pre>
  <p>Below, I have just made some random numpy arrays of length 5 for each node (just pretend that these are realistic).</p>
  <pre><code>ford_prices = np.random.randint(100, size = 5)
  lexus_prices = np.random.randint(100, size = 5)
  audi_prices = np.random.randint(100, size = 5)
  bmw_prices = np.random.randint(100, size = 5)
  peugot_prices = np.random.randint(100, size = 5)
  </code></pre>
  <p>This brings me to the main question:</p>
  <ul>
  <li>How do I set the prices to be the node features of this graph?</li>
  <li>How do I set the labels of the nodes? (and will I need to remove the labels from <code>pyg_graph.Brand</code> when training the network?)</li>
  </ul>
  <p>Thanks in advance and happy holidays.</p>