{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "rQ8UhzFpgRra" }, "source": [ "# ഡാറ്റാ തയ്യാറാക്കൽ\n", "\n", "[*Data Science: Introduction to Machine Learning for Data Science Python and Machine Learning Studio by Lee Stott* എന്ന പുസ്തകത്തിലെ ഒറിജിനൽ നോട്ട്‌ബുക്ക് സോഴ്‌സ്](https://github.com/leestott/intro-Datascience/blob/master/Course%20Materials/4-Cleaning_and_Manipulating-Reference.ipynb)\n", "\n", "## `DataFrame` വിവരങ്ങൾ പരിശോധിക്കൽ\n", "\n", "> **പഠന ലക്ഷ്യം:** ഈ ഉപവിഭാഗം അവസാനിക്കുമ്പോൾ, pandas DataFrames-ൽ സൂക്ഷിച്ചിരിക്കുന്ന ഡാറ്റയെക്കുറിച്ചുള്ള പൊതുവായ വിവരങ്ങൾ കണ്ടെത്തുന്നതിൽ നിങ്ങൾക്ക് സുഖകരമായി അറിയാമാകണം.\n", "\n", "നിങ്ങൾ pandas-ലേക്ക് നിങ്ങളുടെ ഡാറ്റ ലോഡ് ചെയ്തശേഷം, അത് സാധാരണയായി `DataFrame` ആകും. എന്നാൽ, നിങ്ങളുടെ `DataFrame`-ൽ 60,000 വരികളും 400 കോളങ്ങളുമുള്ള ഡാറ്റാസെറ്റ് ഉണ്ടെങ്കിൽ, നിങ്ങൾ എന്തിനാണ് പ്രവർത്തിക്കുന്നത് എന്ന് എങ്ങനെ മനസ്സിലാക്കും? ഭാഗ്യവശാൽ, pandas ഒരു `DataFrame`-ന്റെ മൊത്തം വിവരങ്ങൾ വേഗത്തിൽ നോക്കാൻ ചില സൗകര്യപ്രദമായ ഉപകരണങ്ങൾ നൽകുന്നു, കൂടാതെ ആദ്യ കുറച്ച് വരികളും അവസാന കുറച്ച് വരികളും കാണാൻ കഴിയും.\n", "\n", "ഈ പ്രവർത്തനം പരിശോധിക്കാൻ, നാം Python scikit-learn ലൈബ്രറി ഇറക്കുമതി ചെയ്ത്, എല്ലാ ഡാറ്റാ സയന്റിസ്റ്റുകളും നൂറുകണക്കിന് തവണ കണ്ടിട്ടുള്ള ഒരു ഐക്കോണിക് ഡാറ്റാസെറ്റ് ഉപയോഗിക്കും: ബ്രിട്ടീഷ് ജീവശാസ്ത്രജ്ഞൻ റൊണാൾഡ് ഫിഷറുടെ 1936 ലെ പേപ്പറിൽ ഉപയോഗിച്ച *Iris* ഡാറ്റാസെറ്റ് \"The use of multiple measurements in taxonomic problems\":\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "id": "hB1RofhdgRrp", "trusted": false }, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.datasets import load_iris\n", "\n", "iris = load_iris()\n", "iris_df = pd.DataFrame(data=iris['data'], columns=iris['feature_names'])" ] }, { "cell_type": "markdown", "metadata": { "id": "AGA0A_Y8hMdz" }, "source": [ "### `DataFrame.shape`\n", "നാം `iris_df` എന്ന വേരിയബിളിൽ Iris Dataset ലോഡ് ചെയ്തിട്ടുണ്ട്. ഡാറ്റയിൽ പ്രവേശിക്കുന്നതിന് മുമ്പ്, നമുക്ക് എത്ര ഡാറ്റാപോയിന്റുകൾ ഉണ്ട് എന്നും ഡാറ്റാസെറ്റിന്റെ മൊത്തം വലിപ്പം എന്താണെന്നും അറിയുന്നത് മൂല്യവത്തായിരിക്കും. നാം കൈകാര്യം ചെയ്യുന്ന ഡാറ്റയുടെ വോള്യം നോക്കുന്നത് ഉപകാരപ്രദമാണ്.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LOe5jQohhulf", "outputId": "fb0577ac-3b4a-4623-cb41-20e1b264b3e9" }, "outputs": [ { "data": { "text/plain": [ "(150, 4)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iris_df.shape" ] }, { "cell_type": "markdown", "metadata": { "id": "smE7AGzOhxk2" }, "source": [ "അപ്പോൾ, നാം 150 വരികളും 4 കോളങ്ങളുമുള്ള ഡാറ്റയുമായി ഇടപഴകുകയാണ്. ഓരോ വരിയും ഒരു ഡാറ്റാപോയിന്റിനെ പ്രതിനിധീകരിക്കുന്നു, ഓരോ കോളവും ഡാറ്റാ ഫ്രെയിമുമായി ബന്ധപ്പെട്ട ഒരു സവിശേഷതയെ പ്രതിനിധീകരിക്കുന്നു. അതായത്, അടിസ്ഥാനപരമായി, ഓരോന്നിലും 4 സവിശേഷതകൾ ഉള്ള 150 ഡാറ്റാപോയിന്റുകൾ ഉണ്ട്.\n", "\n", "`shape` ഇവിടെ ഡാറ്റാഫ്രെയിമിന്റെ ഒരു ആട്രിബ്യൂട്ടാണ്, ഫംഗ്ഷൻ അല്ല, അതുകൊണ്ടുതന്നെ ഇത് കോശങ്ങളാൽ അടങ്ങിയ ഒരു ജോഡിയിൽ അവസാനിക്കുന്നില്ല.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "d3AZKs0PinGP" }, "source": [ "### `DataFrame.columns`\n", "ഇപ്പോൾ നാം ഡാറ്റയുടെ 4 കോളങ്ങളിലേക്ക് കടക്കാം. അവയിൽ ഓരോന്നും ശരിയായി എന്താണ് പ്രതിനിധാനം ചെയ്യുന്നത്? `columns` ആട്രിബ്യൂട്ട് ഡാറ്റാഫ്രെയിമിലെ കോളങ്ങളുടെ പേരുകൾ നമുക്ക് നൽകും.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YPGh_ziji-CY", "outputId": "74e7a43a-77cc-4c80-da56-7f50767c37a0" }, "outputs": [ { "data": { "text/plain": [ "Index(['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)',\n", " 'petal width (cm)'],\n", " dtype='object')" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iris_df.columns" ] }, { "cell_type": "markdown", "metadata": { "id": "TsobcU_VjCC_" }, "source": [ "നാം കാണുന്ന പോലെ, നാല്(4) കോളങ്ങൾ ഉണ്ട്. `columns` ഗുണധർമ്മം കോളങ്ങളുടെ പേരുകൾ മാത്രമാണ് നമ്മെ അറിയിക്കുന്നത്, അതിലധികം ഒന്നും അല്ല. ഒരു ഡാറ്റാസെറ്റിൽ ഉള്ള സവിശേഷതകൾ തിരിച്ചറിയാൻ ആഗ്രഹിക്കുമ്പോൾ ഈ ഗുണധർമ്മം പ്രാധാന്യം നേടുന്നു.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "2UTlvkjmgRrs" }, "source": [ "### `DataFrame.info`\n", "ഡാറ്റയുടെ അളവ്(`shape` ആട്രിബ്യൂട്ട് നൽകുന്നു)യും ഫീച്ചറുകളുടെയും കോളങ്ങളുടെയും പേര്(`columns` ആട്രിബ്യൂട്ട് നൽകുന്നു) dataset-നെക്കുറിച്ച് നമ്മെ ചിലത് അറിയിക്കുന്നു. ഇപ്പോൾ, dataset-ൽ കൂടുതൽ ആഴത്തിൽ പ്രവേശിക്കാൻ ആഗ്രഹിക്കുന്നു. ഇതിന് `DataFrame.info()` ഫംഗ്ഷൻ വളരെ ഉപകാരപ്രദമാണ്.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "dHHRyG0_gRrt", "outputId": "d8fb0c40-4f18-4e19-da48-c8db77d1d3a5", "trusted": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 150 entries, 0 to 149\n", "Data columns (total 4 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 sepal length (cm) 150 non-null float64\n", " 1 sepal width (cm) 150 non-null float64\n", " 2 petal length (cm) 150 non-null float64\n", " 3 petal width (cm) 150 non-null float64\n", "dtypes: float64(4)\n", "memory usage: 4.8 KB\n" ] } ], "source": [ "iris_df.info()" ] }, { "cell_type": "markdown", "metadata": { "id": "1XgVMpvigRru" }, "source": [ "ഇവിടെ നിന്ന്, നാം ചില നിരീക്ഷണങ്ങൾ നടത്താം:\n", "1. ഓരോ കോളത്തിന്റെയും ഡാറ്റാ ടൈപ്പ്: ഈ ഡാറ്റാസെറ്റിൽ, എല്ലാ ഡാറ്റയും 64-ബിറ്റ് ഫ്ലോട്ടിംഗ്-പോയിന്റ് നമ്പറുകളായി സൂക്ഷിച്ചിരിക്കുന്നു.\n", "2. നോൺ-നൾ മൂല്യങ്ങളുടെ എണ്ണം: നൾ മൂല്യങ്ങളെ കൈകാര്യം ചെയ്യുന്നത് ഡാറ്റാ തയ്യാറെടുപ്പിലെ ഒരു പ്രധാന ഘട്ടമാണ്. ഇത് പിന്നീട് നോട്ട്‌ബുക്കിൽ കൈകാര്യം ചെയ്യും.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "IYlyxbpWFEF4" }, "source": [ "### DataFrame.describe()\n", "നമ്മുടെ ഡാറ്റാസെറ്റിൽ നമുക്ക് നിരവധി സംഖ്യാത്മക ഡാറ്റ ഉണ്ടെന്ന് പറയാം. ശരാശരി, മധ്യക, ക്വാർട്ടൈൽസ് തുടങ്ങിയ ഏകവിവരസംഖ്യാത്മക കണക്കുകൾ ഓരോ കോളത്തിനും വ്യക്തിഗതമായി ചെയ്യാം. ഒരു ഡാറ്റാസെറ്റിലെ സംഖ്യാത്മക കോളങ്ങളുടെയും ഒരു സംഖ്യാത്മക സംഗ്രഹം നൽകുന്നത് `DataFrame.describe()` ഫംഗ്ഷനാണ്.\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 297 }, "id": "tWV-CMstFIRA", "outputId": "4fc49941-bc13-4b0c-a412-cb39e7d3f289" }, "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", "
sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)
count150.000000150.000000150.000000150.000000
mean5.8433333.0573333.7580001.199333
std0.8280660.4358661.7652980.762238
min4.3000002.0000001.0000000.100000
25%5.1000002.8000001.6000000.300000
50%5.8000003.0000004.3500001.300000
75%6.4000003.3000005.1000001.800000
max7.9000004.4000006.9000002.500000
\n", "
" ], "text/plain": [ " sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n", "count 150.000000 150.000000 150.000000 150.000000\n", "mean 5.843333 3.057333 3.758000 1.199333\n", "std 0.828066 0.435866 1.765298 0.762238\n", "min 4.300000 2.000000 1.000000 0.100000\n", "25% 5.100000 2.800000 1.600000 0.300000\n", "50% 5.800000 3.000000 4.350000 1.300000\n", "75% 6.400000 3.300000 5.100000 1.800000\n", "max 7.900000 4.400000 6.900000 2.500000" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iris_df.describe()" ] }, { "cell_type": "markdown", "metadata": { "id": "zjjtW5hPGMuM" }, "source": [ "മുകളിൽ കാണിച്ചിരിക്കുന്ന ഔട്ട്പുട്ട് ഓരോ കോളത്തിന്റെയും മൊത്തം ഡാറ്റാ പോയിന്റുകളുടെ എണ്ണം, ശരാശരി, സ്റ്റാൻഡേർഡ് ഡിവിയേഷൻ, കുറഞ്ഞത്, താഴ്ന്ന ക്വാർട്ടൈൽ (25%), മധ്യസ്ഥാനം (50%), മുകളിലെ ക്വാർട്ടൈൽ (75%) എന്നിവയും പരമാവധി മൂല്യവും കാണിക്കുന്നു.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "-lviAu99gRrv" }, "source": [ "### `DataFrame.head`\n", "മുകളിൽ പറഞ്ഞ എല്ലാ ഫംഗ്ഷനുകളും ആട്രിബ്യൂട്ടുകളും ഉപയോഗിച്ച്, ഡാറ്റാസെറ്റിന്റെ ഒരു മുകളിൽനിന്നുള്ള ദൃശ്യമാണ് നമ്മുക്ക് ലഭിച്ചത്. എത്ര ഡാറ്റാ പോയിന്റുകൾ ഉണ്ട്, എത്ര ഫീച്ചറുകൾ ഉണ്ട്, ഓരോ ഫീച്ചറിന്റെയും ഡാറ്റാ ടൈപ്പ് എന്താണെന്ന്, ഓരോ ഫീച്ചറിനും എത്ര നോൺ-നൾ മൂല്യങ്ങൾ ഉണ്ട് എന്നതും നമുക്ക് അറിയാം.\n", "\n", "ഇപ്പോൾ ഡാറ്റ തന്നെ നോക്കാനുള്ള സമയം. നമ്മുടെ `DataFrame` ന്റെ ആദ്യ കുറച്ച് വരികൾ (ആദ്യ കുറച്ച് ഡാറ്റാപോയിന്റുകൾ) എങ്ങനെ കാണപ്പെടുന്നു എന്ന് നോക്കാം:\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "id": "DZMJZh0OgRrw", "outputId": "d9393ee5-c106-4797-f815-218f17160e00", "trusted": false }, "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", "
sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)
05.13.51.40.2
14.93.01.40.2
24.73.21.30.2
34.63.11.50.2
45.03.61.40.2
\n", "
" ], "text/plain": [ " sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n", "0 5.1 3.5 1.4 0.2\n", "1 4.9 3.0 1.4 0.2\n", "2 4.7 3.2 1.3 0.2\n", "3 4.6 3.1 1.5 0.2\n", "4 5.0 3.6 1.4 0.2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iris_df.head()" ] }, { "cell_type": "markdown", "metadata": { "id": "EBHEimZuEFQK" }, "source": [ "ഇവിടെ ഔട്ട്പുട്ടായി, ഡാറ്റാസെറ്റിന്റെ അഞ്ച് (5) എൻട്രികൾ കാണാം. ഇടത്തുവശത്തെ ഇൻഡക്സ് നോക്കിയാൽ, ഇവ ആദ്യത്തെ അഞ്ച് വരികളാണെന്ന് കണ്ടെത്താം.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "oj7GkrTdgRry" }, "source": [ "### Exercise:\n", "\n", "മുകളിൽ നൽകിയ ഉദാഹരണത്തിൽ നിന്ന്, ഡീഫോൾട്ടായി, `DataFrame.head` ഒരു `DataFrame`-ന്റെ ആദ്യത്തെ അഞ്ച് വരികൾ തിരികെ നൽകുന്നു എന്ന് വ്യക്തമാകുന്നു. താഴെയുള്ള കോഡ് സെല്ലിൽ, അഞ്ച് വരികളേക്കാൾ കൂടുതൽ വരികൾ പ്രദർശിപ്പിക്കാൻ ഒരു മാർഗം കണ്ടെത്താമോ?\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true, "id": "EKRmRFFegRrz", "trusted": false }, "outputs": [], "source": [ "# Hint: Consult the documentation by using iris_df.head?" ] }, { "cell_type": "markdown", "metadata": { "id": "BJ_cpZqNgRr1" }, "source": [ "### `DataFrame.tail`\n", "ഡാറ്റയെ നോക്കാനുള്ള മറ്റൊരു മാർഗം ആരംഭത്തിൽ നിന്ന് പകരം അവസാനം നിന്നായിരിക്കും. `DataFrame.head` ന്റെ മറുവശം `DataFrame.tail` ആണ്, ഇത് ഒരു `DataFrame` ന്റെ അവസാനത്തെ അഞ്ച് വരികൾ തിരികെ നൽകുന്നു:\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 0 }, "id": "heanjfGWgRr2", "outputId": "6ae09a21-fe09-4110-b0d7-1a1fbf34d7f3", "trusted": false }, "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", "
sepal length (cm)sepal width (cm)petal length (cm)petal width (cm)
1456.73.05.22.3
1466.32.55.01.9
1476.53.05.22.0
1486.23.45.42.3
1495.93.05.11.8
\n", "
" ], "text/plain": [ " sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)\n", "145 6.7 3.0 5.2 2.3\n", "146 6.3 2.5 5.0 1.9\n", "147 6.5 3.0 5.2 2.0\n", "148 6.2 3.4 5.4 2.3\n", "149 5.9 3.0 5.1 1.8" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iris_df.tail()" ] }, { "cell_type": "markdown", "metadata": { "id": "31kBWfyLgRr3" }, "source": [ "പ്രായോഗികമായി, ക്രമീകരിച്ച ഡാറ്റാസെറ്റുകളിൽ ഔട്ട്‌ലൈയർമാരെ കണ്ടെത്താൻ ശ്രമിക്കുമ്പോൾ, ഒരു `DataFrame`-ന്റെ ആദ്യ കുറച്ച് വരികളും അവസാന കുറച്ച് വരികളും എളുപ്പത്തിൽ പരിശോധിക്കാൻ കഴിയുന്നത് ഉപകാരപ്രദമാണ്.\n", "\n", "മുകളിൽ കോഡ് ഉദാഹരണങ്ങളുടെ സഹായത്തോടെ കാണിച്ച എല്ലാ ഫംഗ്ഷനുകളും ആട്രിബ്യൂട്ടുകളും, ഡാറ്റയുടെ ഒരു ദൃശ്യവും അനുഭവവും ലഭിക്കാൻ സഹായിക്കുന്നു.\n", "\n", "> **Takeaway:** ഒരു DataFrame-ലെ വിവരങ്ങളെക്കുറിച്ചുള്ള മെറ്റാഡാറ്റയോ അതിലെ ആദ്യവും അവസാനവും കുറച്ച് മൂല്യങ്ങളോ നോക്കിയാൽ പോലും, നിങ്ങൾ കൈകാര്യം ചെയ്യുന്ന ഡാറ്റയുടെ വലിപ്പം, ആകൃതി, ഉള്ളടക്കം എന്നിവയെക്കുറിച്ച് ഉടൻ ഒരു ആശയം ലഭിക്കും.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "TvurZyLSDxq_" }, "source": [ "### നഷ്ടമായ ഡാറ്റ \n", "നഷ്ടമായ ഡാറ്റയിൽ നാം ആഴത്തിൽ നോക്കാം. ചില കോളങ്ങളിലൊന്നിലും മൂല്യം സൂക്ഷിക്കപ്പെടാത്തപ്പോൾ നഷ്ടമായ ഡാറ്റ സംഭവിക്കുന്നു. \n", "\n", "ഒരു ഉദാഹരണം എടുത്തു നോക്കാം: ആരെങ്കിലും തന്റെ ഭാരം സംബന്ധിച്ച് ജാഗ്രതയുള്ളവനാണെന്ന് കരുതുക, അവൻ/അവൾ ഒരു സർവേയിൽ ഭാരത്തിന്റെ ഫീൽഡ് പൂരിപ്പിക്കാതെ പോകുന്നു. അപ്പോൾ ആ വ്യക്തിയുടെ ഭാര മൂല്യം നഷ്ടമായിരിക്കും. \n", "\n", "വാസ്തവ ലോക ഡാറ്റാസെറ്റുകളിൽ പലപ്പോഴും നഷ്ടമായ മൂല്യങ്ങൾ ഉണ്ടാകാറുണ്ട്. \n", "\n", "**പാൻഡാസ് നഷ്ടമായ ഡാറ്റ കൈകാര്യം ചെയ്യുന്നത്** \n", "\n", "പാൻഡാസ് നഷ്ടമായ മൂല്യങ്ങൾ കൈകാര്യം ചെയ്യുന്നത് രണ്ട് രീതികളിലാണ്. ആദ്യത്തേത് നിങ്ങൾ മുമ്പത്തെ വിഭാഗങ്ങളിൽ കണ്ടതാണ്: `NaN`, അല്ലെങ്കിൽ Not a Number. ഇത് യഥാർത്ഥത്തിൽ IEEE ഫ്ലോട്ടിംഗ്-പോയിന്റ് സ്പെസിഫിക്കേഷന്റെ ഭാഗമായ ഒരു പ്രത്യേക മൂല്യമാണ്, ഇത് നഷ്ടമായ ഫ്ലോട്ടിംഗ്-പോയിന്റ് മൂല്യങ്ങൾ സൂചിപ്പിക്കാൻ മാത്രമാണ് ഉപയോഗിക്കുന്നത്. \n", "\n", "ഫ്ലോട്ടുകൾക്ക് പുറമേ നഷ്ടമായ മൂല്യങ്ങൾക്ക്, പാൻഡാസ് Python `None` ഒബ്ജക്റ്റ് ഉപയോഗിക്കുന്നു. നിങ്ങൾക്ക് രണ്ട് വ്യത്യസ്ത തരത്തിലുള്ള മൂല്യങ്ങൾ കാണപ്പെടുന്നത് ആശയക്കുഴപ്പമുണ്ടാക്കാം, എന്നാൽ ഈ ഡിസൈൻ തിരഞ്ഞെടുപ്പിന് പ്രോഗ്രാമാറ്റിക് കാരണങ്ങൾ ഉണ്ട്, പ്രായോഗികമായി ഈ മാർഗ്ഗം പാൻഡാസിന് ഭൂരിഭാഗം കേസുകൾക്കായി നല്ല ഒരു സമാധാനം നൽകാൻ സഹായിക്കുന്നു. ഇതിന് പുറമേ, `None` ഉം `NaN` ഉം ഉപയോഗിക്കുമ്പോൾ ശ്രദ്ധിക്കേണ്ട ചില നിയന്ത്രണങ്ങൾ ഉണ്ട്.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "lOHqUlZFgRr5" }, "source": [ "### `None`: ഫ്ലോട്ട് അല്ലാത്ത നഷ്ടപ്പെട്ട ഡാറ്റ\n", "`None` പൈത്തണിൽ നിന്നാണ് വന്നത്, അതിനാൽ അത് `'object'` ഡാറ്റ ടൈപ്പല്ലാത്ത NumPy, pandas അറകളിൽ ഉപയോഗിക്കാൻ കഴിയില്ല. ഓർക്കുക, NumPy അറകളും pandas-ലെ ഡാറ്റ ഘടനകളും ഒരേ തരത്തിലുള്ള ഡാറ്റ മാത്രമേ ഉൾക്കൊള്ളൂ. ഇതാണ് അവയ്ക്ക് വലിയ തോതിലുള്ള ഡാറ്റയും കണക്കുകൂട്ടലും ചെയ്യാനുള്ള അത്ഭുതശക്തി നൽകുന്നത്, പക്ഷേ ഇത് അവയുടെ സൗകര്യക്ഷമതയെ പരിമിതപ്പെടുത്തുന്നു. ഇത്തരം അറകൾ \"കുറഞ്ഞ പൊതുവായ ഡിനോമിനേറ്റർ\" എന്ന ഡാറ്റ ടൈപ്പിലേക്ക് അപ്കാസ്റ്റ് ചെയ്യേണ്ടിവരും, അതായത് അറയിൽ ഉള്ള എല്ലാം ഉൾക്കൊള്ളുന്ന ഡാറ്റ ടൈപ്പ്. അറയിൽ `None` ഉണ്ടെങ്കിൽ, നിങ്ങൾ പൈത്തൺ ഒബ്ജക്റ്റുകളുമായി പ്രവർത്തിക്കുകയാണ് എന്നർത്ഥം.\n", "\n", "ഇത് പ്രവർത്തനത്തിൽ കാണാൻ, താഴെ കൊടുത്ത ഉദാഹരണ അറ (അതിനുള്ള `dtype` ശ്രദ്ധിക്കുക) പരിഗണിക്കുക:\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "QIoNdY4ngRr7", "outputId": "92779f18-62f4-4a03-eca2-e9a101604336", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "array([2, None, 6, 8], dtype=object)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "example1 = np.array([2, None, 6, 8])\n", "example1" ] }, { "cell_type": "markdown", "metadata": { "id": "pdlgPNbhgRr7" }, "source": [ "ഉപ്കാസ്റ്റ് ഡാറ്റാ ടൈപ്പുകളുടെ യാഥാർത്ഥ്യത്തിന് രണ്ട് പാർശ്വഫലങ്ങൾ ഉണ്ട്. ആദ്യം, പ്രവർത്തനങ്ങൾ കമ്പൈൽ ചെയ്ത NumPy കോഡിന്റെ പകരം വ്യാഖ്യാനിച്ച Python കോഡ് നിലയിൽ നടത്തപ്പെടും. അടിസ്ഥാനപരമായി, ഇതിന്റെ അർത്ഥം `None` ഉള്ള `Series` അല്ലെങ്കിൽ `DataFrames` ഉൾപ്പെടുന്ന ഏതെങ്കിലും പ്രവർത്തനങ്ങളും മന്ദഗതിയിലാകും. നിങ്ങൾക്ക് ഈ പ്രകടന നഷ്ടം ശ്രദ്ധിക്കാതിരിക്കാം, എന്നാൽ വലിയ ഡാറ്റാസെറ്റുകൾക്കായി ഇത് പ്രശ്നമായി മാറാം.\n", "\n", "രണ്ടാമത്തെ പാർശ്വഫലം ആദ്യത്തേതിൽ നിന്നാണ് ഉത്ഭവിക്കുന്നത്. കാരണം `None` അടിസ്ഥാനപരമായി `Series` അല്ലെങ്കിൽ `DataFrame`-കളെ വാനില Python ലോകത്തിലേക്ക് തിരികെ കൊണ്ടുവരുന്നു, അതിനാൽ `None` മൂല്യം അടങ്ങിയ അറകളിൽ NumPy/pandas സംഗ്രഹണങ്ങൾ പോലുള്ള `sum()` അല്ലെങ്കിൽ `min()` ഉപയോഗിക്കുന്നത് സാധാരണയായി ഒരു പിശക് സൃഷ്ടിക്കും:\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 292 }, "id": "gWbx-KB9gRr8", "outputId": "ecba710a-22ec-41d5-a39c-11f67e645b50", "trusted": false }, "outputs": [ { "ename": "TypeError", "evalue": "ignored", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mexample1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/numpy/core/_methods.py\u001b[0m in \u001b[0;36m_sum\u001b[0;34m(a, axis, dtype, out, keepdims, initial, where)\u001b[0m\n\u001b[1;32m 45\u001b[0m def _sum(a, axis=None, dtype=None, out=None, keepdims=False,\n\u001b[1;32m 46\u001b[0m initial=_NoValue, where=True):\n\u001b[0;32m---> 47\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mumr_sum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwhere\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 48\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 49\u001b[0m def _prod(a, axis=None, dtype=None, out=None, keepdims=False,\n", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'NoneType'" ] } ], "source": [ "example1.sum()" ] }, { "cell_type": "markdown", "metadata": { "id": "LcEwO8UogRr9" }, "source": [ "**പ്രധാനപ്പെട്ട കാര്യങ്ങൾ**: പൂർണ്ണസംഖ്യകളും `None` മൂല്യങ്ങളും തമ്മിലുള്ള കൂട്ടിച്ചേർക്കൽ (മറ്റു പ്രവർത്തനങ്ങളും) നിർവചിക്കപ്പെട്ടിട്ടില്ല, ഇത് അവ ഉൾക്കൊള്ളുന്ന ഡാറ്റാസെറ്റുകളുമായി നിങ്ങൾ ചെയ്യാൻ കഴിയുന്ന കാര്യങ്ങളെ പരിമിതപ്പെടുത്താം.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "pWvVHvETgRr9" }, "source": [ "### `NaN`: കാണാനാകാത്ത ഫ്ലോട്ട് മൂല്യങ്ങൾ\n", "\n", "`None`-നോട് വ്യത്യസ്തമായി, NumPy (അതിനാൽ pandas-ഉം) അതിന്റെ വേഗതയുള്ള, വെക്ടറൈസ്ഡ് പ്രവർത്തനങ്ങൾക്കും ufuncs-ക്കും `NaN`-നെ പിന്തുണയ്ക്കുന്നു. മോശം വാർത്ത എന്തെന്നാൽ `NaN`-ൽ ചെയ്ത ഏതെങ്കിലും ഗണിത പ്രവർത്തനവും എപ്പോഴും `NaN`-നെ ഫലമായി നൽകും. ഉദാഹരണത്തിന്:\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "rcFYfMG9gRr9", "outputId": "699e81b7-5c11-4b46-df1d-06071768690f", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "nan" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.nan + 1" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "BW3zQD2-gRr-", "outputId": "4525b6c4-495d-4f7b-a979-efce1dae9bd0", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "nan" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.nan * 0" ] }, { "cell_type": "markdown", "metadata": { "id": "fU5IPRcCgRr-" }, "source": [ "നല്ല വാർത്ത: `NaN` ഉള്ള അറേകളിൽ അഗ്രിഗേഷനുകൾ നടത്തുമ്പോൾ പിശകുകൾ ഉണ്ടാകാറില്ല. മോശം വാർത്ത: ഫലങ്ങൾ ഒരുപോലെ പ്രയോജനപ്രദമല്ല:\n" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "LCInVgSSgRr_", "outputId": "fa06495a-0930-4867-87c5-6023031ea8b5", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "(nan, nan, nan)" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example2 = np.array([2, np.nan, 6, 8]) \n", "example2.sum(), example2.min(), example2.max()" ] }, { "cell_type": "markdown", "metadata": { "id": "nhlnNJT7gRr_" }, "source": [ "### വ്യായാമം:\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true, "id": "yan3QRaOgRr_", "trusted": false }, "outputs": [], "source": [ "# What happens if you add np.nan and None together?\n" ] }, { "cell_type": "markdown", "metadata": { "id": "_iDvIRC8gRsA" }, "source": [ "ഓർമ്മിക്കുക: `NaN` എന്നത് നഷ്ടമായ ഫ്ലോട്ടിംഗ്-പോയിന്റ് മൂല്യങ്ങൾക്കായാണ്; പൂർണ്ണസംഖ്യകൾക്ക്, സ്ട്രിംഗുകൾക്ക്, അല്ലെങ്കിൽ ബൂളിയൻ മൂല്യങ്ങൾക്ക് `NaN` എന്ന സമാനമായത് ഇല്ല.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "kj6EKdsAgRsA" }, "source": [ "### `NaN` ഉം `None` ഉം: pandas-ൽ നൾ മൂല്യങ്ങൾ\n", "\n", "`NaN` ഉം `None` ഉം ചിലപ്പോൾ വ്യത്യസ്തമായി പ്രവർത്തിച്ചേക്കാമെങ്കിലും, pandas അവയെ പരസ്പരം മാറ്റി ഉപയോഗിക്കാൻ രൂപകൽപ്പന ചെയ്തതാണ്. ഞങ്ങൾ പറയുന്നത് എന്താണെന്ന് കാണാൻ, ഒരു പൂർണ്ണസംഖ്യകളുടെ `Series` പരിഗണിക്കാം:\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Nji-KGdNgRsA", "outputId": "36aa14d2-8efa-4bfd-c0ed-682991288822", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "0 1\n", "1 2\n", "2 3\n", "dtype: int64" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int_series = pd.Series([1, 2, 3], dtype=int)\n", "int_series" ] }, { "cell_type": "markdown", "metadata": { "id": "WklCzqb8gRsB" }, "source": [ "### വ്യായാമം:\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true, "id": "Cy-gqX5-gRsB", "trusted": false }, "outputs": [], "source": [ "# Now set an element of int_series equal to None.\n", "# How does that element show up in the Series?\n", "# What is the dtype of the Series?\n" ] }, { "cell_type": "markdown", "metadata": { "id": "WjMQwltNgRsB" }, "source": [ "`Series`-ഉം `DataFrame`-ഉം ഉള്ള ഡാറ്റയുടെ സമാനത ഉറപ്പാക്കുന്നതിനായി ഡാറ്റാ ടൈപ്പുകൾ അപ്കാസ്റ്റ് ചെയ്യുന്നതിനുള്ള പ്രക്രിയയിൽ, pandas `None`-നും `NaN`-നും ഇടയിൽ നഷ്ടമായ മൂല്യങ്ങൾ സ്വീകാര്യമായി മാറ്റും. ഈ ഡിസൈൻ സവിശേഷത കാരണം, pandas-ൽ `None`-നും `NaN`-നും \"null\" എന്ന രണ്ട് വ്യത്യസ്ത രുചികൾ ആയി ചിന്തിക്കുന്നത് സഹായകരമായിരിക്കും. യഥാർത്ഥത്തിൽ, pandas-ൽ നഷ്ടമായ മൂല്യങ്ങൾ കൈകാര്യം ചെയ്യാൻ നിങ്ങൾ ഉപയോഗിക്കുന്ന ചില പ്രധാന രീതികൾ അവരുടെ പേരുകളിൽ ഈ ആശയം പ്രതിഫലിപ്പിക്കുന്നു:\n", "\n", "- `isnull()`: നഷ്ടമായ മൂല്യങ്ങൾ സൂചിപ്പിക്കുന്ന ബൂളിയൻ മാസ്ക് സൃഷ്ടിക്കുന്നു\n", "- `notnull()`: `isnull()`-ന്റെ വിപരീതം\n", "- `dropna()`: ഡാറ്റയുടെ ഫിൽട്ടർ ചെയ്ത പതിപ്പ് നൽകുന്നു\n", "- `fillna()`: നഷ്ടമായ മൂല്യങ്ങൾ പൂരിപ്പിച്ചോ ഇംപ്യൂട്ട് ചെയ്തോ ചെയ്ത ഡാറ്റയുടെ ഒരു പകർപ്പ് നൽകുന്നു\n", "\n", "ഇവ പ്രധാനപ്പെട്ട രീതികളാണ്, അവയിൽ പ്രാവീണ്യം നേടുകയും സുഖകരമായി ഉപയോഗിക്കാൻ പഠിക്കുകയും ചെയ്യേണ്ടത് അത്യന്താപേക്ഷിതമാണ്, അതിനാൽ ഓരോതും നാം വിശദമായി പരിശോധിക്കാം.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Yh5ifd9FgRsB" }, "source": [ "### ശൂന്യ മൂല്യങ്ങൾ കണ്ടെത്തൽ\n", "\n", "ഇപ്പോൾ നാം നഷ്ടമായ മൂല്യങ്ങളുടെ പ്രാധാന്യം മനസിലാക്കിയതിനുശേഷം, അവയെ കൈകാര്യം ചെയ്യുന്നതിന് മുമ്പ് നമ്മുടെ ഡാറ്റാസെറ്റിൽ അവ കണ്ടെത്തേണ്ടതുണ്ട്. \n", "`isnull()` ഉം `notnull()` ഉം നിങ്ങളുടെ പ്രധാന ശൂന്യ ഡാറ്റ കണ്ടെത്തൽ രീതികളാണ്. ഇരുവരും നിങ്ങളുടെ ഡാറ്റയിൽ ബൂളിയൻ മാസ്കുകൾ നൽകുന്നു.\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true, "id": "e-vFp5lvgRsC", "trusted": false }, "outputs": [], "source": [ "example3 = pd.Series([0, np.nan, '', None])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "1XdaJJ7PgRsC", "outputId": "92fc363a-1874-471f-846d-f4f9ce1f51d0", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 True\n", "2 False\n", "3 True\n", "dtype: bool" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example3.isnull()" ] }, { "cell_type": "markdown", "metadata": { "id": "PaSZ0SQygRsC" }, "source": [ "ഫലത്തെ ശ്രദ്ധാപൂർവ്വം നോക്കൂ. അതിൽ എന്തെങ്കിലും നിങ്ങളെ അത്ഭുതപ്പെടുത്തുന്നുണ്ടോ? `0` ഒരു ഗണിത ശൂന്യമാണ് എങ്കിലും, അത് പൂർണ്ണസംഖ്യയായി പാൻഡാസ് പരിഗണിക്കുന്നു. `''` കുറച്ച് സൂക്ഷ്മമാണ്. സെക്ഷൻ 1-ൽ ഞങ്ങൾ ഇത് ശൂന്യമായ സ്ട്രിംഗ് മൂല്യത്തെ പ്രതിനിധീകരിക്കാൻ ഉപയോഗിച്ചിരുന്നെങ്കിലും, പാൻഡാസ് കണക്കിലെടുത്താൽ ഇത് ഒരു സ്ട്രിംഗ് ഒബ്ജക്റ്റാണ്, ശൂന്യത്തിന്റെ പ്രതിനിധാനം അല്ല.\n", "\n", "ഇപ്പോൾ, ഇത് മറിച്ച്, നിങ്ങൾ പ്രായോഗികമായി ഉപയോഗിക്കുന്ന രീതിയിൽ ഈ രീതികൾ ഉപയോഗിക്കാം. ബൂളിയൻ മാസ്കുകൾ നേരിട്ട് ``Series`` അല്ലെങ്കിൽ ``DataFrame`` ഇൻഡക്സ് ആയി ഉപയോഗിക്കാം, ഇത് വേർതിരിച്ചെടുത്ത നഷ്ടപ്പെട്ട (അഥവാ നിലവിലുള്ള) മൂല്യങ്ങളുമായി പ്രവർത്തിക്കുമ്പോൾ ഉപകാരപ്രദമാണ്.\n", "\n", "നഷ്ടപ്പെട്ട മൂല്യങ്ങളുടെ മൊത്തം എണ്ണം അറിയാൻ, `isnull()` രീതിയാൽ സൃഷ്ടിച്ച മാസ്കിൽ ഒരു സംഖ്യ ചെയ്യുക മതി.\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "JCcQVoPkHDUv", "outputId": "001daa72-54f8-4bd5-842a-4df627a79d4d" }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example3.isnull().sum()" ] }, { "cell_type": "markdown", "metadata": { "id": "PlBqEo3mgRsC" }, "source": [ "### വ്യായാമം:\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true, "id": "ggDVf5uygRsD", "trusted": false }, "outputs": [], "source": [ "# Try running example3[example3.notnull()].\n", "# Before you do so, what do you expect to see?\n" ] }, { "cell_type": "markdown", "metadata": { "id": "D_jWN7mHgRsD" }, "source": [ "**പ്രധാനപ്പെട്ട കാര്യങ്ങൾ**: DataFrames-ൽ `isnull()` ഉം `notnull()` ഉം ഉപയോഗിക്കുമ്പോൾ ഇരുവരും സമാനമായ ഫലങ്ങൾ നൽകുന്നു: അവ ഫലങ്ങളും ആ ഫലങ്ങളുടെ ഇൻഡക്സും കാണിക്കുന്നു, ഇത് നിങ്ങളുടെ ഡാറ്റയെ കൈകാര്യം ചെയ്യുമ്പോൾ വളരെ സഹായകമായിരിക്കും.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "BvnoojWsgRr4" }, "source": [ "### നഷ്ടമായ ഡാറ്റ കൈകാര്യം ചെയ്യൽ\n", "\n", "> **പഠന ലക്ഷ്യം:** ഈ ഉപവിഭാഗം അവസാനിക്കുമ്പോൾ, DataFrames-ൽ നിന്നുള്ള നൾ മൂല്യങ്ങൾ എപ്പോൾ എങ്ങനെ മാറ്റി വയ്ക്കാമെന്ന് അല്ലെങ്കിൽ നീക്കം ചെയ്യാമെന്ന് നിങ്ങൾ അറിയണം.\n", "\n", "മെഷീൻ ലേണിംഗ് മോഡലുകൾ തന്നെ നഷ്ടമായ ഡാറ്റ കൈകാര്യം ചെയ്യാൻ കഴിയില്ല. അതിനാൽ, ഡാറ്റ മോഡലിലേക്ക് നൽകുന്നതിന് മുമ്പ്, ഈ നഷ്ടമായ മൂല്യങ്ങളെ കൈകാര്യം ചെയ്യേണ്ടതുണ്ട്.\n", "\n", "നഷ്ടമായ ഡാറ്റ എങ്ങനെ കൈകാര്യം ചെയ്യപ്പെടുന്നു എന്നത് സൂക്ഷ്മമായ വ്യാപാരങ്ങൾ ഉൾക്കൊള്ളുന്നു, നിങ്ങളുടെ അന്തിമ വിശകലനത്തെയും യഥാർത്ഥ ലോക ഫലങ്ങളെയും ബാധിക്കാം.\n", "\n", "നഷ്ടമായ ഡാറ്റ കൈകാര്യം ചെയ്യാനുള്ള പ്രധാനമായ രണ്ട് മാർഗ്ഗങ്ങൾ ഉണ്ട്:\n", "\n", "\n", "1. നഷ്ടമായ മൂല്യം അടങ്ങിയ വരി ഒഴിവാക്കുക\n", "2. നഷ്ടമായ മൂല്യം മറ്റൊരു മൂല്യത്തോടെ മാറ്റി വയ്ക്കുക\n", "\n", "ഈ രണ്ട് രീതികളും അവയുടെ ഗുണദോഷങ്ങളും വിശദമായി ചർച്ച ചെയ്യാം.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "3VaYC1TvgRsD" }, "source": [ "### നൾ മൂല്യങ്ങൾ ഒഴിവാക്കൽ\n", "\n", "നമ്മുടെ മോഡലിലേക്ക് നാം നൽകുന്ന ഡാറ്റയുടെ അളവ് അതിന്റെ പ്രകടനത്തെ നേരിട്ട് ബാധിക്കുന്നു. നൾ മൂല്യങ്ങൾ ഒഴിവാക്കുന്നത് ഡാറ്റാപോയിന്റുകളുടെ എണ്ണം കുറയ്ക്കുന്നതും അതിനാൽ ഡാറ്റാസെറ്റിന്റെ വലിപ്പം കുറയ്ക്കുന്നതുമാണ്. അതിനാൽ, ഡാറ്റാസെറ്റ് വളരെ വലിയതായിരിക്കുമ്പോൾ നൾ മൂല്യങ്ങളുള്ള വരികൾ ഒഴിവാക്കുന്നത് ശുപാർശ ചെയ്യപ്പെടുന്നു.\n", "\n", "മറ്റൊരു ഉദാഹരണം, ഒരു പ്രത്യേക വരിയിലോ കോളത്തിലോ വളരെ കുറവായ മൂല്യങ്ങൾ ഉണ്ടാകാം. അപ്പോൾ, അവ ഒഴിവാക്കപ്പെടാം, കാരണം ആ വരിയിലോ കോളത്തിലോ ഡാറ്റയുടെ ഭൂരിഭാഗവും ഇല്ലാത്തതിനാൽ അവ നമ്മുടെ വിശകലനത്തിന് വലിയ മൂല്യം നൽകില്ല.\n", "\n", "കുറഞ്ഞ മൂല്യങ്ങൾ തിരിച്ചറിയുന്നതിന് പുറമേ, pandas `Series`-ലും `DataFrame`-ലും നിന്നുള്ള നൾ മൂല്യങ്ങൾ നീക്കംചെയ്യാൻ സൗകര്യപ്രദമായ മാർഗ്ഗം നൽകുന്നു. ഇത് പ്രവർത്തനത്തിൽ കാണാൻ, നാം `example3`-ലേക്ക് മടങ്ങാം. `DataFrame.dropna()` ഫംഗ്ഷൻ നൾ മൂല്യങ്ങളുള്ള വരികൾ ഒഴിവാക്കുന്നതിൽ സഹായിക്കുന്നു.\n" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7uIvS097gRsD", "outputId": "c13fc117-4ca1-4145-a0aa-42ac89e6e218", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "0 0\n", "2 \n", "dtype: object" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example3 = example3.dropna()\n", "example3" ] }, { "cell_type": "markdown", "metadata": { "id": "hil2cr64gRsD" }, "source": [ "`example3[example3.notnull()]` ൽ നിന്നുള്ള നിങ്ങളുടെ ഔട്ട്പുട്ട് ഇതുപോലെയിരിക്കണം എന്ന് ശ്രദ്ധിക്കുക. ഇവിടെ വ്യത്യാസം എന്തെന്നാൽ, മസ്ക് ചെയ്ത മൂല്യങ്ങളിൽ മാത്രം ഇൻഡക്സ് ചെയ്യുന്നതിന് പകരം, `dropna` ആ മൂല്യങ്ങൾ `Series` `example3` ൽ നിന്ന് നീക്കം ചെയ്തിരിക്കുന്നു.\n", "\n", "DataFrames-ന് രണ്ട് ഡൈമെൻഷനുകൾ ഉള്ളതിനാൽ, ഡാറ്റ നീക്കം ചെയ്യാനുള്ള കൂടുതൽ ഓപ്ഷനുകൾ അവയ്ക്ക് ലഭ്യമാണ്.\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 142 }, "id": "an-l74sPgRsE", "outputId": "340876a0-63ad-40f6-bd54-6240cdae50ab", "trusted": false }, "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", "
012
01.0NaN7
12.05.08
2NaN6.09
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 1.0 NaN 7\n", "1 2.0 5.0 8\n", "2 NaN 6.0 9" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example4 = pd.DataFrame([[1, np.nan, 7], \n", " [2, 5, 8], \n", " [np.nan, 6, 9]])\n", "example4" ] }, { "cell_type": "markdown", "metadata": { "id": "66wwdHZrgRsE" }, "source": [ "(നിങ്ങൾ ശ്രദ്ധിച്ചിട്ടുണ്ടോ pandas രണ്ട് കോളങ്ങൾ `NaN`-കൾ ഉൾപ്പെടുത്തുന്നതിനായി ഫ്ലോട്ടുകളായി അപ്കാസ്റ്റ് ചെയ്യുന്നത്?)\n", "\n", "`DataFrame`-ൽ നിന്ന് ഒരു മൂല്യം മാത്രം നീക്കം ചെയ്യാൻ കഴിയില്ല, അതിനാൽ നിങ്ങൾക്ക് പൂർണ്ണമായ വരികളും കോളങ്ങളുമാണ് നീക്കം ചെയ്യേണ്ടത്. നിങ്ങൾ ചെയ്യുന്നതിനെ ആശ്രയിച്ച്, നിങ്ങൾക്ക് ഒന്നോ മറ്റൊന്നോ ചെയ്യാൻ ആഗ്രഹിക്കാം, അതിനാൽ pandas ഇരുവിധ ഓപ്ഷനുകളും നൽകുന്നു. ഡാറ്റാ സയൻസിൽ, കോളങ്ങൾ സാധാരണയായി വേരിയബിളുകളെ പ്രതിനിധീകരിക്കുകയും വരികൾ നിരീക്ഷണങ്ങളെ പ്രതിനിധീകരിക്കുകയും ചെയ്യുന്നതിനാൽ, നിങ്ങൾക്ക് ഡാറ്റയുടെ വരികൾ നീക്കം ചെയ്യാനുള്ള സാധ്യത കൂടുതലാണ്; `dropna()`-യുടെ ഡിഫോൾട്ട് ക്രമീകരണം ഏതെങ്കിലും നൾ മൂല്യങ്ങൾ ഉള്ള എല്ലാ വരികളും നീക്കം ചെയ്യുകയാണ്:\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 80 }, "id": "jAVU24RXgRsE", "outputId": "0b5e5aee-7187-4d3f-b583-a44136ae5f80", "trusted": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
012
12.05.08
\n", "
" ], "text/plain": [ " 0 1 2\n", "1 2.0 5.0 8" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example4.dropna()" ] }, { "cell_type": "markdown", "metadata": { "id": "TrQRBuTDgRsE" }, "source": [ "ആവശ്യമായാൽ, നിങ്ങൾ കോളങ്ങളിലെ NA മൂല്യങ്ങൾ ഒഴിവാക്കാം. അതിന് `axis=1` ഉപയോഗിക്കുക:\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 142 }, "id": "GrBhxu9GgRsE", "outputId": "ff4001f3-2e61-4509-d60e-0093d1068437", "trusted": false }, "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", "
2
07
18
29
\n", "
" ], "text/plain": [ " 2\n", "0 7\n", "1 8\n", "2 9" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example4.dropna(axis='columns')" ] }, { "cell_type": "markdown", "metadata": { "id": "KWXiKTfMgRsF" }, "source": [ "കുറഞ്ഞ ഡാറ്റാസെറ്റുകളിൽ നിങ്ങൾ സൂക്ഷിക്കാനാഗ്രഹിക്കുന്ന നിരവധി ഡാറ്റ നഷ്ടപ്പെടാൻ ഇത് കാരണമാകാമെന്ന് ശ്രദ്ധിക്കുക. എന്നാൽ നിങ്ങൾക്ക് ചില നിരകളും കോളങ്ങളുമാണ് മാത്രം ഒഴിവാക്കേണ്ടത്, അവയിൽ പലതും അല്ലെങ്കിൽ മുഴുവൻ നൾ മൂല്യങ്ങളാണെങ്കിൽ? നിങ്ങൾക്ക് `dropna`-യിൽ `how`യും `thresh` പാരാമീറ്ററുകളും ഉപയോഗിച്ച് ആ ക്രമീകരണങ്ങൾ വ്യക്തമാക്കാം.\n", "\n", "ഡീഫോൾട്ടായി, `how='any'` ആണ് (നിങ്ങൾക്ക് സ്വയം പരിശോധിക്കാനോ ഈ മെത്തഡിന് മറ്റ് പാരാമീറ്ററുകൾ എന്തൊക്കെയാണെന്ന് കാണാനോ ആഗ്രഹമുണ്ടെങ്കിൽ, ഒരു കോഡ് സെല്ലിൽ `example4.dropna?` റൺ ചെയ്യുക). അല്ലെങ്കിൽ, മുഴുവൻ നൾ മൂല്യങ്ങളുള്ള നിരകളും കോളങ്ങളുമാണ് മാത്രം ഒഴിവാക്കേണ്ടത് എങ്കിൽ `how='all'` എന്ന് വ്യക്തമാക്കാം. അടുത്ത വ്യായാമത്തിൽ ഇത് പ്രവർത്തനത്തിൽ കാണാൻ നമ്മുടെ ഉദാഹരണ `DataFrame` വിപുലീകരിക്കാം.\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 142 }, "id": "Bcf_JWTsgRsF", "outputId": "72e0b1b8-52fa-4923-98ce-b6fbed6e44b1", "trusted": false }, "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", "
0123
01.0NaN7NaN
12.05.08NaN
2NaN6.09NaN
\n", "
" ], "text/plain": [ " 0 1 2 3\n", "0 1.0 NaN 7 NaN\n", "1 2.0 5.0 8 NaN\n", "2 NaN 6.0 9 NaN" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example4[3] = np.nan\n", "example4" ] }, { "cell_type": "markdown", "metadata": { "id": "pNZer7q9JPNC" }, "source": [ "> പ്രധാനപ്പെട്ട കാര്യങ്ങൾ: \n", "1. ഡാറ്റാസെറ്റ് വലിയതായിരിക്കുമ്പോഴേ മാത്രം നൾ മൂല്യങ്ങൾ ഒഴിവാക്കുന്നത് നല്ല ആശയമാണ്.\n", "2. ഒരു പൂർണ്ണമായ വരി അല്ലെങ്കിൽ കോളം അവരുടെ ഡാറ്റയുടെ ഭൂരിഭാഗം നഷ്ടപ്പെട്ടിട്ടുണ്ടെങ്കിൽ അവ ഒഴിവാക്കാം.\n", "3. `DataFrame.dropna(axis=)` മെത്തഡ് നൾ മൂല്യങ്ങൾ ഒഴിവാക്കാൻ സഹായിക്കുന്നു. `axis` ആർഗ്യുമെന്റ് വരികളെയാണോ കോളങ്ങളെയാണോ ഒഴിവാക്കേണ്ടതെന്ന് സൂചിപ്പിക്കുന്നു.\n", "4. `how` ആർഗ്യുമെന്റും ഉപയോഗിക്കാം. ഡിഫോൾട്ടായി ഇത് `any` ആയി സജ്ജീകരിച്ചിരിക്കുന്നു. അതിനാൽ, ഏതെങ്കിലും നൾ മൂല്യങ്ങൾ ഉള്ള വരികളെയോ കോളങ്ങളെയോ മാത്രമേ ഇത് ഒഴിവാക്കൂ. എല്ലാ മൂല്യങ്ങളും നൾ ആയിരിക്കുന്ന വരികളെയോ കോളങ്ങളെയോ മാത്രം ഒഴിവാക്കണമെങ്കിൽ ഇത് `all` ആയി സജ്ജീകരിക്കാം.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "oXXSfQFHgRsF" }, "source": [ "### വ്യായാമം:\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true, "id": "ExUwQRxpgRsF", "trusted": false }, "outputs": [], "source": [ "# How might you go about dropping just column 3?\n", "# Hint: remember that you will need to supply both the axis parameter and the how parameter.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "38kwAihWgRsG" }, "source": [ "`thresh` പാരാമീറ്റർ നിങ്ങൾക്ക് കൂടുതൽ സൂക്ഷ്മ നിയന്ത്രണം നൽകുന്നു: ഒരു വരി അല്ലെങ്കിൽ കോളം നിലനിർത്താൻ ആവശ്യമായ *നോൺ-നൾ* മൂല്യങ്ങളുടെ എണ്ണം നിങ്ങൾ സജ്ജമാക്കുന്നു:\n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 80 }, "id": "M9dCNMaagRsG", "outputId": "8093713a-54d2-4e54-c73f-4eea315cb6f2", "trusted": false }, "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", "
0123
12.05.08NaN
\n", "
" ], "text/plain": [ " 0 1 2 3\n", "1 2.0 5.0 8 NaN" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example4.dropna(axis='rows', thresh=3)" ] }, { "cell_type": "markdown", "metadata": { "id": "fmSFnzZegRsG" }, "source": [ "ഇവിടെ, ആദ്യവും അവസാനവും വരി ഒഴിവാക്കിയിട്ടുണ്ട്, കാരണം അവയിൽ രണ്ട് മാത്രമേ നൺ-നൾ മൂല്യങ്ങൾ ഉള്ളൂ.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "mCcxLGyUgRsG" }, "source": [ "### നൾ മൂല്യങ്ങൾ പൂരിപ്പിക്കൽ\n", "\n", "കഴിഞ്ഞ മൂല്യങ്ങൾ സാധുവായവയാകാവുന്നവയാൽ പൂരിപ്പിക്കുന്നത് ചിലപ്പോൾ ബുദ്ധിമുട്ടില്ല. നൾ മൂല്യങ്ങൾ പൂരിപ്പിക്കാൻ ചില സാങ്കേതിക വിദ്യകൾ ഉണ്ട്. ആദ്യത്തേത് ഡൊമെയ്ൻ നോളജ് (ഡാറ്റാസെറ്റിന്റെ അടിസ്ഥാനമായ വിഷയത്തെക്കുറിച്ചുള്ള അറിവ്) ഉപയോഗിച്ച് നഷ്ടപ്പെട്ട മൂല്യങ്ങളെ ഏതെങ്കിലും വിധത്തിൽ ഏകദേശം കണക്കാക്കുക എന്നതാണ്.\n", "\n", "നിങ്ങൾക്ക് `isnull` ഉപയോഗിച്ച് ഇത് സ്ഥലത്ത് തന്നെ ചെയ്യാം, പക്ഷേ ഇത് വളരെ കഠിനമായിരിക്കാം, പ്രത്യേകിച്ച് പൂരിപ്പിക്കേണ്ട മൂല്യങ്ങൾ 많으면. ഡാറ്റാ സയൻസിൽ ഇത് വളരെ സാധാരണമായ ഒരു ജോലി ആയതിനാൽ, pandas `fillna` നൽകുന്നു, ഇത് നഷ്ടപ്പെട്ട മൂല്യങ്ങൾ നിങ്ങൾ തിരഞ്ഞെടുക്കുന്ന മൂല്യത്തോടെ മാറ്റിയുള്ള `Series` അല്ലെങ്കിൽ `DataFrame` ന്റെ ഒരു പകർപ്പ് നൽകുന്നു. ഇത് പ്രായോഗികമായി എങ്ങനെ പ്രവർത്തിക്കുന്നുവെന്ന് കാണാൻ മറ്റൊരു ഉദാഹരണ `Series` സൃഷ്ടിക്കാം.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "CE8S7louLezV" }, "source": [ "### വർഗ്ഗീയ ഡാറ്റ (സംഖ്യാത്മകമല്ലാത്തത്)\n", "ആദ്യം നമുക്ക് സംഖ്യാത്മകമല്ലാത്ത ഡാറ്റ പരിഗണിക്കാം. ഡാറ്റാസെറ്റുകളിൽ, നമുക്ക് വർഗ്ഗീയ ഡാറ്റ ഉള്ള കോളങ്ങൾ ഉണ്ടാകുന്നു. ഉദാ. ലിംഗം, സത്യം അല്ലെങ്കിൽ തെറ്റ് തുടങ്ങിയവ.\n", "\n", "ഇവയിൽ പലപ്പോഴും, നാം നഷ്ടപ്പെട്ട മൂല്യങ്ങളെ കോളത്തിന്റെ `mode` ഉപയോഗിച്ച് പൂരിപ്പിക്കുന്നു. ഉദാഹരണത്തിന്, നമുക്ക് 100 ഡാറ്റ പോയിന്റുകൾ ഉണ്ടെന്ന് കരുതുക, അതിൽ 90 സത്യം എന്ന് പറഞ്ഞിട്ടുണ്ട്, 8 തെറ്റ് എന്ന് പറഞ്ഞിട്ടുണ്ട്, 2 പൂരിപ്പിച്ചിട്ടില്ല. അപ്പോൾ, നാം ആ 2 സത്യം എന്ന് പൂരിപ്പിക്കാം, മുഴുവൻ കോളം പരിഗണിച്ചുകൊണ്ട്.\n", "\n", "വീണ്ടും, ഇവിടെ നാം ഡൊമെയ്ൻ അറിവ് ഉപയോഗിക്കാം. മോഡ് ഉപയോഗിച്ച് പൂരിപ്പിക്കുന്ന ഒരു ഉദാഹരണം പരിഗണിക്കാം.\n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "id": "MY5faq4yLdpQ", "outputId": "19ab472e-1eed-4de8-f8a7-db2a3af3cb1a" }, "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", "
012
012True
134None
256False
378True
4910True
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 1 2 True\n", "1 3 4 None\n", "2 5 6 False\n", "3 7 8 True\n", "4 9 10 True" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill_with_mode = pd.DataFrame([[1,2,\"True\"],\n", " [3,4,None],\n", " [5,6,\"False\"],\n", " [7,8,\"True\"],\n", " [9,10,\"True\"]])\n", "\n", "fill_with_mode" ] }, { "cell_type": "markdown", "metadata": { "id": "MLAoMQOfNPlA" }, "source": [ "ഇപ്പോൾ, ആദ്യം `None` മൂല്യം മോഡോടെ പൂരിപ്പിക്കുന്നതിന് മുമ്പ് മോഡ് കണ്ടെത്താം.\n" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "WKy-9Y2tN5jv", "outputId": "8da9fa16-e08c-447e-dea1-d4b1db2feebf" }, "outputs": [ { "data": { "text/plain": [ "True 3\n", "False 1\n", "Name: 2, dtype: int64" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill_with_mode[2].value_counts()" ] }, { "cell_type": "markdown", "metadata": { "id": "6iNz_zG_OKrx" }, "source": [ "അതിനാൽ, നാം None നെ True ആയി മാറ്റും\n" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "id": "TxPKteRvNPOs" }, "outputs": [], "source": [ "fill_with_mode[2].fillna('True',inplace=True)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "id": "tvas7c9_OPWE", "outputId": "ec3c8e44-d644-475e-9e22-c65101965850" }, "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", "
012
012True
134True
256False
378True
4910True
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 1 2 True\n", "1 3 4 True\n", "2 5 6 False\n", "3 7 8 True\n", "4 9 10 True" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill_with_mode" ] }, { "cell_type": "markdown", "metadata": { "id": "SktitLxxOR16" }, "source": [ "നാം കാണുന്ന പോലെ, നൾ മൂല്യം മാറ്റി വച്ചിട്ടുണ്ട്. പറയേണ്ടതില്ല, നാം `'True'` എന്നതിന്റെ പകരം എന്തും എഴുതിയിരുന്നാലും അത് മാറ്റി വച്ചേനെ.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "heYe1I0dOmQ_" }, "source": [ "### സംഖ്യാത്മക ഡാറ്റ \n", "ഇപ്പോൾ, സംഖ്യാത്മക ഡാറ്റയിലേക്ക് വരാം. ഇവിടെ, നഷ്ടപ്പെട്ട മൂല്യങ്ങൾ മാറ്റിവെക്കാനുള്ള രണ്ട് സാധാരണ മാർഗ്ഗങ്ങൾ ഉണ്ട്: \n", "\n", "1. വരിയുടെ മധ്യക (Median) ഉപയോഗിച്ച് മാറ്റിവെക്കുക \n", "2. വരിയുടെ ശരാശരി (Mean) ഉപയോഗിച്ച് മാറ്റിവെക്കുക \n", "\n", "വെളിപ്പെടുത്താത്ത ഡാറ്റയിൽ ഔട്ട്‌ലൈയർമാർ ഉള്ളപ്പോൾ, മധ്യക ഉപയോഗിച്ച് മാറ്റിവെക്കുന്നു. കാരണം, മധ്യക ഔട്ട്‌ലൈയർമാർക്ക് പ്രതിരോധശേഷിയുള്ളതാണ്. \n", "\n", "ഡാറ്റ സാധാരണവത്കരിച്ചിരിക്കുമ്പോൾ, ശരാശരി ഉപയോഗിക്കാം, കാരണം ആ സാഹചര്യത്തിൽ ശരാശരിയും മധ്യകവും വളരെ അടുത്തിരിക്കും. \n", "\n", "ആദ്യം, സാധാരണമായി വിതരണം ചെയ്ത ഒരു കോളം എടുത്ത്, ആ കോളത്തിന്റെ ശരാശരിയാൽ നഷ്ടപ്പെട്ട മൂല്യം പൂരിപ്പിക്കാം.\n" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "id": "09HM_2feOj5Y", "outputId": "7e309013-9acb-411c-9b06-4de795bbeeff" }, "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", "
012
0-2.001
1-1.023
2NaN45
31.067
42.089
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 -2.0 0 1\n", "1 -1.0 2 3\n", "2 NaN 4 5\n", "3 1.0 6 7\n", "4 2.0 8 9" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill_with_mean = pd.DataFrame([[-2,0,1],\n", " [-1,2,3],\n", " [np.nan,4,5],\n", " [1,6,7],\n", " [2,8,9]])\n", "\n", "fill_with_mean" ] }, { "cell_type": "markdown", "metadata": { "id": "ka7-wNfzSxbx" }, "source": [ "നിരയുടെ ശരാശരി ആണ്\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "XYtYEf5BSxFL", "outputId": "68a78d18-f0e5-4a9a-a959-2c3676a57c70" }, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.mean(fill_with_mean[0])" ] }, { "cell_type": "markdown", "metadata": { "id": "oBSRGxKRS39K" }, "source": [ "സാധാരണ മൂല്യത്തോടെ പൂരിപ്പിക്കൽ\n" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "id": "FzncQLmuS5jh", "outputId": "00f74fff-01f4-4024-c261-796f50f01d2e" }, "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", "
012
0-2.001
1-1.023
20.045
31.067
42.089
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 -2.0 0 1\n", "1 -1.0 2 3\n", "2 0.0 4 5\n", "3 1.0 6 7\n", "4 2.0 8 9" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill_with_mean[0].fillna(np.mean(fill_with_mean[0]),inplace=True)\n", "fill_with_mean" ] }, { "cell_type": "markdown", "metadata": { "id": "CwpVFCrPTC5z" }, "source": [ "നാം കാണുന്ന പോലെ, നഷ്ടമായ മൂല്യം അതിന്റെ ശരാശരിയാൽ മാറ്റിസ്ഥാപിച്ചിരിക്കുന്നു.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "jIvF13a1i00Z" }, "source": [ "ഇപ്പോൾ നമുക്ക് മറ്റൊരു ഡാറ്റാഫ്രെയിം പരീക്ഷിക്കാം, ഈ തവണ നാം None മൂല്യങ്ങളെ കോളത്തിന്റെ മധ്യസ്ഥ മൂല്യത്തോടെ മാറ്റിസ്ഥാപിക്കും.\n" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "id": "DA59Bqo3jBYZ", "outputId": "85dae6ec-7394-4c36-fda0-e04769ec4a32" }, "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", "
012
0-20.01
1-12.03
20NaN5
316.07
428.09
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 -2 0.0 1\n", "1 -1 2.0 3\n", "2 0 NaN 5\n", "3 1 6.0 7\n", "4 2 8.0 9" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill_with_median = pd.DataFrame([[-2,0,1],\n", " [-1,2,3],\n", " [0,np.nan,5],\n", " [1,6,7],\n", " [2,8,9]])\n", "\n", "fill_with_median" ] }, { "cell_type": "markdown", "metadata": { "id": "mM1GpXYmjHnc" }, "source": [ "രണ്ടാം കോളത്തിന്റെ മധ്യകമാണ്\n" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "uiDy5v3xjHHX", "outputId": "564b6b74-2004-4486-90d4-b39330a64b88" }, "outputs": [ { "data": { "text/plain": [ "4.0" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill_with_median[1].median()" ] }, { "cell_type": "markdown", "metadata": { "id": "z9PLF75Jj_1s" }, "source": [ "മധ്യകത്തോടെ പൂരിപ്പിക്കൽ\n" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "id": "lFKbOxCMkBbg", "outputId": "a8bd18fb-2765-47d4-e5fe-e965f57ed1f4" }, "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", "
012
0-20.01
1-12.03
204.05
316.07
428.09
\n", "
" ], "text/plain": [ " 0 1 2\n", "0 -2 0.0 1\n", "1 -1 2.0 3\n", "2 0 4.0 5\n", "3 1 6.0 7\n", "4 2 8.0 9" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fill_with_median[1].fillna(fill_with_median[1].median(),inplace=True)\n", "fill_with_median" ] }, { "cell_type": "markdown", "metadata": { "id": "8JtQ53GSkKWC" }, "source": [ "നാം കാണുന്ന പോലെ, NaN മൂല്യം കോളത്തിന്റെ മധ്യകത്തിൽ മാറ്റിയിട്ടുണ്ട്.\n" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0ybtWLDdgRsG", "outputId": "b8c238ef-6024-4ee2-be2b-aa1f0fcac61d", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "a 1.0\n", "b NaN\n", "c 2.0\n", "d NaN\n", "e 3.0\n", "dtype: float64" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example5 = pd.Series([1, np.nan, 2, None, 3], index=list('abcde'))\n", "example5" ] }, { "cell_type": "markdown", "metadata": { "id": "yrsigxRggRsH" }, "source": [ "നിങ്ങൾ എല്ലാ നൾ എൻട്രികളും ഒരു ഏക മൂല്യത്തോടെ പൂരിപ്പിക്കാം, ഉദാഹരണത്തിന് `0`:\n" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KXMIPsQdgRsH", "outputId": "aeedfa0a-a421-4c2f-cb0d-183ce8f0c91d", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "a 1.0\n", "b 0.0\n", "c 2.0\n", "d 0.0\n", "e 3.0\n", "dtype: float64" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example5.fillna(0)" ] }, { "cell_type": "markdown", "metadata": { "id": "RRlI5f_hkfKe" }, "source": [ "> പ്രധാനപ്പെട്ട കാര്യങ്ങൾ:\n", "1. ഡാറ്റ കുറവായിരിക്കുകയോ നഷ്ടപ്പെട്ട ഡാറ്റ പൂരിപ്പിക്കാൻ ഒരു തന്ത്രമുണ്ടായിരിക്കുകയോ ചെയ്താൽ മാത്രമേ നഷ്ടപ്പെട്ട മൂല്യങ്ങൾ പൂരിപ്പിക്കേണ്ടതുള്ളൂ.\n", "2. നഷ്ടപ്പെട്ട മൂല്യങ്ങൾ ഏകദേശം കണക്കുകൂട്ടാൻ ഡൊമെയ്ൻ അറിവ് ഉപയോഗിക്കാം.\n", "3. വർഗ്ഗീയ ഡാറ്റയ്ക്ക്, സാധാരണയായി, നഷ്ടപ്പെട്ട മൂല്യങ്ങൾ ആ കോളത്തിന്റെ മോഡോടെ പകരം വയ്ക്കുന്നു.\n", "4. സംഖ്യാത്മക ഡാറ്റയ്ക്ക്, സാധാരണയായി, നഷ്ടപ്പെട്ട മൂല്യങ്ങൾ ശരാശരി (സാധാരണവത്കരിച്ച ഡാറ്റാസെറ്റുകൾക്കായി) അല്ലെങ്കിൽ കോളത്തിന്റെ മധ്യകമാനത്തോടെ പൂരിപ്പിക്കുന്നു.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "FI9MmqFJgRsH" }, "source": [ "### വ്യായാമം:\n" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": true, "id": "af-ezpXdgRsH", "trusted": false }, "outputs": [], "source": [ "# What happens if you try to fill null values with a string, like ''?\n" ] }, { "cell_type": "markdown", "metadata": { "id": "kq3hw1kLgRsI" }, "source": [ "നിങ്ങൾ **ഫോർവേഡ്-ഫിൽ** ചെയ്യാൻ കഴിയും, അതായത് ഒരു നാൾ മൂല്യം നിറയ്ക്കാൻ അവസാനത്തെ സാധുവായ മൂല്യം ഉപയോഗിക്കുക:\n" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "vO3BuNrggRsI", "outputId": "e2bc591b-0b48-4e88-ee65-754f2737c196", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "a 1.0\n", "b 1.0\n", "c 2.0\n", "d 2.0\n", "e 3.0\n", "dtype: float64" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example5.fillna(method='ffill')" ] }, { "cell_type": "markdown", "metadata": { "id": "nDXeYuHzgRsI" }, "source": [ "നീക്കം ചെയ്യാത്ത ഒരു മൂല്യം പൂരിപ്പിക്കാൻ അടുത്ത സാധുവായ മൂല്യം പിന്‍വശത്തേക്ക് പ്രചരിപ്പിക്കാൻ നിങ്ങൾക്ക് **ബാക്ക്-ഫിൽ** ചെയ്യാനും കഴിയും:\n" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "4M5onHcEgRsI", "outputId": "8f32b185-40dd-4a9f-bd85-54d6b6a414fe", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "a 1.0\n", "b 2.0\n", "c 2.0\n", "d 3.0\n", "e 3.0\n", "dtype: float64" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example5.fillna(method='bfill')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "id": "MbBzTom5gRsI" }, "source": [ "നിങ്ങൾക്ക് തോന്നിയതുപോലെ, ഇത് DataFrames-ഉം ഒരുപോലെ പ്രവർത്തിക്കുന്നു, പക്ഷേ നിങ്ങൾ നുള്ള് മൂല്യങ്ങൾ പൂരിപ്പിക്കാൻ ഒരു `axis` നിർദ്ദിഷ്ടമാക്കാനും കഴിയും:\n" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 142 }, "id": "aRpIvo4ZgRsI", "outputId": "905a980a-a808-4eca-d0ba-224bd7d85955", "trusted": false }, "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", "
0123
01.0NaN7NaN
12.05.08NaN
2NaN6.09NaN
\n", "
" ], "text/plain": [ " 0 1 2 3\n", "0 1.0 NaN 7 NaN\n", "1 2.0 5.0 8 NaN\n", "2 NaN 6.0 9 NaN" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example4" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 142 }, "id": "VM1qtACAgRsI", "outputId": "71f2ad28-9b4e-4ff4-f5c3-e731eb489ade", "trusted": false }, "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", "
0123
01.01.07.07.0
12.05.08.08.0
2NaN6.09.09.0
\n", "
" ], "text/plain": [ " 0 1 2 3\n", "0 1.0 1.0 7.0 7.0\n", "1 2.0 5.0 8.0 8.0\n", "2 NaN 6.0 9.0 9.0" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example4.fillna(method='ffill', axis=1)" ] }, { "cell_type": "markdown", "metadata": { "id": "ZeMc-I1EgRsI" }, "source": [ "മുൻവില ലഭ്യമല്ലാത്തപ്പോൾ ഫോർവേഡ്-ഫില്ലിംഗിനായി, നൾ മൂല്യം നിലനിൽക്കുന്നു എന്ന് ശ്രദ്ധിക്കുക.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "eeAoOU0RgRsJ" }, "source": [ "### വ്യായാമം:\n" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": true, "id": "e8S-CjW8gRsJ", "trusted": false }, "outputs": [], "source": [ "# What output does example4.fillna(method='bfill', axis=1) produce?\n", "# What about example4.fillna(method='ffill') or example4.fillna(method='bfill')?\n", "# Can you think of a longer code snippet to write that can fill all of the null values in example4?\n" ] }, { "cell_type": "markdown", "metadata": { "id": "YHgy0lIrgRsJ" }, "source": [ "`fillna` ഉപയോഗിക്കുന്നതിൽ നിങ്ങൾ സൃഷ്ടിപരമായിരിക്കാം. ഉദാഹരണത്തിന്, വീണ്ടും `example4` നോക്കാം, പക്ഷേ ഈ തവണ `DataFrame`-യിലെ എല്ലാ മൂല്യങ്ങളുടെ ശരാശരിയാൽ നഷ്ടപ്പെട്ട മൂല്യങ്ങൾ പൂരിപ്പിക്കാം:\n" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 142 }, "id": "OtYVErEygRsJ", "outputId": "708b1e67-45ca-44bf-a5ee-8b2de09ece73", "trusted": false }, "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", "
0123
01.05.57NaN
12.05.08NaN
21.56.09NaN
\n", "
" ], "text/plain": [ " 0 1 2 3\n", "0 1.0 5.5 7 NaN\n", "1 2.0 5.0 8 NaN\n", "2 1.5 6.0 9 NaN" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example4.fillna(example4.mean())" ] }, { "cell_type": "markdown", "metadata": { "id": "zpMvCkLSgRsJ" }, "source": [ "നോട്ടീസ് ചെയ്യുക, കോളം 3 ഇപ്പോഴും മൂല്യമില്ല: ഡിഫോൾട്ട് ദിശ മൂല്യങ്ങൾ വരി അനുസരിച്ച് പൂരിപ്പിക്കുകയാണ്.\n", "\n", "> **പ്രധാനപ്പെട്ട കാര്യം:** നിങ്ങളുടെ ഡാറ്റാസെറ്റുകളിൽ കാണാനാകാത്ത മൂല്യങ്ങളെ കൈകാര്യം ചെയ്യാനുള്ള പല മാർഗ്ഗങ്ങളും ഉണ്ട്. നിങ്ങൾ ഉപയോഗിക്കുന്ന പ്രത്യേക തന്ത്രം (അവ നീക്കം ചെയ്യുക, മാറ്റിസ്ഥാപിക്കുക, അല്ലെങ്കിൽ എങ്ങനെ മാറ്റിസ്ഥാപിക്കാമെന്ന്) ആ ഡാറ്റയുടെ പ്രത്യേകതകൾ അനുസരിച്ച് നിർണ്ണയിക്കപ്പെടണം. നിങ്ങൾ കൂടുതൽ ഡാറ്റാസെറ്റുകൾ കൈകാര്യം ചെയ്ത് ഇടപഴകുമ്പോൾ കാണാനാകാത്ത മൂല്യങ്ങളെ എങ്ങനെ കൈകാര്യം ചെയ്യാമെന്ന് നിങ്ങൾക്ക് മികച്ച ബോധം വികസിപ്പിക്കും.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "bauDnESIl9FH" }, "source": [ "### വർഗ്ഗീയ ഡാറ്റ എൻകോഡിംഗ്\n", "\n", "മെഷീൻ ലേണിംഗ് മോഡലുകൾക്ക് സംഖ്യകളും ഏതെങ്കിലും രൂപത്തിലുള്ള സംഖ്യാത്മക ഡാറ്റയും മാത്രമേ കൈകാര്യം ചെയ്യാൻ കഴിയൂ. ഒരു \"അതെ\"യും \"ഇല്ല\"യും തമ്മിലുള്ള വ്യത്യാസം അവയ്ക്ക് തിരിച്ചറിയാൻ കഴിയില്ല, പക്ഷേ 0നും 1നും തമ്മിലുള്ള വ്യത്യാസം തിരിച്ചറിയാൻ കഴിയും. അതിനാൽ, നഷ്ടപ്പെട്ട മൂല്യങ്ങൾ പൂരിപ്പിച്ചതിന് ശേഷം, മോഡൽ മനസ്സിലാക്കാൻ വേണ്ടി വർഗ്ഗീയ ഡാറ്റയെ ഏതെങ്കിലും സംഖ്യാത്മക രൂപത്തിലേക്ക് എൻകോഡ് ചെയ്യേണ്ടതുണ്ട്.\n", "\n", "എൻകോഡിംഗ് രണ്ട് രീതികളിൽ ചെയ്യാം. അവയെക്കുറിച്ച് നാം അടുത്തതായി ചർച്ച ചെയ്യാം.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "uDq9SxB7mu5i" }, "source": [ "**ലേബൽ എൻകോഡിംഗ്**\n", "\n", "\n", "ലേബൽ എൻകോഡിംഗ് അടിസ്ഥാനപരമായി ഓരോ വിഭാഗത്തെയും ഒരു സംഖ്യയിലേക്ക് മാറ്റുകയാണ്. ഉദാഹരണത്തിന്, എങ്കിൽ നമ്മൾക്ക് ഒരു എയർലൈൻ യാത്രക്കാരുടെ ഡാറ്റാസെറ്റ് ഉണ്ടെന്നും അവരുടേതായ ക്ലാസ് താഴെപ്പറയുന്നവയിൽ ഒന്നാണെന്നും കരുതുക ['ബിസിനസ് ക്ലാസ്', 'ഇക്കണോമി ക്ലാസ്', 'ഫസ്റ്റ് ക്ലാസ്']. ഇതിൽ ലേബൽ എൻകോഡിംഗ് ചെയ്താൽ ഇത് [0,1,2] ആയി മാറ്റപ്പെടും. കോഡിലൂടെ ഒരു ഉദാഹരണം നോക്കാം. നമുക്ക് വരാനിരിക്കുന്ന നോട്ട്‌ബുക്കുകളിൽ `scikit-learn` പഠിക്കാനിരിക്കുന്നതിനാൽ, ഇവിടെ അതുപയോഗിക്കില്ല.\n" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "id": "1vGz7uZyoWHL", "outputId": "9e252855-d193-4103-a54d-028ea7787b34" }, "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", "
IDclass
010business class
120first class
230economy class
340economy class
450economy class
560business class
\n", "
" ], "text/plain": [ " ID class\n", "0 10 business class\n", "1 20 first class\n", "2 30 economy class\n", "3 40 economy class\n", "4 50 economy class\n", "5 60 business class" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "label = pd.DataFrame([\n", " [10,'business class'],\n", " [20,'first class'],\n", " [30, 'economy class'],\n", " [40, 'economy class'],\n", " [50, 'economy class'],\n", " [60, 'business class']\n", "],columns=['ID','class'])\n", "label" ] }, { "cell_type": "markdown", "metadata": { "id": "IDHnkwTYov-h" }, "source": [ "1-ാം കോളത്തിൽ ലേബൽ എൻകോഡിംഗ് നടത്താൻ, ഓരോ ക്ലാസിനും ഒരു സംഖ്യയിലേക്ക് മാപ്പിംഗ് ആദ്യം നിർവചിക്കണം, പിന്നീട് മാറ്റം വരുത്തണം.\n" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "id": "ZC5URJG3o1ES", "outputId": "aab0f1e7-e0f3-4c14-8459-9f9168c85437" }, "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", "
IDclass
0100
1202
2301
3401
4501
5600
\n", "
" ], "text/plain": [ " ID class\n", "0 10 0\n", "1 20 2\n", "2 30 1\n", "3 40 1\n", "4 50 1\n", "5 60 0" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class_labels = {'business class':0,'economy class':1,'first class':2}\n", "label['class'] = label['class'].replace(class_labels)\n", "label" ] }, { "cell_type": "markdown", "metadata": { "id": "ftnF-TyapOPt" }, "source": [ "നാം കാണുന്ന പോലെ, ഔട്ട്പുട്ട് നാം കരുതിയതുപോലെ തന്നെയാണ്. അതിനാൽ, ലേബൽ എൻകോഡിംഗ് എപ്പോൾ ഉപയോഗിക്കണം? ലേബൽ എൻകോഡിംഗ് താഴെ പറയുന്ന ഏതെങ്കിലും ഒരു സാഹചര്യത്തിലും അല്ലെങ്കിൽ ഇരുവിലുമാണ് ഉപയോഗിക്കുന്നത്:\n", "1. വിഭാഗങ്ങളുടെ എണ്ണം വലിയപ്പോൾ\n", "2. വിഭാഗങ്ങൾ ക്രമത്തിൽ ഉള്ളപ്പോൾ.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "eQPAPVwsqWT7" }, "source": [ "**ഒന്ന് ഹോട്ട് എൻകോഡിംഗ്**\n", "\n", "മറ്റൊരു എൻകോഡിംഗ് തരം ഒന്ന് ഹോട്ട് എൻകോഡിംഗ് ആണ്. ഈ എൻകോഡിംഗിൽ, കോളത്തിന്റെ ഓരോ വിഭാഗവും വേർതിരിച്ച കോളമായി ചേർക്കപ്പെടും, ഓരോ ഡാറ്റാപോയിന്റിനും ആ വിഭാഗം ഉൾക്കൊള്ളുന്നുണ്ടോ എന്നതിന്റെ അടിസ്ഥാനത്തിൽ 0 അല്ലെങ്കിൽ 1 ലഭിക്കും. അതായത്, n വ്യത്യസ്ത വിഭാഗങ്ങൾ ഉണ്ടെങ്കിൽ, n കോളങ്ങൾ ഡാറ്റാഫ്രെയിമിൽ ചേർക്കപ്പെടും.\n", "\n", "ഉദാഹരണത്തിന്, നാം മുമ്പ് എടുത്ത എയർപ്ലെയിൻ ക്ലാസ് ഉദാഹരണം എടുത്തു നോക്കാം. വിഭാഗങ്ങൾ: ['ബിസിനസ് ക്ലാസ്', 'ഇക്കണോമി ക്ലാസ്', 'ഫസ്റ്റ് ക്ലാസ്'] ആയിരുന്നു. അതിനാൽ, ഒന്ന് ഹോട്ട് എൻകോഡിംഗ് നടത്തുമ്പോൾ, താഴെ പറയുന്ന മൂന്ന് കോളങ്ങൾ ഡാറ്റാസെറ്റിൽ ചേർക്കപ്പെടും: ['class_business class', 'class_economy class', 'class_first class'].\n" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "id": "ZM0eVh0ArKUL", "outputId": "83238a76-b3a5-418d-c0b6-605b02b6891b" }, "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", "
IDclass
010business class
120first class
230economy class
340economy class
450economy class
560business class
\n", "
" ], "text/plain": [ " ID class\n", "0 10 business class\n", "1 20 first class\n", "2 30 economy class\n", "3 40 economy class\n", "4 50 economy class\n", "5 60 business class" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "one_hot = pd.DataFrame([\n", " [10,'business class'],\n", " [20,'first class'],\n", " [30, 'economy class'],\n", " [40, 'economy class'],\n", " [50, 'economy class'],\n", " [60, 'business class']\n", "],columns=['ID','class'])\n", "one_hot" ] }, { "cell_type": "markdown", "metadata": { "id": "aVnZ7paDrWmb" }, "source": [ "നമുക്ക് ആദ്യ കോളത്തിൽ ഒന്ന് ഹോട്ട് എൻകോഡിംഗ് നടത്താം.\n" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "id": "RUPxf7egrYKr" }, "outputs": [], "source": [ "one_hot_data = pd.get_dummies(one_hot,columns=['class'])" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "id": "TM37pHsFr4ge", "outputId": "7be15f53-79b2-447a-979c-822658339a9e" }, "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", "
IDclass_business classclass_economy classclass_first class
010100
120001
230010
340010
450010
560100
\n", "
" ], "text/plain": [ " ID class_business class class_economy class class_first class\n", "0 10 1 0 0\n", "1 20 0 0 1\n", "2 30 0 1 0\n", "3 40 0 1 0\n", "4 50 0 1 0\n", "5 60 1 0 0" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "one_hot_data" ] }, { "cell_type": "markdown", "metadata": { "id": "_zXRLOjXujdA" }, "source": [ "ഓരോ ഒന്ന് ഹോട്ട് എൻകോഡഡ് കോളവും 0 അല്ലെങ്കിൽ 1 അടങ്ങിയിരിക്കുന്നു, അത് ആ ഡാറ്റാപോയിന്റിനായി ആ വിഭാഗം നിലവിലുണ്ടോ എന്നത് വ്യക്തമാക്കുന്നു.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "bDnC4NQOu0qr" }, "source": [ "ഒന്ന് ഹോട്ട് എൻകോഡിംഗ് എപ്പോൾ ഉപയോഗിക്കാം? ഒന്ന് ഹോട്ട് എൻകോഡിംഗ് താഴെ പറയുന്ന ഏതെങ്കിലും ഒരു സാഹചര്യത്തിൽ അല്ലെങ്കിൽ ഇരുവിധവും ഉപയോഗിക്കുന്നു:\n", "\n", "1. വിഭാഗങ്ങളുടെ എണ്ണം കൂടാതെ ഡാറ്റാസെറ്റിന്റെ വലിപ്പം ചെറുതായിരിക്കുമ്പോൾ.\n", "2. വിഭാഗങ്ങൾ പ്രത്യേകമായ ഒരു ക്രമം പാലിക്കാത്തപ്പോൾ.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "XnUmci_4uvyu" }, "source": [ "> പ്രധാനപ്പെട്ട കാര്യങ്ങൾ:\n", "1. സംഖ്യാത്മകമല്ലാത്ത ഡാറ്റ സംഖ്യാത്മക ഡാറ്റയാക്കി മാറ്റാൻ എൻകോഡിംഗ് നടത്തുന്നു.\n", "2. എൻകോഡിംഗിന്റെ രണ്ട് തരം ഉണ്ട്: ലേബൽ എൻകോഡിംഗ്, ഒന്ന് ഹോട്ട് എൻകോഡിംഗ്, ഇവ രണ്ടും ഡാറ്റാസെറ്റിന്റെ ആവശ്യകതകൾ അനുസരിച്ച് നടത്താം.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "K8UXOJYRgRsJ" }, "source": [ "## പകർപ്പുകൾ നീക്കംചെയ്യൽ\n", "\n", "> **പഠനലക്ഷ്യം:** ഈ ഉപവിഭാഗം അവസാനിക്കുമ്പോൾ, ഡാറ്റാഫ്രെയിമുകളിൽ നിന്ന് പകർപ്പുകൾ തിരിച്ചറിയാനും നീക്കംചെയ്യാനും നിങ്ങൾക്ക് സുഖകരമായിരിക്കണം.\n", "\n", "കാണാതായ ഡാറ്റയ്ക്ക് പുറമേ, യാഥാർത്ഥ്യ ലോക ഡാറ്റാസെറ്റുകളിൽ നിങ്ങൾക്ക് പലപ്പോഴും പകർപ്പുകൾ കാണാം. ഭാഗ്യവശാൽ, pandas പകർപ്പുകൾ കണ്ടെത്താനും നീക്കംചെയ്യാനും എളുപ്പമുള്ള മാർഗം നൽകുന്നു.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "qrEG-Wa0gRsJ" }, "source": [ "### പകർപ്പുകൾ തിരിച്ചറിയൽ: `duplicated`\n", "\n", "`duplicated` മെത്തഡ് pandas-ൽ ഉപയോഗിച്ച് പകർപ്പുള്ള മൂല്യങ്ങൾ എളുപ്പത്തിൽ കണ്ടെത്താം, ഇത് ഒരു ബൂളിയൻ മാസ്ക് നൽകുന്നു, അത് ഒരു `DataFrame`-ലെ ഒരു എൻട്രി മുമ്പത്തെ ഒന്നിന്റെ പകർപ്പാണോ എന്ന് സൂചിപ്പിക്കുന്നു. ഇത് പ്രവർത്തനത്തിൽ കാണാൻ മറ്റൊരു ഉദാഹരണ `DataFrame` സൃഷ്ടിക്കാം.\n" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "id": "ZLu6FEnZgRsJ", "outputId": "376512d1-d842-4db1-aea3-71052aeeecaf", "trusted": false }, "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", "
lettersnumbers
0A1
1B2
2A1
3B3
4B3
\n", "
" ], "text/plain": [ " letters numbers\n", "0 A 1\n", "1 B 2\n", "2 A 1\n", "3 B 3\n", "4 B 3" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example6 = pd.DataFrame({'letters': ['A','B'] * 2 + ['B'],\n", " 'numbers': [1, 2, 1, 3, 3]})\n", "example6" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "cIduB5oBgRsK", "outputId": "3da27b3d-4d69-4e1d-bb52-0af21bae87f2", "trusted": false }, "outputs": [ { "data": { "text/plain": [ "0 False\n", "1 False\n", "2 True\n", "3 False\n", "4 True\n", "dtype: bool" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example6.duplicated()" ] }, { "cell_type": "markdown", "metadata": { "id": "0eDRJD4SgRsK" }, "source": [ "### പകർപ്പുകൾ ഒഴിവാക്കൽ: `drop_duplicates`\n", "`drop_duplicates` ലളിതമായി `duplicated` മൂല്യങ്ങൾ എല്ലാം `False` ആയിരിക്കുന്ന ഡാറ്റയുടെ ഒരു പകർപ്പ് തിരികെ നൽകുന്നു:\n" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 142 }, "id": "w_YPpqIqgRsK", "outputId": "ac66bd2f-8671-4744-87f5-8b8d96553dea", "trusted": false }, "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", "
lettersnumbers
0A1
1B2
3B3
\n", "
" ], "text/plain": [ " letters numbers\n", "0 A 1\n", "1 B 2\n", "3 B 3" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example6.drop_duplicates()" ] }, { "cell_type": "markdown", "metadata": { "id": "69AqoCZAgRsK" }, "source": [ "`duplicated` ഉം `drop_duplicates` ഉം ഡിഫോൾട്ടായി എല്ലാ കോളങ്ങളെയും പരിഗണിക്കുന്നു, പക്ഷേ നിങ്ങൾക്ക് നിങ്ങളുടെ `DataFrame` ൽ ചില കോളങ്ങളിലേയ്ക്ക് മാത്രം അവ പരിശോധിക്കണമെന്ന് വ്യക്തമാക്കാം:\n" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 111 }, "id": "BILjDs67gRsK", "outputId": "ef6dcc08-db8b-4352-c44e-5aa9e2bec0d3", "trusted": false }, "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", "
lettersnumbers
0A1
1B2
\n", "
" ], "text/plain": [ " letters numbers\n", "0 A 1\n", "1 B 2" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example6.drop_duplicates(['letters'])" ] }, { "cell_type": "markdown", "metadata": { "id": "GvX4og1EgRsL" }, "source": [ "> **പ്രധാനപ്പെട്ടത്:** പകർപ്പുള്ള ഡാറ്റ നീക്കംചെയ്യുന്നത് ഏകദേശം എല്ലാ ഡാറ്റാ-സയൻസ് പ്രോജക്ടുകളുടെയും അനിവാര്യമായ ഭാഗമാണ്. പകർപ്പുള്ള ഡാറ്റ നിങ്ങളുടെ വിശകലനങ്ങളുടെ ഫലങ്ങൾ മാറ്റുകയും തെറ്റായ ഫലങ്ങൾ നൽകുകയും ചെയ്യാം!\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## യഥാർത്ഥ ലോക ഡാറ്റ ഗുണനിലവാര പരിശോധനകൾ\n", "\n", "> **പഠന ലക്ഷ്യം:** ഈ വിഭാഗം അവസാനിക്കുമ്പോൾ, അസംഘടിത വർഗ്ഗീയ മൂല്യങ്ങൾ, അസാധാരണ സംഖ്യാത്മക മൂല്യങ്ങൾ (ഔട്ട്‌ലൈയർമാർ), വ്യത്യാസങ്ങളുള്ള പകർപ്പുകൾ ഉൾപ്പെടെയുള്ള സാധാരണ യഥാർത്ഥ ലോക ഡാറ്റ ഗുണനിലവാര പ്രശ്നങ്ങൾ കണ്ടെത്താനും ശരിയാക്കാനും നിങ്ങൾക്ക് സുഖകരമാകണം.\n", "\n", "മിസ്സിംഗ് മൂല്യങ്ങളും കൃത്യമായ പകർപ്പുകളും സാധാരണ പ്രശ്നങ്ങളായിരുന്നാലും, യഥാർത്ഥ ലോക ഡാറ്റാസെറ്റുകളിൽ കൂടുതൽ സൂക്ഷ്മമായ പ്രശ്നങ്ങൾ ഉണ്ടാകാറുണ്ട്:\n", "\n", "1. **അസംഘടിത വർഗ്ഗീയ മൂല്യങ്ങൾ**: ഒരേ വർഗ്ഗം വ്യത്യസ്തമായി എഴുതപ്പെട്ടത് (ഉദാ., \"USA\", \"U.S.A\", \"United States\")\n", "2. **അസാധാരണ സംഖ്യാത്മക മൂല്യങ്ങൾ**: ഡാറ്റാ എൻട്രി പിശകുകൾ സൂചിപ്പിക്കുന്ന അത്യന്തം വ്യത്യസ്ത മൂല്യങ്ങൾ (ഉദാ., പ്രായം = 999)\n", "3. **സമീപ പകർപ്പ് വരികൾ**: ചെറിയ വ്യത്യാസങ്ങളോടെ ഒരേ ഘടകത്തെ പ്രതിനിധീകരിക്കുന്ന രേഖകൾ\n", "\n", "ഈ പ്രശ്നങ്ങൾ കണ്ടെത്താനും കൈകാര്യം ചെയ്യാനും ഉള്ള സാങ്കേതിക വിദ്യകൾ പരിശോധിക്കാം.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ഒരു സാമ്പിൾ \"അഴുക്കുള്ള\" ഡാറ്റാസെറ്റ് സൃഷ്ടിക്കൽ\n", "\n", "ആദ്യം, നാം സാധാരണയായി യാഥാർത്ഥ്യ ഡാറ്റയിൽ കാണുന്ന പ്രശ്നങ്ങളുള്ള ഒരു സാമ്പിൾ ഡാറ്റാസെറ്റ് സൃഷ്ടിക്കാം:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "# Create a sample dataset with quality issues\n", "dirty_data = pd.DataFrame({\n", " 'customer_id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],\n", " 'name': ['John Smith', 'Jane Doe', 'John Smith', 'Bob Johnson', \n", " 'Alice Williams', 'Charlie Brown', 'John Smith', 'Eva Martinez',\n", " 'Bob Johnson', 'Diana Prince', 'Frank Castle', 'Alice Williams'],\n", " 'age': [25, 32, 25, 45, 28, 199, 25, 31, 45, 27, -5, 28],\n", " 'country': ['USA', 'UK', 'U.S.A', 'Canada', 'USA', 'United Kingdom',\n", " 'United States', 'Mexico', 'canada', 'USA', 'UK', 'usa'],\n", " 'purchase_amount': [100.50, 250.00, 105.00, 320.00, 180.00, 90.00,\n", " 102.00, 275.00, 325.00, 195.00, 410.00, 185.00]\n", "})\n", "\n", "print(\"Sample 'Dirty' Dataset:\")\n", "print(dirty_data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. അസംഘടിത വർഗ്ഗീയ മൂല്യങ്ങൾ കണ്ടെത്തൽ\n", "\n", "`country` കോളത്തിൽ ഒരേ രാജ്യങ്ങൾക്ക് പല പ്രതിനിധാനങ്ങളും ഉള്ളതായി ശ്രദ്ധിക്കുക. ഈ അസംഘടിതത്വങ്ങൾ കണ്ടെത്താം:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check unique values in the country column\n", "print(\"Unique country values:\")\n", "print(dirty_data['country'].unique())\n", "print(f\"\\nTotal unique values: {dirty_data['country'].nunique()}\")\n", "\n", "# Count occurrences of each variation\n", "print(\"\\nValue counts:\")\n", "print(dirty_data['country'].value_counts())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### വർഗ്ഗീകരണ മൂല്യങ്ങൾ സ്റ്റാൻഡേർഡൈസ് ചെയ്യൽ\n", "\n", "ഈ മൂല്യങ്ങൾ സ്റ്റാൻഡേർഡൈസ് ചെയ്യാൻ നാം ഒരു മാപ്പിംഗ് സൃഷ്ടിക്കാം. ലളിതമായ ഒരു സമീപനം lowercase ആക്കുകയും ഒരു മാപ്പിംഗ് ഡിക്ഷണറി സൃഷ്ടിക്കുകയുമാണ്:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a standardization mapping\n", "country_mapping = {\n", " 'usa': 'USA',\n", " 'u.s.a': 'USA',\n", " 'united states': 'USA',\n", " 'uk': 'UK',\n", " 'united kingdom': 'UK',\n", " 'canada': 'Canada',\n", " 'mexico': 'Mexico'\n", "}\n", "\n", "# Standardize the country column\n", "dirty_data['country_clean'] = dirty_data['country'].str.lower().map(country_mapping)\n", "\n", "print(\"Before standardization:\")\n", "print(dirty_data['country'].value_counts())\n", "print(\"\\nAfter standardization:\")\n", "print(dirty_data[['country_clean']].value_counts())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**മാറ്റുവഴി: ഫസി മാച്ചിംഗ് ഉപയോഗിക്കൽ**\n", "\n", "കൂടുതൽ സങ്കീർണ്ണമായ കേസുകൾക്കായി, സമാനമായ സ്ട്രിംഗുകൾ സ്വയം കണ്ടെത്താൻ `rapidfuzz` ലൈബ്രറി ഉപയോഗിച്ച് ഫസി സ്ട്രിംഗ് മാച്ചിംഗ് ചെയ്യാം:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " from rapidfuzz import process, fuzz\n", "except ImportError:\n", " print(\"rapidfuzz is not installed. Please install it with 'pip install rapidfuzz' to use fuzzy matching.\")\n", " process = None\n", " fuzz = None\n", "\n", "# Get unique countries\n", "unique_countries = dirty_data['country'].unique()\n", "\n", "# For each country, find similar matches\n", "if process is not None and fuzz is not None:\n", " print(\"Finding similar country names (similarity > 70%):\")\n", " for country in unique_countries:\n", " matches = process.extract(country, unique_countries, scorer=fuzz.ratio, limit=3)\n", " # Filter matches with similarity > 70 and not identical\n", " similar = [m for m in matches if m[1] > 70 and m[0] != country]\n", " if similar:\n", " print(f\"\\n'{country}' is similar to:\")\n", " for match, score, _ in similar:\n", " print(f\" - '{match}' (similarity: {score}%)\")\n", "else:\n", " print(\"Skipping fuzzy matching because rapidfuzz is not available.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. അസാധാരണ സംഖ്യാത്മക മൂല്യങ്ങൾ (ഔട്ട്‌ലൈയർമാർ) കണ്ടെത്തൽ\n", "\n", "`age` കോളം നോക്കുമ്പോൾ, 199, -5 പോലുള്ള ചില സംശയാസ്പദമായ മൂല്യങ്ങൾ കാണാം. ഈ ഔട്ട്‌ലൈയർമാർ കണ്ടെത്താൻ നാം സാംഖ്യിക രീതികൾ ഉപയോഗിക്കാം.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Display basic statistics\n", "print(\"Age column statistics:\")\n", "print(dirty_data['age'].describe())\n", "\n", "# Identify impossible values using domain knowledge\n", "print(\"\\nRows with impossible age values (< 0 or > 120):\")\n", "impossible_ages = dirty_data[(dirty_data['age'] < 0) | (dirty_data['age'] > 120)]\n", "print(impossible_ages[['customer_id', 'name', 'age']])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### IQR (ഇന്റർക്വാർട്ടൈൽ റേഞ്ച്) രീതിയുടെ ഉപയോഗം\n", "\n", "IQR രീതി അത്യന്തം മൂല്യങ്ങൾക്ക് കുറവായ സാന്ദ്രതയുള്ള ഒരു ശക്തമായ സ്ഥിതിവിവരശാസ്ത്ര സാങ്കേതിക വിദ്യയാണ്, ഇത് ഔട്ട്‌ലൈയർ കണ്ടെത്തലിനായി ഉപയോഗിക്കുന്നു:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Calculate IQR for age (excluding impossible values)\n", "valid_ages = dirty_data[(dirty_data['age'] >= 0) & (dirty_data['age'] <= 120)]['age']\n", "\n", "Q1 = valid_ages.quantile(0.25)\n", "Q3 = valid_ages.quantile(0.75)\n", "IQR = Q3 - Q1\n", "\n", "# Define outlier bounds\n", "lower_bound = Q1 - 1.5 * IQR\n", "upper_bound = Q3 + 1.5 * IQR\n", "\n", "print(f\"IQR-based outlier bounds for age: [{lower_bound:.2f}, {upper_bound:.2f}]\")\n", "\n", "# Identify outliers\n", "age_outliers = dirty_data[(dirty_data['age'] < lower_bound) | (dirty_data['age'] > upper_bound)]\n", "print(f\"\\nRows with age outliers:\")\n", "print(age_outliers[['customer_id', 'name', 'age']])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Z-സ്കോർ രീതി ഉപയോഗിച്ച്\n", "\n", "Z-സ്കോർ രീതി ശരാശരിയിൽ നിന്നുള്ള സ്റ്റാൻഡേർഡ് ഡിവിയേഷനുകളുടെ അടിസ്ഥാനത്തിൽ ഔട്ട്‌ലൈയർമാരെ തിരിച്ചറിയുന്നു:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " from scipy import stats\n", "except ImportError:\n", " print(\"scipy is required for Z-score calculation. Please install it with 'pip install scipy' and rerun this cell.\")\n", "else:\n", " # Calculate Z-scores for age, handling NaN values\n", " age_nonan = dirty_data['age'].dropna()\n", " zscores = np.abs(stats.zscore(age_nonan))\n", " dirty_data['age_zscore'] = np.nan\n", " dirty_data.loc[age_nonan.index, 'age_zscore'] = zscores\n", "\n", " # Typically, Z-score > 3 indicates an outlier\n", " print(\"Rows with age Z-score > 3:\")\n", " zscore_outliers = dirty_data[dirty_data['age_zscore'] > 3]\n", " print(zscore_outliers[['customer_id', 'name', 'age', 'age_zscore']])\n", "\n", " # Clean up the temporary column\n", " dirty_data = dirty_data.drop('age_zscore', axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ഔട്ട്‌ലൈയർമാരെ കൈകാര്യം ചെയ്യൽ\n", "\n", "ഒരിക്കൽ കണ്ടെത്തിയാൽ, ഔട്ട്‌ലൈയർമാരെ പലവിധമായി കൈകാര്യം ചെയ്യാം:\n", "1. **നീക്കം ചെയ്യുക**: ഔട്ട്‌ലൈയർമാർ ഉള്ള വരികൾ ഒഴിവാക്കുക (അവ പിശകുകൾ ആണെങ്കിൽ)\n", "2. **ക്യാപ് ചെയ്യുക**: അതിർത്തി മൂല്യങ്ങളാൽ മാറ്റിസ്ഥാപിക്കുക\n", "3. **NaN ആയി മാറ്റുക**: നഷ്ടപ്പെട്ട ഡാറ്റയായി പരിഗണിച്ച് ഇംപ്യൂട്ടേഷൻ സാങ്കേതികവിദ്യകൾ ഉപയോഗിക്കുക\n", "4. **വെച്ച് വയ്ക്കുക**: അവ യഥാർത്ഥ അത്യന്തം മൂല്യങ്ങളാണെങ്കിൽ\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a cleaned version by replacing impossible ages with NaN\n", "dirty_data['age_clean'] = dirty_data['age'].apply(\n", " lambda x: np.nan if (x < 0 or x > 120) else x\n", ")\n", "\n", "print(\"Age column before and after cleaning:\")\n", "print(dirty_data[['customer_id', 'name', 'age', 'age_clean']])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. സമാനമായ പദങ്ങൾ കണ്ടെത്തൽ\n", "\n", "ഞങ്ങളുടെ ഡാറ്റാസെറ്റിൽ \"ജോൺ സ്മിത്ത്\" എന്ന പേരിൽ ചെറിയ വ്യത്യാസമുള്ള പല എൻട്രികളും ഉണ്ടെന്ന് ശ്രദ്ധിക്കുക. പേരിന്റെ സമാനതയുടെ അടിസ്ഥാനത്തിൽ സാധ്യതയുള്ള പദങ്ങൾ കണ്ടെത്താം.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# First, let's look at exact name matches (ignoring extra whitespace)\n", "dirty_data['name_normalized'] = dirty_data['name'].str.strip().str.lower()\n", "\n", "print(\"Checking for duplicate names:\")\n", "duplicate_names = dirty_data[dirty_data.duplicated(['name_normalized'], keep=False)]\n", "print(duplicate_names.sort_values('name_normalized')[['customer_id', 'name', 'age', 'country']])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### ഫസി മാച്ചിങ്ങ് ഉപയോഗിച്ച് സമീപ ഡ്യൂപ്ലിക്കേറ്റുകൾ കണ്ടെത്തൽ\n", "\n", "കൂടുതൽ സങ്കീർണ്ണമായ ഡ്യൂപ്ലിക്കേറ്റ് കണ്ടെത്തലിനായി, സമാനമായ പേരുകൾ കണ്ടെത്താൻ ഫസി മാച്ചിങ്ങ് ഉപയോഗിക്കാം:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " from rapidfuzz import process, fuzz\n", "\n", " # Function to find potential duplicates\n", " def find_near_duplicates(df, column, threshold=90):\n", " \"\"\"\n", " Find near-duplicate entries in a column using fuzzy matching.\n", " \n", " Parameters:\n", " - df: DataFrame\n", " - column: Column name to check for duplicates\n", " - threshold: Similarity threshold (0-100)\n", " \n", " Returns: List of potential duplicate groups\n", " \"\"\"\n", " values = df[column].unique()\n", " duplicate_groups = []\n", " checked = set()\n", " \n", " for value in values:\n", " if value in checked:\n", " continue\n", " \n", " # Find similar values\n", " matches = process.extract(value, values, scorer=fuzz.ratio, limit=len(values))\n", " similar = [m[0] for m in matches if m[1] >= threshold]\n", " \n", " if len(similar) > 1:\n", " duplicate_groups.append(similar)\n", " checked.update(similar)\n", " \n", " return duplicate_groups\n", "\n", " # Find near-duplicate names\n", " duplicate_groups = find_near_duplicates(dirty_data, 'name', threshold=90)\n", "\n", " print(\"Potential duplicate groups:\")\n", " for i, group in enumerate(duplicate_groups, 1):\n", " print(f\"\\nGroup {i}:\")\n", " for name in group:\n", " matching_rows = dirty_data[dirty_data['name'] == name]\n", " print(f\" '{name}': {len(matching_rows)} occurrence(s)\")\n", " for _, row in matching_rows.iterrows():\n", " print(f\" - Customer {row['customer_id']}: age={row['age']}, country={row['country']}\")\n", "except ImportError:\n", " print(\"rapidfuzz is not installed. Skipping fuzzy matching for near-duplicates.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### പകർപ്പുകൾ കൈകാര്യം ചെയ്യൽ\n", "\n", "ഒരിക്കൽ തിരിച്ചറിയുമ്പോൾ, പകർപ്പുകൾ എങ്ങനെ കൈകാര്യം ചെയ്യണമെന്ന് നിങ്ങൾ തീരുമാനിക്കണം:\n", "1. **ആദ്യത്തെ സംഭവമുണ്ടായിടം സൂക്ഷിക്കുക**: `drop_duplicates(keep='first')` ഉപയോഗിക്കുക\n", "2. **അവസാനത്തെ സംഭവമുണ്ടായിടം സൂക്ഷിക്കുക**: `drop_duplicates(keep='last')` ഉപയോഗിക്കുക\n", "3. **വിവരം സംയോജിപ്പിക്കുക**: പകർപ്പ് വരികളിൽ നിന്നുള്ള വിവരങ്ങൾ സംയോജിപ്പിക്കുക\n", "4. **മാനുവൽ അവലോകനം**: മനുഷ്യ അവലോകനത്തിനായി ഫ്ലാഗ് ചെയ്യുക\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Example: Remove duplicates based on normalized name, keeping first occurrence\n", "cleaned_data = dirty_data.drop_duplicates(subset=['name_normalized'], keep='first')\n", "\n", "print(f\"Original dataset: {len(dirty_data)} rows\")\n", "print(f\"After removing name duplicates: {len(cleaned_data)} rows\")\n", "print(f\"Removed: {len(dirty_data) - len(cleaned_data)} duplicate rows\")\n", "\n", "print(\"\\nCleaned dataset:\")\n", "print(cleaned_data[['customer_id', 'name', 'age', 'country_clean']])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Summary: Complete Data Cleaning Pipeline\n", "\n", "എല്ലാം ചേർത്ത് ഒരു സമഗ്രമായ ക്ലീനിംഗ് പൈപ്പ്‌ലൈൻ ആയി മാറ്റാം:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def clean_dataset(df):\n", " \"\"\"\n", " Comprehensive data cleaning function.\n", " \"\"\"\n", " # Create a copy to avoid modifying the original\n", " cleaned = df.copy()\n", " \n", " # 1. Standardize categorical values (country)\n", " country_mapping = {\n", " 'usa': 'USA', 'u.s.a': 'USA', 'united states': 'USA',\n", " 'uk': 'UK', 'united kingdom': 'UK',\n", " 'canada': 'Canada', 'mexico': 'Mexico'\n", " }\n", " cleaned['country'] = cleaned['country'].str.lower().map(country_mapping)\n", " \n", " # 2. Clean abnormal age values\n", " cleaned['age'] = cleaned['age'].apply(\n", " lambda x: np.nan if (x < 0 or x > 120) else x\n", " )\n", " \n", " # 3. Remove near-duplicate names (normalize whitespace)\n", " cleaned['name'] = cleaned['name'].str.strip()\n", " cleaned = cleaned.drop_duplicates(subset=['name'], keep='first')\n", " \n", " return cleaned\n", "\n", "# Apply the cleaning pipeline\n", "final_cleaned_data = clean_dataset(dirty_data)\n", "\n", "print(\"Before cleaning:\")\n", "print(f\" Rows: {len(dirty_data)}\")\n", "print(f\" Unique countries: {dirty_data['country'].nunique()}\")\n", "print(f\" Invalid ages: {((dirty_data['age'] < 0) | (dirty_data['age'] > 120)).sum()}\")\n", "\n", "print(\"\\nAfter cleaning:\")\n", "print(f\" Rows: {len(final_cleaned_data)}\")\n", "print(f\" Unique countries: {final_cleaned_data['country'].nunique()}\")\n", "print(f\" Invalid ages: {((final_cleaned_data['age'] < 0) | (final_cleaned_data['age'] > 120)).sum()}\")\n", "\n", "print(\"\\nCleaned dataset:\")\n", "print(final_cleaned_data[['customer_id', 'name', 'age', 'country', 'purchase_amount']])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 🎯 ചലഞ്ച് വ്യായാമം\n", "\n", "ഇപ്പോൾ നിങ്ങളുടെ തവണ! താഴെ നിരവധി ഗുണനിലവാര പ്രശ്നങ്ങളുള്ള ഒരു പുതിയ ഡാറ്റാ വരി നൽകിയിരിക്കുന്നു. നിങ്ങൾക്ക് ചെയ്യാൻ കഴിയുമോ:\n", "\n", "1. ഈ വരിയിൽ ഉള്ള എല്ലാ പ്രശ്നങ്ങളും തിരിച്ചറിയുക\n", "2. ഓരോ പ്രശ്നവും ശുദ്ധമാക്കാൻ കോഡ് എഴുതുക\n", "3. ശുദ്ധമാക്കിയ വരി ഡാറ്റാസെറ്റിൽ ചേർക്കുക\n", "\n", "പ്രശ്നമുള്ള ഡാറ്റ ഇവിടെ:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# New problematic row\n", "new_row = pd.DataFrame({\n", " 'customer_id': [13],\n", " 'name': [' Diana Prince '], # Extra whitespace\n", " 'age': [250], # Impossible age\n", " 'country': ['U.S.A.'], # Inconsistent format\n", " 'purchase_amount': [150.00]\n", "})\n", "\n", "print(\"New row to clean:\")\n", "print(new_row)\n", "\n", "# TODO: Your code here to clean this row\n", "# Hints:\n", "# 1. Strip whitespace from the name\n", "# 2. Check if the name is a duplicate (Diana Prince already exists)\n", "# 3. Handle the impossible age value\n", "# 4. Standardize the country name\n", "\n", "# Example solution (uncomment and modify as needed):\n", "# new_row_cleaned = new_row.copy()\n", "# new_row_cleaned['name'] = new_row_cleaned['name'].str.strip()\n", "# new_row_cleaned['age'] = np.nan # Invalid age\n", "# new_row_cleaned['country'] = 'USA' # Standardized\n", "# print(\"\\nCleaned row:\")\n", "# print(new_row_cleaned)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### പ്രധാനപ്പെട്ട കാര്യങ്ങൾ\n", "\n", "1. **അസമതുല്യ വിഭാഗങ്ങൾ** യഥാർത്ഥ ഡാറ്റയിൽ സാധാരണമാണ്. എപ്പോഴും വ്യത്യസ്ത മൂല്യങ്ങൾ പരിശോധിച്ച് മാപ്പിംഗുകൾ അല്ലെങ്കിൽ ഫസി മാച്ചിംഗ് ഉപയോഗിച്ച് അവയെ സ്റ്റാൻഡർഡൈസ് ചെയ്യുക.\n", "\n", "2. **ഔട്ട്‌ലൈയർമാർ** നിങ്ങളുടെ വിശകലനത്തെ ഗണ്യമായി ബാധിക്കാം. അവ കണ്ടെത്താൻ ഡൊമെയ്ൻ അറിവും സാങ്കേതിക രീതികളും (IQR, Z-സ്കോർ) സംയോജിപ്പിച്ച് ഉപയോഗിക്കുക.\n", "\n", "3. **സമീപ ഡ്യൂപ്ലിക്കേറ്റുകൾ** കൃത്യമായ ഡ്യൂപ്ലിക്കേറ്റുകളേക്കാൾ കണ്ടെത്താൻ ബുദ്ധിമുട്ടാണ്. അവ തിരിച്ചറിയാൻ ഫസി മാച്ചിംഗ് ഉപയോഗിക്കുകയും ഡാറ്റ സാധാരണ രൂപത്തിലാക്കുകയും (ലോവർകേസിൽ മാറ്റൽ, വെളിച്ചം നീക്കംചെയ്യൽ) പരിഗണിക്കുക.\n", "\n", "4. **ഡാറ്റ ക്ലീനിംഗ് ആവർത്തനപരമാണ്**. നിങ്ങൾക്ക് പല സാങ്കേതിക വിദ്യകളും പ്രയോഗിച്ച് ഫലങ്ങൾ അവലോകനം ചെയ്ത് ശേഷം ക്ലീനിംഗ് പൂർത്തിയാക്കേണ്ടി വരാം.\n", "\n", "5. **നിങ്ങളുടെ തീരുമാനങ്ങൾ രേഖപ്പെടുത്തുക**. ഏത് ക്ലീനിംഗ് ഘട്ടങ്ങൾ നിങ്ങൾ പ്രയോഗിച്ചു, എന്തുകൊണ്ടെന്ന് ട്രാക്ക് ചെയ്യുക, ഇത് പുനരുത്പാദനക്ഷമതക്കും പരദർശിത്വത്തിനും പ്രധാനമാണ്.\n", "\n", "> **മികച്ച പ്രാക്ടീസ്:** നിങ്ങളുടെ യഥാർത്ഥ \"അഴുക്കുള്ള\" ഡാറ്റയുടെ ഒരു പകർപ്പ് എപ്പോഴും സൂക്ഷിക്കുക. ഉറവിട ഡാറ്റ ഫയലുകൾ മുകളിലേയ്ക്ക് എഴുതരുത് - `data_cleaned.csv` പോലുള്ള വ്യക്തമായ നാമകരണം ഉപയോഗിച്ച് ക്ലീനുചെയ്ത പതിപ്പുകൾ സൃഷ്ടിക്കുക.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n\n\n**അസൂയാപത്രം**: \nഈ രേഖ AI വിവർത്തന സേവനം [Co-op Translator](https://github.com/Azure/co-op-translator) ഉപയോഗിച്ച് വിവർത്തനം ചെയ്തതാണ്. നാം കൃത്യതയ്ക്ക് ശ്രമിച്ചിട്ടുണ്ടെങ്കിലും, സ്വയം പ്രവർത്തിക്കുന്ന വിവർത്തനങ്ങളിൽ പിശകുകൾ അല്ലെങ്കിൽ തെറ്റുകൾ ഉണ്ടാകാമെന്ന് ദയവായി ശ്രദ്ധിക്കുക. അതിന്റെ മാതൃഭാഷയിലുള്ള യഥാർത്ഥ രേഖ അധികാരപരമായ ഉറവിടമായി കണക്കാക്കപ്പെടണം. നിർണായക വിവരങ്ങൾക്ക്, പ്രൊഫഷണൽ മനുഷ്യ വിവർത്തനം ശുപാർശ ചെയ്യപ്പെടുന്നു. ഈ വിവർത്തനത്തിന്റെ ഉപയോഗത്തിൽ നിന്നുണ്ടാകുന്ന ഏതെങ്കിലും തെറ്റിദ്ധാരണകൾക്കോ തെറ്റായ വ്യാഖ്യാനങ്ങൾക്കോ ഞങ്ങൾ ഉത്തരവാദികളല്ല.\n\n" ] } ], "metadata": { "anaconda-cloud": {}, "colab": { "name": "notebook.ipynb", "provenance": [] }, "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.5.4" }, "coopTranslator": { "original_hash": "6301339d1c9a301b00639c635dc9b731", "translation_date": "2025-12-19T17:28:20+00:00", "source_file": "2-Working-With-Data/08-data-preparation/notebook.ipynb", "language_code": "ml" } }, "nbformat": 4, "nbformat_minor": 0 }