Skip to content

Instantly share code, notes, and snippets.

@Clivern
Created December 17, 2023 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Clivern/c35182fd192120d1c1377010fc45174e to your computer and use it in GitHub Desktop.
Save Clivern/c35182fd192120d1c1377010fc45174e to your computer and use it in GitHub Desktop.
Supervised Learning With scikit-learn
Display the source blob
Display the rendered blob
Raw
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip3 install numpy scipy matplotlib ipython scikit-learn pandas ","metadata":{"execution":{"iopub.status.busy":"2023-12-16T22:57:07.279424Z","iopub.execute_input":"2023-12-16T22:57:07.280252Z","iopub.status.idle":"2023-12-16T22:57:23.325605Z","shell.execute_reply.started":"2023-12-16T22:57:07.280213Z","shell.execute_reply":"2023-12-16T22:57:23.324046Z"},"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"Requirement already satisfied: numpy in /opt/conda/lib/python3.10/site-packages (1.24.3)\nRequirement already satisfied: scipy in /opt/conda/lib/python3.10/site-packages (1.11.4)\nRequirement already satisfied: matplotlib in /opt/conda/lib/python3.10/site-packages (3.7.4)\nRequirement already satisfied: ipython in /opt/conda/lib/python3.10/site-packages (8.14.0)\nRequirement already satisfied: scikit-learn in /opt/conda/lib/python3.10/site-packages (1.2.2)\nRequirement already satisfied: pandas in /opt/conda/lib/python3.10/site-packages (2.0.3)\nRequirement already satisfied: contourpy>=1.0.1 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (1.1.0)\nRequirement already satisfied: cycler>=0.10 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (0.11.0)\nRequirement already satisfied: fonttools>=4.22.0 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (4.42.1)\nRequirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (1.4.4)\nRequirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (21.3)\nRequirement already satisfied: pillow>=6.2.0 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (10.1.0)\nRequirement already satisfied: pyparsing>=2.3.1 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (3.0.9)\nRequirement already satisfied: python-dateutil>=2.7 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (2.8.2)\nRequirement already satisfied: backcall in /opt/conda/lib/python3.10/site-packages (from ipython) (0.2.0)\nRequirement already satisfied: decorator in /opt/conda/lib/python3.10/site-packages (from ipython) (5.1.1)\nRequirement already satisfied: jedi>=0.16 in /opt/conda/lib/python3.10/site-packages (from ipython) (0.19.0)\nRequirement already satisfied: matplotlib-inline in /opt/conda/lib/python3.10/site-packages (from ipython) (0.1.6)\nRequirement already satisfied: pickleshare in /opt/conda/lib/python3.10/site-packages (from ipython) (0.7.5)\nRequirement already satisfied: prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30 in /opt/conda/lib/python3.10/site-packages (from ipython) (3.0.39)\nRequirement already satisfied: pygments>=2.4.0 in /opt/conda/lib/python3.10/site-packages (from ipython) (2.16.1)\nRequirement already satisfied: stack-data in /opt/conda/lib/python3.10/site-packages (from ipython) (0.6.2)\nRequirement already satisfied: traitlets>=5 in /opt/conda/lib/python3.10/site-packages (from ipython) (5.9.0)\nRequirement already satisfied: pexpect>4.3 in /opt/conda/lib/python3.10/site-packages (from ipython) (4.8.0)\nRequirement already satisfied: joblib>=1.1.1 in /opt/conda/lib/python3.10/site-packages (from scikit-learn) (1.3.2)\nRequirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.10/site-packages (from scikit-learn) (3.2.0)\nRequirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.10/site-packages (from pandas) (2023.3)\nRequirement already satisfied: tzdata>=2022.1 in /opt/conda/lib/python3.10/site-packages (from pandas) (2023.3)\nRequirement already satisfied: parso<0.9.0,>=0.8.3 in /opt/conda/lib/python3.10/site-packages (from jedi>=0.16->ipython) (0.8.3)\nRequirement already satisfied: ptyprocess>=0.5 in /opt/conda/lib/python3.10/site-packages (from pexpect>4.3->ipython) (0.7.0)\nRequirement already satisfied: wcwidth in /opt/conda/lib/python3.10/site-packages (from prompt-toolkit!=3.0.37,<3.1.0,>=3.0.30->ipython) (0.2.6)\nRequirement already satisfied: six>=1.5 in /opt/conda/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)\nRequirement already satisfied: executing>=1.2.0 in /opt/conda/lib/python3.10/site-packages (from stack-data->ipython) (1.2.0)\nRequirement already satisfied: asttokens>=2.1.0 in /opt/conda/lib/python3.10/site-packages (from stack-data->ipython) (2.2.1)\nRequirement already satisfied: pure-eval in /opt/conda/lib/python3.10/site-packages (from stack-data->ipython) (0.2.2)\n","output_type":"stream"}]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\n\ndata = [\n {\n 'age': 66,\n 'num_of_cars': 1,\n 'owns_house': 'yes',\n 'num_of_childs': 2,\n 'marital_status': 'widowed',\n 'owns_dog': 'no',\n 'bought_boat': 'yes'\n },\n {\n 'age': 52,\n 'num_of_cars': 2,\n 'owns_house': 'yes',\n 'num_of_childs': 3,\n 'marital_status': 'married',\n 'owns_dog': 'no',\n 'bought_boat': 'yes'\n },\n {\n 'age': 22,\n 'num_of_cars': 0,\n 'owns_house': 'no',\n 'num_of_childs': 0,\n 'marital_status': 'married',\n 'owns_dog': 'yes',\n 'bought_boat': 'no'\n },\n {\n 'age': 25,\n 'num_of_cars': 1,\n 'owns_house': 'no',\n 'num_of_childs': 1,\n 'marital_status': 'single',\n 'owns_dog': 'no',\n 'bought_boat': 'no'\n },\n {\n 'age': 44,\n 'num_of_cars': 0,\n 'owns_house': 'no',\n 'num_of_childs': 2,\n 'marital_status': 'divorced',\n 'owns_dog': 'yes',\n 'bought_boat': 'no'\n },\n {\n 'age': 39,\n 'num_of_cars': 1,\n 'owns_house': 'yes',\n 'num_of_childs': 2,\n 'marital_status': 'married',\n 'owns_dog': 'yes',\n 'bought_boat': 'no'\n },\n {\n 'age': 26,\n 'num_of_cars': 1,\n 'owns_house': 'no',\n 'num_of_childs': 2,\n 'marital_status': 'single',\n 'owns_dog': 'no',\n 'bought_boat': 'no'\n },\n {\n 'age': 40,\n 'num_of_cars': 3,\n 'owns_house': 'yes',\n 'num_of_childs': 1,\n 'marital_status': 'married',\n 'owns_dog': 'yes',\n 'bought_boat': 'no'\n },\n {\n 'age': 53,\n 'num_of_cars': 2,\n 'owns_house': 'yes',\n 'num_of_childs': 2,\n 'marital_status': 'divorced',\n 'owns_dog': 'no',\n 'bought_boat': 'no'\n },\n {\n 'age': 64,\n 'num_of_cars': 2,\n 'owns_house': 'yes',\n 'num_of_childs': 3,\n 'marital_status': 'divorced',\n 'owns_dog': 'no',\n 'bought_boat': 'no'\n },\n {\n 'age': 58,\n 'num_of_cars': 2,\n 'owns_house': 'yes',\n 'num_of_childs': 2,\n 'marital_status': 'married',\n 'owns_dog': 'yes',\n 'bought_boat': 'yes'\n },\n {\n 'age': 33,\n 'num_of_cars': 1,\n 'owns_house': 'no',\n 'num_of_childs': 1,\n 'marital_status': 'single',\n 'owns_dog': 'no',\n 'bought_boat': 'no'\n }\n]\n\nflags = {\n 'single': 1,\n 'married': 2,\n 'divorced': 3,\n 'widowed': 4,\n 'no': 0,\n 'yes': 1\n}","metadata":{"execution":{"iopub.status.busy":"2023-12-16T22:58:01.940716Z","iopub.execute_input":"2023-12-16T22:58:01.941156Z","iopub.status.idle":"2023-12-16T22:58:02.757730Z","shell.execute_reply.started":"2023-12-16T22:58:01.941121Z","shell.execute_reply":"2023-12-16T22:58:02.756457Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"def transform_data(data):\n out = {'x': [], 'y': []}\n for item in data:\n out['x'].append([\n item['age'],\n item['num_of_cars'],\n 1 if item['owns_house'] == 'yes' else 0,\n item['num_of_childs'],\n flags[item['marital_status']],\n 1 if item['owns_dog'] == 'yes' else 0\n ])\n out['y'].append(1 if item['bought_boat'] == 'yes' else 0)\n return out\n\n_data = transform_data(data)\nprint(_data)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T22:58:06.524914Z","iopub.execute_input":"2023-12-16T22:58:06.526058Z","iopub.status.idle":"2023-12-16T22:58:06.535429Z","shell.execute_reply.started":"2023-12-16T22:58:06.526016Z","shell.execute_reply":"2023-12-16T22:58:06.533918Z"},"trusted":true},"execution_count":3,"outputs":[{"name":"stdout","text":"{'x': [[66, 1, 1, 2, 4, 0], [52, 2, 1, 3, 2, 0], [22, 0, 0, 0, 2, 1], [25, 1, 0, 1, 1, 0], [44, 0, 0, 2, 3, 1], [39, 1, 1, 2, 2, 1], [26, 1, 0, 2, 1, 0], [40, 3, 1, 1, 2, 1], [53, 2, 1, 2, 3, 0], [64, 2, 1, 3, 3, 0], [58, 2, 1, 2, 2, 1], [33, 1, 0, 1, 1, 0]], 'y': [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0]}\n","output_type":"stream"}]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(_data['x'], _data['y'])\n\n# Train the model\nmodel = LogisticRegression()\nmodel.fit(X_train, y_train)\n\naccuracy = model.score(X_test, y_test)\n\nprint(accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-12-16T22:58:56.442791Z","iopub.execute_input":"2023-12-16T22:58:56.443190Z","iopub.status.idle":"2023-12-16T22:58:56.467709Z","shell.execute_reply.started":"2023-12-16T22:58:56.443158Z","shell.execute_reply":"2023-12-16T22:58:56.466561Z"},"trusted":true},"execution_count":13,"outputs":[{"name":"stdout","text":"0.6666666666666666\n","output_type":"stream"}]},{"cell_type":"code","source":"inputs = [\n {\n 'age': 58,\n 'num_of_cars': 2,\n 'owns_house': 'yes',\n 'num_of_childs': 2,\n 'marital_status': 'married',\n 'owns_dog': 'yes'\n },\n {\n 'age': 33,\n 'num_of_cars': 1,\n 'owns_house': 'no',\n 'num_of_childs': 1,\n 'marital_status': 'single',\n 'owns_dog': 'no'\n }\n]\n\ndef transform_inputs(inputs):\n ins = []\n for inp in inputs:\n ins.append([\n inp['age'],\n inp['num_of_cars'],\n 1 if inp['owns_house'] == 'yes' else 0,\n inp['num_of_childs'],\n flags[inp['marital_status']],\n 1 if inp['owns_dog'] == 'yes' else 0\n ])\n return ins\n\ndef transform_prediction(predictions):\n out = []\n for prediction in predictions:\n out.append({\"buys_boat\": True} if prediction == 1 else {\"buys_boat\": False})\n return out\n\nprediction = model.predict(transform_inputs(inputs))\nprint(transform_prediction(prediction))","metadata":{"execution":{"iopub.status.busy":"2023-12-16T22:58:58.379727Z","iopub.execute_input":"2023-12-16T22:58:58.381033Z","iopub.status.idle":"2023-12-16T22:58:58.392885Z","shell.execute_reply.started":"2023-12-16T22:58:58.380964Z","shell.execute_reply":"2023-12-16T22:58:58.391697Z"},"trusted":true},"execution_count":14,"outputs":[{"name":"stdout","text":"[{'buys_boat': True}, {'buys_boat': False}]\n","output_type":"stream"}]}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment