Kaggle competition: Leaf Classification
<SOS> metric: MulticlassLoss Kaggle data source: leaf-classification<define_variables>
import csv as csv
import numpy as np
import pandas as pd
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from scipy import stats
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import KFold,GridSearchCV
train_data["has_cactus"].value_counts().plot(kind="pie")
 train_dir = "train/train/"
val_dir = "train/train"
train_data.head(5)
 train_data.values[:,1]<categorify>
traindf = pd.read_csv('.. /input/train.csv', header=0)
 x_train = traindf.drop(['id', 'species'], axis=1)
 y_train = traindf.pop('species')
 scaler = StandardScaler().fit(x_train)
 x_train = scaler.transform(x_train)
class dataset_(torch.utils.data.Dataset):
def __init__(self,labels,data_directory,transform):
super().__init__()
self.list_id=labels.values[:,0]
self.labels=labels.values[:,1]
self.data_dir=data_directory
self.transform=transform
def __len__(self):
return len(self.list_id)
 def __getitem__(self,index):
name=self.list_id[index]
img=Image.open('.. /input/aerial-cactus-identification/{}/{}'.format(self.data_dir,name))
img=self.transform(img)
 return img,torch.tensor(self.labels[index],dtype=torch.float32 )<categorify>
kfold = KFold(n_splits=5, shuffle=True, random_state=4)
image_transform={"train":transforms.Compose([transforms.RandomRotation(degrees=0),transforms.RandomResizedCrop(224),transforms.RandomHorizontalFlip(p=0.5),transforms.ToTensor() ,transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])]),
"test": transforms.Compose([transforms.Resize(224),transforms.ToTensor() ,transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])])}<load_pretrained>
rf = ExtraTreesClassifier(n_estimators=500, random_state=0)
 rf_validation=[rf.fit(x_train[train], y_train[train] ).score(x_train[test], y_train[test] ).mean() \
for train, test in kfold.split(x_train)]
<split><EOS> 
np.random.seed(42)
 train = pd.read_csv('.. /input/train.csv')
 x_train = train.drop(['id', 'species', 'margin7', 'margin15', 'margin33', 'texture14','margin51','margin60'], axis=1 ).values
le = LabelEncoder().fit(train['species'])
 y_train = le.transform(train['species'])
 scaler = StandardScaler().fit(x_train)
 x_train = scaler.transform(x_train)
 test = pd.read_csv('.. /input/test.csv')
 test = test.drop(['margin7', 'margin15', 'margin33', 'texture14','margin51','margin60'], axis=1)
 test_ids = test.pop('id')
 x_test = test.values
scaler = StandardScaler().fit(x_test)
 x_test = scaler.transform(x_test)
 log_reg = LogisticRegression(C=2200, multi_class="multinomial", tol=0.0001, solver='newton-cg')
 log_reg.fit(x_train, y_train)
 y_test = log_reg.predict_proba(x_test)
 submission = pd.DataFrame(y_test, index=test_ids, columns=le.classes_)
 submission.to_csv('submission.csv' )