Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<load_from_csv>
%matplotlib inline
warnings.filterwarnings(action='once')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
df = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv' )<feature_engineering>
train_data = pd.read_csv('/kaggle/input/titanic/train.csv')
 test_data = pd.read_csv('/kaggle/input/titanic/test.csv' )
df['Label'] = predictions<save_to_csv>
train_data['PassengerId'].isnull().sum()
df.to_csv('submission.csv',line_terminator='\r
', index=False )<import_modules>
test_data['PassengerId'].isnull().sum()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D, Flatten, BatchNormalization, Dropout
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools<load_from_csv>
train_data['Survived'].isnull().sum()
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )<prepare_x_and_y>
train_data['Pclass'].isnull().sum()
X_train = train.iloc[:,1:]
y_train = train.iloc[:,0]<categorify>
test_data['Pclass'].isnull().sum()
X_train = X_train.values.reshape(-1, 28, 28, 1)/255.
 test = test.values.reshape(-1, 28, 28, 1)/255.
 y_train = to_categorical(y_train, 10 )<split>
train_data['Sex'].isnull().sum()
random_seed = 0
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=random_seed )<choose_model_class>
test_data['Sex'].isnull().sum()
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1
)<choose_model_class>
train_data['Age'].isnull().sum()
model = Sequential()
model.add(Conv2D(32,(5,5), padding='same', input_shape=X_train.shape[1:], activation='relu'))
model.add(Conv2D(32,(5,5), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(Conv2D(64,(3,3), padding='same', activation='relu'))
model.add(MaxPool2D(2,2))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.summary()<choose_model_class>
test_data['Age'].isnull().sum()
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )<train_model>
mean = train_data['Age'].mean()
train_data['Age'] = train_data['Age'].replace(np.NAN,mean )
EPOCHS = 20
BATCH_SIZE = 20
callback_list = [
ReduceLROnPlateau(monitor='val_loss', factor=0.25, patience=1, verbose=1, mode='auto',
min_delta=0.0001)
 ]
history = model.fit(datagen.flow(X_train, y_train, batch_size=BATCH_SIZE),
epochs=EPOCHS,
callbacks=callback_list,
validation_data=(X_val, y_val),
steps_per_epoch=X_train.shape[0] // BATCH_SIZE )<save_to_csv>
mean = test_data['Age'].mean()
test_data['Age'] = test_data['Age'].replace(np.NAN,mean )
results = model.predict(test)
 results = np.argmax(results, axis=1)
 results = pd.Series(results, name='Label')
 submission = pd.concat([pd.Series(range(1,28001), name='ImageID'), results], axis=1)
 submission.to_csv('submission.csv', index=False )<load_from_csv>
train_data['SibSp'].isnull().sum()
pd.set_option('display.max_rows', 1000)
 warnings.filterwarnings("ignore")
 dftrain = pd.read_csv('.. /input/digit-recognizer/train.csv')
 dftest = pd.read_csv('.. /input/digit-recognizer/test.csv' )<prepare_x_and_y>
test_data['SibSp'].isnull().sum()
IMG_SIZE = 28
x_train = dftrain.iloc[:,1:]
x_train = x_train.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
 y_train = dftrain.iloc[:,0]
x_test = dftest
x_test = x_test.values.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
 x_train = x_train/255.0
x_test = x_test/255.0<split>
train_data['Parch'].isnull().sum()
X_train, X_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state = 42)
 datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1)
 datagen.fit(X_train )<choose_model_class>
test_data['Parch'].isnull().sum()
earlystopping = EarlyStopping(monitor ="val_accuracy",
mode = 'auto', patience = 30,
restore_best_weights = True)
 model = Sequential()
model.add(Conv2D(128,(3, 3), input_shape = x_train.shape[1:]))
model.add(BatchNormalization())
 model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
model.add(Conv2D(512,(3, 3)))
 model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), padding='same'))
model.add(Flatten())
 model.add(Dense(512))
model.add(BatchNormalization())
 model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))<define_variables>
train_data['Cabin'].isnull().sum()
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True )<train_model>
test_data['Cabin'].isnull().sum()
model.compile(optimizer = 'adam',
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
 EPOCHS = 1000
BATCH_SIZE=64
history = model.fit(datagen.flow(X_train, y_train), epochs=EPOCHS, batch_size=BATCH_SIZE, validation_data=(X_val, y_val), callbacks=[earlystopping] )<compute_test_metric>
train_data['Embarked'].isnull().sum()
print("Max.Validation Accuracy: {}%".format(round(100*max(history.history['val_accuracy']), 2)) )<predict_on_test>
test_data['Embarked'].isnull().sum()
predictions = model.predict([x_test])
 solutions = []
for i in range(len(predictions)) :
solutions.append(np.argmax(predictions[i]))<save_to_csv>
train_data['Cabin'].value_counts().to_frame()
final = pd.DataFrame()
final['ImageId']=[i+1 for i in dftest.index]
final['Label']=solutions
final.to_csv('submission.csv', index=False )<define_variables>
test_data['Cabin'].value_counts().to_frame()
sample_sub = '.. /input/digit-recognizer/sample_submission.csv'
test = '.. /input/digit-recognizer/test.csv'
train = '.. /input/digit-recognizer/train.csv'<set_options>
train_data['Embarked'].value_counts().to_frame()
%matplotlib inline 
<load_from_csv>
new_train_data = train_data.copy()
train_data = pd.read_csv(train)
 test_data = pd.read_csv(test )<prepare_x_and_y>
new_train_data.drop('Name',axis=1,inplace=True )
y_train = train_data ['label']
X_train = train_data.drop(labels = ["label"], axis = 1)
 X_test = test_data<feature_engineering>
new_train_data['Embarked'] = new_train_data['Embarked'].replace(np.NAN,"S")
 new_train_data['Embarked'].isnull().sum()
X_train = X_train/255
X_test = X_test/255<categorify>
new_train_data['Sex'] = pd.get_dummies(new_train_data['Sex'] )
y_train = to_categorical(y_train, num_classes = 10 )<choose_model_class>
lb = LabelEncoder()
new_train_data['Embarked'] = lb.fit_transform(new_train_data['Embarked'])
 onehotencode = OneHotEncoder(handle_unknown='ignore')
 encf = pd.DataFrame(onehotencode.fit_transform(new_train_data[['Embarked']] ).toarray())
 new_train_data = new_train_data.join(encf)
 new_train_data.head()
datagen = ImageDataGenerator(
 rotation_range = 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1 )<choose_model_class>
new_train_data.rename(columns={0:"C",1:"Q",2:"S"},inplace=True )
model = Sequential()
model.add(Conv2D(32,kernel_size=3, activation='relu', input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,kernel_size=5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Conv2D(64, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=5, strides=2, padding='same' ,activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Flatten())
 model.add(Dense(128, activation ='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))<choose_model_class>
new_train_data = new_train_data[new_train_data.Fare > 0]
new_train_data = new_train_data[new_train_data.Fare <= 300]
model.compile(optimizer = "adam", loss = "categorical_crossentropy", metrics = ["accuracy"] )<split>
missingno.matrix(test_data )
X_train, X_val, Y_train, Y_val = train_test_split(X_train, y_train, test_size = 0.2, random_state = 64 )<train_model>
new_train_data['Cabin'].value_counts()
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size = 64),
epochs = 50, steps_per_epoch = X_train.shape[0]//64, validation_data =(X_val, Y_val), verbose=1 )<predict_on_test>
test_data['Cabin'].value_counts()
predictions = model.predict(X_test)
 predictions = np.argmax(predictions, axis = 1)
 predictions = pd.Series(predictions, name = "Label" )<save_to_csv>
new_train_data_cabin = new_train_data.copy()
submission = pd.concat([pd.Series(range(1, 28001), name = "ImageId"), predictions], axis = 1)
 submission.to_csv("MNIST_top_CNN_submission.csv", index = False )<import_modules>
new_test_data = test_data.copy()
import numpy
import pandas
import matplotlib.pyplot as plt
from keras.utils import to_categorical
from keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout
from keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping<load_from_csv>
new_train_data_cabin['Cabin_type'] = new_train_data_cabin['Cabin'].str.split(r'[0-9]' ).str[0].tolist()
new_test_data['Cabin_type'] = new_test_data['Cabin'].str.split(r'[0-9]' ).str[0].tolist()
train_data = pandas.read_csv('.. /input/digit-recognizer/train.csv')
 test_data = pandas.read_csv('.. /input/digit-recognizer/test.csv' )<prepare_x_and_y>
new_train_data_cabin['Cabin_type'].value_counts()
train_y = to_categorical(train_data["label"])
 train_x = train_data.loc[:, train_data.columns != "label"]
train_x /= 256<choose_model_class>
new_test_data['Cabin_type'].value_counts()
callback = EarlyStopping(monitor='loss', patience=8, restore_best_weights=True )<choose_model_class>
lb1 = LabelEncoder()
new_train_data_cabin['Cabin_type'] = lb1.fit_transform(new_train_data_cabin['Cabin_type'].astype(str))
new_test_data['Cabin_type'] = lb1.fit_transform(new_test_data['Cabin_type'].astype(str))
model = Sequential()
model.add(Conv2D(64,(3,3), activation='relu', input_shape=(28,28, 1)))
 model.add(MaxPooling2D(( 2,2)))
 model.add(Conv2D(64,(3,3), activation='relu'))
model.add(MaxPooling2D(( 2,2)))
 model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='sigmoid'))<choose_model_class>
new_train_data_cabin['Cabin_type'].value_counts()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'] )<train_model>
new_train_data_cabin['Cabin_type'].value_counts()
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1 
)
 datagen.fit(train_x )<train_model>
new_train_data_cabin['Cabin_type'] = new_train_data_cabin['Cabin_type'].replace(0,11)
 new_train_data_cabin['Cabin_type'] = new_train_data_cabin['Cabin_type'].replace(10,0)
 new_train_data_cabin['Cabin_type'] = new_train_data_cabin['Cabin_type'].replace(11,10 )
history = model.fit(datagen.flow(train_x, train_y, batch_size=32), epochs=80, callbacks=[callback] )<predict_on_test>
new_test_data['Cabin_type'] = new_test_data['Cabin_type'].replace(0,11)
 new_test_data['Cabin_type'] = new_test_data['Cabin_type'].replace(10,0)
 new_test_data['Cabin_type'] = new_test_data['Cabin_type'].replace(11,10 )
test_data /= 256
test_x = test_data.values.reshape(-1, 28, 28, 1)
 y_pred = model.predict(test_x)
 y_pred = numpy.argmax(y_pred, axis=1)
 y_pred = pandas.Series(y_pred,name='Label')
 submission = pandas.concat([pandas.Series(range(1, 28001), name='ImageId'), y_pred], axis=1 )<save_to_csv>
new_train_data_cabin['Cabin_type'].value_counts()
submission.to_csv('my_submission.csv', index=False )<load_from_csv>
new_test_data['Cabin_type'].value_counts()
X_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 X_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 print('Shape of the training data: ', X_train.shape)
 print('Shape of the test data: ', X_test.shape )<drop_column>
new_train_data_cabin['Family'] = new_train_data_cabin['SibSp'] + new_train_data_cabin['Parch'] + 1
new_train_data_cabin
y_train = X_train['label']
X_train.drop(labels = ['label'], axis=1, inplace=True )<count_missing_values>
new_test_data['Family'] = new_test_data['SibSp'] + new_test_data['Parch'] + 1
new_test_data
print('Null values in training data: ',X_train.isna().any().sum())
 print('Null values in test data: ',X_test.isna().any().sum())
 X_train = X_train / 255.0
X_test = X_test / 255.0<categorify>
new_test_data.drop('Name',axis=1,inplace=True)
 new_test_data['Sex'] = pd.get_dummies(new_test_data['Sex'])
 new_test_data
y_train = to_categorical(y_train, num_classes=10 )<split>
len(set(test_data.Ticket)- set(test_data.Ticket ).intersection(set(new_train_data_cabin.Ticket))),len(set(test_data.Ticket)) ,len(test_data )
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1 )<choose_model_class>
group = new_train_data_cabin.groupby(['Sex'] ).agg({'Fare':['mean']})
 group.columns = ['mean_fare_sex']
group.reset_index(inplace=True)
 new_train_data_cabin = pd.merge(new_train_data_cabin,group,on=['Sex'], how='left')
 new_train_data_cabin
model = keras.models.Sequential([layers.Conv2D(32,(3,3), activation="relu",padding='same', input_shape=(28,28,1)) ,
layers.MaxPooling2D(2,2),
layers.Dropout(0.25),
layers.Conv2D(64,(3,3), activation="relu",padding='same'),
layers.MaxPooling2D(2,2),
layers.Dropout(0.25),
layers.Flatten() ,
layers.BatchNormalization() ,
layers.Dense(128, activation="elu", kernel_initializer="he_normal"),
layers.BatchNormalization() ,
layers.Dense(32, activation="elu", kernel_initializer="he_normal"),
layers.BatchNormalization() ,
layers.Dense(10, activation="softmax")])
 model.summary()<choose_model_class>
group = new_test_data.groupby(['Sex'] ).agg({'Fare':['mean']})
 group.columns = ['mean_fare_sex']
group.reset_index(inplace=True)
 new_test_data = pd.merge(new_test_data,group,on=['Sex'], how='left')
 new_test_data
optimizer = Adam(learning_rate=0.001, epsilon=1e-07)
 model.compile(optimizer = optimizer, loss = 'categorical_crossentropy', metrics=['accuracy'])
 earlyStopping = EarlyStopping(monitor='val_accuracy', patience=10, verbose=0, mode='auto')
 mcp = ModelCheckpoint('.mdl_wts.hdf5', save_best_only=True, monitor='val_accuracy', mode='auto')
 reduce_lr_loss = ReduceLROnPlateau(monitor='val_accuracy', factor=0.1, patience=7, verbose=1, epsilon=1e-4, mode='auto' )<train_model>
new_train_data_cabin.drop('Ticket',axis=1, inplace=True)
 new_train_data_cabin
datagen = ImageDataGenerator(
 rotation_range=5,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=5,
zoom_range=0.1)
 datagen.fit(X_train )<train_model>
new_test_data.drop('Ticket',axis=1, inplace=True)
 new_test_data
Batch_size=100
Epochs = 100
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size=Batch_size),
epochs = Epochs,
validation_data =(X_val,y_val),
verbose = 2,
steps_per_epoch=X_train.shape[0]//Batch_size,
callbacks = [earlyStopping, mcp, reduce_lr_loss] )<train_model>
new_train_data_cabin.drop('PassengerId',axis=1,inplace=True )
model.load_weights(filepath = '.mdl_wts.hdf5')
 scores = model.evaluate(X_val, y_val, callbacks = [earlyStopping, mcp, reduce_lr_loss] )<categorify>
new_test_data.drop('PassengerId',axis=1,inplace=True )
for layer in model.layers:
if'conv' in layer.name:
filters, biases = layer.get_weights()
print('Layer: ', layer.name, filters.shape)
 print('Filter size:(', filters.shape[0], ',', filters.shape[1], ')')
 print('Channels in this layer: ', filters.shape[2])
 print('Number of filters: ', filters.shape[3])
 count = 1
plt.figure(figsize =(18, 4))
for i in range(filters.shape[3]):
ax= plt.subplot(4, filters.shape[3]/4, count)
 ax.set_xticks([])
 ax.set_yticks([])
 plt.imshow(filters[:,:,0, i], cmap=plt.cm.binary)
 count+=1
plt.show()<define_variables>
new_train_data_cabin['Pclass'].value_counts()
print('total number of layers',len(model.layers))<choose_model_class>
new_train_data_cabin.drop('Cabin',axis=1,inplace=True)
 new_train_data_cabin
layer_outputs = [layer.output for layer in model.layers[0:6]]
activation_model = models.Model(inputs = model.input, outputs = layer_outputs )<predict_on_test>
new_test_data.drop('Cabin',axis=1,inplace=True)
 new_test_data
img_tensor = X_test[4].reshape(-1, 28, 28, 1)
 activations = activation_model.predict(img_tensor )<save_to_csv>
bins = np.linspace(min(new_test_data['Age']),max(new_test_data['Age']),4)
 group_names = [1,2,3]
new_test_data['Age_binned'] = pd.cut(new_test_data['Age'],bins,labels=group_names,include_lowest=True)
 new_test_data
submissions = pd.DataFrame({"ImageId": list(range(1,len(test_pred)+1)) ,
"Label": test_pred})
 submissions.to_csv("submission.csv", index=False, header=True )<install_modules>
bins = np.linspace(min(new_test_data['Fare']),max(new_test_data['Fare']),4)
 group_names = [1,2,3]
new_test_data['Fare_binned'] = pd.cut(new_test_data['Fare'],bins,labels=group_names,include_lowest=True)
 new_test_data
<import_modules>
new_train_data_cabin.duplicated().sum()
from fastai.vision.all import *<categorify>
new_train_data_cabin.drop_duplicates(inplace=True)
 new_train_data_cabin.duplicated().sum()
class AlbumentationsTransform(RandTransform):
"A transform handler for multiple `Albumentation` transforms"
split_idx,order=None,2
def __init__(self, train_aug, valid_aug): store_attr()
def before_call(self, b, split_idx):
self.idx = split_idx
def encodes(self, img: PILImage):
if self.idx == 0:
aug_img = self.train_aug(image=np.array(img)) ['image']
else:
aug_img = self.valid_aug(image=np.array(img)) ['image']
return PILImage.create(aug_img)
<prepare_x_and_y>
training = new_train_data_cabin.copy()
training
def get_x(row): return row['image_id']
def get_y(row): return row['label']<install_modules>
training.drop('C',axis=1,inplace=True)
 training.drop('Q',axis=1,inplace=True)
 training.drop('S',axis=1,inplace=True)
 training
<load_pretrained>
x_data = training.drop('Survived',axis=1,inplace=False)
 y_data = training[['Survived']]
learn=load_learner(".. /input/resnext50/baseline_rsnx",cpu=False )<categorify>
x_data.drop('mean_fare_sex',axis=1,inplace = True )
learn = learn.to_native_fp32()<define_variables>
x_data['Age_binned']= x_data['Age_binned'].astype('int16')
 x_data['Fare_binned']= x_data['Fare_binned'].astype('int16')
 x_data.dtypes
data_path=".. /input/cassava-leaf-disease-classification/"<load_from_csv>
new_test_data.drop('mean_fare_sex',axis=1,inplace = True )
sample_df = pd.read_csv(data_path+'sample_submission.csv')
 sample_df.head()<prepare_output>
new_test_data['Age_binned']= new_test_data['Age_binned'].astype('int16')
 new_test_data['Fare_binned']= new_test_data['Fare_binned'].astype('int16')
 new_test_data.dtypes
sample_copy = sample_df.copy()
sample_copy['image_id'] = sample_copy['image_id'].apply(lambda x: ".. /input/cassava-leaf-disease-classification/test_images/"+x )<train_model>
Dt = DecisionTreeClassifier()
Dt.fit(x_data,y_data)
 y_pred = Dt.predict(x_data)
 accuracy_score(y_data,y_pred)*100
test_dl = learn.dls.test_dl(sample_copy )<normalization>
rto = RandomForestClassifier(n_estimators=28)
 rto.fit(x_data,y_data.values.ravel())
 y_pred = rto.predict(x_data)
 accuracy_score(y_data,y_pred)*100
preds, _ = learn.tta(dl=test_dl, n=15, beta=0 )<feature_engineering>
new_test_data['Embarked'] = lb1.fit_transform(new_test_data['Embarked'])
 new_test_data
sample_df['label'] = preds.argmax(dim=-1 ).numpy()<save_to_csv>
x_data1 = x_data.drop('Fare_binned',axis= 1,inplace= False)
 x_data1
sample_df.to_csv('submission.csv',index=False )<load_from_csv>
new_test_data1 = new_test_data.drop('Fare_binned',axis= 1,inplace= False)
 new_test_data1
pd.read_csv("./submission.csv" )<import_modules>
model_names = {
'svm' : {
'model': SVC(gamma='auto'),
'params': {
'C': [1,5,10,15,20,25,26,28,29,30],
'kernel' : ['rbf','linear']
}
},
'logistic_regression': {
'model': LogisticRegression(solver='liblinear',multi_class='auto'),
'params' : {
'C': [1,5,10,15,20,25,26,28,29,30],
}
},
'random_forest' : {
'model' : RandomForestClassifier() ,
'params' : {
'n_estimators' : [1,5,10,15,20,25,26,28,29,30]
}
},
'xgboost' : {
'model' : xgb.XGBClassifier(objective ='reg:logistic', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5),
'params' : {
'alpha' : [0.1,1,10,100,1.1],
'n_estimators' : [1,5,10,15,20,25,26,28,29,30]
}
},
'bagging_classifier' : {
'model' : BaggingClassifier(DecisionTreeClassifier() , max_samples= 0.5,max_features=1.0),
'params' : {
'n_estimators' : [1,5,10,15,20,25,26,28,29,30]
}
},
'adbboost_classifier' : {
'model' : AdaBoostClassifier(DecisionTreeClassifier() ,learning_rate=1),
'params' : {
'n_estimators' : [1,5,10,15,20,25,26,28,29,30]
}
}
}
!nvidia-smi<install_modules>
from sklearn.model_selection import GridSearchCV
!pip install.. /input/timmmodels/dist/timm-0.3.4.tar<import_modules>
from sklearn.model_selection import GridSearchCV
import os
import cv2
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import models
from torch.utils.data import DataLoader, Dataset
from torch.cuda import amp
import albumentations as A
from albumentations.pytorch import ToTensorV2
import timm<define_variables>
scores = []
for model_name, mp in model_names.items() :
clf = GridSearchCV(mp['model'],mp['params'],cv=5,return_train_score=False)
 clf.fit(x_data1,y_data.values.ravel())
 scores.append({
'model' : model_name,
'best_score' : clf.best_score_,
'best_params' : clf.best_params_
} )
ROOT_DIR = ".. /input/cassava-leaf-disease-classification"
TEST_DIR = ".. /input/cassava-leaf-disease-classification/test_images"<define_variables>
d=pd.DataFrame(scores,columns=['model','best_score','best_params'] )
class CFG:
model_name = 'tf_efficientnet_b4_ns'
img_size = 512
loadmodelpath = '/kaggle/input/cassava-bitempered-logistic-loss/bitemp-01.pth'
num_classes = 5
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<choose_model_class>
bgc = BaggingClassifier(DecisionTreeClassifier() , max_samples= 0.5,max_features=1.0,n_estimators=28)
 bgc.fit(x_data,y_data.values.ravel())
 pred = bgc.predict(x_data)
 accuracy_score(y_data,pred)*100
model = timm.create_model(CFG.model_name, pretrained=False)
 num_features = model.classifier.in_features
model.classifier = nn.Linear(num_features, CFG.num_classes)
 model.to(CFG.device);<load_pretrained>
predic1 = bgc.predict(new_test_data)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predic1})
 output.to_csv('my_submission31.csv', index=False)
 print("Your submission was successfully saved!" )
model = torch.load(CFG.loadmodelpath)
 model.eval()<load_from_csv>
rto1 = RandomForestClassifier(n_estimators=10)
 rto1.fit(x_data,y_data.values.ravel())
 y_pred = rto1.predict(x_data)
 accuracy_score(y_data,y_pred)*100
T_DIR = TEST_DIR
t_df = pd.read_csv(f"{ROOT_DIR}/sample_submission.csv" )<create_dataframe>
predic = rto.predict(new_test_data)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predic})
 output.to_csv('my_submission32.csv', index=False)
 print("Your submission was successfully saved!" )
t_data = CassavaLeafDataset(T_DIR, t_df, transforms=data_transforms["valid"])
 t_loader = DataLoader(dataset=t_data, batch_size=1, num_workers=4, pin_memory=True, shuffle=False )<prepare_output>
xgb = xgb.XGBClassifier(objective ='reg:logistic', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5,alpha=1,n_estimators=28)
 xgb.fit(x_data,y_data.values.ravel())
 y_pred = xgb.predict(x_data)
 accuracy_score(y_data,y_pred)*100
submit_df = pd.DataFrame(t_df ).copy(deep=True)
 for i,(inputs, _)in enumerate(t_loader):
inputs = inputs.to(CFG.device)
 outputs = model(inputs ).detach().cpu().numpy()
pred_label = np.argmax(outputs)
 submit_df.iloc[i] = [t_df.iloc[i]['image_id'], pred_label]<save_to_csv>
predic3 = xgb.predict(new_test_data)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predic3})
 output.to_csv('my_submission33.csv', index=False)
 print("Your submission was successfully saved!" )
submit_df.to_csv("/kaggle/working/submission.csv", index=False )<set_options>
estimator3 = []
estimator3.append(( 'svm', SVC(gamma='auto',kernel='linear',C=1)))
 estimator3.append(( 'lg', LogisticRegression(solver='liblinear',multi_class='auto',C=1)))
 estimator3.append(( 'rfc', RandomForestClassifier(n_estimators=28)))
 estimator3.append(( 'xgb', xgb.XGBClassifier(objective ='reg:logistic', colsample_bytree = 0.3, learning_rate = 0.1,
max_depth = 5, alpha=1.1,n_estimators=28)))
 estimator3.append(( 'bg', BaggingClassifier(DecisionTreeClassifier() , max_samples= 0.5,max_features=1.0,n_estimators=28)))
 estimator3.append(( 'ada2', AdaBoostClassifier(DecisionTreeClassifier() ,n_estimators=5,learning_rate=1)))
 votin = VotingClassifier(estimators=estimator3,voting='hard')
 votin.fit(x_data1,y_data.values.ravel() )
<train_model><EOS> 
predic = votin.predict(new_test_data1)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predic})
 output.to_csv('my_submission21.csv', index=False)
 print("Your submission was successfully saved!" )