{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Extraction des données climate des echantillons IRIGIN barthii et glaberrima"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np               # linear algebra\n",
    "import pandas as pd              # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "import seaborn as sns            # Statistical Data Visualization\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.font_manager as fm\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.decomposition import PCA"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Christine m'a donnée les données de latitude et longitude qu'elle avais récupéré precedement pour l'article de Cubry et al 2020"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Extraction des données clima avec le package R raster"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = \"coord-finales-bg.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(file, delimiter='\\t', header=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code_vcf</th>\n",
       "      <th>species</th>\n",
       "      <th>country</th>\n",
       "      <th>passeport</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AA</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>518G1</td>\n",
       "      <td>15,7333333333333</td>\n",
       "      <td>-4,76666666666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AB</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>519W1</td>\n",
       "      <td>15,716667</td>\n",
       "      <td>-4,916667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AC</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>520W1</td>\n",
       "      <td>14,7666666666667</td>\n",
       "      <td>-4,21666666666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>AQ</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Lake Chad</td>\n",
       "      <td>537W1</td>\n",
       "      <td>12,8333333333333</td>\n",
       "      <td>14,9333333333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AR</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Lake Chad</td>\n",
       "      <td>538W1</td>\n",
       "      <td>12,5</td>\n",
       "      <td>14,9166666666667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  code_vcf     species    country passeport          latitude  \\\n",
       "0       AA  O. barthii       Mali     518G1  15,7333333333333   \n",
       "1       AB  O. barthii       Mali     519W1         15,716667   \n",
       "2       AC  O. barthii       Mali     520W1  14,7666666666667   \n",
       "3       AQ  O. barthii  Lake Chad     537W1  12,8333333333333   \n",
       "4       AR  O. barthii  Lake Chad     538W1              12,5   \n",
       "\n",
       "           longitude  \n",
       "0  -4,76666666666667  \n",
       "1          -4,916667  \n",
       "2  -4,21666666666667  \n",
       "3   14,9333333333333  \n",
       "4   14,9166666666667  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['latitude'] = df['latitude'].str.replace(',','.').astype(float)\n",
    "df['longitude'] = df['longitude'].str.replace(',','.').astype(float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code_vcf</th>\n",
       "      <th>species</th>\n",
       "      <th>country</th>\n",
       "      <th>passeport</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AA</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>518G1</td>\n",
       "      <td>15.733333</td>\n",
       "      <td>-4.766667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AB</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>519W1</td>\n",
       "      <td>15.716667</td>\n",
       "      <td>-4.916667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AC</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>520W1</td>\n",
       "      <td>14.766667</td>\n",
       "      <td>-4.216667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>AQ</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Lake Chad</td>\n",
       "      <td>537W1</td>\n",
       "      <td>12.833333</td>\n",
       "      <td>14.933333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AR</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Lake Chad</td>\n",
       "      <td>538W1</td>\n",
       "      <td>12.500000</td>\n",
       "      <td>14.916667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  code_vcf     species    country passeport   latitude  longitude\n",
       "0       AA  O. barthii       Mali     518G1  15.733333  -4.766667\n",
       "1       AB  O. barthii       Mali     519W1  15.716667  -4.916667\n",
       "2       AC  O. barthii       Mali     520W1  14.766667  -4.216667\n",
       "3       AQ  O. barthii  Lake Chad     537W1  12.833333  14.933333\n",
       "4       AR  O. barthii  Lake Chad     538W1  12.500000  14.916667"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'coord-finales-bg.clima.csv'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_name=f\"{file}\".replace('.csv' ,'.clima.csv')\n",
    "output_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "### https://emilypiche.github.io/BIO381/raster.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# package pour lancer du R\n",
    "%load_ext rpy2.ipython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "R[write to console]: Le chargement a nécessité le package : raster\n",
      "\n",
      "R[write to console]: Le chargement a nécessité le package : sp\n",
      "\n",
      "R[write to console]: \n",
      "Attachement du package : ‘dplyr’\n",
      "\n",
      "\n",
      "R[write to console]: The following objects are masked from ‘package:raster’:\n",
      "\n",
      "    intersect, select, union\n",
      "\n",
      "\n",
      "R[write to console]: The following objects are masked from ‘package:stats’:\n",
      "\n",
      "    filter, lag\n",
      "\n",
      "\n",
      "R[write to console]: The following objects are masked from ‘package:base’:\n",
      "\n",
      "    intersect, setdiff, setequal, union\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%R -i df -i output_name\n",
    "require(raster)\n",
    "set.seed(802)\n",
    "long = df[,5]\n",
    "lat = df[,6]\n",
    "\n",
    "# downloading the data from worlclim\n",
    "r <- getData(\"worldclim\", var=\"bio\", res=2.5)\n",
    "# selecting the layers we want and naming them\n",
    "names(r) = c(\"b1.Annual_Mean_Temp\",\"b2.Mean_Diurnal_Range\", \"b3.Isothermality\", \"b4.Temp_Seasonality\", \n",
    "             \"b5.Max_Temp_of_Warmest_Month\", \"b6.Min_Temp_of_Coldest_Month\", \"b7.Temp_Annual_Range\",\n",
    "             \"b8.Mean_Temp_of_Wettest_Quarter\",\"b9.Mean_Temp_of_Driest_Quarter\",\"b10.Mean_Temp_of_Warmest_Quarter\",\n",
    "             \"b11.Mean_Temp_of_Coldest_Quarter\", \"b12.Annual_Precipitation\", \"b13.Precipitation_of_Wettest_Month\",\n",
    "             \"b14.Precipitation_of_Driest_Month\", \"b15.Precipitation_Seasonality\", \"b16.Precipitation_of_Wettest_Quarter\",\n",
    "             \"b17.Precipitation_of_Driest_Quarter\", \"b18.Precipitation_of_Warmest_Quarter\", \"b19.Precipitation_of_Coldest_Quarter\")\n",
    "#\"code_vcf\",\"species\",\"country\",\"passeport\",\"latitude\",\"longitude\"\n",
    "\n",
    "# putting our coordinates into the format r::raster likes\n",
    "points <- SpatialPoints(df[,5:6], proj4string = r@crs)\n",
    "# extracting the values for coordinates (lattitude and longitud)\n",
    "cc <- extract(r, points)\n",
    "# creating a dataframe with results\n",
    "climate <- cbind.data.frame(df, cc)\n",
    "\n",
    "# What is with those weird temperatures? They are actually in C*10!! \n",
    "# Having temperature in this format reduces download time and filesize for the tiles.\n",
    "# We can easily manipulate those columns into normal Celsius using a function from dplyr.\n",
    "library(dplyr)\n",
    "climate <- mutate(climate, MAT=b1.Annual_Mean_Temp/10) %>%\n",
    "  select(-b1.Annual_Mean_Temp)\n",
    "write.csv(climate, output_name, row.names = TRUE, sep=\"\\t\")\n",
    "#print(climate)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# recovery objet R to python\n",
    "climate_all = %R climate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code_vcf</th>\n",
       "      <th>species</th>\n",
       "      <th>country</th>\n",
       "      <th>passeport</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>b2.Mean_Diurnal_Range</th>\n",
       "      <th>b3.Isothermality</th>\n",
       "      <th>b4.Temp_Seasonality</th>\n",
       "      <th>b5.Max_Temp_of_Warmest_Month</th>\n",
       "      <th>...</th>\n",
       "      <th>b11.Mean_Temp_of_Coldest_Quarter</th>\n",
       "      <th>b12.Annual_Precipitation</th>\n",
       "      <th>b13.Precipitation_of_Wettest_Month</th>\n",
       "      <th>b14.Precipitation_of_Driest_Month</th>\n",
       "      <th>b15.Precipitation_Seasonality</th>\n",
       "      <th>b16.Precipitation_of_Wettest_Quarter</th>\n",
       "      <th>b17.Precipitation_of_Driest_Quarter</th>\n",
       "      <th>b18.Precipitation_of_Warmest_Quarter</th>\n",
       "      <th>b19.Precipitation_of_Coldest_Quarter</th>\n",
       "      <th>MAT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AA</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>518G1</td>\n",
       "      <td>15.733333</td>\n",
       "      <td>-4.766667</td>\n",
       "      <td>99.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1230.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>...</td>\n",
       "      <td>226.0</td>\n",
       "      <td>1462.0</td>\n",
       "      <td>249.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>573.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>24.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AB</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>519W1</td>\n",
       "      <td>15.716667</td>\n",
       "      <td>-4.916667</td>\n",
       "      <td>100.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1235.0</td>\n",
       "      <td>301.0</td>\n",
       "      <td>...</td>\n",
       "      <td>217.0</td>\n",
       "      <td>1525.0</td>\n",
       "      <td>259.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>603.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>575.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>23.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AC</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>520W1</td>\n",
       "      <td>14.766667</td>\n",
       "      <td>-4.216667</td>\n",
       "      <td>93.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>1389.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>...</td>\n",
       "      <td>223.0</td>\n",
       "      <td>1416.0</td>\n",
       "      <td>264.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>73.0</td>\n",
       "      <td>579.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>544.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>24.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>AQ</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Lake Chad</td>\n",
       "      <td>537W1</td>\n",
       "      <td>12.833333</td>\n",
       "      <td>14.933333</td>\n",
       "      <td>154.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>3955.0</td>\n",
       "      <td>403.0</td>\n",
       "      <td>...</td>\n",
       "      <td>214.0</td>\n",
       "      <td>118.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AR</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Lake Chad</td>\n",
       "      <td>538W1</td>\n",
       "      <td>12.500000</td>\n",
       "      <td>14.916667</td>\n",
       "      <td>154.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>3913.0</td>\n",
       "      <td>403.0</td>\n",
       "      <td>...</td>\n",
       "      <td>214.0</td>\n",
       "      <td>109.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>172.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>164</th>\n",
       "      <td>NB</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Guinea</td>\n",
       "      <td>80</td>\n",
       "      <td>9.998333</td>\n",
       "      <td>-11.318889</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>165</th>\n",
       "      <td>ND</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Zimbabwe</td>\n",
       "      <td>82</td>\n",
       "      <td>-19.013056</td>\n",
       "      <td>29.146389</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>166</th>\n",
       "      <td>NF</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Burkina Faso</td>\n",
       "      <td>87</td>\n",
       "      <td>12.241667</td>\n",
       "      <td>-1.556667</td>\n",
       "      <td>99.0</td>\n",
       "      <td>73.0</td>\n",
       "      <td>1013.0</td>\n",
       "      <td>295.0</td>\n",
       "      <td>...</td>\n",
       "      <td>213.0</td>\n",
       "      <td>1905.0</td>\n",
       "      <td>318.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>813.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>642.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>NG</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Mali</td>\n",
       "      <td>89</td>\n",
       "      <td>17.579722</td>\n",
       "      <td>-3.998611</td>\n",
       "      <td>102.0</td>\n",
       "      <td>77.0</td>\n",
       "      <td>636.0</td>\n",
       "      <td>309.0</td>\n",
       "      <td>...</td>\n",
       "      <td>241.0</td>\n",
       "      <td>1577.0</td>\n",
       "      <td>216.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>586.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>522.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>25.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>168</th>\n",
       "      <td>NI</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Liberia</td>\n",
       "      <td>97</td>\n",
       "      <td>6.452222</td>\n",
       "      <td>-9.428333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>169 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    code_vcf        species       country passeport   latitude  longitude  \\\n",
       "0         AA     O. barthii          Mali     518G1  15.733333  -4.766667   \n",
       "1         AB     O. barthii          Mali     519W1  15.716667  -4.916667   \n",
       "2         AC     O. barthii          Mali     520W1  14.766667  -4.216667   \n",
       "3         AQ     O. barthii     Lake Chad     537W1  12.833333  14.933333   \n",
       "4         AR     O. barthii     Lake Chad     538W1  12.500000  14.916667   \n",
       "..       ...            ...           ...       ...        ...        ...   \n",
       "164       NB  O. glaberrima        Guinea        80   9.998333 -11.318889   \n",
       "165       ND  O. glaberrima      Zimbabwe        82 -19.013056  29.146389   \n",
       "166       NF  O. glaberrima  Burkina Faso        87  12.241667  -1.556667   \n",
       "167       NG  O. glaberrima          Mali        89  17.579722  -3.998611   \n",
       "168       NI  O. glaberrima       Liberia        97   6.452222  -9.428333   \n",
       "\n",
       "     b2.Mean_Diurnal_Range  b3.Isothermality  b4.Temp_Seasonality  \\\n",
       "0                     99.0              68.0               1230.0   \n",
       "1                    100.0              68.0               1235.0   \n",
       "2                     93.0              65.0               1389.0   \n",
       "3                    154.0              55.0               3955.0   \n",
       "4                    154.0              55.0               3913.0   \n",
       "..                     ...               ...                  ...   \n",
       "164                    NaN               NaN                  NaN   \n",
       "165                    NaN               NaN                  NaN   \n",
       "166                   99.0              73.0               1013.0   \n",
       "167                  102.0              77.0                636.0   \n",
       "168                    NaN               NaN                  NaN   \n",
       "\n",
       "     b5.Max_Temp_of_Warmest_Month  ...  b11.Mean_Temp_of_Coldest_Quarter  \\\n",
       "0                           310.0  ...                             226.0   \n",
       "1                           301.0  ...                             217.0   \n",
       "2                           310.0  ...                             223.0   \n",
       "3                           403.0  ...                             214.0   \n",
       "4                           403.0  ...                             214.0   \n",
       "..                            ...  ...                               ...   \n",
       "164                           NaN  ...                               NaN   \n",
       "165                           NaN  ...                               NaN   \n",
       "166                         295.0  ...                             213.0   \n",
       "167                         309.0  ...                             241.0   \n",
       "168                           NaN  ...                               NaN   \n",
       "\n",
       "     b12.Annual_Precipitation  b13.Precipitation_of_Wettest_Month  \\\n",
       "0                      1462.0                               249.0   \n",
       "1                      1525.0                               259.0   \n",
       "2                      1416.0                               264.0   \n",
       "3                       118.0                                62.0   \n",
       "4                       109.0                                57.0   \n",
       "..                        ...                                 ...   \n",
       "164                       NaN                                 NaN   \n",
       "165                       NaN                                 NaN   \n",
       "166                    1905.0                               318.0   \n",
       "167                    1577.0                               216.0   \n",
       "168                       NaN                                 NaN   \n",
       "\n",
       "     b14.Precipitation_of_Driest_Month  b15.Precipitation_Seasonality  \\\n",
       "0                                  3.0                           68.0   \n",
       "1                                  3.0                           67.0   \n",
       "2                                  0.0                           73.0   \n",
       "3                                  0.0                          184.0   \n",
       "4                                  0.0                          172.0   \n",
       "..                                 ...                            ...   \n",
       "164                                NaN                            NaN   \n",
       "165                                NaN                            NaN   \n",
       "166                                3.0                           65.0   \n",
       "167                               10.0                           51.0   \n",
       "168                                NaN                            NaN   \n",
       "\n",
       "     b16.Precipitation_of_Wettest_Quarter  \\\n",
       "0                                   573.0   \n",
       "1                                   603.0   \n",
       "2                                   579.0   \n",
       "3                                   107.0   \n",
       "4                                   100.0   \n",
       "..                                    ...   \n",
       "164                                   NaN   \n",
       "165                                   NaN   \n",
       "166                                 813.0   \n",
       "167                                 586.0   \n",
       "168                                   NaN   \n",
       "\n",
       "     b17.Precipitation_of_Driest_Quarter  \\\n",
       "0                                   17.0   \n",
       "1                                   18.0   \n",
       "2                                    8.0   \n",
       "3                                    0.0   \n",
       "4                                    0.0   \n",
       "..                                   ...   \n",
       "164                                  NaN   \n",
       "165                                  NaN   \n",
       "166                                 30.0   \n",
       "167                                 71.0   \n",
       "168                                  NaN   \n",
       "\n",
       "     b18.Precipitation_of_Warmest_Quarter  \\\n",
       "0                                   549.0   \n",
       "1                                   575.0   \n",
       "2                                   544.0   \n",
       "3                                    45.0   \n",
       "4                                     8.0   \n",
       "..                                    ...   \n",
       "164                                   NaN   \n",
       "165                                   NaN   \n",
       "166                                 642.0   \n",
       "167                                 522.0   \n",
       "168                                   NaN   \n",
       "\n",
       "     b19.Precipitation_of_Coldest_Quarter   MAT  \n",
       "0                                    17.0  24.6  \n",
       "1                                    18.0  23.7  \n",
       "2                                     8.0  24.5  \n",
       "3                                     0.0  27.3  \n",
       "4                                     0.0  27.3  \n",
       "..                                    ...   ...  \n",
       "164                                   NaN   NaN  \n",
       "165                                   NaN   NaN  \n",
       "166                                  30.0  23.0  \n",
       "167                                  71.0  25.1  \n",
       "168                                   NaN   NaN  \n",
       "\n",
       "[169 rows x 25 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "climate_all\n",
    "#172 rows × 25 columns"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Traitement des NaN\n",
    "\n",
    "Possible de faire de l'imputation mais par facilité j'ai juste viré les NaN\n",
    "\n",
    "Si imputation voir https://scikit-learn.org/stable/modules/impute.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "climate_all = climate_all.dropna()\n",
    "#113 rows × 25 columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code_vcf</th>\n",
       "      <th>species</th>\n",
       "      <th>country</th>\n",
       "      <th>passeport</th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>b2.Mean_Diurnal_Range</th>\n",
       "      <th>b3.Isothermality</th>\n",
       "      <th>b4.Temp_Seasonality</th>\n",
       "      <th>b5.Max_Temp_of_Warmest_Month</th>\n",
       "      <th>...</th>\n",
       "      <th>b11.Mean_Temp_of_Coldest_Quarter</th>\n",
       "      <th>b12.Annual_Precipitation</th>\n",
       "      <th>b13.Precipitation_of_Wettest_Month</th>\n",
       "      <th>b14.Precipitation_of_Driest_Month</th>\n",
       "      <th>b15.Precipitation_Seasonality</th>\n",
       "      <th>b16.Precipitation_of_Wettest_Quarter</th>\n",
       "      <th>b17.Precipitation_of_Driest_Quarter</th>\n",
       "      <th>b18.Precipitation_of_Warmest_Quarter</th>\n",
       "      <th>b19.Precipitation_of_Coldest_Quarter</th>\n",
       "      <th>MAT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>AA</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>518G1</td>\n",
       "      <td>15.733333</td>\n",
       "      <td>-4.766667</td>\n",
       "      <td>99.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1230.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>...</td>\n",
       "      <td>226.0</td>\n",
       "      <td>1462.0</td>\n",
       "      <td>249.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>573.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>24.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>AB</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>519W1</td>\n",
       "      <td>15.716667</td>\n",
       "      <td>-4.916667</td>\n",
       "      <td>100.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1235.0</td>\n",
       "      <td>301.0</td>\n",
       "      <td>...</td>\n",
       "      <td>217.0</td>\n",
       "      <td>1525.0</td>\n",
       "      <td>259.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>603.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>575.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>23.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>AC</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Mali</td>\n",
       "      <td>520W1</td>\n",
       "      <td>14.766667</td>\n",
       "      <td>-4.216667</td>\n",
       "      <td>93.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>1389.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>...</td>\n",
       "      <td>223.0</td>\n",
       "      <td>1416.0</td>\n",
       "      <td>264.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>73.0</td>\n",
       "      <td>579.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>544.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>24.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>AQ</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Lake Chad</td>\n",
       "      <td>537W1</td>\n",
       "      <td>12.833333</td>\n",
       "      <td>14.933333</td>\n",
       "      <td>154.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>3955.0</td>\n",
       "      <td>403.0</td>\n",
       "      <td>...</td>\n",
       "      <td>214.0</td>\n",
       "      <td>118.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>AR</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>Lake Chad</td>\n",
       "      <td>538W1</td>\n",
       "      <td>12.500000</td>\n",
       "      <td>14.916667</td>\n",
       "      <td>154.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>3913.0</td>\n",
       "      <td>403.0</td>\n",
       "      <td>...</td>\n",
       "      <td>214.0</td>\n",
       "      <td>109.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>172.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>159</th>\n",
       "      <td>MN</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Chad</td>\n",
       "      <td>459</td>\n",
       "      <td>9.333333</td>\n",
       "      <td>14.666667</td>\n",
       "      <td>148.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>3202.0</td>\n",
       "      <td>396.0</td>\n",
       "      <td>...</td>\n",
       "      <td>227.0</td>\n",
       "      <td>224.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>162.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>160</th>\n",
       "      <td>MP</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Chad</td>\n",
       "      <td>461</td>\n",
       "      <td>9.250000</td>\n",
       "      <td>16.083333</td>\n",
       "      <td>154.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>3926.0</td>\n",
       "      <td>402.0</td>\n",
       "      <td>...</td>\n",
       "      <td>214.0</td>\n",
       "      <td>116.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>171.0</td>\n",
       "      <td>105.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>161</th>\n",
       "      <td>MR</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Senegal</td>\n",
       "      <td>498</td>\n",
       "      <td>14.850000</td>\n",
       "      <td>-13.000000</td>\n",
       "      <td>129.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>1183.0</td>\n",
       "      <td>284.0</td>\n",
       "      <td>...</td>\n",
       "      <td>176.0</td>\n",
       "      <td>1270.0</td>\n",
       "      <td>259.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>562.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>342.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>19.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>166</th>\n",
       "      <td>NF</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Burkina Faso</td>\n",
       "      <td>87</td>\n",
       "      <td>12.241667</td>\n",
       "      <td>-1.556667</td>\n",
       "      <td>99.0</td>\n",
       "      <td>73.0</td>\n",
       "      <td>1013.0</td>\n",
       "      <td>295.0</td>\n",
       "      <td>...</td>\n",
       "      <td>213.0</td>\n",
       "      <td>1905.0</td>\n",
       "      <td>318.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>813.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>642.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>NG</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>Mali</td>\n",
       "      <td>89</td>\n",
       "      <td>17.579722</td>\n",
       "      <td>-3.998611</td>\n",
       "      <td>102.0</td>\n",
       "      <td>77.0</td>\n",
       "      <td>636.0</td>\n",
       "      <td>309.0</td>\n",
       "      <td>...</td>\n",
       "      <td>241.0</td>\n",
       "      <td>1577.0</td>\n",
       "      <td>216.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>586.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>522.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>25.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>110 rows × 25 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    code_vcf        species       country passeport   latitude  longitude  \\\n",
       "0         AA     O. barthii          Mali     518G1  15.733333  -4.766667   \n",
       "1         AB     O. barthii          Mali     519W1  15.716667  -4.916667   \n",
       "2         AC     O. barthii          Mali     520W1  14.766667  -4.216667   \n",
       "3         AQ     O. barthii     Lake Chad     537W1  12.833333  14.933333   \n",
       "4         AR     O. barthii     Lake Chad     538W1  12.500000  14.916667   \n",
       "..       ...            ...           ...       ...        ...        ...   \n",
       "159       MN  O. glaberrima          Chad       459   9.333333  14.666667   \n",
       "160       MP  O. glaberrima          Chad       461   9.250000  16.083333   \n",
       "161       MR  O. glaberrima       Senegal       498  14.850000 -13.000000   \n",
       "166       NF  O. glaberrima  Burkina Faso        87  12.241667  -1.556667   \n",
       "167       NG  O. glaberrima          Mali        89  17.579722  -3.998611   \n",
       "\n",
       "     b2.Mean_Diurnal_Range  b3.Isothermality  b4.Temp_Seasonality  \\\n",
       "0                     99.0              68.0               1230.0   \n",
       "1                    100.0              68.0               1235.0   \n",
       "2                     93.0              65.0               1389.0   \n",
       "3                    154.0              55.0               3955.0   \n",
       "4                    154.0              55.0               3913.0   \n",
       "..                     ...               ...                  ...   \n",
       "159                  148.0              57.0               3202.0   \n",
       "160                  154.0              55.0               3926.0   \n",
       "161                  129.0              67.0               1183.0   \n",
       "166                   99.0              73.0               1013.0   \n",
       "167                  102.0              77.0                636.0   \n",
       "\n",
       "     b5.Max_Temp_of_Warmest_Month  ...  b11.Mean_Temp_of_Coldest_Quarter  \\\n",
       "0                           310.0  ...                             226.0   \n",
       "1                           301.0  ...                             217.0   \n",
       "2                           310.0  ...                             223.0   \n",
       "3                           403.0  ...                             214.0   \n",
       "4                           403.0  ...                             214.0   \n",
       "..                            ...  ...                               ...   \n",
       "159                         396.0  ...                             227.0   \n",
       "160                         402.0  ...                             214.0   \n",
       "161                         284.0  ...                             176.0   \n",
       "166                         295.0  ...                             213.0   \n",
       "167                         309.0  ...                             241.0   \n",
       "\n",
       "     b12.Annual_Precipitation  b13.Precipitation_of_Wettest_Month  \\\n",
       "0                      1462.0                               249.0   \n",
       "1                      1525.0                               259.0   \n",
       "2                      1416.0                               264.0   \n",
       "3                       118.0                                62.0   \n",
       "4                       109.0                                57.0   \n",
       "..                        ...                                 ...   \n",
       "159                     224.0                                95.0   \n",
       "160                     116.0                                53.0   \n",
       "161                    1270.0                               259.0   \n",
       "166                    1905.0                               318.0   \n",
       "167                    1577.0                               216.0   \n",
       "\n",
       "     b14.Precipitation_of_Driest_Month  b15.Precipitation_Seasonality  \\\n",
       "0                                  3.0                           68.0   \n",
       "1                                  3.0                           67.0   \n",
       "2                                  0.0                           73.0   \n",
       "3                                  0.0                          184.0   \n",
       "4                                  0.0                          172.0   \n",
       "..                                 ...                            ...   \n",
       "159                                0.0                          162.0   \n",
       "160                                0.0                          171.0   \n",
       "161                                0.0                           85.0   \n",
       "166                                3.0                           65.0   \n",
       "167                               10.0                           51.0   \n",
       "\n",
       "     b16.Precipitation_of_Wettest_Quarter  \\\n",
       "0                                   573.0   \n",
       "1                                   603.0   \n",
       "2                                   579.0   \n",
       "3                                   107.0   \n",
       "4                                   100.0   \n",
       "..                                    ...   \n",
       "159                                 197.0   \n",
       "160                                 105.0   \n",
       "161                                 562.0   \n",
       "166                                 813.0   \n",
       "167                                 586.0   \n",
       "\n",
       "     b17.Precipitation_of_Driest_Quarter  \\\n",
       "0                                   17.0   \n",
       "1                                   18.0   \n",
       "2                                    8.0   \n",
       "3                                    0.0   \n",
       "4                                    0.0   \n",
       "..                                   ...   \n",
       "159                                  0.0   \n",
       "160                                  0.0   \n",
       "161                                  1.0   \n",
       "166                                 30.0   \n",
       "167                                 71.0   \n",
       "\n",
       "     b18.Precipitation_of_Warmest_Quarter  \\\n",
       "0                                   549.0   \n",
       "1                                   575.0   \n",
       "2                                   544.0   \n",
       "3                                    45.0   \n",
       "4                                     8.0   \n",
       "..                                    ...   \n",
       "159                                  26.0   \n",
       "160                                  11.0   \n",
       "161                                 342.0   \n",
       "166                                 642.0   \n",
       "167                                 522.0   \n",
       "\n",
       "     b19.Precipitation_of_Coldest_Quarter   MAT  \n",
       "0                                    17.0  24.6  \n",
       "1                                    18.0  23.7  \n",
       "2                                     8.0  24.5  \n",
       "3                                     0.0  27.3  \n",
       "4                                     0.0  27.3  \n",
       "..                                    ...   ...  \n",
       "159                                   0.0  27.3  \n",
       "160                                   0.0  27.2  \n",
       "161                                  11.0  19.4  \n",
       "166                                  30.0  23.0  \n",
       "167                                  71.0  25.1  \n",
       "\n",
       "[110 rows x 25 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "climate_all"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Separation de clima et sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# on vire les colonnes innecessaires\n",
    "climate = climate_all.drop(\n",
    "    columns=['code_vcf','species','country','passeport','latitude','longitude'])\n",
    "#on extrait la variable diagnostique. espece dans notre cas\n",
    "species = climate_all[\"species\"]\n",
    "code_vcf = climate_all[\"code_vcf\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    O. barthii\n",
       "1    O. barthii\n",
       "2    O. barthii\n",
       "3    O. barthii\n",
       "4    O. barthii\n",
       "Name: species, dtype: object"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "species.head()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Correlation de pearson pour les variables clima"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b2.Mean_Diurnal_Range</th>\n",
       "      <th>b3.Isothermality</th>\n",
       "      <th>b4.Temp_Seasonality</th>\n",
       "      <th>b5.Max_Temp_of_Warmest_Month</th>\n",
       "      <th>b6.Min_Temp_of_Coldest_Month</th>\n",
       "      <th>b7.Temp_Annual_Range</th>\n",
       "      <th>b8.Mean_Temp_of_Wettest_Quarter</th>\n",
       "      <th>b9.Mean_Temp_of_Driest_Quarter</th>\n",
       "      <th>b10.Mean_Temp_of_Warmest_Quarter</th>\n",
       "      <th>b11.Mean_Temp_of_Coldest_Quarter</th>\n",
       "      <th>b12.Annual_Precipitation</th>\n",
       "      <th>b13.Precipitation_of_Wettest_Month</th>\n",
       "      <th>b14.Precipitation_of_Driest_Month</th>\n",
       "      <th>b15.Precipitation_Seasonality</th>\n",
       "      <th>b16.Precipitation_of_Wettest_Quarter</th>\n",
       "      <th>b17.Precipitation_of_Driest_Quarter</th>\n",
       "      <th>b18.Precipitation_of_Warmest_Quarter</th>\n",
       "      <th>b19.Precipitation_of_Coldest_Quarter</th>\n",
       "      <th>MAT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>99.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1230.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>166.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>250.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>1462.0</td>\n",
       "      <td>249.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>573.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>24.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>100.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1235.0</td>\n",
       "      <td>301.0</td>\n",
       "      <td>155.0</td>\n",
       "      <td>146.0</td>\n",
       "      <td>241.0</td>\n",
       "      <td>217.0</td>\n",
       "      <td>248.0</td>\n",
       "      <td>217.0</td>\n",
       "      <td>1525.0</td>\n",
       "      <td>259.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>603.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>575.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>23.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   b2.Mean_Diurnal_Range  b3.Isothermality  b4.Temp_Seasonality  \\\n",
       "0                   99.0              68.0               1230.0   \n",
       "1                  100.0              68.0               1235.0   \n",
       "\n",
       "   b5.Max_Temp_of_Warmest_Month  b6.Min_Temp_of_Coldest_Month  \\\n",
       "0                         310.0                         166.0   \n",
       "1                         301.0                         155.0   \n",
       "\n",
       "   b7.Temp_Annual_Range  b8.Mean_Temp_of_Wettest_Quarter  \\\n",
       "0                 144.0                            250.0   \n",
       "1                 146.0                            241.0   \n",
       "\n",
       "   b9.Mean_Temp_of_Driest_Quarter  b10.Mean_Temp_of_Warmest_Quarter  \\\n",
       "0                           226.0                             258.0   \n",
       "1                           217.0                             248.0   \n",
       "\n",
       "   b11.Mean_Temp_of_Coldest_Quarter  b12.Annual_Precipitation  \\\n",
       "0                             226.0                    1462.0   \n",
       "1                             217.0                    1525.0   \n",
       "\n",
       "   b13.Precipitation_of_Wettest_Month  b14.Precipitation_of_Driest_Month  \\\n",
       "0                               249.0                                3.0   \n",
       "1                               259.0                                3.0   \n",
       "\n",
       "   b15.Precipitation_Seasonality  b16.Precipitation_of_Wettest_Quarter  \\\n",
       "0                           68.0                                 573.0   \n",
       "1                           67.0                                 603.0   \n",
       "\n",
       "   b17.Precipitation_of_Driest_Quarter  b18.Precipitation_of_Warmest_Quarter  \\\n",
       "0                                 17.0                                 549.0   \n",
       "1                                 18.0                                 575.0   \n",
       "\n",
       "   b19.Precipitation_of_Coldest_Quarter   MAT  \n",
       "0                                  17.0  24.6  \n",
       "1                                  18.0  23.7  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "climate.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(110, 19)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "climate.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(110,)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "species.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x7efbd8b28490>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 278936 characters]",
      "text/plain": [
       "<Figure size 864x864 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "font1 = fm.FontProperties(size=20)\n",
    "font2 = fm.FontProperties(size=24)\n",
    "plt.figure(figsize=(12,12))\n",
    "plt.title('Correlation Pearson des variables', y=1.05, size=18)\n",
    "sns.heatmap(climate.corr(),linewidths=0.3,vmax=1.0, \n",
    "            square=True, cmap='coolwarm', linecolor='white', annot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.PairGrid at 0x7efbc0428ac0>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 1086604 characters]",
      "text/plain": [
       "<Figure size 3420x3420 with 380 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.pairplot(data=climate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.PairGrid at 0x7efbb1e7d910>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 21440 characters]",
      "text/plain": [
       "<Figure size 360x360 with 6 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "new = climate[['MAT','b12.Annual_Precipitation']]\n",
    "sns.pairplot(data=new)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### check of \"Nan\" data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], dtype: int64)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# show features with count of NaN values\n",
    "s = climate.isnull().sum()\n",
    "s = s.sort_values(ascending=False)\n",
    "s = s[s > 0]\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check unique names for variables with NaN values (only print 5 firts columns)\n",
    "for i in s.index:\n",
    "    print(i, climate[i].unique().tolist()[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "for feature in climate.columns:\n",
    "    climate[feature] = climate[feature].fillna('NaN')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b2.Mean_Diurnal_Range</th>\n",
       "      <th>b3.Isothermality</th>\n",
       "      <th>b4.Temp_Seasonality</th>\n",
       "      <th>b5.Max_Temp_of_Warmest_Month</th>\n",
       "      <th>b6.Min_Temp_of_Coldest_Month</th>\n",
       "      <th>b7.Temp_Annual_Range</th>\n",
       "      <th>b8.Mean_Temp_of_Wettest_Quarter</th>\n",
       "      <th>b9.Mean_Temp_of_Driest_Quarter</th>\n",
       "      <th>b10.Mean_Temp_of_Warmest_Quarter</th>\n",
       "      <th>b11.Mean_Temp_of_Coldest_Quarter</th>\n",
       "      <th>b12.Annual_Precipitation</th>\n",
       "      <th>b13.Precipitation_of_Wettest_Month</th>\n",
       "      <th>b14.Precipitation_of_Driest_Month</th>\n",
       "      <th>b15.Precipitation_Seasonality</th>\n",
       "      <th>b16.Precipitation_of_Wettest_Quarter</th>\n",
       "      <th>b17.Precipitation_of_Driest_Quarter</th>\n",
       "      <th>b18.Precipitation_of_Warmest_Quarter</th>\n",
       "      <th>b19.Precipitation_of_Coldest_Quarter</th>\n",
       "      <th>MAT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>99.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1230.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>166.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>250.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>1462.0</td>\n",
       "      <td>249.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>573.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>24.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>100.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1235.0</td>\n",
       "      <td>301.0</td>\n",
       "      <td>155.0</td>\n",
       "      <td>146.0</td>\n",
       "      <td>241.0</td>\n",
       "      <td>217.0</td>\n",
       "      <td>248.0</td>\n",
       "      <td>217.0</td>\n",
       "      <td>1525.0</td>\n",
       "      <td>259.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>603.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>575.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>23.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   b2.Mean_Diurnal_Range  b3.Isothermality  b4.Temp_Seasonality  \\\n",
       "0                   99.0              68.0               1230.0   \n",
       "1                  100.0              68.0               1235.0   \n",
       "\n",
       "   b5.Max_Temp_of_Warmest_Month  b6.Min_Temp_of_Coldest_Month  \\\n",
       "0                         310.0                         166.0   \n",
       "1                         301.0                         155.0   \n",
       "\n",
       "   b7.Temp_Annual_Range  b8.Mean_Temp_of_Wettest_Quarter  \\\n",
       "0                 144.0                            250.0   \n",
       "1                 146.0                            241.0   \n",
       "\n",
       "   b9.Mean_Temp_of_Driest_Quarter  b10.Mean_Temp_of_Warmest_Quarter  \\\n",
       "0                           226.0                             258.0   \n",
       "1                           217.0                             248.0   \n",
       "\n",
       "   b11.Mean_Temp_of_Coldest_Quarter  b12.Annual_Precipitation  \\\n",
       "0                             226.0                    1462.0   \n",
       "1                             217.0                    1525.0   \n",
       "\n",
       "   b13.Precipitation_of_Wettest_Month  b14.Precipitation_of_Driest_Month  \\\n",
       "0                               249.0                                3.0   \n",
       "1                               259.0                                3.0   \n",
       "\n",
       "   b15.Precipitation_Seasonality  b16.Precipitation_of_Wettest_Quarter  \\\n",
       "0                           68.0                                 573.0   \n",
       "1                           67.0                                 603.0   \n",
       "\n",
       "   b17.Precipitation_of_Driest_Quarter  b18.Precipitation_of_Warmest_Quarter  \\\n",
       "0                                 17.0                                 549.0   \n",
       "1                                 18.0                                 575.0   \n",
       "\n",
       "   b19.Precipitation_of_Coldest_Quarter   MAT  \n",
       "0                                  17.0  24.6  \n",
       "1                                  18.0  23.7  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "climate.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Series([], dtype: int64)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check again NaN values\n",
    "s = climate.isnull().sum()\n",
    "s = s.sort_values(ascending=False)\n",
    "s = s[s > 0]\n",
    "s"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Distribution des variables clima"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 22732 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 19480 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 24360 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 26088 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 24172 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 26996 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 24816 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 23616 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 23884 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 22260 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 24352 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 25088 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 19096 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 25344 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 25644 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17140 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 23608 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 16496 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/orjuela/anaconda3/lib/python3.8/site-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms).\n",
      "  warnings.warn(msg, FutureWarning)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 17820 characters]",
      "text/plain": [
       "<Figure size 504x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i in climate.columns:\n",
    "    plt.figure(figsize=(7,6))\n",
    "    plt.title(i)\n",
    "    sns.distplot(climate[i], color=\"steelblue\")\n",
    "    plt.show()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b2.Mean_Diurnal_Range</th>\n",
       "      <th>b3.Isothermality</th>\n",
       "      <th>b4.Temp_Seasonality</th>\n",
       "      <th>b5.Max_Temp_of_Warmest_Month</th>\n",
       "      <th>b6.Min_Temp_of_Coldest_Month</th>\n",
       "      <th>b7.Temp_Annual_Range</th>\n",
       "      <th>b8.Mean_Temp_of_Wettest_Quarter</th>\n",
       "      <th>b9.Mean_Temp_of_Driest_Quarter</th>\n",
       "      <th>b10.Mean_Temp_of_Warmest_Quarter</th>\n",
       "      <th>b11.Mean_Temp_of_Coldest_Quarter</th>\n",
       "      <th>b12.Annual_Precipitation</th>\n",
       "      <th>b13.Precipitation_of_Wettest_Month</th>\n",
       "      <th>b14.Precipitation_of_Driest_Month</th>\n",
       "      <th>b15.Precipitation_Seasonality</th>\n",
       "      <th>b16.Precipitation_of_Wettest_Quarter</th>\n",
       "      <th>b17.Precipitation_of_Driest_Quarter</th>\n",
       "      <th>b18.Precipitation_of_Warmest_Quarter</th>\n",
       "      <th>b19.Precipitation_of_Coldest_Quarter</th>\n",
       "      <th>MAT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>99.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1230.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>166.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>250.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>1462.0</td>\n",
       "      <td>249.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>573.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>24.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>100.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1235.0</td>\n",
       "      <td>301.0</td>\n",
       "      <td>155.0</td>\n",
       "      <td>146.0</td>\n",
       "      <td>241.0</td>\n",
       "      <td>217.0</td>\n",
       "      <td>248.0</td>\n",
       "      <td>217.0</td>\n",
       "      <td>1525.0</td>\n",
       "      <td>259.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>603.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>575.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>23.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>93.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>1389.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>168.0</td>\n",
       "      <td>142.0</td>\n",
       "      <td>250.0</td>\n",
       "      <td>223.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>223.0</td>\n",
       "      <td>1416.0</td>\n",
       "      <td>264.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>73.0</td>\n",
       "      <td>579.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>544.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>24.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>154.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>3955.0</td>\n",
       "      <td>403.0</td>\n",
       "      <td>123.0</td>\n",
       "      <td>280.0</td>\n",
       "      <td>296.0</td>\n",
       "      <td>234.0</td>\n",
       "      <td>315.0</td>\n",
       "      <td>214.0</td>\n",
       "      <td>118.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>184.0</td>\n",
       "      <td>107.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>45.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>154.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>3913.0</td>\n",
       "      <td>403.0</td>\n",
       "      <td>124.0</td>\n",
       "      <td>279.0</td>\n",
       "      <td>294.0</td>\n",
       "      <td>234.0</td>\n",
       "      <td>314.0</td>\n",
       "      <td>214.0</td>\n",
       "      <td>109.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>172.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>159</th>\n",
       "      <td>148.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>3202.0</td>\n",
       "      <td>396.0</td>\n",
       "      <td>139.0</td>\n",
       "      <td>257.0</td>\n",
       "      <td>275.0</td>\n",
       "      <td>247.0</td>\n",
       "      <td>314.0</td>\n",
       "      <td>227.0</td>\n",
       "      <td>224.0</td>\n",
       "      <td>95.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>162.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>160</th>\n",
       "      <td>154.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>3926.0</td>\n",
       "      <td>402.0</td>\n",
       "      <td>122.0</td>\n",
       "      <td>280.0</td>\n",
       "      <td>290.0</td>\n",
       "      <td>233.0</td>\n",
       "      <td>315.0</td>\n",
       "      <td>214.0</td>\n",
       "      <td>116.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>171.0</td>\n",
       "      <td>105.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>27.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>161</th>\n",
       "      <td>129.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>1183.0</td>\n",
       "      <td>284.0</td>\n",
       "      <td>93.0</td>\n",
       "      <td>191.0</td>\n",
       "      <td>197.0</td>\n",
       "      <td>177.0</td>\n",
       "      <td>206.0</td>\n",
       "      <td>176.0</td>\n",
       "      <td>1270.0</td>\n",
       "      <td>259.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>85.0</td>\n",
       "      <td>562.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>342.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>19.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>166</th>\n",
       "      <td>99.0</td>\n",
       "      <td>73.0</td>\n",
       "      <td>1013.0</td>\n",
       "      <td>295.0</td>\n",
       "      <td>161.0</td>\n",
       "      <td>134.0</td>\n",
       "      <td>233.0</td>\n",
       "      <td>213.0</td>\n",
       "      <td>239.0</td>\n",
       "      <td>213.0</td>\n",
       "      <td>1905.0</td>\n",
       "      <td>318.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>65.0</td>\n",
       "      <td>813.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>642.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>23.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>167</th>\n",
       "      <td>102.0</td>\n",
       "      <td>77.0</td>\n",
       "      <td>636.0</td>\n",
       "      <td>309.0</td>\n",
       "      <td>177.0</td>\n",
       "      <td>132.0</td>\n",
       "      <td>251.0</td>\n",
       "      <td>241.0</td>\n",
       "      <td>257.0</td>\n",
       "      <td>241.0</td>\n",
       "      <td>1577.0</td>\n",
       "      <td>216.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>586.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>522.0</td>\n",
       "      <td>71.0</td>\n",
       "      <td>25.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>110 rows × 19 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     b2.Mean_Diurnal_Range  b3.Isothermality  b4.Temp_Seasonality  \\\n",
       "0                     99.0              68.0               1230.0   \n",
       "1                    100.0              68.0               1235.0   \n",
       "2                     93.0              65.0               1389.0   \n",
       "3                    154.0              55.0               3955.0   \n",
       "4                    154.0              55.0               3913.0   \n",
       "..                     ...               ...                  ...   \n",
       "159                  148.0              57.0               3202.0   \n",
       "160                  154.0              55.0               3926.0   \n",
       "161                  129.0              67.0               1183.0   \n",
       "166                   99.0              73.0               1013.0   \n",
       "167                  102.0              77.0                636.0   \n",
       "\n",
       "     b5.Max_Temp_of_Warmest_Month  b6.Min_Temp_of_Coldest_Month  \\\n",
       "0                           310.0                         166.0   \n",
       "1                           301.0                         155.0   \n",
       "2                           310.0                         168.0   \n",
       "3                           403.0                         123.0   \n",
       "4                           403.0                         124.0   \n",
       "..                            ...                           ...   \n",
       "159                         396.0                         139.0   \n",
       "160                         402.0                         122.0   \n",
       "161                         284.0                          93.0   \n",
       "166                         295.0                         161.0   \n",
       "167                         309.0                         177.0   \n",
       "\n",
       "     b7.Temp_Annual_Range  b8.Mean_Temp_of_Wettest_Quarter  \\\n",
       "0                   144.0                            250.0   \n",
       "1                   146.0                            241.0   \n",
       "2                   142.0                            250.0   \n",
       "3                   280.0                            296.0   \n",
       "4                   279.0                            294.0   \n",
       "..                    ...                              ...   \n",
       "159                 257.0                            275.0   \n",
       "160                 280.0                            290.0   \n",
       "161                 191.0                            197.0   \n",
       "166                 134.0                            233.0   \n",
       "167                 132.0                            251.0   \n",
       "\n",
       "     b9.Mean_Temp_of_Driest_Quarter  b10.Mean_Temp_of_Warmest_Quarter  \\\n",
       "0                             226.0                             258.0   \n",
       "1                             217.0                             248.0   \n",
       "2                             223.0                             258.0   \n",
       "3                             234.0                             315.0   \n",
       "4                             234.0                             314.0   \n",
       "..                              ...                               ...   \n",
       "159                           247.0                             314.0   \n",
       "160                           233.0                             315.0   \n",
       "161                           177.0                             206.0   \n",
       "166                           213.0                             239.0   \n",
       "167                           241.0                             257.0   \n",
       "\n",
       "     b11.Mean_Temp_of_Coldest_Quarter  b12.Annual_Precipitation  \\\n",
       "0                               226.0                    1462.0   \n",
       "1                               217.0                    1525.0   \n",
       "2                               223.0                    1416.0   \n",
       "3                               214.0                     118.0   \n",
       "4                               214.0                     109.0   \n",
       "..                                ...                       ...   \n",
       "159                             227.0                     224.0   \n",
       "160                             214.0                     116.0   \n",
       "161                             176.0                    1270.0   \n",
       "166                             213.0                    1905.0   \n",
       "167                             241.0                    1577.0   \n",
       "\n",
       "     b13.Precipitation_of_Wettest_Month  b14.Precipitation_of_Driest_Month  \\\n",
       "0                                 249.0                                3.0   \n",
       "1                                 259.0                                3.0   \n",
       "2                                 264.0                                0.0   \n",
       "3                                  62.0                                0.0   \n",
       "4                                  57.0                                0.0   \n",
       "..                                  ...                                ...   \n",
       "159                                95.0                                0.0   \n",
       "160                                53.0                                0.0   \n",
       "161                               259.0                                0.0   \n",
       "166                               318.0                                3.0   \n",
       "167                               216.0                               10.0   \n",
       "\n",
       "     b15.Precipitation_Seasonality  b16.Precipitation_of_Wettest_Quarter  \\\n",
       "0                             68.0                                 573.0   \n",
       "1                             67.0                                 603.0   \n",
       "2                             73.0                                 579.0   \n",
       "3                            184.0                                 107.0   \n",
       "4                            172.0                                 100.0   \n",
       "..                             ...                                   ...   \n",
       "159                          162.0                                 197.0   \n",
       "160                          171.0                                 105.0   \n",
       "161                           85.0                                 562.0   \n",
       "166                           65.0                                 813.0   \n",
       "167                           51.0                                 586.0   \n",
       "\n",
       "     b17.Precipitation_of_Driest_Quarter  \\\n",
       "0                                   17.0   \n",
       "1                                   18.0   \n",
       "2                                    8.0   \n",
       "3                                    0.0   \n",
       "4                                    0.0   \n",
       "..                                   ...   \n",
       "159                                  0.0   \n",
       "160                                  0.0   \n",
       "161                                  1.0   \n",
       "166                                 30.0   \n",
       "167                                 71.0   \n",
       "\n",
       "     b18.Precipitation_of_Warmest_Quarter  \\\n",
       "0                                   549.0   \n",
       "1                                   575.0   \n",
       "2                                   544.0   \n",
       "3                                    45.0   \n",
       "4                                     8.0   \n",
       "..                                    ...   \n",
       "159                                  26.0   \n",
       "160                                  11.0   \n",
       "161                                 342.0   \n",
       "166                                 642.0   \n",
       "167                                 522.0   \n",
       "\n",
       "     b19.Precipitation_of_Coldest_Quarter   MAT  \n",
       "0                                    17.0  24.6  \n",
       "1                                    18.0  23.7  \n",
       "2                                     8.0  24.5  \n",
       "3                                     0.0  27.3  \n",
       "4                                     0.0  27.3  \n",
       "..                                    ...   ...  \n",
       "159                                   0.0  27.3  \n",
       "160                                   0.0  27.2  \n",
       "161                                  11.0  19.4  \n",
       "166                                  30.0  23.0  \n",
       "167                                  71.0  25.1  \n",
       "\n",
       "[110 rows x 19 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "climate"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PCA avec toutes les variables clima"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# plus haut j'ai separé les variables clima des speces (glab et barthii)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = climate\n",
    "y = species"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b2.Mean_Diurnal_Range</th>\n",
       "      <th>b3.Isothermality</th>\n",
       "      <th>b4.Temp_Seasonality</th>\n",
       "      <th>b5.Max_Temp_of_Warmest_Month</th>\n",
       "      <th>b6.Min_Temp_of_Coldest_Month</th>\n",
       "      <th>b7.Temp_Annual_Range</th>\n",
       "      <th>b8.Mean_Temp_of_Wettest_Quarter</th>\n",
       "      <th>b9.Mean_Temp_of_Driest_Quarter</th>\n",
       "      <th>b10.Mean_Temp_of_Warmest_Quarter</th>\n",
       "      <th>b11.Mean_Temp_of_Coldest_Quarter</th>\n",
       "      <th>b12.Annual_Precipitation</th>\n",
       "      <th>b13.Precipitation_of_Wettest_Month</th>\n",
       "      <th>b14.Precipitation_of_Driest_Month</th>\n",
       "      <th>b15.Precipitation_Seasonality</th>\n",
       "      <th>b16.Precipitation_of_Wettest_Quarter</th>\n",
       "      <th>b17.Precipitation_of_Driest_Quarter</th>\n",
       "      <th>b18.Precipitation_of_Warmest_Quarter</th>\n",
       "      <th>b19.Precipitation_of_Coldest_Quarter</th>\n",
       "      <th>MAT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>99.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1230.0</td>\n",
       "      <td>310.0</td>\n",
       "      <td>166.0</td>\n",
       "      <td>144.0</td>\n",
       "      <td>250.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>258.0</td>\n",
       "      <td>226.0</td>\n",
       "      <td>1462.0</td>\n",
       "      <td>249.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>573.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>549.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>24.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>100.0</td>\n",
       "      <td>68.0</td>\n",
       "      <td>1235.0</td>\n",
       "      <td>301.0</td>\n",
       "      <td>155.0</td>\n",
       "      <td>146.0</td>\n",
       "      <td>241.0</td>\n",
       "      <td>217.0</td>\n",
       "      <td>248.0</td>\n",
       "      <td>217.0</td>\n",
       "      <td>1525.0</td>\n",
       "      <td>259.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>67.0</td>\n",
       "      <td>603.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>575.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>23.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   b2.Mean_Diurnal_Range  b3.Isothermality  b4.Temp_Seasonality  \\\n",
       "0                   99.0              68.0               1230.0   \n",
       "1                  100.0              68.0               1235.0   \n",
       "\n",
       "   b5.Max_Temp_of_Warmest_Month  b6.Min_Temp_of_Coldest_Month  \\\n",
       "0                         310.0                         166.0   \n",
       "1                         301.0                         155.0   \n",
       "\n",
       "   b7.Temp_Annual_Range  b8.Mean_Temp_of_Wettest_Quarter  \\\n",
       "0                 144.0                            250.0   \n",
       "1                 146.0                            241.0   \n",
       "\n",
       "   b9.Mean_Temp_of_Driest_Quarter  b10.Mean_Temp_of_Warmest_Quarter  \\\n",
       "0                           226.0                             258.0   \n",
       "1                           217.0                             248.0   \n",
       "\n",
       "   b11.Mean_Temp_of_Coldest_Quarter  b12.Annual_Precipitation  \\\n",
       "0                             226.0                    1462.0   \n",
       "1                             217.0                    1525.0   \n",
       "\n",
       "   b13.Precipitation_of_Wettest_Month  b14.Precipitation_of_Driest_Month  \\\n",
       "0                               249.0                                3.0   \n",
       "1                               259.0                                3.0   \n",
       "\n",
       "   b15.Precipitation_Seasonality  b16.Precipitation_of_Wettest_Quarter  \\\n",
       "0                           68.0                                 573.0   \n",
       "1                           67.0                                 603.0   \n",
       "\n",
       "   b17.Precipitation_of_Driest_Quarter  b18.Precipitation_of_Warmest_Quarter  \\\n",
       "0                                 17.0                                 549.0   \n",
       "1                                 18.0                                 575.0   \n",
       "\n",
       "   b19.Precipitation_of_Coldest_Quarter   MAT  \n",
       "0                                  17.0  24.6  \n",
       "1                                  18.0  23.7  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    O. barthii\n",
       "1    O. barthii\n",
       "Name: species, dtype: object"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Z-score the feature # optional\n",
    "modelStd = StandardScaler()\n",
    "modelStd.fit(X)\n",
    "x = modelStd.transform(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(110, 19)"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# check taille de la matrice transformé pour pca\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.15368535,  0.94439025, -1.10869418, ...,  2.2059862 ,\n",
       "        -0.37325165, -0.23942165],\n",
       "       [-1.12003237,  0.94439025, -1.10592891, ...,  2.34668601,\n",
       "        -0.36842247, -0.58443974],\n",
       "       [-1.35560323,  0.63148986, -1.02075884, ...,  2.17892854,\n",
       "        -0.41671426, -0.27775699],\n",
       "       ...,\n",
       "       [-0.14409594,  0.84009012, -1.13468764, ...,  1.08579919,\n",
       "        -0.40222672, -2.23285951],\n",
       "       [-1.15368535,  1.46589089, -1.22870656, ...,  2.70925862,\n",
       "        -0.31047232, -0.85278714],\n",
       "       [-1.05272641,  1.8830914 , -1.43720733, ...,  2.05987485,\n",
       "        -0.11247597, -0.04774493]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# x sont les zscores transformés\n",
    "x"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Fit PCA avec toutes les dimensions "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# on fit a partir de la matrice x des Zcores \n",
    "pca = PCA()\n",
    "model = pca.fit(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.explained_variance_ratio_.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 61.87296504,  79.94869219,  88.16107807,  92.47564587,\n",
       "        95.6349085 ,  97.63843376,  99.32680413,  99.600575  ,\n",
       "        99.81835888,  99.87990182,  99.93228812,  99.96004237,\n",
       "        99.97404748,  99.9858426 ,  99.99357045,  99.99648378,\n",
       "        99.99880062, 100.        , 100.        ])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.explained_variance_ratio_.cumsum()*100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x7efbae64bb50>]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14508 characters]",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(8,6))\n",
    "plt.plot(pca.explained_variance_ratio_.cumsum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5 axes expliquen 95 de la variance\n",
    "sum(pca.explained_variance_ratio_.cumsum() < .95) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33956 characters]",
      "text/plain": [
       "<Figure size 720x480 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# on plotte l'inertia dans c/axe\n",
    "\n",
    "font1 = fm.FontProperties(size=12)\n",
    "\n",
    "inertie = pd.DataFrame(pca.explained_variance_ratio_,columns=['Inertie']) \n",
    "inertie['label'] = np.round(pca.explained_variance_ratio_.cumsum()*100,2)\n",
    "inertie['Dimension'] = range(0,pca.explained_variance_ratio_.shape[0])\n",
    "\n",
    "ndim = 8\n",
    "plt.figure(figsize=(6,4),dpi=120)\n",
    "sns.set_style(\"whitegrid\")\n",
    "g = sns.barplot(x=\"Dimension\",y='Inertie', \n",
    "                data=inertie.iloc[:ndim,:], \n",
    "                palette=\"Blues_d\")\n",
    "\n",
    "for index, row in inertie.iloc[:ndim,:].iterrows():\n",
    "    g.text(row.name,row.Inertie, \n",
    "           row.label, \n",
    "           color='black', ha=\"center\", \n",
    "           rotation=30, \n",
    "           fontproperties=font1)\n",
    "    \n",
    "plt.xlabel('Dimension', fontproperties=font1)\n",
    "plt.ylabel('Inertie', fontproperties=font1)\n",
    "plt.show()\n",
    "plt.style.use('ggplot')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n_components = sum(pca.explained_variance_ratio_.cumsum() < .95 ) + 1 \n",
    "n_components"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### PCA avec n_components\n",
    "\n",
    "avec le nb des dimensions qui expliquent > 95 de la variance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PCA(n_components=5)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelPCA = PCA(n_components=n_components)\n",
    "modelPCA.fit(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0         O. barthii\n",
       "1         O. barthii\n",
       "2         O. barthii\n",
       "3         O. barthii\n",
       "4         O. barthii\n",
       "           ...      \n",
       "159    O. glaberrima\n",
       "160    O. glaberrima\n",
       "161    O. glaberrima\n",
       "166    O. glaberrima\n",
       "167    O. glaberrima\n",
       "Name: species, Length: 110, dtype: object"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Dim1</th>\n",
       "      <th>Dim2</th>\n",
       "      <th>Dim3</th>\n",
       "      <th>Dim4</th>\n",
       "      <th>Dim5</th>\n",
       "      <th>species</th>\n",
       "      <th>code_vcf</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4.031195</td>\n",
       "      <td>-0.088648</td>\n",
       "      <td>-1.570894</td>\n",
       "      <td>0.101177</td>\n",
       "      <td>1.070254</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.221275</td>\n",
       "      <td>0.588817</td>\n",
       "      <td>-1.583834</td>\n",
       "      <td>-0.024921</td>\n",
       "      <td>1.156196</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AB</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3.695843</td>\n",
       "      <td>0.086782</td>\n",
       "      <td>-2.150803</td>\n",
       "      <td>0.604053</td>\n",
       "      <td>1.140034</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-2.409618</td>\n",
       "      <td>-1.145944</td>\n",
       "      <td>-0.087064</td>\n",
       "      <td>0.125204</td>\n",
       "      <td>-0.339303</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AQ</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-2.382106</td>\n",
       "      <td>-1.080300</td>\n",
       "      <td>-0.010332</td>\n",
       "      <td>0.154423</td>\n",
       "      <td>-0.401289</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AR</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>105</th>\n",
       "      <td>-1.467733</td>\n",
       "      <td>-1.527080</td>\n",
       "      <td>-0.130744</td>\n",
       "      <td>0.712269</td>\n",
       "      <td>-0.556617</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>MN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>106</th>\n",
       "      <td>-2.374944</td>\n",
       "      <td>-1.000977</td>\n",
       "      <td>0.006877</td>\n",
       "      <td>0.172448</td>\n",
       "      <td>-0.393815</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>MP</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>107</th>\n",
       "      <td>2.982515</td>\n",
       "      <td>3.709241</td>\n",
       "      <td>-1.527550</td>\n",
       "      <td>-0.136411</td>\n",
       "      <td>0.025491</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>MR</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>108</th>\n",
       "      <td>5.396140</td>\n",
       "      <td>0.693827</td>\n",
       "      <td>-1.512382</td>\n",
       "      <td>-0.261747</td>\n",
       "      <td>1.536999</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>NF</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>109</th>\n",
       "      <td>5.620136</td>\n",
       "      <td>-0.950623</td>\n",
       "      <td>0.461885</td>\n",
       "      <td>-1.351045</td>\n",
       "      <td>0.744609</td>\n",
       "      <td>O. glaberrima</td>\n",
       "      <td>NG</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>110 rows × 7 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Dim1      Dim2      Dim3      Dim4      Dim5        species code_vcf\n",
       "0    4.031195 -0.088648 -1.570894  0.101177  1.070254     O. barthii       AA\n",
       "1    4.221275  0.588817 -1.583834 -0.024921  1.156196     O. barthii       AB\n",
       "2    3.695843  0.086782 -2.150803  0.604053  1.140034     O. barthii       AC\n",
       "3   -2.409618 -1.145944 -0.087064  0.125204 -0.339303     O. barthii       AQ\n",
       "4   -2.382106 -1.080300 -0.010332  0.154423 -0.401289     O. barthii       AR\n",
       "..        ...       ...       ...       ...       ...            ...      ...\n",
       "105 -1.467733 -1.527080 -0.130744  0.712269 -0.556617  O. glaberrima       MN\n",
       "106 -2.374944 -1.000977  0.006877  0.172448 -0.393815  O. glaberrima       MP\n",
       "107  2.982515  3.709241 -1.527550 -0.136411  0.025491  O. glaberrima       MR\n",
       "108  5.396140  0.693827 -1.512382 -0.261747  1.536999  O. glaberrima       NF\n",
       "109  5.620136 -0.950623  0.461885 -1.351045  0.744609  O. glaberrima       NG\n",
       "\n",
       "[110 rows x 7 columns]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# transform of x (matrice de zscores)\n",
    "dfPCA = pd.DataFrame(data=modelPCA.transform(x),\n",
    "                  columns=[f'Dim{str(x+1)}' for x in range(0,n_components)])\n",
    "# on lui colle la colonne sp\n",
    "dfPCA['species'] = climate_all['species'].values\n",
    "dfPCA['code_vcf'] = climate_all['code_vcf'].values\n",
    "dfPCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28412 characters]",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.title('Correlation Pearson des variables', y=1.05, size=18)\n",
    "ax = sns.heatmap(dfPCA.corr(), annot=True, cmap='viridis')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24512 characters]",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot de la correlation de pearson des variables de l'acp\n",
    "#plt.figure(figsize=(8,8))\n",
    "#sns.set(font_scale=2.5)\n",
    "plt.title('Correlation Pearson des variables', y=1.05, size=18)\n",
    "sns.heatmap(dfPCA.corr(),linewidths=0.3,vmax=1.0, fmt='.3f',\n",
    "            square=True, cmap='coolwarm', linecolor='white', annot=True)\n",
    "sns.set(font_scale=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Figure size 432x288 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 196500 characters]",
      "text/plain": [
       "<Figure size 1957.95x864 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# PCA representation\n",
    "sns.set(font_scale=2)\n",
    "font1 = fm.FontProperties(size=16)\n",
    "font2 = fm.FontProperties(size=32)\n",
    "\n",
    "plt.figure()\n",
    "\n",
    "ax = sns.lmplot(x='Dim1',y='Dim2', hue='species', data=dfPCA, \n",
    "                fit_reg=False, height = 12, aspect =2 ) \n",
    "\n",
    "plt.title('PCA clima pour glaberrima et barthii', fontproperties=font2)\n",
    "plt.xlabel('Dimmension 1', fontproperties=font1)\n",
    "plt.ylabel('Dimmension 2', fontproperties=font1)\n",
    "\n",
    "\n",
    "def label_point(x, y, val, ax):\n",
    "    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)\n",
    "    for i, point in a.iterrows():\n",
    "        ax.text(point['x']+.02, point['y']+.02, str(point['val']), \n",
    "                fontproperties=font1)\n",
    "\n",
    "label_point(dfPCA.Dim1, dfPCA.Dim2, dfPCA.species, plt.gca())"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pearson pour les variables dans les nouvelles dimensions de l'ACP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 463236 characters]",
      "text/plain": [
       "<Figure size 3744x3744 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(52,52))\n",
    "sns.set(font_scale=2.5)\n",
    "plt.title(\"L'influence des variables dans les nouvelles dimensions\", y=1.05, size=24)\n",
    "sns.heatmap(pd.DataFrame(modelPCA.components_, columns=X.columns),\n",
    "            fmt= '.2f',linewidths=0.3,vmax=1.0, \n",
    "            square=True, cmap='coolwarm', linecolor='white', annot=True);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---------------\n",
    "### biplot \n",
    "\n",
    "on va essayer de voir quelle sont les variables avec plus de poid dans c/dimension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "### TEST ACP \n",
    "#https://towardsdatascience.com/pca-clearly-explained-how-when-why-to-use-it-and-feature-importance-a-guide-in-python-7c274582c37e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-53-981da70d0ace>:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  y.mask(y == 'O. glaberrima', 0, inplace=True)\n",
      "<ipython-input-53-981da70d0ace>:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  y.mask(y == 'O. barthii', 1, inplace=True)\n"
     ]
    }
   ],
   "source": [
    "# remplacer les 'sp' par des int\n",
    "y.mask(y == 'O. glaberrima', 0, inplace=True)\n",
    "y.mask(y == 'O. barthii', 1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 44560 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Let’s plot the data before and after the PCA transform and also color code each point (sample) using the corresponding class \n",
    "plt.rcParams.update(plt.rcParamsDefault) # defaults params\n",
    "X = climate\n",
    "# Z-score the features\n",
    "scaler = StandardScaler()\n",
    "scaler.fit(X)\n",
    "X = scaler.transform(X)\n",
    "\n",
    "# The PCA model\n",
    "pca = PCA(n_components=5) # estimate only 2 PCs\n",
    "X_new = pca.fit_transform(X) # project the original data into the PCA space\n",
    "\n",
    "fig, axes = plt.subplots(1,2)\n",
    "axes[0].scatter(X[:,0], X[:,1], c=y)\n",
    "axes[0].set_xlabel('x1')\n",
    "axes[0].set_ylabel('x2')\n",
    "axes[0].set_title('Before PCA')\n",
    "\n",
    "axes[1].scatter(X_new[:,0], X_new[:,1], c=y)\n",
    "axes[1].set_xlabel('PC1')\n",
    "axes[1].set_ylabel('PC2')\n",
    "axes[1].set_title('After PCA')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'PCA(n_components=5)'"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(pca)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.61872965 0.18075727 0.08212386 0.04314568 0.03159263]\n"
     ]
    }
   ],
   "source": [
    "print(pca.explained_variance_ratio_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.18637153e+01, -3.49261686e-16,  2.57990370e-16,\n",
       "        -8.95319837e-16, -6.70183512e-17],\n",
       "       [-3.49261686e-16,  3.46589631e+00, -1.36389165e-16,\n",
       "         5.53374445e-16,  4.18303017e-16],\n",
       "       [ 2.57990370e-16, -1.36389165e-16,  1.57466849e+00,\n",
       "        -4.06009871e-16,  2.65084834e-17],\n",
       "       [-8.95319837e-16,  5.53374445e-16, -4.06009871e-16,\n",
       "         8.27288689e-01, -9.10531246e-17],\n",
       "       [-6.70183512e-17,  4.18303017e-16,  2.65084834e-17,\n",
       "        -9.10531246e-17,  6.05766871e-01]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#The maximum variance proof can be also seen by estimating the covariance matrix of the reduced space\n",
    "np.cov(X_new.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11.86371532,  3.46589631,  1.57466849,  0.82728869,  0.60576687])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.explained_variance_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.60120524e-01,  2.50932746e-01, -2.63276776e-01,\n",
       "        -2.50240538e-01,  2.23595442e-01, -2.76843057e-01,\n",
       "        -1.87831349e-01,  7.67845381e-02, -2.38631084e-01,\n",
       "         1.11633351e-01,  2.77904183e-01,  2.73872982e-01,\n",
       "         2.15735700e-01, -2.52752660e-01,  2.76243993e-01,\n",
       "         2.25318385e-01,  2.36928998e-01,  1.91940443e-01,\n",
       "        -1.32273413e-01],\n",
       "       [-1.03654047e-01, -1.22337100e-01,  1.14065268e-01,\n",
       "        -2.25432765e-01, -3.14898780e-01,  8.23331780e-03,\n",
       "        -2.91820956e-01, -3.60252231e-01, -2.80801935e-01,\n",
       "        -4.74059073e-01, -5.06031138e-02, -1.02336821e-01,\n",
       "        -4.66440888e-02, -1.81439082e-01, -8.87426417e-02,\n",
       "        -1.70335548e-02,  7.68519331e-03, -1.09527481e-01,\n",
       "        -4.73663081e-01],\n",
       "       [ 1.63357921e-01, -2.81944611e-02,  2.10737206e-01,\n",
       "         1.95492113e-01, -1.44930021e-01,  2.01431886e-01,\n",
       "        -1.89054362e-01,  2.98889152e-01,  1.07335719e-01,\n",
       "        -1.97392571e-01,  3.17592754e-02, -2.52803172e-02,\n",
       "         4.19223925e-01, -7.42377774e-02,  1.57103124e-02,\n",
       "         4.23558835e-01, -3.10070928e-01,  4.32831739e-01,\n",
       "        -8.18990953e-02],\n",
       "       [-1.48979292e-01, -3.70321024e-01, -8.74814567e-03,\n",
       "         4.27805770e-02,  3.18436949e-02,  1.23662239e-02,\n",
       "        -4.68124309e-01,  6.14448193e-01,  2.01411007e-02,\n",
       "        -4.37034491e-04,  1.00593395e-02,  7.77940471e-02,\n",
       "        -3.74947310e-01, -5.34530512e-02,  8.62679329e-02,\n",
       "        -2.79459021e-01,  2.48764634e-03,  4.07127746e-03,\n",
       "        -7.30956162e-02],\n",
       "       [-7.72983558e-03, -2.77767616e-01,  3.20383186e-01,\n",
       "         1.98698137e-01, -9.66020673e-02,  1.79435240e-01,\n",
       "         2.12409951e-01, -7.77921793e-02,  2.43515754e-01,\n",
       "        -1.54549989e-01,  3.19011265e-01,  2.25242932e-01,\n",
       "        -1.52521692e-02, -3.37483283e-01,  1.98040224e-01,\n",
       "         7.70479036e-02,  5.21105665e-01, -1.04339343e-01,\n",
       "         1.02716050e-01]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.components_\n",
    "# on dirait que pour l'axe1 toutes les variables contribuent un peu (7,8,10,18 et 19 un peu moins), \n",
    "# pour l'axe2 5,7,8,10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PC1</th>\n",
       "      <th>PC2</th>\n",
       "      <th>PC3</th>\n",
       "      <th>PC4</th>\n",
       "      <th>PC5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.260121</td>\n",
       "      <td>-0.103654</td>\n",
       "      <td>0.163358</td>\n",
       "      <td>-0.148979</td>\n",
       "      <td>-0.007730</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.250933</td>\n",
       "      <td>-0.122337</td>\n",
       "      <td>-0.028194</td>\n",
       "      <td>-0.370321</td>\n",
       "      <td>-0.277768</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.263277</td>\n",
       "      <td>0.114065</td>\n",
       "      <td>0.210737</td>\n",
       "      <td>-0.008748</td>\n",
       "      <td>0.320383</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.250241</td>\n",
       "      <td>-0.225433</td>\n",
       "      <td>0.195492</td>\n",
       "      <td>0.042781</td>\n",
       "      <td>0.198698</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.223595</td>\n",
       "      <td>-0.314899</td>\n",
       "      <td>-0.144930</td>\n",
       "      <td>0.031844</td>\n",
       "      <td>-0.096602</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-0.276843</td>\n",
       "      <td>0.008233</td>\n",
       "      <td>0.201432</td>\n",
       "      <td>0.012366</td>\n",
       "      <td>0.179435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.187831</td>\n",
       "      <td>-0.291821</td>\n",
       "      <td>-0.189054</td>\n",
       "      <td>-0.468124</td>\n",
       "      <td>0.212410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.076785</td>\n",
       "      <td>-0.360252</td>\n",
       "      <td>0.298889</td>\n",
       "      <td>0.614448</td>\n",
       "      <td>-0.077792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-0.238631</td>\n",
       "      <td>-0.280802</td>\n",
       "      <td>0.107336</td>\n",
       "      <td>0.020141</td>\n",
       "      <td>0.243516</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.111633</td>\n",
       "      <td>-0.474059</td>\n",
       "      <td>-0.197393</td>\n",
       "      <td>-0.000437</td>\n",
       "      <td>-0.154550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.277904</td>\n",
       "      <td>-0.050603</td>\n",
       "      <td>0.031759</td>\n",
       "      <td>0.010059</td>\n",
       "      <td>0.319011</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.273873</td>\n",
       "      <td>-0.102337</td>\n",
       "      <td>-0.025280</td>\n",
       "      <td>0.077794</td>\n",
       "      <td>0.225243</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0.215736</td>\n",
       "      <td>-0.046644</td>\n",
       "      <td>0.419224</td>\n",
       "      <td>-0.374947</td>\n",
       "      <td>-0.015252</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>-0.252753</td>\n",
       "      <td>-0.181439</td>\n",
       "      <td>-0.074238</td>\n",
       "      <td>-0.053453</td>\n",
       "      <td>-0.337483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0.276244</td>\n",
       "      <td>-0.088743</td>\n",
       "      <td>0.015710</td>\n",
       "      <td>0.086268</td>\n",
       "      <td>0.198040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.225318</td>\n",
       "      <td>-0.017034</td>\n",
       "      <td>0.423559</td>\n",
       "      <td>-0.279459</td>\n",
       "      <td>0.077048</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.236929</td>\n",
       "      <td>0.007685</td>\n",
       "      <td>-0.310071</td>\n",
       "      <td>0.002488</td>\n",
       "      <td>0.521106</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0.191940</td>\n",
       "      <td>-0.109527</td>\n",
       "      <td>0.432832</td>\n",
       "      <td>0.004071</td>\n",
       "      <td>-0.104339</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>-0.132273</td>\n",
       "      <td>-0.473663</td>\n",
       "      <td>-0.081899</td>\n",
       "      <td>-0.073096</td>\n",
       "      <td>0.102716</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         PC1       PC2       PC3       PC4       PC5\n",
       "0  -0.260121 -0.103654  0.163358 -0.148979 -0.007730\n",
       "1   0.250933 -0.122337 -0.028194 -0.370321 -0.277768\n",
       "2  -0.263277  0.114065  0.210737 -0.008748  0.320383\n",
       "3  -0.250241 -0.225433  0.195492  0.042781  0.198698\n",
       "4   0.223595 -0.314899 -0.144930  0.031844 -0.096602\n",
       "5  -0.276843  0.008233  0.201432  0.012366  0.179435\n",
       "6  -0.187831 -0.291821 -0.189054 -0.468124  0.212410\n",
       "7   0.076785 -0.360252  0.298889  0.614448 -0.077792\n",
       "8  -0.238631 -0.280802  0.107336  0.020141  0.243516\n",
       "9   0.111633 -0.474059 -0.197393 -0.000437 -0.154550\n",
       "10  0.277904 -0.050603  0.031759  0.010059  0.319011\n",
       "11  0.273873 -0.102337 -0.025280  0.077794  0.225243\n",
       "12  0.215736 -0.046644  0.419224 -0.374947 -0.015252\n",
       "13 -0.252753 -0.181439 -0.074238 -0.053453 -0.337483\n",
       "14  0.276244 -0.088743  0.015710  0.086268  0.198040\n",
       "15  0.225318 -0.017034  0.423559 -0.279459  0.077048\n",
       "16  0.236929  0.007685 -0.310071  0.002488  0.521106\n",
       "17  0.191940 -0.109527  0.432832  0.004071 -0.104339\n",
       "18 -0.132273 -0.473663 -0.081899 -0.073096  0.102716"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loadings = pca.components_\n",
    "num_pc = pca.n_features_\n",
    "pc_list = [\"PC\"+str(i) for i in list(range(1, num_pc+1))]\n",
    "loadings_df = pd.DataFrame.from_dict(dict(zip(pc_list, loadings)))\n",
    "#loadings_df['variable'] = df.columns.values\n",
    "#loadings_df = loadings_df.set_index('variable')\n",
    "loadings_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 106272 characters]",
      "text/plain": [
       "<Figure size 640x480 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# get correlation matrix plot for loadings\n",
    "plt.rcParams.update(plt.rcParamsDefault) # defaults params\n",
    "ax = sns.heatmap(loadings_df, annot=True, cmap='viridis')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "findfont: Font family ['Arial'] not found. Falling back to DejaVu Sans.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 138360 characters]",
      "text/plain": [
       "<Figure size 1200x900 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# get PC scores\n",
    "df = climate\n",
    "df_st =  StandardScaler().fit_transform(df)  \n",
    "pd.DataFrame(df_st, columns=df.columns).head(2)\n",
    "\n",
    "pca_out = PCA().fit(df_st)\n",
    "\n",
    "# get 2D biplot\n",
    "from bioinfokit.visuz import cluster\n",
    "cluster.biplot(cscore=X_new, \n",
    "               loadings=loadings, \n",
    "               labels=df.columns.values, \n",
    "               var1=round(pca.explained_variance_ratio_[0]*100, 2),\n",
    "               var2=round(pca.explained_variance_ratio_[1]*100, 2),\n",
    "               #colordot='blue',\n",
    "               arrowcolor='red',\n",
    "               dim=(12,9),\n",
    "               r=600,\n",
    "               #theme='white',\n",
    "               show=True)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Associations between genotype (kmers) et le phénotype (clima)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Lfmm Ridge"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The lfmm R package contains two main functions for estimating the parameters of LFMMs : lfmm and lasso\n",
    "\n",
    "* Latent factor mixed models are statistical regression models to test associations between a multidimensional set of response variables and a set of variables of interest\n",
    "\n",
    "https://bcm-uga.github.io/lfmm/articles/lfmm"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### \"code_vcf\" and \"samples name\" in kmers matrice are not similar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>OB_AAOSW</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>OB_ABOSW</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0  1\n",
       "0  OB_AAOSW  1\n",
       "1  OB_ABOSW  1"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "samples_file = \"../samples_OGOB.txt\"\n",
    "samples_df = pd.read_csv(samples_file, delimiter='\\t', header=None)\n",
    "samples_df2 = pd.read_csv(samples_file, delimiter='\\t', header=None)\n",
    "samples_df.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sp</th>\n",
       "      <th>code_vcf</th>\n",
       "      <th>sample_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>OB</td>\n",
       "      <td>AA</td>\n",
       "      <td>OB_AAOSW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>OB</td>\n",
       "      <td>AB</td>\n",
       "      <td>OB_ABOSW</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   sp code_vcf sample_name\n",
       "0  OB       AA    OB_AAOSW\n",
       "1  OB       AB    OB_ABOSW"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "samples_df = samples_df[0].str.split('OSW', expand=True)\n",
    "samples_df = samples_df[0].str.split('_', expand=True)\n",
    "samples_df.columns = [\"sp\",\"code_vcf\" ]\n",
    "samples_df['sample_name'] = samples_df2[0] \n",
    "samples_df.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Dim1</th>\n",
       "      <th>Dim2</th>\n",
       "      <th>Dim3</th>\n",
       "      <th>Dim4</th>\n",
       "      <th>Dim5</th>\n",
       "      <th>species</th>\n",
       "      <th>code_vcf</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4.031195</td>\n",
       "      <td>-0.088648</td>\n",
       "      <td>-1.570894</td>\n",
       "      <td>0.101177</td>\n",
       "      <td>1.070254</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.221275</td>\n",
       "      <td>0.588817</td>\n",
       "      <td>-1.583834</td>\n",
       "      <td>-0.024921</td>\n",
       "      <td>1.156196</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AB</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Dim1      Dim2      Dim3      Dim4      Dim5     species code_vcf\n",
       "0  4.031195 -0.088648 -1.570894  0.101177  1.070254  O. barthii       AA\n",
       "1  4.221275  0.588817 -1.583834 -0.024921  1.156196  O. barthii       AB"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfPCA.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(110, 7) (240, 3)\n"
     ]
    }
   ],
   "source": [
    "print(dfPCA.shape, samples_df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfPCA = dfPCA.join(samples_df.set_index('code_vcf'), on='code_vcf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Dim1</th>\n",
       "      <th>Dim2</th>\n",
       "      <th>Dim3</th>\n",
       "      <th>Dim4</th>\n",
       "      <th>Dim5</th>\n",
       "      <th>species</th>\n",
       "      <th>code_vcf</th>\n",
       "      <th>sp</th>\n",
       "      <th>sample_name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4.031195</td>\n",
       "      <td>-0.088648</td>\n",
       "      <td>-1.570894</td>\n",
       "      <td>0.101177</td>\n",
       "      <td>1.070254</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AA</td>\n",
       "      <td>OB</td>\n",
       "      <td>OB_AAOSW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.221275</td>\n",
       "      <td>0.588817</td>\n",
       "      <td>-1.583834</td>\n",
       "      <td>-0.024921</td>\n",
       "      <td>1.156196</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AB</td>\n",
       "      <td>OB</td>\n",
       "      <td>OB_ABOSW</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3.695843</td>\n",
       "      <td>0.086782</td>\n",
       "      <td>-2.150803</td>\n",
       "      <td>0.604053</td>\n",
       "      <td>1.140034</td>\n",
       "      <td>O. barthii</td>\n",
       "      <td>AC</td>\n",
       "      <td>OB</td>\n",
       "      <td>OB_ACOSW</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Dim1      Dim2      Dim3      Dim4      Dim5     species code_vcf  sp  \\\n",
       "0  4.031195 -0.088648 -1.570894  0.101177  1.070254  O. barthii       AA  OB   \n",
       "1  4.221275  0.588817 -1.583834 -0.024921  1.156196  O. barthii       AB  OB   \n",
       "2  3.695843  0.086782 -2.150803  0.604053  1.140034  O. barthii       AC  OB   \n",
       "\n",
       "  sample_name  \n",
       "0    OB_AAOSW  \n",
       "1    OB_ABOSW  \n",
       "2    OB_ACOSW  "
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfPCA.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(110, 9)"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfPCA.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    sample_name      Dim1\n",
      "0      OB_AAOSW  4.031195\n",
      "1      OB_ABOSW  4.221275\n",
      "2      OB_ACOSW  3.695843\n",
      "3      OB_AQOSW -2.409618\n",
      "4      OB_AROSW -2.382106\n",
      "5      OB_ASOSW -1.873862\n",
      "6      OB_ATOSW -1.803358\n",
      "7      OB_AVOSW -2.155763\n",
      "8      OB_BAOSW -3.863397\n",
      "9      OB_BBOSW -2.756852\n",
      "10     OB_BCOSW -2.762945\n",
      "11     OB_BDOSW -2.983117\n",
      "12     OB_BEOSW -2.903098\n",
      "13     OB_BFOSW -3.515698\n",
      "14     OB_BGOSW -3.515698\n",
      "15     OB_BHOSW -3.515698\n",
      "16     OB_BIOSW -3.911752\n",
      "17     OB_BKOSW -3.904434\n",
      "18     OB_BLOSW -3.904434\n",
      "19     OB_BNOSW  0.664745\n",
      "20     OB_BPOSW -3.863397\n",
      "21     OB_BQOSW -2.084895\n",
      "22     OB_BROSW  4.496354\n",
      "23     OB_BSOSW -3.144731\n",
      "24     OB_BTOSW -3.515698\n",
      "25     OB_BVOSW -3.515698\n",
      "26     OB_CAOSW -2.333033\n",
      "27     OB_CBOSW -3.978768\n",
      "28     OB_CDOSW -2.382106\n",
      "29     OB_CEOSW -2.211159\n",
      "30     OB_CFOSW -3.842417\n",
      "31     OB_CGOSW -2.382106\n",
      "32     OB_CHOSW -2.211159\n",
      "33     OB_CIOSW -3.863397\n",
      "34     OB_CKOSW -3.144731\n",
      "35     OB_CLOSW -3.073312\n",
      "36     OB_CMOSW -3.454439\n",
      "37     OB_CNOSW -3.515698\n",
      "38     OB_CPOSW -3.681901\n",
      "39     OB_CQOSW -3.809509\n",
      "40     OB_CROSW -4.236285\n",
      "41     OB_CSOSW -3.895947\n",
      "42     OB_CTOSW -2.000915\n",
      "43     OB_CVOSW -1.966460\n",
      "44     OB_DAOSW -1.720929\n",
      "45     OB_DBOSW -1.426546\n",
      "46     OB_DCOSW  2.872753\n",
      "47     OB_DDOSW -2.150860\n",
      "48     OB_DEOSW -2.432333\n",
      "49     OB_DFOSW -2.750533\n",
      "50     OB_DGOSW -2.038983\n",
      "51     OB_DHOSW -3.838394\n",
      "52     OB_DKOSW -2.098054\n",
      "53     OB_DLOSW -2.098054\n",
      "54     OB_DMOSW -2.244765\n",
      "55     OB_DNOSW -3.643424\n",
      "56     OB_NKOSW  4.221275\n",
      "57     OG_DTOSW  5.567462\n",
      "58     OG_DVOSW  5.567462\n",
      "59     OG_EAOSW  2.935872\n",
      "60     OG_EIOSW  1.873265\n",
      "61     OG_EKOSW  0.057472\n",
      "62     OG_EMOSW  5.567462\n",
      "63     OG_ENOSW -1.165650\n",
      "64     OG_FCOSW -1.218612\n",
      "65     OG_FFOSW  5.567462\n",
      "66     OG_FIOSW  0.915138\n",
      "67     OG_FLOSW  1.024822\n",
      "68     OG_FPOSW  2.463832\n",
      "69     OG_FQOSW  5.529100\n",
      "70     OG_GBOSW -1.201831\n",
      "71     OG_GCOSW  3.363563\n",
      "72     OG_GKOSW  0.987478\n",
      "73     OG_GMOSW  6.503314\n",
      "74     OG_GPOSW  2.070406\n",
      "75     OG_GQOSW  6.907201\n",
      "76     OG_GROSW  1.307547\n",
      "77     OG_GVOSW  6.503314\n",
      "78     OG_HBOSW -1.257248\n",
      "79     OG_HCOSW -1.218612\n",
      "80     OG_HEOSW  2.037220\n",
      "81     OG_HFOSW  1.090084\n",
      "82     OG_HKOSW  2.961719\n",
      "83     OG_HMOSW  1.100354\n",
      "84     OG_IAOSW  5.567462\n",
      "85     OG_IDOSW  2.364799\n",
      "86     OG_IHOSW  2.961719\n",
      "87     OG_IKOSW -0.354657\n",
      "88     OG_IMOSW -1.347622\n",
      "89     OG_IQOSW -1.347622\n",
      "90     OG_KCOSW -0.015635\n",
      "91     OG_KEOSW  5.465020\n",
      "92     OG_KFOSW  2.946231\n",
      "93     OG_KHOSW  5.567462\n",
      "94     OG_KIOSW  4.167320\n",
      "95     OG_KMOSW -1.211549\n",
      "96     OG_KTOSW  5.349530\n",
      "97     OG_LAOSW  0.850433\n",
      "98     OG_LBOSW -2.880044\n",
      "99     OG_LROSW -2.447048\n",
      "100    OG_MAOSW  5.567462\n",
      "101    OG_MDOSW  6.455070\n",
      "102    OG_MEOSW  6.503314\n",
      "103    OG_MGOSW -1.423967\n",
      "104    OG_MIOSW  3.775636\n",
      "105    OG_MNOSW -1.467733\n",
      "106    OG_MPOSW -2.374944\n",
      "107    OG_MROSW  2.982515\n",
      "108    OG_NFOSW  5.396140\n",
      "109    OG_NGOSW  5.620136\n"
     ]
    }
   ],
   "source": [
    "#df = dfPCA[['sample_name','Dim1', 'Dim2', 'Dim3', 'Dim4', 'Dim5']]\n",
    "df = dfPCA[['sample_name','Dim1']]\n",
    "print(df.to_string())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The rpy2.ipython extension is already loaded. To reload it, use:\n",
      "  %reload_ext rpy2.ipython\n"
     ]
    }
   ],
   "source": [
    "%load_ext rpy2.ipython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]   170 26943\n",
      "[1] 170   1\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 10576 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%R\n",
    "\n",
    "library(\"lfmm\")\n",
    "genotype <- example.data$genotype\n",
    "phenotype <- example.data$phenotype #scaled phenotype\n",
    "print (dim(genotype))\n",
    "print (dim(phenotype))\n",
    "\n",
    "#print(head(genotype, n=5))\n",
    "#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]\n",
    "#[1,]    0    1    1    1    1    1    1    0    1     1     1     0     1     0\n",
    "#[2,]    0    1    1    1    1    1    1    0    1     1     1     1     0     0\n",
    "#[3,]    0    1    0    1    1    1    1    1    1     1     1     0     1     0\n",
    "\n",
    "#print(head(phenotype, n=5))\n",
    "#            [,1]\n",
    "#[1,]  0.48749391\n",
    "#[2,] -0.06499919\n",
    "#[3,]  0.48749391\n",
    "#[4,] -0.06499919\n",
    "#[5,] -0.61749228\n",
    "\n",
    "mod.lfmm <- lfmm_ridge(Y = genotype, X = phenotype, K = 2)\n",
    "pv <- lfmm_test(Y = genotype, X = phenotype, lfmm = mod.lfmm, calibrate = \"gif\")\n",
    "\n",
    "pvalues <- pv$calibrated.pvalue \n",
    "qqplot(rexp(length(pvalues), rate = log(10)),\n",
    "       -log10(pvalues), xlab = \"Expected quantile\",\n",
    "       pch = 19, cex = .4)\n",
    "abline(0,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "R[write to console]: Extracting number of samples and rownames from output_file.1.fam...\n",
      "\n",
      "R[write to console]: Extracting number of variants and colnames from output_file.1.bim...\n",
      "\n",
      "R[write to console]: Read 610 items\n",
      "\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 11960 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 14460 characters]"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%R -i df\n",
    "\n",
    "bed <- \"output_file.1.bed\"\n",
    "bim <- \"output_file.1.bim\"\n",
    "fam <- \"output_file.1.fam\"\n",
    "samples_file <- \"../samples_OGOB.txt\"\n",
    "kmer_list <- \"output_file.1/1short.txt\"\n",
    "\n",
    "# loading R packages\n",
    "library(\"BEDMatrix\")\n",
    "library(\"pcadapt\")\n",
    "library(\"lfmm\")\n",
    "library(\"rlist\")\n",
    "\n",
    "set.seed(802)\n",
    "\n",
    "# reading samples file from FAM file\n",
    "file_to_pop = read.delim(fam, sep = ' ', header = FALSE)\n",
    "\n",
    "# get nb of samples from samples.txt\n",
    "nb_samples = length(file_to_pop[[1]])\n",
    "\n",
    "############# phenotype ##############\"\n",
    "# le phenotype est la matrice zscores pour les 109 ind qui avaient des donnes clima dispo\n",
    "phenotype = df\n",
    "\n",
    "#print(phenotype[,1])\n",
    "\n",
    "############# genotype ##############\n",
    "# les genotypes correspond à la table de presence/absence des kmers\n",
    "# create a BEDMatrix object the example .bed file\n",
    "bed <- BEDMatrix(bed, simple_names = T)\n",
    "# extraction of random kmers found in segment file\n",
    "kl <- scan(kmer_list)\n",
    "\n",
    "# reduction de la matrix bedmatrix\n",
    "##genotype <- bed[which(rownames(bed) %in% rownames(phenotype)), sort(kl)]\n",
    "genotype = bed[1:nb_samples, sort(kl)]\n",
    "genotype_reduit = genotype[which(rownames(genotype) %in% phenotype[,1]),]\n",
    "\n",
    "#str(genotype_reduit)\n",
    "############# LFMM ##############\n",
    "#The ridge_lfmm function returns an object that contains the latent variable score matrix U, \n",
    "#the latent variable loading matrix U, and B the effect sizes for all SNPs. \n",
    "#The result can be used to perform an association study:\n",
    "\n",
    "mod.lfmm <- lfmm_ridge(Y = genotype_reduit, X = phenotype[,2], K = 2)\n",
    "#mod.lfmm\n",
    "\n",
    "pv <- lfmm_test(Y = genotype_reduit, X = phenotype[,2], lfmm = mod.lfmm, calibrate = \"gif\")\n",
    "\n",
    "pvalues <- pv$calibrated.pvalue \n",
    "\n",
    "# Screen plot\n",
    "#pdf(file=\"lfmm_rplot.pdf\")\n",
    "qqplot(rexp(length(pvalues), rate = log(10)),\n",
    "       -log10(pvalues), xlab = \"Expected quantile\",\n",
    "       pch = 19, cex = .4)\n",
    "abline(0,1)\n",
    "\n",
    "## Manhattan plot with causal loci shown\n",
    "plot(-log10(pvalues), pch = 19,\n",
    "     cex = .2, col = \"grey\", xlab = \"KMER\")\n",
    "\n",
    "points(example.data$causal.set,\n",
    "      -log10(pvalues)[example.data$causal.set],\n",
    "       type = \"h\", col = \"blue\")\n",
    "#dev.off()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "pv = %R pv\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pvalues    0.001264\n",
      "dtype: float64 pvalues    0.999307\n",
      "dtype: float64 pvalues    0.49955\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "pvalues = %R pvalues\n",
    "pvalues\n",
    "df = pd.DataFrame(pvalues, columns = ['pvalues'])\n",
    "print(df.min(),df.max(),df.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Importing the NumPy module\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# Calculating variance using var()\n",
    "variance= np.var(pvalues)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pvalues</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.710430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.370562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-4.780717</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.932885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.516312</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>605</th>\n",
       "      <td>5.925368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>606</th>\n",
       "      <td>-4.986213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>607</th>\n",
       "      <td>-6.031309</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>608</th>\n",
       "      <td>-0.508185</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>609</th>\n",
       "      <td>-2.090845</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>610 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      pvalues\n",
       "0   -0.710430\n",
       "1    2.370562\n",
       "2   -4.780717\n",
       "3    1.932885\n",
       "4    5.516312\n",
       "..        ...\n",
       "605  5.925368\n",
       "606 -4.986213\n",
       "607 -6.031309\n",
       "608 -0.508185\n",
       "609 -2.090845\n",
       "\n",
       "[610 rows x 1 columns]"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_centered = df.apply(lambda x: (x - x.mean())/x.var())\n",
    "df_centered"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pvalues    6.263114e-15\n",
       "dtype: float64"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#display mean of each column in the DataFrame\n",
    "df_centered.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "## run qqplot en python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33756 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import statsmodels.api as sm\n",
    "import pylab as py\n",
    "\n",
    "# view normalized data\n",
    "display()\n",
    "#sm.qqplot(-np.log10(df_centered['pvalues']), line ='45')\n",
    "#sm.qqplot(df_centered['pvalues'], line ='45')\n",
    "sm.qqplot(df_centered['pvalues'],fit=True,line=\"45\")\n",
    "\n",
    "py.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "manhattanplot(df_centered['pvalues'], xticklabel_kws={\"rotation\": \"horizontal\"})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
