Kaggle competition: PadhAI: Tamil Vowel - Consonant Classification
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<define_variables>
class VowelConsonantDataset(Dataset):
def __init__(self, file_path,train=True,transform=None):
self.transform = transform
self.file_path=file_path
self.train=train
self.file_names=[file for _,_,files in os.walk(self.file_path)for file in files]
self.len = len(self.file_names)
 if self.train:
self.classes_mapping=self.get_classes()
def __len__(self):
return len(self.file_names)
 def __getitem__(self, index):
file_name=self.file_names[index]
image_data=self.pil_loader(self.file_path+"/"+file_name)
 if self.transform:
image_data = self.transform(image_data)
 if self.train:
file_name_splitted=file_name.split("_")
 Y1 = self.classes_mapping[file_name_splitted[0]]
Y2 = self.classes_mapping[file_name_splitted[1]]
z1,z2=torch.zeros(10),torch.zeros(10)
 z1[Y1-10],z2[Y2]=1,1
label=torch.stack([z1,z2])
 return image_data, label
else:
return image_data, file_name
def pil_loader(self,path):
with open(path, 'rb')as f:
img = Image.open(f)
 return img.convert('RGB')
 def get_classes(self):
classes=[]
for name in self.file_names:
name_splitted=name.split("_")
 classes.extend([name_splitted[0],name_splitted[1]])
 classes=list(set(classes))
classes_mapping={}
for i,cl in enumerate(sorted(classes)) :
classes_mapping[cl]=i
return classes_mapping
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women survived:", rate_women)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men survived:", rate_men )<feature_engineering>
train_on_gpu = torch.cuda.is_available()
test_data["Fare"] = test_data["Fare"].fillna(test_data["Fare"].median())
 train_data['FareGroup'] = pd.cut(train_data['Fare'],4)
 print(train_data[['FareGroup', 'Survived']].groupby('FareGroup', as_index=False ).mean().sort_values('Survived', ascending=False))
def group_fare(fare):
if fare <= 128: return 0
if fare > 128 and fare <= 256: return 1
if fare > 256 and fare <= 384: return 2
if fare > 384 : return 3
for i, row in train_data.iterrows() :
train_data.at[i,'Fare Group'] = int(group_fare(row["Fare"]))
for i, row in test_data.iterrows() :
test_data.at[i,'Fare Group'] = int(group_fare(row["Fare"]))
train_data.head()<find_best_model_class>
transform_train = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor() ,
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
] )
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", "Age","Embarked","Fare Group"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 imp = SimpleImputer()
imp_X = imp.fit_transform(X)
 imp_X_test = imp.transform(X_test)
 model1 = RandomForestClassifier(n_estimators = 100, max_depth = 5, random_state = 1)
 model1.fit(imp_X,y)
 predictions1 = model1.predict(imp_X_test)
 model1_preds = cross_val_predict(model1, imp_X, y)
 model1_acc = accuracy_score(y, model1_preds)
 print("Random Forest Accuracy:", model1_acc )<compute_train_metric>
full_data = VowelConsonantDataset(".. /input/train/train",train=True,transform=transform_train)
 train_size = int(0.95 * len(full_data))
test_size = len(full_data)- train_size
train_data, validation_data = random_split(full_data, [train_size, test_size])
 train_loader = torch.utils.data.DataLoader(train_data, batch_size=20, shuffle=True)
 validation_loader = torch.utils.data.DataLoader(validation_data, batch_size=20, shuffle=True )
model2 = GradientBoostingClassifier(random_state=42)
 model2.fit(imp_X,y)
 predictions2 = model2.predict(imp_X_test)
 model2_preds = cross_val_predict(model2, imp_X, y)
 model2_acc = accuracy_score(y, model2_preds)
 print("Gradient Booster Accuracy:", model2_acc )<compute_train_metric>
test_data = VowelConsonantDataset(".. /input/test/test",train=False,transform=transform_train)
 test_loader = torch.utils.data.DataLoader(test_data, batch_size=20,shuffle=False )
model3 = XGBClassifier(max_depth=3, n_estimators=1000, learning_rate=0.05)
 model3.fit(imp_X, y)
 predictions3 = model3.predict(imp_X_test)
 model3_preds = cross_val_predict(model3, imp_X, y)
 model3_acc = accuracy_score(y, model3_preds)
 print("XGBoost Accuracy:", model3_acc )<compute_train_metric>
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device )
model4 = SVC(random_state = 1)
 model4.fit(imp_X, y)
 predictions4 = model4.predict(imp_X_test)
 model4_preds = cross_val_predict(model4, imp_X, y)
 model4_acc = accuracy_score(y, model4_preds)
 print("SVC Accuracy:", model4_acc )<save_to_csv>
class MyModel(nn.Module):
def __init__(self, num_classes1, num_classes2):
super(MyModel, self ).__init__()
self.model_resnet = models.resnet50(pretrained=True)
 num_ftrs = self.model_resnet.fc.in_features
self.model_resnet.fc = nn.Sequential()
self.fc1 = nn.Sequential(
 nn.BatchNorm1d(num_ftrs),
nn.Dropout(0.3),
nn.Linear(in_features=num_ftrs, out_features=1000,bias=True),
nn.ReLU() ,
nn.BatchNorm1d(1000, eps=1e-05, momentum=0.3),
nn.Dropout(0.3),
nn.Linear(in_features=1000, out_features=10,bias=True))
self.fc2 = nn.Sequential(
 nn.BatchNorm1d(num_ftrs),
nn.Dropout(0.3),
nn.Linear(in_features=num_ftrs,out_features=1000,bias=True),
nn.ReLU() ,
nn.BatchNorm1d(1000, eps=1e-05, momentum=0.3),
nn.Dropout(0.3),
nn.Linear(in_features=1000, out_features=10,bias=True))
def forward(self, x):
x = self.model_resnet(x)
 out1 = self.fc1(x)
 out2 = self.fc2(x)
 return out1, out2
output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions1})
 output.to_csv('my_submission.csv', index=False)
 print("Your submission was succesfully saved!" )<load_from_csv>
my_model = MyModel(10,10 )
train_data = pd.read_csv("/kaggle/input/titanic/train.csv")
 train_data.head(10)
 train_data.tail(10)
 train_data.query('Cabin == Cabin' ).shape
test_data = pd.read_csv("/kaggle/input/titanic/test.csv")
 test_data.head()<define_variables>
my_model = my_model.to(device )
len(train_data.loc[(train_data.Sex == 'female')&(train_data.Survived == 1)]["Survived"])
 train_data.loc[:,["Name", "Age", "Pclass"]]
train_data[["Name", "Age", "Pclass"]]
women = train_data.loc[train_data.Sex == 'female']["Survived"]
rate_women = sum(women)/len(women)
 print("% of women who survived:", rate_women)
 men = train_data.loc[train_data.Sex == 'male']["Survived"]
rate_men = sum(men)/len(men)
 print("% of men who survived=", rate_men )<choose_model_class>
def evaluation(dataloader,model):
total,correct=0,0
for data in dataloader:
inputs,labels=data
inputs,labels=inputs.to(device),labels.to(device)
 out1,out2=my_model(inputs)
 _,pred1=torch.max(out1.data,1)
 _,pred2=torch.max(out2.data,1)
 _,labels1=torch.max(labels[:,0,:].data,1)
 _,labels2=torch.max(labels[:,1,:].data,1)
 total+=labels.size(0)
 fin1=(pred1==labels1)
 fin2=(pred2==labels2)
 correct+=(fin1==fin2 ).sum().item()
return 100*correct/total
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 actual = model.predict(X)
 predictions = model.predict(X_test)
 print("X data accuracy: " , round(accuracy_score(y, actual)* 100, 2), "%")
<prepare_x_and_y>
loss_fn = nn.CrossEntropyLoss()
opt = optim.SGD(my_model.parameters() ,lr=0.01,momentum=0.9 )
y = train_data["Survived"]
features = ["Pclass", "Sex", "SibSp", "Parch", "Embarked"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 X['Age'] = train_data['Age']
X['Fare'] = train_data['Fare']
X['Cabin'] = np.where(train_data['Cabin'].isna() ==True,0,1)
 X.groupby('Cabin' ).count()
X_test['Age'] = test_data['Age']
X_test['Fare'] = test_data['Fare']
X_test['Cabin'] = np.where(test_data['Cabin'].isna() ==True,0,1)
 X['Age'] = X['Age'].fillna(X['Age'].mean())
 X_test['Age'] = X_test['Age'].fillna(X_test['Age'].mean())
 X_test['Fare'] = X_test['Fare'].fillna(X_test['Fare'].mean())
 model = RandomForestClassifier(n_estimators=1000, max_depth=5, random_state=1)
 model.fit(X, y)
 actual = model.predict(X)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('my_submission.csv', index=False)
 print("X data accuracy: " , round(accuracy_score(y, actual)* 100, 2), "%")
 print("Your submission was successfully saved!" )<choose_model_class>
loss_epoch_arr = []
loss_arr = []
max_epochs = 6
min_loss = 1000
batch_size = 32
n_iters = np.ceil(9000/batch_size)
 for epoch in range(max_epochs):
for i, data in enumerate(train_loader, 0):
my_model.train()
images, labels = data
images = images.to(device)
 targetnp=labels[:,0,:].cpu().numpy()
targetnp1 = labels[:,1,:].cpu().numpy()
with torch.no_grad() :
new_targets1 = np.argmax(targetnp,axis=1)
 new_targets2 = np.argmax(targetnp1,axis=1)
 new_targets1=torch.LongTensor(new_targets1)
 new_targets2=torch.LongTensor(new_targets2)
 new_targets1 = new_targets1.to(device)
 new_targets2 = new_targets2.to(device)
 opt.zero_grad()
out = my_model.forward(images)
 loss_fc1 = loss_fn(out[0], new_targets1)
 loss_fc2 = loss_fn(out[1],new_targets2)
 loss = torch.add(loss_fc1,loss_fc2)
 loss.backward()
opt.step()
if min_loss > loss.item() :
min_loss = loss.item()
best_model = copy.deepcopy(my_model.state_dict())
 print('Min loss %0.2f' % min_loss)
 if i % 100 == 0:
print('Iteration: %d/%d, Loss: %0.2f' %(i, n_iters, loss.item()))
del images, labels, out
torch.cuda.empty_cache()
loss_arr.append(loss.item())
 print("Epoch number :",epoch)
 print("Train Accuracy :",evaluation(train_loader,my_model))
print("Test Accuracy :" ,evaluation(validation_loader,my_model))
loss_epoch_arr.append(loss.item())
 plt.plot(loss_arr)
 plt.show()
np.random.seed(7)
<import_modules>
print(evaluation(validation_loader,my_model))
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<load_from_csv>
my_model.eval()
plist=[]
fn_list=[]
for inputs_test, fn in test_loader:
inputs_test=inputs_test.to(device)
 out1,out2=my_model.forward(inputs_test)
 _,pred1=torch.max(out1,1)
 pred1=pred1.tolist()
_,pred2=torch.max(out2,1)
 pred2=pred2.tolist()
for x,y,z in zip(pred1,pred2,fn):
p="V"+str(x)+"_"+"C"+str(y)
 plist.append(p)
 fn_list.append(z )
<count_missing_values><EOS> 
submission = pd.DataFrame({"ImageId":fn_list, "Class":plist})
 submission.to_csv('submission.csv', index=False )