diff --git a/3-Web-App/1-Web-App/notebook.ipynb b/3-Web-App/1-Web-App/notebook.ipynb index c55a2876..a4685d24 100644 --- a/3-Web-App/1-Web-App/notebook.ipynb +++ b/3-Web-App/1-Web-App/notebook.ipynb @@ -1,8 +1,423 @@ { - "cells": [], + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Building the web Appliation" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
datetimecitystatecountryshapeduration (seconds)duration (hours/min)commentsdate postedlatitudelongitude
010/10/1949 20:30san marcostxuscylinder2700.045 minutesThis event took place in early fall around 194...4/27/200429.883056-97.941111
110/10/1949 21:00lackland afbtxNaNlight7200.01-2 hrs1949 Lackland AFB&#44 TX. Lights racing acros...12/16/200529.384210-98.581082
210/10/1955 17:00chester (uk/england)NaNgbcircle20.020 secondsGreen/Orange circular disc over Chester&#44 En...1/21/200853.200000-2.916667
310/10/1956 21:00ednatxuscircle20.01/2 hourMy older brother and twin sister were leaving ...1/17/200428.978333-96.645833
410/10/1960 20:00kaneohehiuslight900.015 minutesAS a Marine 1st Lt. flying an FJ4B fighter/att...1/22/200421.418056-157.803611
\n", + "
" + ], + "text/plain": [ + " datetime city state country shape \\\n", + "0 10/10/1949 20:30 san marcos tx us cylinder \n", + "1 10/10/1949 21:00 lackland afb tx NaN light \n", + "2 10/10/1955 17:00 chester (uk/england) NaN gb circle \n", + "3 10/10/1956 21:00 edna tx us circle \n", + "4 10/10/1960 20:00 kaneohe hi us light \n", + "\n", + " duration (seconds) duration (hours/min) \\\n", + "0 2700.0 45 minutes \n", + "1 7200.0 1-2 hrs \n", + "2 20.0 20 seconds \n", + "3 20.0 1/2 hour \n", + "4 900.0 15 minutes \n", + "\n", + " comments date posted latitude \\\n", + "0 This event took place in early fall around 194... 4/27/2004 29.883056 \n", + "1 1949 Lackland AFB, TX. Lights racing acros... 12/16/2005 29.384210 \n", + "2 Green/Orange circular disc over Chester, En... 1/21/2008 53.200000 \n", + "3 My older brother and twin sister were leaving ... 1/17/2004 28.978333 \n", + "4 AS a Marine 1st Lt. flying an FJ4B fighter/att... 1/22/2004 21.418056 \n", + "\n", + " longitude \n", + "0 -97.941111 \n", + "1 -98.581082 \n", + "2 -2.916667 \n", + "3 -96.645833 \n", + "4 -157.803611 " + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd \n", + "import numpy as np\n", + "ufos = pd.read_csv('./data/ufos.csv')\n", + "ufos.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['us', nan, 'gb', 'ca', 'au', 'de'], dtype=object)" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ufos = pd.DataFrame({\n", + " 'Seconds': ufos['duration (seconds)'],\n", + " 'Country': ufos['country'],\n", + " 'Latitude': ufos['latitude'],\n", + " 'Longitude': ufos['longitude']\n", + "})\n", + "ufos['Country'].unique()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Index: 25863 entries, 2 to 80330\n", + "Data columns (total 4 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 Seconds 25863 non-null float64\n", + " 1 Country 25863 non-null object \n", + " 2 Latitude 25863 non-null float64\n", + " 3 Longitude 25863 non-null float64\n", + "dtypes: float64(3), object(1)\n", + "memory usage: 1010.3+ KB\n" + ] + } + ], + "source": [ + "ufos.dropna(inplace=True)\n", + "ufos = ufos[(ufos['Seconds'] >= 1) & (ufos['Seconds'] <= 60)]\n", + "ufos.info()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
SecondsCountryLatitudeLongitude
220.0353.200000-2.916667
320.0428.978333-96.645833
1430.0435.823889-80.253611
2360.0445.582778-122.352222
243.0351.783333-0.783333
\n", + "
" + ], + "text/plain": [ + " Seconds Country Latitude Longitude\n", + "2 20.0 3 53.200000 -2.916667\n", + "3 20.0 4 28.978333 -96.645833\n", + "14 30.0 4 35.823889 -80.253611\n", + "23 60.0 4 45.582778 -122.352222\n", + "24 3.0 3 51.783333 -0.783333" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.preprocessing import LabelEncoder\n", + "ufos['Country'] = LabelEncoder().fit_transform(ufos['Country'])\n", + "ufos.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.model_selection import train_test_split\n", + "Selected_features = ['Seconds','Latitude','Longitude']\n", + "x = ufos[Selected_features]\n", + "y = ufos['Country']\n", + "Xtrain, Xtest, ytrain, ytest = train_test_split(x, y, test_size=0.2, random_state=42)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Confusion matrix result\n", + "##### [0,0] = 41: 41 samples of country 0 were correctly predicted as 0.\n", + "##### [1,1] = 50: 50 samples of country 1 were correctly predicted as 1.\n", + "##### [1,4] = 238: 238 samples of country 1 were incorrectly predicted as country 4.\n", + "##### [4,4] = 4686: 4686 samples of country 4 were correctly predicted as 4.\n", + "##### [4,1] = 14: 14 samples of country 4 were incorrectly predicted as 1." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " precision recall f1-score support\n", + "\n", + " 0 1.00 1.00 1.00 41\n", + " 1 0.78 0.17 0.28 288\n", + " 2 1.00 0.90 0.95 10\n", + " 3 0.99 1.00 1.00 134\n", + " 4 0.95 1.00 0.97 4700\n", + "\n", + " accuracy 0.95 5173\n", + " macro avg 0.95 0.81 0.84 5173\n", + "weighted avg 0.94 0.95 0.94 5173\n", + "\n", + "Predicted labels: [4 4 4 ... 4 4 1]\n", + "Accuracy [4 4 4 ... 4 4 1]\n", + "Confusion matrix:\n", + " [[ 41 0 0 0 0]\n", + " [ 0 50 0 0 238]\n", + " [ 0 0 9 1 0]\n", + " [ 0 0 0 134 0]\n", + " [ 0 14 0 0 4686]]\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "d:\\AI\\MachineLearning\\ML-For-Beginners\\.venv\\Lib\\site-packages\\sklearn\\linear_model\\_logistic.py:465: 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" + ] + } + ], + "source": [ + "from sklearn.metrics import accuracy_score, classification_report, confusion_matrix\n", + "from sklearn.linear_model import LogisticRegression\n", + "model = LogisticRegression()\n", + "model.fit(Xtrain, ytrain)\n", + "prediction = model.predict(Xtest)\n", + "print(classification_report(ytest, prediction))\n", + "print('Predicted labels:', prediction)\n", + "print('Accuracy', prediction)\n", + "print('Confusion matrix:\\n', confusion_matrix(ytest, prediction))\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.3" } }, "nbformat": 4,