{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e1bdbd46-1f35-4373-80ec-727f0e26f009", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "from sklearn.datasets import load_iris\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.linear_model import LinearRegression\n", "from sklearn.metrics import accuracy_score\n", "\n", "import warnings\n", "warnings.filterwarnings(\"ignore\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "327dafe0-68d4-4200-a889-b03bc97a1057", "metadata": {}, "outputs": [], "source": [ "iris = load_iris()" ] }, { "cell_type": "code", "execution_count": 3, "id": "5ced7579-bb9f-4a20-abe2-c0c258ef4073", "metadata": {}, "outputs": [], "source": [ "X = iris.data[:, :2]\n", "y = iris.target" ] }, { "cell_type": "code", "execution_count": 4, "id": "50359601-4b0d-4a94-a1c8-44a833b8f4e5", "metadata": { "tags": [] }, "outputs": [], "source": [ "x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)" ] }, { "cell_type": "code", "execution_count": 5, "id": "17fc4619-c5c0-4beb-81df-81617b1c7a56", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([[6.3, 3.3],\n", " [6.5, 3. ],\n", " [5.6, 2.5],\n", " [5.7, 2.8],\n", " [6.4, 2.8]]),\n", " array([1, 2, 1, 1, 2]))" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_train[:5], y_train[:5]" ] }, { "cell_type": "code", "execution_count": 6, "id": "510d7a07-7746-4305-96d6-a74bc5a7f144", "metadata": {}, "outputs": [], "source": [ "model = LinearRegression()" ] }, { "cell_type": "code", "execution_count": 7, "id": "733f81f4-fc25-41a2-8c7d-e6e4abd70143", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "LinearRegression()" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.fit(x_train, y_train)" ] }, { "cell_type": "code", "execution_count": 8, "id": "267ed05e-7285-4873-ae8c-2396f986bf31", "metadata": {}, "outputs": [], "source": [ "y_pred = model.predict(x_test)" ] }, { "cell_type": "code", "execution_count": 9, "id": "0dd6f9d6-89d4-461f-9110-601d44126512", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.39289151, 0.42634152, 2.71549092, 1.2516593 , 1.912959 ,\n", " 0.47120288, 0.95447788, 1.78644379, 1.86880801, 1.2369423 ,\n", " 1.42232551, 0.29317817, 0.47856138, 0.30053667, -0.01943061])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_pred" ] }, { "cell_type": "code", "execution_count": 10, "id": "6a65cd71-b625-4f31-894d-a4d0403fd1b1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1., 0., 3., 1., 2., 0., 1., 2., 2., 1., 1., 0., 0.,\n", " 0., -0.])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_pred = np.round(y_pred)\n", "y_pred" ] }, { "cell_type": "code", "execution_count": 11, "id": "ef5d8327-7ee8-43a6-b4f9-0785e8467d23", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 0, 2, 1, 1, 0, 1, 2, 1, 1, 2, 0, 0, 0, 0])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y_test" ] }, { "cell_type": "code", "execution_count": 12, "id": "b28aa4ac-87ab-45a4-b5c8-e7b125895c25", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7333333333333333" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "accuracy_score(y_test, np.round(y_pred))" ] }, { "cell_type": "code", "execution_count": 13, "id": "d5cf8629-f623-4a3e-9400-8d7f6215383e", "metadata": {}, "outputs": [], "source": [ "from joblib import dump, load" ] }, { "cell_type": "code", "execution_count": 14, "id": "caa6d389-b358-4160-a342-215013c5b2d9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['reg_arr_model_2_dim.joblib']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dump(model, \"reg_arr_model_2_dim.joblib\")" ] }, { "cell_type": "code", "execution_count": 15, "id": "f0804a91-46d4-4cec-bd60-bb5f022443bf", "metadata": {}, "outputs": [], "source": [ "model = load(\"reg_arr_model_2_dim.joblib\")" ] }, { "cell_type": "code", "execution_count": 16, "id": "07584f03-f1da-4014-a539-ca0e033a6356", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1.39289151])" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.predict(x_test[:1])" ] }, { "cell_type": "code", "execution_count": 17, "id": "06cf5cbb-0a96-4501-8fa6-bfc680d8aa20", "metadata": {}, "outputs": [], "source": [ "import skops.hub_utils as hub_utils" ] }, { "cell_type": "code", "execution_count": 18, "id": "f4349089-88c9-49d2-8b65-351cabb74fd8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[6.1, 2.8],\n", " [5.7, 3.8],\n", " [7.7, 2.6],\n", " [6. , 2.9],\n", " [6.8, 2.8],\n", " [5.4, 3.4],\n", " [5.6, 2.9],\n", " [6.9, 3.1],\n", " [6.2, 2.2],\n", " [5.8, 2.7],\n", " [6.5, 3.2],\n", " [4.8, 3. ],\n", " [5.5, 3.5],\n", " [4.9, 3.1],\n", " [5.1, 3.8]])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x_test" ] }, { "cell_type": "code", "execution_count": 19, "id": "a4294f1f-5eeb-460f-a872-ba487a229093", "metadata": {}, "outputs": [], "source": [ "!rm -rf /Users/macbookpro/MyProjects/dev/dst\n", "!mkdir /Users/macbookpro/MyProjects/dev/dst" ] }, { "cell_type": "code", "execution_count": 20, "id": "41a49678-1a01-439d-8f92-29f1884e5f79", "metadata": {}, "outputs": [], "source": [ "hub_utils.init(\n", " model=\"/Users/macbookpro/MyProjects/dev/reg_arr_model_2_dim.joblib\",\n", " requirements=[\"scikit-learn\", \"numpy\"],\n", " dst=\"/Users/macbookpro/MyProjects/dev/dst\",\n", " task=\"tabular-classification\",\n", " data=x_train[:3]\n", ")" ] }, { "cell_type": "code", "execution_count": 21, "id": "fe1a1620-66e3-4543-a506-1195ac39831e", "metadata": {}, "outputs": [], "source": [ "from skops.card import metadata_from_config" ] }, { "cell_type": "code", "execution_count": 22, "id": "9e0a6d81-e1c4-4a75-b510-772eb44e924d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "library_name: sklearn\n", "tags:\n", "- sklearn\n", "- skops\n", "- tabular-classification\n", "widget:\n", " structuredData:\n", " x0:\n", " - 6.3\n", " - 6.5\n", " - 5.6\n", " x1:\n", " - 3.3\n", " - 3.0\n", " - 2.5" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "metadata_from_config(\"/Users/macbookpro/MyProjects/dev/dst/config.json\")" ] }, { "cell_type": "code", "execution_count": null, "id": "412fc8e8-ab28-44bf-88be-6cac61c168f1", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.15" } }, "nbformat": 4, "nbformat_minor": 5 }