<jupyter_start><jupyter_text>Machine learning models for magnetic and topological property prediction in transition metal oxides.* Build a classifier that predicts FM vs AFM ground states from structure and composition features.* Build a classifier (using structure and composition features) that predicts if a magnetic material has nontrivial band topology for some value of Hubbard $U$.This notebook accompanies (Frey *et al*., High-throughput search for magnetic and topological order in transition metal oxides)<jupyter_code>__author__ = "<NAME>"
__copyright__ = "MIT License"
__version__ = "0.0.1"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__date__ = "May 5 2020"
# Check requirements.txt for version requirements
import pandas as pd
import numpy as np

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import KFold
from sklearn.metrics import f1_score, classification_report

from joblib import dump, load

from monty.serialization import loadfn, dumpfn<jupyter_output><empty_output><jupyter_text>Machine learning model for predicting magnetic ground state ordering<jupyter_code>datadir = "data/"
modeldir = "models/"
X_train = pd.read_json(datadir + 'X_train.json')
X_train = X_train.values
y_train = loadfn(datadir + 'y_train.json')<jupyter_output><empty_output><jupyter_text>Most important features to consider are values and averages of:* Number of magnetic sublattices  * Sine Coulomb Matrix  * Nearest-neighbor and next-nearest-neighbor distances between magnetic ions * Number of unfilled $d$ orbitals* Coordination number of magnetic ions  * Structural complexity per atom* Spacegroup number<jupyter_code>clf = load(modeldir + 'mag_clf.joblib')
fm_scores = []
afm_scores = []

kf = KFold(n_splits=5, shuffle=True)
kf.get_n_splits(X_train, y_train)

for k, (train, test) in enumerate(kf.split(X_train, y_train)):
    clf.fit(X_train[train], y_train[train])
    f1s_fm = f1_score(y_train[test], clf.predict(X_train[test]), pos_label=1)
    f1s_afm = f1_score(y_train[test], clf.predict(X_train[test]), pos_label=0)
    fm_scores.append(f1s_fm)
    afm_scores.append(f1s_afm)
    
    print("[fold {0}] FM score: {1:.3f}".format(k, f1s_fm))
    print("[fold {0}] AFM score: {1:.3f}".format(k, f1s_afm))

fm_scores = np.array(fm_scores)
afm_scores = np.array(afm_scores)
print('FM Mean: %.2f median: %.2f stdev: %.2f' % (np.mean(fm_scores), np.median(fm_scores), np.std(fm_scores)))
print('AFM Mean: %.2f median: %.2f stdev: %.2f' % (np.mean(afm_scores), np.median(afm_scores), np.std(afm_scores)))
X_test = pd.read_json(datadir + 'X_test.json')
X_test = X_test.values
y_test = loadfn(datadir + 'y_test.json')
round(clf.score(X_test, y_test), 2)
print(classification_report(y_test, clf.predict(X_test), target_names=["AFM", "FM"]))<jupyter_output>precision    recall  f1-score   support

         AFM       0.88      0.84      0.86       236
          FM       0.59      0.67      0.63        79

    accuracy                           0.80       315
   macro avg       0.74      0.76      0.75       315
weighted avg       0.81      0.80      0.80       315<jupyter_text>Machine learning model for magnetic topological phase classificationMAGNDATA and appropriate citations available from:* https://arxiv.org/ftp/arxiv/papers/2003/2003.00012.pdf and* https://www.topologicalquantumchemistry.fr/magnetic/index.html* http://webbdcrista1.ehu.es/magndata/<jupyter_code>X_train = pd.read_json(datadir + 'mtm_X_train.json')
X_train = X_train.values
y_train = loadfn(datadir + 'mtm_y_train.json')<jupyter_output><empty_output><jupyter_text>Most important features to consider are values and averages of symmetry- and orbital-related descriptors. Some examples are:* Number of $d$ valence electrons* Spacegroup number* Number of unfilled $f$ orbitals* Crystal system<jupyter_code>clf = load(modeldir + 'mag_topo_clf.joblib')
topo_scores = []
triv_scores = []

kf = KFold(n_splits=5, shuffle=True)
kf.get_n_splits(X_train, y_train)

for k, (train, test) in enumerate(kf.split(X_train, y_train)):
    clf.fit(X_train[train], y_train[train])
    f1s_topo = f1_score(y_train[test], clf.predict(X_train[test]), pos_label=1)
    f1s_triv = f1_score(y_train[test], clf.predict(X_train[test]), pos_label=0)
    topo_scores.append(f1s_topo)
    triv_scores.append(f1s_triv)
    
    print("[fold {0}] Topological score: {1:.3f}".format(k, f1s_topo))
    print("[fold {0}] Trivial score: {1:.3f}".format(k, f1s_triv))

topo_scores = np.array(topo_scores)
triv_scores = np.array(triv_scores)
print('Topological Mean: %.2f median: %.2f stdev: %.2f' % (np.mean(topo_scores), np.median(topo_scores), np.std(topo_scores)))
print('Trivial Mean: %.2f median: %.2f stdev: %.2f' % (np.mean(triv_scores), np.median(triv_scores), np.std(triv_scores)))
X_test = pd.read_json(datadir + 'mtm_X_test.json')
X_test = X_test.values
y_test = loadfn(datadir + 'mtm_y_test.json')
round(clf.score(X_test, y_test), 2)
print(classification_report(y_test, clf.predict(X_test), target_names=["Trivial", "Topological"]))<jupyter_output>precision    recall  f1-score   support

     Trivial       0.87      0.71      0.78        28
 Topological       0.50      0.73      0.59        11

    accuracy                           0.72        39
   macro avg       0.68      0.72      0.69        39
weighted avg       0.77      0.72      0.73        39
{'fraction_non_alphanumeric': 0.11883408071748879, 'fraction_numerical': 0.032698056801195814, 'mean_word_length': 4.2275390625, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '97050', 'license': 'MIT', 'n_tokens_mistral': 2045, 'n_tokens_neox': 1896, 'n_words': 443, 'path': 'models.ipynb', 'repo_name': 'ncfrey/magnetic-topological-materials', 'size': '5375'}