Kaggle competition: DSNet: fastai Hackathon
<SOS> metric: categorizationaccuracy Kaggle data source: dsnet:-fastai-hackathon<categorify>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
train_init_df = pd.get_dummies(train_init_df,columns = ["Pclass"])
 train_init_df.head()<categorify>
SEED = 42
VALIDATION_PCT = 0.1
IMAGE_SIZE_64 = 64
IMAGE_SIZE_224 = 224
BATCH_SIZE_64 = 64
BATCH_SIZE_224 = 224
PATH = Path('.. /input')
 TRAIN_PATH = PATH/'train'
TEST_FOLDER_PATH = "test/test"
SAMPLE_SUBMISSION_PATH = PATH/"sample_submission.csv"
train_init_df = pd.get_dummies(train_init_df,columns = ["Alpha_ticket"])
 train_init_df.head()<categorify>
def load_data(path, image_size, batch_size, validation_pct = VALIDATION_PCT):
data =(ImageList.from_folder(path)
.split_by_rand_pct(validation_pct, seed = SEED)
.label_from_folder() 
.transform(get_transforms() , size = image_size)
.databunch(bs = batch_size)
.normalize(imagenet_stats))
return data
train_init_df["Sex"] = train_init_df["Sex"].astype("category")
 train_init_df = pd.get_dummies(train_init_df, columns = ["Sex"])
 train_init_df.head()<feature_engineering>
data_64 = load_data(TRAIN_PATH, IMAGE_SIZE_64, BATCH_SIZE_224, VALIDATION_PCT)
 data_64
train_init_df["Fsize"] = train_init_df["SibSp"] + train_init_df["Parch"] + 1
train_init_df.head()<feature_engineering>
learn = cnn_learner(data_64,
models.resnet152,
metrics = [error_rate, accuracy],
model_dir = '/tmp/model/' )
train_init_df["family_size"] = [1 if i < 5 else 0 for i in train_init_df["Fsize"]]
train_init_df.head()<categorify>
learn.fit_one_cycle(1, 1e-2 )
train_init_df = pd.get_dummies(train_init_df, columns= ["family_size"])
 train_init_df.head()<drop_column>
test_df = pd.read_csv(SAMPLE_SUBMISSION_PATH)
 test_df.head()
test_df = train_init_df.copy(deep=True)
 test_df.drop(labels = ["Survived"],axis = 1, inplace = True )<split>
test_images = ImageList.from_df(test_df, PATH/"", folder = TEST_FOLDER_PATH )
train = train_init_df.copy(deep=True)
 X_train = train.drop(labels = "Survived", axis = 1)
 y_train = train["Survived"]
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size = 0.33, random_state = 42)
 print("X_train",len(X_train))
print("X_test",len(X_test))
print("y_train",len(y_train))
print("y_test",len(y_test))
print("test",len(test_df))<train_model>
data_256 = load_data(TRAIN_PATH, IMAGE_SIZE_224, BATCH_SIZE_64, VALIDATION_PCT)
 data_256.add_test(test_images )
logreg = LogisticRegression()
logreg.fit(X_train, y_train)
 acc_log_train = round(logreg.score(X_train, y_train)*100,2)
 acc_log_test = round(logreg.score(X_test, y_test)*100,2)
 print("Training Accuracy: % {}".format(acc_log_train))
print("Testing Accuracy: % {}".format(acc_log_test))<choose_model_class>
learn.data = data_256
learn.lr_find()
learn.recorder.plot()
random_state = 42
classifier = [DecisionTreeClassifier(random_state = random_state),
SVC(random_state = random_state),
RandomForestClassifier(random_state = random_state),
LogisticRegression(random_state = random_state),
KNeighborsClassifier() ]
dt_param_grid = {"min_samples_split" : range(10,500,20),
"max_depth": range(1,20,2)}
svc_param_grid = {"kernel" : ["rbf"],
"gamma": [0.001, 0.01, 0.1, 1],
"C": [1,10,50,100,200,300,1000]}
rf_param_grid = {"max_features": [1,3,10],
"min_samples_split":[2,3,10],
"min_samples_leaf":[1,3,10],
"bootstrap":[False],
"n_estimators":[100,300],
"criterion":["gini"]}
logreg_param_grid = {"C":np.logspace(-3,3,7),
"penalty": ["l1","l2"]}
knn_param_grid = {"n_neighbors": np.linspace(1,19,10, dtype = int ).tolist() ,
"weights": ["uniform","distance"],
"metric":["euclidean","manhattan"]}
classifier_param = [dt_param_grid,
svc_param_grid,
rf_param_grid,
logreg_param_grid,
knn_param_grid]<train_on_grid>
learn.unfreeze()
learn.fit_one_cycle(30, max_lr = slice(1.0e-4, 1.0e-3))
cv_result = []
best_estimators = []
for i in range(len(classifier)) :
clf = GridSearchCV(classifier[i], param_grid=classifier_param[i], cv = StratifiedKFold(n_splits = 10), scoring = "accuracy", n_jobs = -1,verbose = 1)
 clf.fit(X_train,y_train)
 cv_result.append(clf.best_score_)
 best_estimators.append(clf.best_estimator_)
 print(cv_result[i] )<train_model>
test_probabalities, _ = learn.get_preds(ds_type=DatasetType.Test)
 test_predictions = [data_256.classes[pred] for pred in np.argmax(test_probabalities.numpy() , axis=-1)]
<drop_column><EOS> 
test_df.predicted_class = test_predictions
test_df.to_csv("submission.csv", index=False)
 test_df.head()