Kaggle competition: Digit Recognizer
<feature_engineering><EOS> 
import pandas as pd
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<feature_engineering>
import pandas as pd
class feature_engineering(BaseEstimator, TransformerMixin):
def __init__(self, columns=None):
self.columns = columns
def fit(self, X, y=None, **fit_params):
return self
def transform(self, X, **transform_params):
X['Title'] = X.Name.str.split(',' ).str[1].str.split('.' ).str[0].str.strip()
X['Title'] = X['Title'].apply(lambda x: x if x in ['Mrs', 'Master', 'Miss', 'Dr','Mr','Rev' ] else 'else')
 X['Family_Size'] = X['SibSp']+X['Parch'] + 1
X = X.drop(columns=['SibSp', 'Parch'])
 X['has_cabin'] = X['Cabin'].apply(lambda x: 0 if pd.isnull(x)else 1)
 return X<categorify>
true_test = pd.read_csv(".. /input/mnist-in-csv/mnist_test.csv")
 true_train = pd.read_csv(".. /input/mnist-in-csv/mnist_train.csv")
 sample_submission = pd.read_csv(".. /input/digit-recognizer/sample_submission.csv")
 given_test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 given_train = pd.read_csv(".. /input/digit-recognizer/train.csv" )
cat_pipeline = Pipeline([ 
("imputer", SimpleImputer(strategy="most_frequent")) , 
("cat_encoder", OneHotEncoder()),
] )<feature_engineering>
cols = given_test.columns
given_test['dataset'] = 'test'
given_train['dataset'] = 'train'
num_attribs = ["Age", "Fare","Family_Size"]
cat_attribs = [ "Sex", "Embarked","Pclass","Title","has_cabin"]
columns_trans = ColumnTransformer([ 
("num", num_pipeline, num_attribs), 
("cat", cat_pipeline, cat_attribs),
] )<drop_column>
given_dataset = pd.concat([given_train.drop('label', axis=1), given_test] ).reset_index()
true_mnist = pd.concat([true_train, true_test] ).reset_index(drop=True)
 labels = true_mnist['label'].values
true_mnist.drop('label', axis=1, inplace=True)
 true_mnist.columns = cols
full_pipeline = Pipeline([ 
('age_inferer', age_inferer(['Age', 'Pclass'])) , 
('feature_engineering', feature_engineering()), 
('ColumnTransformer', columns_trans),
] )<prepare_x_and_y>
true_idx = true_mnist.sort_values(by=list(true_mnist.columns)).index
dataset_from = given_dataset.sort_values(by=list(true_mnist.columns)) ['dataset'].values
original_idx = given_dataset.sort_values(by=list(true_mnist.columns)) ['index'].values
y_train = train["Survived"]
train = train.drop(columns=["Survived"])
 x_train = full_pipeline.fit_transform(train)
<prepare_x_and_y>
for i in range(len(true_idx)) :
if dataset_from[i] == 'test':
sample_submission.loc[original_idx[i], 'Label'] = labels[true_idx[i]]
<compute_test_metric><EOS> 
sample_submission.to_csv('submission.csv', index=False )