{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-10T19:13:18.787344Z","iopub.execute_input":"2022-10-10T19:13:18.788124Z","iopub.status.idle":"2022-10-10T19:13:18.823199Z","shell.execute_reply.started":"2022-10-10T19:13:18.787985Z","shell.execute_reply":"2022-10-10T19:13:18.821916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom sklearn import metrics\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:14:07.715457Z","iopub.execute_input":"2022-10-10T19:14:07.716591Z","iopub.status.idle":"2022-10-10T19:14:08.693053Z","shell.execute_reply.started":"2022-10-10T19:14:07.716546Z","shell.execute_reply":"2022-10-10T19:14:08.691688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_wine = pd.read_csv('../input/dataset123/winequality-red.csv')\ndf_wine.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:16:30.287296Z","iopub.execute_input":"2022-10-10T19:16:30.287752Z","iopub.status.idle":"2022-10-10T19:16:30.325871Z","shell.execute_reply.started":"2022-10-10T19:16:30.287719Z","shell.execute_reply":"2022-10-10T19:16:30.324528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_wine.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:16:41.673720Z","iopub.execute_input":"2022-10-10T19:16:41.674202Z","iopub.status.idle":"2022-10-10T19:16:41.700326Z","shell.execute_reply.started":"2022-10-10T19:16:41.674163Z","shell.execute_reply":"2022-10-10T19:16:41.699347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_wine.drop('quality', axis = 1)\ny = df_wine['quality']\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 893)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:16:49.548492Z","iopub.execute_input":"2022-10-10T19:16:49.548908Z","iopub.status.idle":"2022-10-10T19:16:49.560060Z","shell.execute_reply.started":"2022-10-10T19:16:49.548877Z","shell.execute_reply":"2022-10-10T19:16:49.558793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Common Trained Model\n\nmodel = RandomForestClassifier()\n\nmodel.fit(X_train, y_train)\ny_pred = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:16:56.869127Z","iopub.execute_input":"2022-10-10T19:16:56.870418Z","iopub.status.idle":"2022-10-10T19:16:57.274468Z","shell.execute_reply.started":"2022-10-10T19:16:56.870342Z","shell.execute_reply":"2022-10-10T19:16:57.273288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def accuracy(y_true, y_pred):\n    \n    \"\"\"\n    Function to calculate accuracy\n    -> param y_true: list of true values\n    -> param y_pred: list of predicted values\n    -> return: accuracy score\n    \n    \"\"\"\n    \n    # Intitializing variable to store count of correctly predicted classes\n    correct_predictions = 0\n    \n    for yt, yp in zip(y_true, y_pred):\n        \n        if yt == yp:\n            \n            correct_predictions += 1\n    \n    #returns accuracy\n    return correct_predictions / len(y_true)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:17:06.735077Z","iopub.execute_input":"2022-10-10T19:17:06.735521Z","iopub.status.idle":"2022-10-10T19:17:06.741906Z","shell.execute_reply.started":"2022-10-10T19:17:06.735484Z","shell.execute_reply":"2022-10-10T19:17:06.740626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy(y_test, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:17:13.070698Z","iopub.execute_input":"2022-10-10T19:17:13.071118Z","iopub.status.idle":"2022-10-10T19:17:13.078390Z","shell.execute_reply.started":"2022-10-10T19:17:13.071079Z","shell.execute_reply":"2022-10-10T19:17:13.077289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(metrics.accuracy_score(y_test, y_pred))","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:17:19.923251Z","iopub.execute_input":"2022-10-10T19:17:19.924067Z","iopub.status.idle":"2022-10-10T19:17:19.930903Z","shell.execute_reply.started":"2022-10-10T19:17:19.923987Z","shell.execute_reply":"2022-10-10T19:17:19.929971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Functions to compute True Positives, True Negatives, False Positives and False Negatives\n\ndef true_positive(y_true, y_pred):\n    \n    tp = 0\n    \n    for yt, yp in zip(y_true, y_pred):\n        \n        if yt == 1 and yp == 1:\n            tp += 1\n    \n    return tp\n\ndef true_negative(y_true, y_pred):\n    \n    tn = 0\n    \n    for yt, yp in zip(y_true, y_pred):\n        \n        if yt == 0 and yp == 0:\n            tn += 1\n            \n    return tn\n\ndef false_positive(y_true, y_pred):\n    \n    fp = 0\n    \n    for yt, yp in zip(y_true, y_pred):\n        \n        if yt == 0 and yp == 1:\n            fp += 1\n            \n    return fp\n\ndef false_negative(y_true, y_pred):\n    \n    fn = 0\n    \n    for yt, yp in zip(y_true, y_pred):\n        \n        if yt == 1 and yp == 0:\n            fn += 1\n            \n    return fn","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:17:44.963617Z","iopub.execute_input":"2022-10-10T19:17:44.964053Z","iopub.status.idle":"2022-10-10T19:17:44.974394Z","shell.execute_reply.started":"2022-10-10T19:17:44.964016Z","shell.execute_reply":"2022-10-10T19:17:44.973004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Computation of macro-averaged precision\n\ndef macro_precision(y_true, y_pred):\n\n    # find the number of classes\n    num_classes = len(np.unique(y_true))\n\n    # initialize precision to 0\n    precision = 0\n    \n    # loop over all classes\n    for class_ in list(y_true.unique()):\n        \n        # all classes except current are considered negative\n        temp_true = [1 if p == class_ else 0 for p in y_true]\n        temp_pred = [1 if p == class_ else 0 for p in y_pred]\n        \n        \n        # compute true positive for current class\n        tp = true_positive(temp_true, temp_pred)\n        \n        # compute false positive for current class\n        fp = false_positive(temp_true, temp_pred)\n        \n        \n        # compute precision for current class\n        temp_precision = tp / (tp + fp + 1e-6)\n        # keep adding precision for all classes\n        precision += temp_precision\n        \n    # calculate and return average precision over all classes\n    precision /= num_classes\n    \n    return precision","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:18:00.029639Z","iopub.execute_input":"2022-10-10T19:18:00.030065Z","iopub.status.idle":"2022-10-10T19:18:00.038198Z","shell.execute_reply.started":"2022-10-10T19:18:00.030031Z","shell.execute_reply":"2022-10-10T19:18:00.037196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Computation of macro-averaged fi score\n\ndef macro_f1(y_true, y_pred):\n\n    # find the number of classes\n    num_classes = len(np.unique(y_true))\n\n    # initialize f1 to 0\n    f1 = 0\n    \n    # loop over all classes\n    for class_ in list(y_true.unique()):\n        \n        # all classes except current are considered negative\n        temp_true = [1 if p == class_ else 0 for p in y_true]\n        temp_pred = [1 if p == class_ else 0 for p in y_pred]\n        \n        \n        # compute true positive for current class\n        tp = true_positive(temp_true, temp_pred)\n        \n        # compute false negative for current class\n        fn = false_negative(temp_true, temp_pred)\n        \n        # compute false positive for current class\n        fp = false_positive(temp_true, temp_pred)\n        \n        \n        # compute recall for current class\n        temp_recall = tp / (tp + fn + 1e-6)\n        \n        # compute precision for current class\n        temp_precision = tp / (tp + fp + 1e-6)\n        \n        \n        temp_f1 = 2 * temp_precision * temp_recall / (temp_precision + temp_recall + 1e-6)\n        \n        # keep adding f1 score for all classes\n        f1 += temp_f1\n        \n    # calculate and return average f1 score over all classes\n    f1 /= num_classes\n    \n    return f1","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:18:42.961219Z","iopub.execute_input":"2022-10-10T19:18:42.961661Z","iopub.status.idle":"2022-10-10T19:18:42.971849Z","shell.execute_reply.started":"2022-10-10T19:18:42.961620Z","shell.execute_reply":"2022-10-10T19:18:42.970770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.unique()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:19:04.352037Z","iopub.execute_input":"2022-10-10T19:19:04.352506Z","iopub.status.idle":"2022-10-10T19:19:04.363031Z","shell.execute_reply.started":"2022-10-10T19:19:04.352456Z","shell.execute_reply":"2022-10-10T19:19:04.361829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (18,8))\nsns.heatmap(metrics.confusion_matrix(y_test, y_pred), annot = True, xticklabels = y_test.unique(), yticklabels = y_test.unique(), cmap = 'summer')\nplt.xlabel('Predicted Labels')\nplt.ylabel('True Labels')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-10T19:19:10.218993Z","iopub.execute_input":"2022-10-10T19:19:10.220228Z","iopub.status.idle":"2022-10-10T19:19:10.647469Z","shell.execute_reply.started":"2022-10-10T19:19:10.220177Z","shell.execute_reply":"2022-10-10T19:19:10.646539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}