Kaggle competition: Птица или самолет?
<SOS> metric: auc Kaggle data source: bird-or-aircraft-2020<compute_test_metric>
%matplotlib inline
print(tf.config.experimental.list_physical_devices('CPU'))
print(tf.config.experimental.list_physical_devices('GPU'))
print(tf.__version__ )
cross_val_score(model , train , train_y , cv=5 )<predict_on_test>
train_data = pd.read_csv('.. /input/bird-or-aircraft-dafe-open/train_x.csv', index_col=0, header=None)
 train_labels = pd.read_csv('.. /input/bird-or-aircraft-dafe-open/train_y.csv', index_col=0)
 test_data = pd.read_csv('.. /input/bird-or-aircraft-dafe-open/test_x.csv', index_col=0, header=None )
def applyModel(X_test):
X = X_test[features].copy()
reformat(X)
 X = scaler.transform(X)
 preds = model.predict(X)
 return preds<save_to_csv>
train_labels['target'].value_counts()
X_test = pd.read_csv('.. /input/test.csv')
 preds = applyModel(X_test)
 output = pd.DataFrame({'PassengerId': X_test.PassengerId,
'Survived': preds})
 output.to_csv('submission.csv', index=False )<load_from_csv>
train_data = train_data.to_numpy()
test_data = test_data.to_numpy()
train_labels = train_labels.to_numpy()
raw_data = pd.read_csv('.. /input/train.csv')
 raw_data.head()<count_missing_values>
train_data = train_data / 255
train_data = train_data.reshape(train_data.shape[0], 32, 32, 3 )
print(raw_data.columns)
 print(raw_data.isnull().sum() )<groupby>
def build_model() :
model = Sequential([
Conv2D(16, 3, padding='same', activation='relu', input_shape=(32, 32, 3)) ,
MaxPooling2D() ,
Conv2D(32, 3, padding='same', activation='relu'),
MaxPooling2D() ,
Conv2D(64, 3, padding='same', activation='relu'),
MaxPooling2D() ,
Flatten() ,
Dropout(0.5),
Dense(512, activation='relu', kernel_regularizer=regularizers.l1_l2(0.0001)) ,
Dense(1, activation='sigmoid')
 ])
 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 return model
raw_data.groupby(['Pclass', 'Sex', 'Embarked'])['Survived'].mean()<prepare_x_and_y>
kf = KFold(n_splits=5, shuffle=True, random_state=42)
 epochs_num = 150
all_loss = []
all_accuracy = []
i = 0
for train_index, val_index in kf.split(train_data):
i += 1
print('Processing fold
X_train = train_data[train_index]
y_train = train_labels[train_index]
X_val = train_data[val_index]
y_val = train_labels[val_index]
model = build_model()
history = model.fit(X_train, y_train,
epochs=epochs_num,
batch_size=128,
validation_data=(X_val, y_val),
verbose=0)
 loss_history = history.history['val_loss']
all_loss.append(loss_history)
 accuracy_history = history.history['val_accuracy']
all_accuracy.append(accuracy_history )
y = raw_data.Survived
features = ['Pclass', 'Sex', 'Embarked']
X = raw_data[features].copy()<categorify>
epochs_num = 75
X_train = train_data
y_train = train_labels
model = build_model()
model.fit(X_train, y_train, epochs=epochs_num, batch_size=128, verbose=0);
def reformat(x):
d_sex = {'female': 0, 'male': 1}
d_emb = {'C': 0, 'Q': 1, 'S': 2, '3':3}
x['Sex'] = [d_sex[sex] for sex in x['Sex'].values]
x['Embarked'] = x['Embarked'].fillna('3')
 x['Embarked'] = [d_emb[elem] for elem in x['Embarked'].values]
reformat(X)
 X.head()<normalization>
X_test = test_data / 255
X_test = X_test.reshape(test_data.shape[0], 32, 32, 3 )
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X )<train_on_grid>
y_pred = model.predict(X_test)
 y_pred[:10]
model = RandomForestClassifier()
model.fit(X_scaled,y)
 cross_val_score(model , X_scaled , y , cv=5 )<predict_on_test>
submission = pd.DataFrame({'id': range(test_data.shape[0]),
'target':(y_pred >= 0.5 ).astype('int' ).flatten()
} )
def applyModel(X_test):
X = X_test[features].copy()
reformat(X)
 X = scaler.transform(X)
 preds = model.predict(X)
 return preds<save_to_csv>
submission.to_csv('submission.csv', index=False )
<set_options><EOS> 
submission.to_csv('submission.csv', index=False )