Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<init_hyperparams>
%reload_ext autoreload
%autoreload 2
lgb_params = {
"objective" : "binary",
"metric" : "auc",
"boosting": 'gbdt',
"max_depth" : 1,
"num_leaves" : 13,
"learning_rate" : 0.03,
"bagging_freq": 5,
"bagging_fraction" : 0.4,
"feature_fraction" : 0.05,
"min_data_in_leaf": 80,
"min_sum_hessian_in_leaf": 10,
"tree_learner": "serial",
"boost_from_average": "false",
"bagging_seed" : 42,
"verbosity" : 1,
"seed": 42
}<train_model>
train=pd.read_csv(inputs/"train.csv")
 train.head(3 )
folds = StratifiedKFold(n_splits=5, shuffle=True, random_state=44000)
 oof = np.zeros(len(train))
predictions = np.zeros(len(test))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train.values, y.values)) :
print("Fold {}".format(fold_))
trn_data = lgb.Dataset(train.iloc[trn_idx], label=y.iloc[trn_idx])
 val_data = lgb.Dataset(train.iloc[val_idx], label=y.iloc[val_idx])
 num_round = 15000
clf = lgb.train(lgb_params, trn_data, num_round, valid_sets = [trn_data, val_data],
verbose_eval=1000, early_stopping_rounds = 200)
 oof[val_idx] = clf.predict(train.iloc[val_idx], num_iteration=clf.best_iteration)
 predictions += clf.predict(test, num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(y, oof)) )<load_from_csv>
test=pd.read_csv(inputs/"test.csv")
 test.head(3 )
sub_file = pd.read_csv('/kaggle/input/santander-customer-transaction-prediction/sample_submission.csv' )<merge>
tfms = get_transforms(do_flip=False)
 tr=Path(".. /train")
 te=Path(".. /test" )
sub = pd.DataFrame()
sub['ID_code'] = test_index
sub['target'] = predictions
final_sub = pd.merge(sub_file, sub, on='ID_code', how='left')[['ID_code', 'target_y']]
final_sub = final_sub.fillna(0 ).rename(columns={'target_y': 'target'})
 final_sub<save_to_csv>
sorted(os.listdir(tr))
final_sub.to_csv('Finalsub.csv', index=False )<load_from_csv>
for index, row in train.iterrows() :
label,digit = row[0], row[1:]
filepath = tr/str(label)
 filename = f"{index}.jpg"
digit = digit.values
digit = digit.reshape(28,28)
 digit = digit.astype(np.uint8)
 img = Image.fromarray(digit)
 img.save(filepath/filename)
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv')
 train_data['Age'].fillna(train_data['Age'].mean() , inplace=True)
 test_data['Age'].fillna(test_data['Age'].mean() , inplace=True)
 train_data['Fare'].fillna(train_data['Fare'].mean() , inplace=True)
 test_data['Fare'].fillna(test_data['Fare'].mean() , inplace=True )<prepare_x_and_y>
for index, digit in test.iterrows() :
filepath = te
filename = f"{index}.jpg"
digit = digit.values
digit = digit.reshape(28,28)
 digit = digit.astype(np.uint8)
 img = Image.fromarray(digit)
 img.save(filepath/filename )
features = ["Pclass", "Sex", "SibSp", "Parch", "Age", "Fare"]
train_y = train_data["Survived"]
train_x = pd.get_dummies(train_data[features])
 test_x = pd.get_dummies(test_data[features] )<train_model>
data = ImageDataBunch.from_folder(path=".. /train",test=".. /test",ds_tfms=tfms, valid_pct=0.2,bs=32,size=24 ).normalize(imagenet_stats )
model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(train_x, train_y)
 test_y = model.predict(test_x )<save_to_csv>
data.show_batch(rows=3 ,figsize=(5,5))
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': test_y})
 output.to_csv('submission.csv', index=False )<import_modules>
learn = cnn_learner(data, models.resnet50, metrics=accuracy, model_dir = Path('.. /kaggle/input/ResNet-50'), callback_fns=ShowGraph )
import numpy as np
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from sklearn.ensemble import VotingClassifier<load_from_csv>
learn.fit_one_cycle(5 )
train_data = pd.read_csv('.. /input/titanic/train.csv')
 test_data = pd.read_csv('.. /input/titanic/test.csv' )<count_missing_values>
learn.save("501" )
missing_values = train_data.isna().any()
print('Columns which have missing values:
{0}'.format(missing_values[missing_values == True].index.tolist()))<count_missing_values>
learn.unfreeze()
learn.fit_one_cycle(5,max_lr=slice(1e-3,1e-1))
print("Percentage of missing values in `Age` column: {0:.2f}".format(100.*(train_data.Age.isna().sum() /len(train_data))))
print("Percentage of missing values in `Cabin` column: {0:.2f}".format(100.*(train_data.Cabin.isna().sum() /len(train_data))))
print("Percentage of missing values in `Embarked` column: {0:.2f}".format(100.*(train_data.Embarked.isna().sum() /len(train_data))))<count_duplicates>
learn.save("502" )
duplicates = train_data.duplicated().sum()
print('Duplicates in train data: {0}'.format(duplicates))<count_unique_values>
class_score,y=learn.get_preds(DatasetType.Test )
categorical = train_data.nunique().sort_values(ascending=True)
 print('Categorical variables in train data:
{0}'.format(categorical))<drop_column>
probs= class_score[0].tolist()
[f"{index}: {probs[index]}" for index in range(len(probs)) ]
for data in [train_data, test_data]:
data.drop(['Cabin'], axis=1, inplace=True)
 data.drop(['Ticket', 'Fare'], axis=1, inplace=True )<drop_column>
class_score=np.argmax(class_score,axis=1 )
for data in [train_data, test_data]:
data['Title'] = data.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
data['Woman_Or_Boy'] =(data.Title == 'Master')|(data.Sex == 'female')
 data.drop('Title', axis=1, inplace=True)
 data.drop('Name', axis=1, inplace=True )<categorify>
samplesub=pd.read_csv(inputs/"sample_submission.csv")
 samplesub.head()
label_encoder = LabelEncoder()
for data in [train_data, test_data]:
data['Sex'] = label_encoder.fit_transform(data['Sex'])
 data['Woman_Or_Boy'] = label_encoder.fit_transform(data['Woman_Or_Boy'] )<categorify>
ImageId = [os.path.splitext(path)[0] for path in os.listdir(te)]
ImageId = [int(path)for path in ImageId]
ImageId = [ID+1 for ID in ImageId]
ImageId[:5]
most_common = all_data.Embarked.mode()
print("Most common Embarked value: {0}".format(most_common[0]))
for data in [train_data, test_data]:
data.fillna(value={'Embarked': most_common[0]}, inplace=True )<drop_column>
subs=pd.DataFrame({"ImageId":ImageId,"Label":class_score} )
<categorify><EOS> 
subs.to_csv("submission.csv",index=False)
 subs.head(3 )