diff --git a/your-code/main.ipynb b/your-code/main.ipynb index f1794a8..0b74c27 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -52,10 +52,13 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.model_selection import train_test_split\n", + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=111)\n" + ] }, { "cell_type": "markdown", @@ -66,10 +69,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.linear_model import LinearRegression\n", + "lr = LinearRegression()\n", + "lr.fit(X_train, y_train)\n", + "y_train_pred = lr.predict(X_train).round(1)\n", + "y_test_pred = lr.predict(X_test).round(1)\n" + ] }, { "cell_type": "markdown", @@ -80,10 +89,32 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn import metrics\n", + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "train: 0.737\n", + "test: 0.745\n" + ] + } + ], + "source": [ + "print(\"train: \",metrics.r2_score(y_train, y_train_pred).round(3))\n", + "print(\"test: \",metrics.r2_score(y_test, y_test_pred).round(3))" + ] }, { "cell_type": "markdown", @@ -94,10 +125,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "train: 21.775\n", + "test: 23.105\n" + ] + } + ], + "source": [ + "print(\"train: \",metrics.mean_squared_error(y_train, y_train_pred).round(3))\n", + "print(\"test: \",metrics.mean_squared_error(y_test, y_test_pred).round(3))" + ] }, { "cell_type": "markdown", @@ -108,10 +151,22 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "train: 3.225\n", + "test: 3.382\n" + ] + } + ], + "source": [ + "print(\"train: \",metrics.mean_absolute_error(y_train, y_train_pred).round(3))\n", + "print(\"test: \",metrics.mean_absolute_error(y_test, y_test_pred).round(3))" + ] }, { "cell_type": "markdown", @@ -122,7 +177,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -147,10 +202,12 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=141)\n" + ] }, { "cell_type": "markdown", @@ -161,10 +218,62 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.linear_model import LogisticRegression" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/alejandro/.local/lib/python3.8/site-packages/sklearn/utils/validation.py:63: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n", + " return f(*args, **kwargs)\n", + "/home/alejandro/.local/lib/python3.8/site-packages/sklearn/linear_model/_logistic.py:763: ConvergenceWarning: lbfgs failed to converge (status=1):\n", + "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n", + "\n", + "Increase the number of iterations (max_iter) or scale the data as shown in:\n", + " https://scikit-learn.org/stable/modules/preprocessing.html\n", + "Please also refer to the documentation for alternative solver options:\n", + " https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n", + " n_iter_i = _check_optimize_result(\n" + ] + }, + { + "data": { + "text/plain": [ + "LogisticRegression()" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "log = LogisticRegression()\n", + "log.fit(\n", + " X_train, \n", + " y_train\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "y_train_pred = log.predict(X_train)\n", + "y_test_pred = log.predict(X_test)" + ] }, { "cell_type": "markdown", @@ -175,10 +284,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.metrics import accuracy_score" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "train: 0.9833333333333333\n", + "test: 0.9666666666666667\n" + ] + } + ], + "source": [ + "print(\"train: \",accuracy_score(y_train, y_train_pred))\n", + "print(\"test: \",accuracy_score(y_test, y_test_pred))" + ] }, { "cell_type": "markdown", @@ -189,10 +319,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.metrics import balanced_accuracy_score" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "train: 0.981981981981982\n", + "test: 0.9583333333333334\n" + ] + } + ], + "source": [ + "print(\"train: \",balanced_accuracy_score(y_train, y_train_pred))\n", + "print(\"test: \",balanced_accuracy_score(y_test, y_test_pred))" + ] }, { "cell_type": "markdown", @@ -203,10 +354,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.metrics import precision_score" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "train: 0.9840909090909091\n", + "test: 0.9690476190476189\n" + ] + } + ], + "source": [ + "print(\"train: \",precision_score(y_train, y_train_pred, average=\"weighted\"))\n", + "print(\"test: \",precision_score(y_test, y_test_pred, average=\"weighted\"))" + ] }, { "cell_type": "markdown", @@ -217,10 +389,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.metrics import recall_score" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "train: 0.9833333333333333\n", + "test: 0.9666666666666667\n" + ] + } + ], + "source": [ + "print(\"train: \",recall_score(y_train, y_train_pred, average=\"weighted\"))\n", + "print(\"test: \",recall_score(y_test, y_test_pred, average=\"weighted\"))" + ] }, { "cell_type": "markdown", @@ -231,10 +424,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from sklearn.metrics import f1_score" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "train: 0.9832956503014643\n", + "test: 0.9661728395061729\n" + ] + } + ], + "source": [ + "print(\"train: \",f1_score(y_train, y_train_pred, average=\"weighted\"))\n", + "print(\"test: \",f1_score(y_test, y_test_pred, average=\"weighted\"))" + ] }, { "cell_type": "markdown", @@ -245,17 +459,163 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col_0012
class
04100
10352
20042
\n", + "
" + ], + "text/plain": [ + "col_0 0 1 2\n", + "class \n", + "0 41 0 0\n", + "1 0 35 2\n", + "2 0 0 42" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.crosstab(\n", + " y_train[\"class\"],\n", + " y_train_pred\n", + ")" + ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
col_0012
class
0900
10130
2017
\n", + "
" + ], + "text/plain": [ + "col_0 0 1 2\n", + "class \n", + "0 9 0 0\n", + "1 0 13 0\n", + "2 0 1 7" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pd.crosstab(\n", + " y_test[\"class\"],\n", + " y_test_pred\n", + ")" + ] }, { "cell_type": "markdown", @@ -267,7 +627,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -281,7 +641,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.8.10" } }, "nbformat": 4,