{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings('ignore')\n\nfrom sklearn.model_selection import StratifiedKFold\nimport lightgbm as lgb\n\nfrom tqdm import tqdm\nfrom PIL import Image\n\nfrom sklearn.metrics import accuracy_score","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:12:09.846401Z","iopub.execute_input":"2022-02-13T13:12:09.846824Z","iopub.status.idle":"2022-02-13T13:12:11.869524Z","shell.execute_reply.started":"2022-02-13T13:12:09.846715Z","shell.execute_reply":"2022-02-13T13:12:11.868521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR='../input/happy-whale-and-dolphin'\nTRAIN_DIR='../input/happy-whale-and-dolphin/train_images'\nTEST_DIR='../input/happy-whale-and-dolphin/test_images'","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:12:11.871131Z","iopub.execute_input":"2022-02-13T13:12:11.871395Z","iopub.status.idle":"2022-02-13T13:12:11.875318Z","shell.execute_reply.started":"2022-02-13T13:12:11.871363Z","shell.execute_reply":"2022-02-13T13:12:11.874797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_train_file_path(image_id):\n    return f'{TRAIN_DIR}/{image_id}'\ndef get_test_file_path(image_id):\n    return f'{TEST_DIR}/{image_id}'","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:12:11.876544Z","iopub.execute_input":"2022-02-13T13:12:11.877021Z","iopub.status.idle":"2022-02-13T13:12:11.886336Z","shell.execute_reply.started":"2022-02-13T13:12:11.876977Z","shell.execute_reply":"2022-02-13T13:12:11.885754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df=pd.read_csv(os.path.join(ROOT_DIR,'train.csv'))\n#test_df=pd.read_csv(os.path.join(ROOT_DIR,'sample_submission.csv'))\n\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:12:11.888272Z","iopub.execute_input":"2022-02-13T13:12:11.888523Z","iopub.status.idle":"2022-02-13T13:12:12.067453Z","shell.execute_reply.started":"2022-02-13T13:12:11.888494Z","shell.execute_reply":"2022-02-13T13:12:12.066844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_path']=train_df['image'].apply(lambda x:get_train_file_path(x))\n#test_df['image_path']=test_df['image'].apply(lambda x:get_test_file_path(x))\n\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:12:12.068793Z","iopub.execute_input":"2022-02-13T13:12:12.069049Z","iopub.status.idle":"2022-02-13T13:12:12.138251Z","shell.execute_reply.started":"2022-02-13T13:12:12.069015Z","shell.execute_reply":"2022-02-13T13:12:12.137494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image size","metadata":{}},{"cell_type":"code","source":"def create_shape_feature(df):\n    width_height_list = []\n    file_size_list = []\n    for path_ in tqdm(df['image_path']):\n        width_height_list.append(Image.open(path_).size)\n        file_size_list.append(os.path.getsize(path_))\n    df['width_height'] = width_height_list\n    #print(width_height_list)\n    df['file_size'] = file_size_list\n    df['width'] = df['width_height'].apply(lambda x: x[0])\n    df['height'] = df['width_height'].apply(lambda x: x[1])\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:12:12.139736Z","iopub.execute_input":"2022-02-13T13:12:12.140143Z","iopub.status.idle":"2022-02-13T13:12:12.146820Z","shell.execute_reply.started":"2022-02-13T13:12:12.140097Z","shell.execute_reply":"2022-02-13T13:12:12.146296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = create_shape_feature(train_df)\n#test_df = create_shape_feature(test_df)\n\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:12:12.147654Z","iopub.execute_input":"2022-02-13T13:12:12.148280Z","iopub.status.idle":"2022-02-13T13:24:27.898276Z","shell.execute_reply.started":"2022-02-13T13:12:12.148249Z","shell.execute_reply":"2022-02-13T13:24:27.897385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['area'] = train_df['width'] * train_df['height']\ntrain_df['size_per_ pixel'] = train_df['file_size'] / train_df['area']\n\n#test_df['area'] = test_df['width'] * test_df['height']\n#test_df['size_per_ pixel'] = test_df['file_size'] / test_df['area']\n\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:24:27.899687Z","iopub.execute_input":"2022-02-13T13:24:27.899937Z","iopub.status.idle":"2022-02-13T13:24:27.929533Z","shell.execute_reply.started":"2022-02-13T13:24:27.899907Z","shell.execute_reply":"2022-02-13T13:24:27.928927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# label encoding","metadata":{}},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\nle = LabelEncoder()\ntrain_df['species_label']=le.fit_transform(train_df['species'])\n\nprint('species_label amount:',train_df['species_label'].nunique())\ndisplay(train_df)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:24:27.930661Z","iopub.execute_input":"2022-02-13T13:24:27.931069Z","iopub.status.idle":"2022-02-13T13:24:27.972816Z","shell.execute_reply.started":"2022-02-13T13:24:27.931020Z","shell.execute_reply":"2022-02-13T13:24:27.971967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CV","metadata":{}},{"cell_type":"code","source":"#https://www.kaggle.com/debarshichanda/pytorch-arcface-gem-pooling-starter#Create-Folds\n\nskf = StratifiedKFold(n_splits=5)\nfor fold, ( _, val_) in enumerate(skf.split(X=train_df, y=train_df['species_label'])):\n    \n      train_df.loc[val_ , \"kfold\"] = fold\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:24:27.974981Z","iopub.execute_input":"2022-02-13T13:24:27.975218Z","iopub.status.idle":"2022-02-13T13:24:28.027904Z","shell.execute_reply.started":"2022-02-13T13:24:27.975189Z","shell.execute_reply":"2022-02-13T13:24:28.027025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# lgbm","metadata":{}},{"cell_type":"code","source":"params = {\n    'learning_rate':0.01,\n    \"objective\": \"multiclass\",\n    'boosting_type': \"gbdt\",\n    'verbosity': -1,\n    'n_jobs': -1, \n    'seed': 42,\n    'max_depth': 5,\n    'n_estimators': 1000, \n}\n\n\nfor fold in range(5):\n    train=train_df[train_df['kfold']!=fold]\n    valid=train_df[train_df['kfold']==fold]\n\n    X_train=train.drop(['image','species','individual_id','image_path','width_height','kfold','species_label'],axis=1)\n    y_train=train['species_label']\n    X_valid=valid.drop(['image','species','individual_id','image_path','width_height','kfold','species_label'],axis=1)\n    y_valid=valid['species_label']\n\n    model=lgb.LGBMClassifier(**params)\n    model.fit(X_train,y_train,eval_set=[(X_train,y_train),(X_valid,y_valid)],verbose=1000,early_stopping_rounds=15)\n    pred=model.predict(X_valid)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:24:28.029019Z","iopub.execute_input":"2022-02-13T13:24:28.029228Z","iopub.status.idle":"2022-02-13T13:31:29.345366Z","shell.execute_reply.started":"2022-02-13T13:24:28.029201Z","shell.execute_reply":"2022-02-13T13:31:29.344206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#feature_importance\nfi=model.feature_importances_\n\nlgb_imp = pd.DataFrame()\nlgb_imp['Image feature'] = X_train.columns\nlgb_imp['importance'] = fi\n\nplt.figure(figsize=(5,5))\nsns.barplot(x=\"importance\", y=\"Image feature\",data=lgb_imp.sort_values(by=\"importance\",ascending=False))\nplt.title('LightGBM Features (avg over folds)')\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:32:15.322320Z","iopub.execute_input":"2022-02-13T13:32:15.323042Z","iopub.status.idle":"2022-02-13T13:32:15.689657Z","shell.execute_reply.started":"2022-02-13T13:32:15.322984Z","shell.execute_reply":"2022-02-13T13:32:15.688654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#accuracy\nacc = accuracy_score(y_valid,pred)\nprint('accuracy:',acc)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T13:32:17.736740Z","iopub.execute_input":"2022-02-13T13:32:17.737695Z","iopub.status.idle":"2022-02-13T13:32:17.746466Z","shell.execute_reply.started":"2022-02-13T13:32:17.737629Z","shell.execute_reply":"2022-02-13T13:32:17.745581Z"},"trusted":true},"execution_count":null,"outputs":[]}]}