Skip to content

Instantly share code, notes, and snippets.

@rohithteja
Last active March 20, 2022 13:43
Show Gist options
  • Select an option

  • Save rohithteja/f8f28bbbc178ab5c23bd21f0f72ab6ba to your computer and use it in GitHub Desktop.

Select an option

Save rohithteja/f8f28bbbc178ab5c23bd21f0f72ab6ba to your computer and use it in GitHub Desktop.
elliptic RF model
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn import metrics
# read data
classes = pd.read_csv("../input/elliptic-data-set/elliptic_bitcoin_dataset/elliptic_txs_classes.csv")
edgelist = pd.read_csv("../input/elliptic-data-set/elliptic_bitcoin_dataset/elliptic_txs_edgelist.csv")
features = pd.read_csv("../input/elliptic-data-set/elliptic_bitcoin_dataset/elliptic_txs_features.csv", header=None)
# select nodes that are labeled licit or illicit only
nodes = classes[classes['class']!='unknown'].reset_index(drop=True)
edges = edgelist[(edgelist['txId1'].isin(nodes['txId']))
& (edgelist['txId2'].isin(nodes['txId']))].reset_index(drop=True)
node_features = features[features.iloc[:,0].isin(classes['txId'])].reset_index(drop=True)
node_features = node_features.rename(columns={0: 'txId', 1: 'time'})
nodes = pd.merge(nodes,node_features,on='txId')
# Data Preparation
X = nodes.iloc[:,3:]
y = nodes.iloc[:,1]
y[y=='2']=0 #changing labels to 0 and 1
y = y.astype('int')
X_train, X_test, y_train, y_test = train_test_split(X, y,stratify=y,
test_size=0.2,
random_state=42)
# Random Forest model
rf = RandomForestClassifier(random_state=42)
rf.fit(X_train,y_train)
y_pred_train = rf.predict(X_train)
y_pred_test = rf.predict(X_test)
print('***** RF MODEL *****')
print('ACC: Train:', metrics.accuracy_score(y_train, y_pred_train).round(2),
'Test:', metrics.accuracy_score(y_test, y_pred_test).round(3))
print('ROC: Train:', metrics.roc_auc_score(y_train, y_pred_train).round(2),
'Test:', metrics.roc_auc_score(y_test, y_pred_test).round(3))
print('F1: Train:', metrics.f1_score(y_train, y_pred_train, pos_label=0).round(2),
'Test:', metrics.f1_score(y_test, y_pred_test, pos_label=0).round(3))
print('**********************')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment