<jupyter_start><jupyter_text>Scikit-Graph A simple graph machine learning example using sklearn and scikit-graph's transformers.<jupyter_code># Importing the packages and functions.
import scikitgraph as sg
import pandas as pd
import networkx as nx
import numpy as np

from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import accuracy_score
# Set a seed.
np.random.seed(31415) 
# Generate a Graph and a dataframe.
G = nx.karate_club_graph()
f = pd.DataFrame(data = {'name': range(34)}) # Each row of the dataframe corresponds to a node and each column to a feature. In this case there are no features, just the name of the node.
f["target"] = [1 if G.node[i]['club'] == 'Officer' else 0  for i in G.nodes] # The target of our machine learning model.
# Defining the transformers.
Degree = sg.Degree(G)
DropName = sg.DropName()
Pagerank = sg.Pagerank(G)
Centrality = sg.Centrality(G)
Communities_label_propagation = sg.Communities_label_propagation(G)
Communities_greedy_modularity = sg.Communities_greedy_modularity(G)
# Create the pipline object.
steps = [("Degree", Degree), ("Pagerank", Pagerank), ("Centrality", Centrality), 
        ("CLP", Communities_label_propagation), ("CGM", Communities_greedy_modularity),
         ("DropName", DropName), ('SVM', SVC())]

pipeline = Pipeline(steps)
# Split the dataset.
X = f.drop(['target'],axis=1)
Y = f['target']
X_train, X_test, y_train, y_test = train_test_split(X,Y,test_size=0.2, random_state=30, stratify=Y)
# Perform a randomized search with cross validation in order to find the best hyperparameters for the support vector machine model (SVM).
parameteres = {'SVM__C':[0.001,0.1,10,100,10e5], 'SVM__gamma':[0.1,0.01]}
random_search = RandomizedSearchCV(pipeline, param_distributions = parameteres, cv=3, n_iter = 4)
random_search.fit(X_train, y_train)
# The score of the best model.
y_pred = random_search.best_estimator_.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print('Score: ', accuracy)<jupyter_output>Score:  1.0
{'fraction_non_alphanumeric': 0.10815765352887259, 'fraction_numerical': 0.01695692025664528, 'mean_word_length': 5.97444089456869, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '239346', 'license': 'MIT', 'n_tokens_mistral': 762, 'n_tokens_neox': 727, 'n_words': 216, 'path': 'Examples/ScikitGraph_Tutorial.ipynb', 'repo_name': 'fedealbanese/scikitgraph', 'size': '2182'}