{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "NnI3VscLsD6z"
},
"outputs": [],
"source": [
"#import library\n",
"\n",
"import pickle\n",
"import json\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZuS5HPBkBbZ4",
"outputId": "7561ff89-4d83-48c3-b809-28e633e72508"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Name: scikit-learn\n",
"Version: 1.5.1\n",
"Summary: A set of python modules for machine learning and data mining\n",
"Home-page: https://scikit-learn.org\n",
"Author: \n",
"Author-email: \n",
"License: new BSD\n",
"Location: /opt/anaconda3/envs/phase1/lib/python3.12/site-packages\n",
"Requires: joblib, numpy, scipy, threadpoolctl\n",
"Required-by: feature-engine\n"
]
}
],
"source": [
"!pip show scikit-learn"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bDmKBK0SolkE"
},
"source": [
"### load model"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "4aqmhcqinrlU"
},
"outputs": [],
"source": [
"#Load model\n",
"\n",
"with open('list_cat_cols.txt', 'r') as file_1:\n",
" list_cat_col = json.load(file_1)\n",
"\n",
"with open('list_num_cols.txt', 'r') as file_2:\n",
" list_num_col = json.load(file_2)\n",
"\n",
"with open('model_encoder.pkl', 'rb') as file_3:\n",
" model_encoder = pickle.load(file_3)\n",
"\n",
"with open('model_scaler.pkl', 'rb') as file_4:\n",
" model_scaler = pickle.load(file_4)\n",
"\n",
"with open('model_lin_reg.pkl', 'rb') as file_5:\n",
" model_lin_reg = pickle.load(file_5)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ra7wa9DyokUs"
},
"source": [
"###Inferece"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 100
},
"id": "_6zz1wBVoYFG",
"outputId": "e7389c1d-6944-45e7-a7d2-311fdc3b022d"
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Name | \n",
" Age | \n",
" Height | \n",
" Weight | \n",
" Price | \n",
" AttackingWorkRate | \n",
" DefensiveWorkRate | \n",
" PaceTotal | \n",
" ShootingTotal | \n",
" PassingTotal | \n",
" DribblingTotal | \n",
" DefendingTotal | \n",
" PhysicalityTotal | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" Hana | \n",
" 50 | \n",
" 180 | \n",
" 70 | \n",
" 30000000 | \n",
" Medium | \n",
" Low | \n",
" 60 | \n",
" 80 | \n",
" 30 | \n",
" 70 | \n",
" 60 | \n",
" 80 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Name Age Height Weight Price AttackingWorkRate DefensiveWorkRate \\\n",
"0 Hana 50 180 70 30000000 Medium Low \n",
"\n",
" PaceTotal ShootingTotal PassingTotal DribblingTotal DefendingTotal \\\n",
"0 60 80 30 70 60 \n",
"\n",
" PhysicalityTotal \n",
"0 80 "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
" #Create new data\n",
"#Gunakan keseluruhan data\n",
"\n",
"data_inf = {\n",
" 'Name' : 'Hana',\n",
" 'Age' : 50,\n",
" 'Height' : 180,\n",
" 'Weight' : 70,\n",
" 'Price' : 30000000,\n",
" 'AttackingWorkRate' : 'Medium',\n",
" 'DefensiveWorkRate' : 'Low',\n",
" 'PaceTotal' :60,\n",
" 'ShootingTotal': 80,\n",
" 'PassingTotal' : 30,\n",
" 'DribblingTotal' :70,\n",
" 'DefendingTotal' :60,\n",
" 'PhysicalityTotal':80,\n",
"}\n",
"\n",
"data_inf = pd.DataFrame([data_inf])\n",
"data_inf"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 80
},
"id": "JHPUX35kpjyL",
"outputId": "a30b72c5-036a-46fe-e0dd-1a61788d0dc5"
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Age | \n",
" Height | \n",
" Weight | \n",
" Price | \n",
" PaceTotal | \n",
" ShootingTotal | \n",
" PassingTotal | \n",
" DribblingTotal | \n",
" DefendingTotal | \n",
" PhysicalityTotal | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 50 | \n",
" 180 | \n",
" 70 | \n",
" 30000000 | \n",
" 60 | \n",
" 80 | \n",
" 30 | \n",
" 70 | \n",
" 60 | \n",
" 80 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Age Height Weight Price PaceTotal ShootingTotal PassingTotal \\\n",
"0 50 180 70 30000000 60 80 30 \n",
"\n",
" DribblingTotal DefendingTotal PhysicalityTotal \n",
"0 70 60 80 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#split between numerical and categorical columns\n",
"\n",
"data_inf_num = data_inf[list_num_col]\n",
"data_inf_cat = data_inf[list_cat_col]\n",
"data_inf_num"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "Yligyt0up4Ld"
},
"outputs": [],
"source": [
"#feature scaling and encoding\n",
"\n",
"data_inf_num_scaled = model_scaler.transform(data_inf_num)\n",
"data_inf_cat_encoded = model_encoder.transform(data_inf_cat)\n",
"data_inf_final = np.concatenate([data_inf_num_scaled, data_inf_cat_encoded], axis = 1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "5EBS6wQYqUfb",
"outputId": "a29e15a7-27dd-4284-8152-9ba9a69a1916"
},
"outputs": [
{
"data": {
"text/plain": [
"array([82.7433109])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#predict using linear reg model\n",
"\n",
"y_pred_inf = model_lin_reg.predict(data_inf_final)\n",
"y_pred_inf"
]
}
],
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"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.12.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
}