diff --git a/.ipynb_checkpoints/Solutions-checkpoint.ipynb b/.ipynb_checkpoints/Solutions-checkpoint.ipynb new file mode 100644 index 0000000..27531ed --- /dev/null +++ b/.ipynb_checkpoints/Solutions-checkpoint.ipynb @@ -0,0 +1,579 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "8e88c40c-b872-41a6-9080-3fb1ed56102b", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import datetime\n", + "import warnings\n", + "import matplotlib.pyplot as plt\n", + "import seaborn as sns\n", + "\n", + "pd.set_option('display.max_columns', None)\n", + "warnings.filterwarnings('ignore')" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "b12acf4b-8181-46da-8767-2d460269e4e7", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_csv(\"/Users/spicasumampouw/IronSpica/activities/Unit_6/learningSet.csv\")" + ] + }, + { + "cell_type": "markdown", + "id": "bf32ae3b", + "metadata": {}, + "source": [ + "# Lab | Revisiting Machine Learning Case Study" + ] + }, + { + "cell_type": "markdown", + "id": "73df37d0", + "metadata": {}, + "source": [ + "In this lab, you will use learningSet.csv file which you already have cloned in today's activities." + ] + }, + { + "cell_type": "markdown", + "id": "5bbb5252", + "metadata": {}, + "source": [ + "### 1. Check for null values in all the columns" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "dbe69e8b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Column NameUnknown ValuesData Types
414RDATE_595403float64
436RAMNT_595403float64
412RDATE_395170float64
434RAMNT_395170float64
413RDATE_495131float64
............
480GEOCODE2132object
479CLUSTER2132float64
409NUMPROM0int64
408MAXADATE0int64
307AFC60int64
\n", + "

95 rows × 3 columns

\n", + "
" + ], + "text/plain": [ + " Column Name Unknown Values Data Types\n", + "414 RDATE_5 95403 float64\n", + "436 RAMNT_5 95403 float64\n", + "412 RDATE_3 95170 float64\n", + "434 RAMNT_3 95170 float64\n", + "413 RDATE_4 95131 float64\n", + ".. ... ... ...\n", + "480 GEOCODE2 132 object\n", + "479 CLUSTER2 132 float64\n", + "409 NUMPROM 0 int64\n", + "408 MAXADATE 0 int64\n", + "307 AFC6 0 int64\n", + "\n", + "[95 rows x 3 columns]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unk_lst = []\n", + "for col in df.columns:\n", + " column_name = col\n", + " unk_values = int(df[col].isnull().sum())\n", + " unk_dtypes = df[col].dtypes\n", + " unk_lst.append([column_name,unk_values,unk_dtypes])\n", + " \n", + "unk_df = pd.DataFrame(unk_lst,columns=[\"Column Name\",\"Unknown Values\", \"Data Types\"])\n", + "unk_df.sort_values(\"Unknown Values\", ascending=False).iloc[:95]" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "79a6bb89-abad-4499-9a04-60fc5082acc0", + "metadata": {}, + "outputs": [], + "source": [ + "df.columns = [column.lower().replace(' ', '_') for column in df.columns]" + ] + }, + { + "cell_type": "markdown", + "id": "6febccdc", + "metadata": {}, + "source": [ + "### 2. Exclude the following variables by looking at the definitions. Create a new empty list called drop_list. We will append this list and then drop all the columns in this list later:\n", + "\n", + "- `OSOURCE` - symbol definitions not provided, too many categories\n", + "- `ZIP CODE` - we are including state already\n" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "3a07b75a", + "metadata": {}, + "outputs": [], + "source": [ + "#drop_list = df.drop(columns='OSOURCE', 'ZIP')\n", + "drop_list = ['osource', 'zip']" + ] + }, + { + "cell_type": "markdown", + "id": "6a2470e4", + "metadata": {}, + "source": [ + "### 3. Identify columns that over 85% missing values" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "2b6f01b6", + "metadata": {}, + "outputs": [], + "source": [ + "percent_df = df.isna().sum().sort_values(ascending = False)*100/len(df)\n", + "percent_df = percent_df.reset_index()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "a8647cbd-d71f-422b-a667-611586586268", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
column_namepercent
0rdate_599.990567
1ramnt_599.990567
2rdate_399.746363
3ramnt_399.746363
4rdate_499.705488
5ramnt_499.705488
6ramnt_699.186685
7rdate_699.186685
8ramnt_1592.388798
9rdate_1592.388798
10rdate_2391.763091
11ramnt_2391.763091
12rdate_2091.732696
13ramnt_2091.732696
14ramnt_790.677273
15rdate_790.677273
16ramnt_1790.146942
17rdate_1790.146942
18rdate_2190.029556
19ramnt_2190.029556
20ramnt_1089.035970
21rdate_1089.035970
22rdate_1387.160944
23ramnt_1387.160944
24numchld87.018404
\n", + "
" + ], + "text/plain": [ + " column_name percent\n", + "0 rdate_5 99.990567\n", + "1 ramnt_5 99.990567\n", + "2 rdate_3 99.746363\n", + "3 ramnt_3 99.746363\n", + "4 rdate_4 99.705488\n", + "5 ramnt_4 99.705488\n", + "6 ramnt_6 99.186685\n", + "7 rdate_6 99.186685\n", + "8 ramnt_15 92.388798\n", + "9 rdate_15 92.388798\n", + "10 rdate_23 91.763091\n", + "11 ramnt_23 91.763091\n", + "12 rdate_20 91.732696\n", + "13 ramnt_20 91.732696\n", + "14 ramnt_7 90.677273\n", + "15 rdate_7 90.677273\n", + "16 ramnt_17 90.146942\n", + "17 rdate_17 90.146942\n", + "18 rdate_21 90.029556\n", + "19 ramnt_21 90.029556\n", + "20 ramnt_10 89.035970\n", + "21 rdate_10 89.035970\n", + "22 rdate_13 87.160944\n", + "23 ramnt_13 87.160944\n", + "24 numchld 87.018404" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "percent_df.columns = ['column_name', 'percent']\n", + "above85 = percent_df[percent_df['percent'] >= 85]\n", + "above85\n", + "#above85['name'].unique()" + ] + }, + { + "cell_type": "markdown", + "id": "70865946", + "metadata": {}, + "source": [ + "### 4. Remove those columns from the dataframe" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "ca6c4d91", + "metadata": {}, + "outputs": [], + "source": [ + "for i in above85['column_name']:\n", + " drop_list.append(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "85a511c0-67b7-428b-a035-29248927def0", + "metadata": {}, + "outputs": [], + "source": [ + "for i in drop_list: \n", + " df = df.drop(i, axis = 1)" + ] + }, + { + "cell_type": "markdown", + "id": "7bbcf161", + "metadata": {}, + "source": [ + "### 5. Reduce the number of categories in the column GENDER. The column should only have either \"M\" for males, \"F\" for females, and \"other\" for all the rest\n", + "\n", + "Note that there are a few null values in the column. We will first replace those null values using the code below:\n", + "\n", + "```python\n", + "print(categorical['GENDER'].value_counts())\n", + "categorical['GENDER'] = categorical['GENDER'].fillna('F')\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "b583feaa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['F', 'M', ' ', 'C', 'U', 'J', 'A'], dtype=object)" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#data['gender'].value_counts()\n", + "df['gender'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "b148fb95-d33f-44e1-8aa7-d1828914d5bf", + "metadata": {}, + "outputs": [], + "source": [ + "df['gender'] = df['gender'].fillna('F')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "376d39ed-3df9-48f5-b9a0-60dbdcf4f074", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "F 51277\n", + "M 39094\n", + "other 5041\n", + "Name: gender, dtype: int64" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['gender'] = df['gender'].fillna('F')\n", + "df['gender'] = df['gender'].apply(lambda x: x if x in ['M', 'F'] else 'other')\n", + "df['gender'].value_counts()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00f9bf63-f832-46f7-8077-39dcf941e877", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.8" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Solutions.ipynb b/Solutions.ipynb index 89aa338..fec1df4 100644 --- a/Solutions.ipynb +++ b/Solutions.ipynb @@ -1,5 +1,30 @@ { "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "8e88c40c-b872-41a6-9080-3fb1ed56102b", + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import numpy as np\n", + "import warnings\n", + "\n", + "pd.set_option('display.max_columns', None)\n", + "warnings.filterwarnings('ignore')" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "b12acf4b-8181-46da-8767-2d460269e4e7", + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.read_csv(\"/Users/spicasumampouw/IronSpica/activities/Unit_6/learningSet.csv\")" + ] + }, { "cell_type": "markdown", "id": "bf32ae3b", @@ -26,11 +51,151 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "id": "dbe69e8b", "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
Column NameUnknown ValuesData Types
414RDATE_595403float64
436RAMNT_595403float64
412RDATE_395170float64
434RAMNT_395170float64
413RDATE_495131float64
............
480GEOCODE2132object
479CLUSTER2132float64
409NUMPROM0int64
408MAXADATE0int64
307AFC60int64
\n", + "

95 rows × 3 columns

\n", + "
" + ], + "text/plain": [ + " Column Name Unknown Values Data Types\n", + "414 RDATE_5 95403 float64\n", + "436 RAMNT_5 95403 float64\n", + "412 RDATE_3 95170 float64\n", + "434 RAMNT_3 95170 float64\n", + "413 RDATE_4 95131 float64\n", + ".. ... ... ...\n", + "480 GEOCODE2 132 object\n", + "479 CLUSTER2 132 float64\n", + "409 NUMPROM 0 int64\n", + "408 MAXADATE 0 int64\n", + "307 AFC6 0 int64\n", + "\n", + "[95 rows x 3 columns]" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "unk_lst = []\n", + "for col in df.columns:\n", + " column_name = col\n", + " unk_values = int(df[col].isnull().sum())\n", + " unk_dtypes = df[col].dtypes\n", + " unk_lst.append([column_name,unk_values,unk_dtypes])\n", + " \n", + "unk_df = pd.DataFrame(unk_lst,columns=[\"Column Name\",\"Unknown Values\", \"Data Types\"])\n", + "unk_df.sort_values(\"Unknown Values\", ascending=False).iloc[:95]" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "79a6bb89-abad-4499-9a04-60fc5082acc0", + "metadata": {}, "outputs": [], - "source": [] + "source": [ + "df.columns = [column.lower().replace(' ', '_') for column in df.columns]" + ] }, { "cell_type": "markdown", @@ -45,11 +210,14 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "3a07b75a", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "#drop_list = df.drop(columns='OSOURCE', 'ZIP')\n", + "drop_list = ['osource', 'zip']" + ] }, { "cell_type": "markdown", @@ -61,11 +229,216 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "2b6f01b6", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "percent_df = df.isna().sum().sort_values(ascending = False)*100/len(df)\n", + "percent_df = percent_df.reset_index()" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "a8647cbd-d71f-422b-a667-611586586268", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
column_namepercent
0rdate_599.990567
1ramnt_599.990567
2rdate_399.746363
3ramnt_399.746363
4rdate_499.705488
5ramnt_499.705488
6ramnt_699.186685
7rdate_699.186685
8ramnt_1592.388798
9rdate_1592.388798
10rdate_2391.763091
11ramnt_2391.763091
12rdate_2091.732696
13ramnt_2091.732696
14ramnt_790.677273
15rdate_790.677273
16ramnt_1790.146942
17rdate_1790.146942
18rdate_2190.029556
19ramnt_2190.029556
20ramnt_1089.035970
21rdate_1089.035970
22rdate_1387.160944
23ramnt_1387.160944
24numchld87.018404
\n", + "
" + ], + "text/plain": [ + " column_name percent\n", + "0 rdate_5 99.990567\n", + "1 ramnt_5 99.990567\n", + "2 rdate_3 99.746363\n", + "3 ramnt_3 99.746363\n", + "4 rdate_4 99.705488\n", + "5 ramnt_4 99.705488\n", + "6 ramnt_6 99.186685\n", + "7 rdate_6 99.186685\n", + "8 ramnt_15 92.388798\n", + "9 rdate_15 92.388798\n", + "10 rdate_23 91.763091\n", + "11 ramnt_23 91.763091\n", + "12 rdate_20 91.732696\n", + "13 ramnt_20 91.732696\n", + "14 ramnt_7 90.677273\n", + "15 rdate_7 90.677273\n", + "16 ramnt_17 90.146942\n", + "17 rdate_17 90.146942\n", + "18 rdate_21 90.029556\n", + "19 ramnt_21 90.029556\n", + "20 ramnt_10 89.035970\n", + "21 rdate_10 89.035970\n", + "22 rdate_13 87.160944\n", + "23 ramnt_13 87.160944\n", + "24 numchld 87.018404" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "percent_df.columns = ['column_name', 'percent']\n", + "above85 = percent_df[percent_df['percent'] >= 85]\n", + "above85\n", + "#above85['name'].unique()" + ] }, { "cell_type": "markdown", @@ -77,11 +450,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "ca6c4d91", "metadata": {}, "outputs": [], - "source": [] + "source": [ + "for i in above85['column_name']:\n", + " drop_list.append(i)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "85a511c0-67b7-428b-a035-29248927def0", + "metadata": {}, + "outputs": [], + "source": [ + "for i in drop_list: \n", + " df = df.drop(i, axis = 1)" + ] }, { "cell_type": "markdown", @@ -100,9 +487,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "id": "b583feaa", "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['F', 'M', ' ', 'C', 'U', 'J', 'A'], dtype=object)" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#data['gender'].value_counts()\n", + "df['gender'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "b148fb95-d33f-44e1-8aa7-d1828914d5bf", + "metadata": {}, + "outputs": [], + "source": [ + "df['gender'] = df['gender'].fillna('F')" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "376d39ed-3df9-48f5-b9a0-60dbdcf4f074", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "F 51277\n", + "M 39094\n", + "other 5041\n", + "Name: gender, dtype: int64" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df['gender'] = df['gender'].fillna('F')\n", + "df['gender'] = df['gender'].apply(lambda x: x if x in ['M', 'F'] else 'other')\n", + "df['gender'].value_counts()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00f9bf63-f832-46f7-8077-39dcf941e877", + "metadata": {}, "outputs": [], "source": [] } @@ -123,7 +568,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.5" + "version": "3.8.8" } }, "nbformat": 4,