{"cells":[{"metadata":{"_uuid":"5b3898690fa900ce6b5f2ec265d4ad7c6f4139ce"},"cell_type":"markdown","source":"# PetFinder.my Adoption Prediction\n\nTarget: Predict the speed at which a pet is adopted, based on the pet’s listing on PetFinder\n\nSource: https://www.kaggle.com/c/petfinder-adoption-prediction"},{"metadata":{"trusted":true,"_uuid":"42eb21af6ff46d261006865c3f5515c1fbc3fd9c"},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nfrom wordcloud import WordCloud\n\nimport folium\n\nimport os\nimport cv2\n\n%matplotlib inline\npd.set_option('max_columns', 30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8ce2eda187b4d6937204a7aac2230b24e171ec37"},"cell_type":"code","source":"# DIY\n\ndef missing_values(df):\n    for column in df.columns:\n        null_rows = df[column].isnull()\n        if null_rows.any() == True:\n            print('%s: %d nulls' % (column, null_rows.sum()))\n            \ndef cicle(df):    \n    plt.figure(figsize=(10,7))\n    names= 'Dog', 'Cat'\n    size=df['Type'].value_counts()\n    my_circle=plt.Circle((0,0), 0.7, color='white')\n    plt.pie(size, labels=names, colors=['skyblue','red'])\n    p=plt.gcf()\n    p.gca().add_artist(my_circle)\n    plt.title('Type of pets distribution', fontsize=15)\n    plt.show()\n    \ndef buzz_name(txt):\n    wordcloud = WordCloud(width=480, height=480, max_font_size=50, min_font_size=10).generate(dog_txt)\n    plt.figure()\n    plt.imshow(wordcloud, interpolation=\"bilinear\")\n    plt.axis(\"off\")\n    plt.margins(x=0, y=0)\n    plt.show()\n    \ndef show_rand_img():    \n    plt.rc('axes', grid = True)\n    _, ax = plt.subplots(1, 3, figsize=(20, 20))\n    images_train = os.listdir(\"../input/train_images/\")\n    random_img = np.random.randint(0, len(images_train) - 3)\n\n    for i , file in enumerate(images_train[random_img:random_img + 3]):\n        img = cv2.imread('../input/train_images/{}'.format(file))\n        ax[i].imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4cbbdbe78fd9e0b892880a6ded4e72f7a5313e8a"},"cell_type":"code","source":"df_train = pd.read_csv('../input/train/train.csv')\ndf_breed = pd.read_csv('../input/breed_labels.csv')\ndf_color = pd.read_csv('../input/color_labels.csv')\ndf_state = pd.read_csv('../input/state_labels.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8ab31ee21eee5d2a4436d5931d73816584f946a"},"cell_type":"code","source":"df_train.sample(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b0d83556c8114399474ce2b0ec141dd6ea23b791"},"cell_type":"code","source":"df_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3e5dcdaef2cb5057dde6e7b659d70c70d8a4c654"},"cell_type":"code","source":"df_breed.sample(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"662ad72546231bfcfea56cb1f4f04345043278af"},"cell_type":"code","source":"df_color","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d2001cac367aa163e5cec65fc0280082a5700a40"},"cell_type":"code","source":"df_state","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3197053077503607f2e529fbd2d2d3de8bbe51ab"},"cell_type":"code","source":"df_train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bc4d8617afa5a12a602ab8a8926fdcd2a6b8623a"},"cell_type":"code","source":"df_train.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"87bb8cf9bbcd03433b9f01243bddc35d0fd97182"},"cell_type":"code","source":"df_train.isnull().any().any()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5e836fd5cda5f55615e71d1d3a08d1cc25a6d7d0"},"cell_type":"code","source":"missing_values(df_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4fb355884cc44205c9606bf25c4fdfec337dd7c0"},"cell_type":"code","source":"plt.rcParams['figure.figsize']=(18,10)\nsns.heatmap(df_train.corr(), annot=True, linewidths=.5, fmt = \".2f\", cmap=\"YlGnBu\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad47ef1c745d13e6ea87734d2a08dead0c4f1791"},"cell_type":"code","source":"# target variable\nax = sns.countplot(x = 'AdoptionSpeed', data = df_train, palette = 'hls');\nax.set_title(label='Count of adoption speed', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"6f27ed8ccccf4830db049a17a004b8cfcd1e2f08"},"cell_type":"markdown","source":"0 - Pet was adopted on the same day as it was listed.  \n1 - Pet was adopted between 1 and 7 days (1st week) after being listed.  \n2 - Pet was adopted between 8 and 30 days (1st month) after being listed.  \n3 - Pet was adopted between 31 and 90 days (2nd & 3rd month) after being listed.  \n4 - No adoption after 100 days of being listed. (There are no pets in this dataset that waited between 90 and 100 days). "},{"metadata":{"trusted":true,"_uuid":"c178e1f812737b5e4629a7070947f11330e80e70"},"cell_type":"code","source":"ax = sns.kdeplot(df_train['Age'], shade=True);\nax.set_title(label='Count of pets age', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e7c6b94178f98a60fcb0f0ea44d74d8a82b0185"},"cell_type":"code","source":"ax = sns.countplot(x = 'Gender', data = df_train, palette = 'hls');\nax.set_title(label='Count of adoption speed per gender', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3be855c2ba1a5401380304c15c90dcc43eda2aaf"},"cell_type":"code","source":"ax = sns.countplot(x=\"Color1\", data=df_train, hue=\"AdoptionSpeed\")\nax.set_title(label='Count of color and adoption speed', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fbc5236abfb4213bd4cd3905d8d0ba9c1d71b093"},"cell_type":"code","source":"ax = sns.countplot(x=\"FurLength\", data=df_train, hue=\"AdoptionSpeed\")\nax.set_title(label='Count of fur lenght and adoption speed', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78958ce7e8c5a84f42d1bba35b5493d9b23e91a0"},"cell_type":"code","source":"ax = sns.countplot(x=\"MaturitySize\", data=df_train, hue=\"AdoptionSpeed\")\nax.set_title(label='Count of maturity size and adoption speed', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"edcc244ffcb0022af39e9f7d2a9ee4d174a98145"},"cell_type":"code","source":"ax = sns.countplot(x=\"Health\", data=df_train, hue=\"AdoptionSpeed\")\nax.set_title(label='Count of health and adoption speed', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d6e94073da8ce1a55f3ba944b24311124af5bf69"},"cell_type":"code","source":"ax = sns.countplot(x=\"Sterilized\", data=df_train, hue=\"AdoptionSpeed\")\nax.set_title(label='Count of sterilized and adoption speed', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f1f444f2e20129f68ad8b089f24c8e552b3529c4"},"cell_type":"code","source":"# we have two type of animals - dog(1) and cat(2)\ndf_train['Type'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"790d12c4c8ff6676ed6d591c76161eeb39cb9861"},"cell_type":"code","source":"cicle(df_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e0ba430aa75bb547f2036addf4ec6c377548a6b5"},"cell_type":"code","source":"ax = sns.countplot(x='Type',hue='Gender',data=df_train);\nax.set_title(label='Sex of the animal', fontsize=15);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"251cea1610301bc8590e53953cc492317e4661bb"},"cell_type":"code","source":"ax = sns.violinplot(x='MaturitySize', y='Age',\n                     hue='AdoptionSpeed',\n                     data=df_train)\nax.set_title(label='Relation between maturity size and age with respected adoption speed', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b20e4e487671e8aa5f12041229ec961dffe41572"},"cell_type":"code","source":"plt.figure(figsize=(10,7))\nax = sns.violinplot(x='Gender', y='Age',\n                     hue='AdoptionSpeed',\n                     data=df_train)\nax.set_title(label='Relation between gender and age with respected adoption speed', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"52cb74b7626f61591aade3a5f24971aa1a95a9c0"},"cell_type":"code","source":"plt.figure(figsize=(10,7))\nax = sns.violinplot(x='FurLength', y='Age',\n                     hue='AdoptionSpeed',\n                     data=df_train)\nax.set_title(label='Relation between fur length and age with respected adoption speed', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d60f978d53527efcdf96dd73ab0f9835fe61ccf8"},"cell_type":"code","source":"plt.title('Adoption time:')\nax = sns.countplot(x='Type',hue='AdoptionSpeed',data=df_train);\nax.set_title(label='Relation between adoption time and type', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4072476c1621d033e9c49aed7be28bddcd426a12"},"cell_type":"code","source":"top10_names = df_train['Name'].value_counts().head(10)\ntop10_names.plot(kind='bar', title = 'Top 10 pet names');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1aed52b9fe071021f2a795d2fcdc50bfdf24b42b"},"cell_type":"code","source":"sns.boxenplot(y='Age',x='AdoptionSpeed',hue='Type',data=df_train);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2db8fe390809a0bdc48ba4a6a59746d1e9d22f39"},"cell_type":"code","source":"df_train[df_train['Age'] == df_train['Age'].max()]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5513d14a80ce56398dca08aaf09509488fdc23b0"},"cell_type":"code","source":"ax = sns.violinplot(y='Fee',x='AdoptionSpeed',hue='Type',data=df_train);\nax.set_title(label='The most expensive fee', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"271049805ac862dada6024a3531cab55684f8c8c"},"cell_type":"code","source":"ax = sns.pointplot(x = 'Sterilized', y = 'AdoptionSpeed', hue = 'Health', data = df_train);\nax.set_title(label='Adoption speed vs Health and Sterilized', fontsize=20);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f6932f00a84470fd10f8093f6a55aae892d7582c"},"cell_type":"code","source":"df_train['AdoptionSpeed'].value_counts().sort_index().plot('barh');\nplt.title('Adoption speed classes counts');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b9908c2e9e3a03d1410949790a1d0281688cf3ba"},"cell_type":"code","source":"plt.rcParams['figure.figsize']=(15,8)\ndog_txt = ' '.join(df_train.loc[df_train['Type'] == 1, 'Name'].fillna('').values)\nbuzz_name(dog_txt)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"575e8f606f35f7d943939432a737eab5b80518c2"},"cell_type":"code","source":"cat_txt = ' '.join(df_train.loc[df_train['Type'] == 2, 'Name'].fillna('').values)\nbuzz_name(cat_txt)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36f194f932463cc87a54acf86c0a9b55acd56429"},"cell_type":"code","source":"show_rand_img()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ddb0fcc94e65388f7e265a1cf6d2747aeee15a9f"},"cell_type":"code","source":"data = pd.DataFrame({\n'lat':[103.733333, 100.362778, 102, 101.692222, 115.219033, 102.251111, 102.25, 102.5, 101, 100.25, 100.3292, 117, 113.781111, 101.5, 103],\n'lon':[1.483333, 6.128333, 5.25, 3.153889, 5.315894, 2.188889, 2.75, 3.75, 4.75, 6.5, 5.4145, 5.25, 3.038056, 3.333333, 4.75],\n'name':['Johor', 'Kedah', 'Kelantan', 'Kuala Lumpur', 'Labuan', 'Malakka', 'Negeri Sembilan', 'Pahang', 'Perak', 'Perlis', 'Penang', 'Sabah', 'Sarawak', 'Selangor', 'Terengganu']\n})\ndata\n \nm = folium.Map(location=[5, 108], tiles=\"Mapbox Bright\", zoom_start=6)\n \nfor i in range(0,len(data)):\n    folium.Marker([data.iloc[i]['lon'], data.iloc[i]['lat']], popup=data.iloc[i]['name']).add_to(m)\n    \ndisplay(m)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f92cf2d644001edb8b998c5d6b5742d375df95db"},"cell_type":"code","source":"# df_train = df_train[df_train['Description'].notnull()]","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}