Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import numpy as np
import pandas as pd
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.metrics import roc_auc_score, precision_score, recall_score, f1_score, accuracy_score
import torch
from torch import nn, tensor, optim
from skorch import NeuralNetClassifier
def get_data():
raw_data = pd.read_csv('train.csv')
X = raw_data.drop(columns=['PassengerId', 'Survived', 'Name', 'Ticket', 'Cabin'])
X = pd.get_dummies(X)
X.fillna({'Age': X.Age.median()}, inplace=True)
y = raw_data.Survived
X = X.to_numpy(dtype=np.float32)
y = y.to_numpy(dtype=np.int64)
X_train, X_test, y_train, y_test = train_test_split(X, y)
return X_train, X_test, y_train, y_test
class MyNN(nn.Module):
def __init__(self, num_units=10):
super().__init__()
self.net = nn.Sequential(
nn.Linear(10, num_units),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(num_units, 10),
nn.ReLU(),
nn.Linear(10, 2),
nn.Softmax()
)
def forward(self, X, **kwargs):
return self.net(X)
X_train, X_test, y_train, y_test = get_data()
net = NeuralNetClassifier(
MyNN,
max_epochs=12,
lr=0.1,
)
params = {
'lr': [0.05, 0.1],
'module__num_units': [10, 20],
}
selector = GridSearchCV(net, params, cv=5)
selector.fit(X_train, y_train)
best_model = selector.best_estimator_
y_pred = best_model.predict(X_test)
print(selector.best_params_)
print("score: {}, accuracy_score: {}, roc_auc_score: {}, precision: {}, recall: {}, f1: {}".format(
best_model.score(X_test, y_test),
accuracy_score(y_test, y_pred),
roc_auc_score(y_test, y_pred),
precision_score(y_test, y_pred),
recall_score(y_test, y_pred),
f1_score(y_test, y_pred)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.