<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexander Ryzhkov on Medium]]></title>
        <description><![CDATA[Stories by Alexander Ryzhkov on Medium]]></description>
        <link>https://medium.com/@alexmryzhkov?source=rss-d920cc498f35------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*pSyrC3FFSo5Tg_7oBp17lw.jpeg</url>
            <title>Stories by Alexander Ryzhkov on Medium</title>
            <link>https://medium.com/@alexmryzhkov?source=rss-d920cc498f35------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 14:20:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alexmryzhkov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[LightAutoML preset usage tutorial]]></title>
            <link>https://medium.com/data-science/lightautoml-preset-usage-tutorial-2cce7da6f936?source=rss-d920cc498f35------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cce7da6f936</guid>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[automl]]></category>
            <category><![CDATA[ensembling]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Alexander Ryzhkov]]></dc:creator>
            <pubDate>Mon, 12 Apr 2021 21:52:03 GMT</pubDate>
            <atom:updated>2021-04-15T19:16:20.382Z</atom:updated>
            <content:encoded><![CDATA[<h3>LightAutoML vs Titanic: 80% accuracy in several lines of code</h3><p>In this tutorial, we are going to talk about how to automatically create ML models in several lines of code using the open-source framework LightAutoML for the Titanic Survival competition on Kaggle.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*grZb0sIZyS6qeM6c" /><figcaption>Photo by <a href="https://unsplash.com/@clarktibbs?utm_source=medium&amp;utm_medium=referral">Clark Tibbs</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>At the end of 2020 open-source python library <a href="https://github.com/sberbank-ai-lab/LightAutoML">LightAutoML</a> was released by the AutoML Team at Sber AI Lab as an Automated Machine Learning (AutoML) framework. It is designed to be lightweight and efficient for various tasks (binary/multiclass classification and regression) on tabular datasets, which contains different types of features: numeric, categorical, dates, texts etc.</p><p>LightAutoML installation is pretty simple — pip install -U lightautoml <br><a href="https://lightautoml.readthedocs.io/">Official LightAutoML documentation</a></p><p>LightAutoML provides not only presets for end-to-end ML task solving but also the easy-to-use ML pipeline creation constructor, including data preprocessing elements, advanced feature generation, CV schemes (including nested CVs), hyperparameters tuning, different models, and composition building methods. It also gives the user an option to generate model training and profiling reports to check model results and find insights that are not obvious from initial dataset.</p><p>Below we are going to show how to solve <a href="https://www.kaggle.com/c/titanic">Titanic — Machine Learning from Disaster</a> competition using LightAutoML — from python libraries import up to saving final submission file.</p><h3>Step 0.0. Super-short Titanic solution based on LightAutoML</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DpJvQVTiirzaxR_1fhg7hA.png" /><figcaption>Extreme short Titanic solution based on LightAutoML</figcaption></figure><p>The code above is available as a kernel <a href="https://www.kaggle.com/alexryzhkov/lightautoml-extreme-short-titanic-solution">here</a> and scores <strong>0.77990</strong> on Kaggle public leaderboard in just 7 minutes and 12 lines. The main LightAutoML part is only 3 lines — from 8th to 10th.</p><p>Below we will discuss <a href="https://www.kaggle.com/alexryzhkov/lightautoml-titanic-love">another kernel</a> with a score of <strong>0.79665, </strong>which is more structured like a real business ML solution and can be used as a template for your own projects.</p><h3>Step 0.1. Import necessary libraries</h3><p>At this step we import 3 standard python libraries, several libraries from the usual data scientist set, including <em>numpy</em>, <em>pandas,</em> and <em>sklearn</em> and 2 presets from LightAutoML — <em>TabularAutoML</em> and <em>TabularUtilizedAutoML</em>. We will discuss later what they can do and what the differences are between them.</p><pre><em># Standard python libraries</em><br>import os<br>import time<br>import re<br><br><em># Installed libraries</em><br>import numpy as np<br>import pandas as pd<br>from sklearn.metrics import accuracy_score, f1_score<br>from sklearn.model_selection import train_test_split<br><br><em># Imports from LightAutoML package</em><br>from lightautoml.automl.presets.tabular_presets import TabularAutoML, TabularUtilizedAutoML<br>from lightautoml.tasks import Task</pre><h3>Step 0.2. Datasets load</h3><p>Now we need to load the train and test dataset and the submission file, which we should fill with the predicted class:</p><pre>%%time<br><br>train_data = pd.read_csv(&#39;../input/titanic/train.csv&#39;)<br>train_data.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LRQ5duPcSarpL7HMr457VQ.png" /><figcaption>Train dataset</figcaption></figure><pre>test_data = pd.read_csv(&#39;../input/titanic/test.csv&#39;)<br>test_data.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qGZriWxeRiP5TgJZL6KbjQ.png" /><figcaption>Test dataset</figcaption></figure><pre>submission = pd.read_csv(&#39;../input/titanic/gender_submission.csv&#39;)<br>submission.head()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*hcBaw9panWgR0NtNmfhJVA.png" /><figcaption>Submission file template</figcaption></figure><h3>Step 0.3. Additional expert features creation block</h3><p>The cell below shows some user feature preparations, which can be helpful for LightAutoML to separate positive and negative class objects. The logic behind these features is ticket type extraction for Ticket column, family size calculation, name feature cleaning etc.:</p><pre>def get_title(name):<br>    title_search = re.search(&#39; ([A-Za-z]+)\.&#39;, name)<br>    <em># If the title exists, extract and return it.</em><br>    if title_search:<br>        return title_search.group(1)<br>    return &quot;&quot;<br><br>def create_extra_features(data):<br>    data[&#39;Ticket_type&#39;] = data[&#39;Ticket&#39;].map(lambda x: x[0:3])<br>    data[&#39;Name_Words_Count&#39;] = data[&#39;Name&#39;].map(lambda x: len(x.split()))<br>    data[&#39;Has_Cabin&#39;] = data[&quot;Cabin&quot;].map(lambda x: 1 - int(type(x) == float))<br>    data[&#39;FamilySize&#39;] = data[&#39;SibSp&#39;] + data[&#39;Parch&#39;] + 1<br>    <br>    data[&#39;CategoricalFare&#39;] = pd.qcut(data[&#39;Fare&#39;], 5).astype(str)<br>    data[&#39;CategoricalAge&#39;] = pd.cut(data[&#39;Age&#39;], 5).astype(str)<br>    <br>    data[&#39;Title&#39;] = data[&#39;Name&#39;].apply(get_title).replace([&#39;Lady&#39;, &#39;Countess&#39;,&#39;Capt&#39;, &#39;Col&#39;,&#39;Don&#39;, &#39;Dr&#39;, &#39;Major&#39;, &#39;Rev&#39;, &#39;Sir&#39;, &#39;Jonkheer&#39;, &#39;Dona&#39;], &#39;Rare&#39;)<br>    data[&#39;Title&#39;] = data[&#39;Title&#39;].replace(&#39;Mlle&#39;, &#39;Miss&#39;)<br>    data[&#39;Title&#39;] = data[&#39;Title&#39;].replace(&#39;Ms&#39;, &#39;Miss&#39;)<br>    data[&#39;Title&#39;] = data[&#39;Title&#39;].replace(&#39;Mme&#39;, &#39;Mrs&#39;)<br>    data[&#39;Title&#39;] = data[&#39;Title&#39;].map({&quot;Mr&quot;: 1, &quot;Miss&quot;: 2, &quot;Mrs&quot;: 3, &quot;Master&quot;: 4, &quot;Rare&quot;: 5}).fillna(0)<br>    return data<br><br>train_data = create_extra_features(train_data)<br>test_data = create_extra_features(test_data)</pre><h3>Step 0.4. Data splitting for train-validation</h3><p>To validate the models we are going to build, we need to split the dataset into train and validation parts:</p><pre>%%time<br><br>tr_data, valid_data = train_test_split(train_data, <br>                                       test_size=0.2, <br>                                       stratify=data[‘Survived’], <br>                                       random_state=42)<br>logging.info(‘Parts sizes: tr_data = {}, valid_data = {}’<br>              .format(tr_data.shape, valid_data.shape))</pre><h3>= LightAutoML preset usage =</h3><h3>Step 1. Create Task object</h3><p>Below this line we are ready to build the model for Survived target variable prediction. First of all, we setup the type of model we need using LightAutoML Task class object, there the valid values can be:</p><ul><li>‘binary’ for binary classification</li><li>‘reg’ for regression and</li><li>‘multiclass’ for multiclass classification</li></ul><p>As we have a binary classification competition, we setup Task object with ‘binary’ value with F1 metric to pay more attention for model predictions precision-recall balance:</p><pre>def f1_metric(y_true, y_pred):<br>    return f1_score(y_true, (y_pred &gt; 0.5).astype(int))<br><br>task = Task(&#39;binary&#39;, metric = f1_metric)</pre><h3>Step 2. Setup columns roles</h3><p>Roles setup here is to set target column called <em>Survived </em>and drop column <em>PassengerId</em> from the dataset with the already used for expert features <em>Name</em> and <em>Ticket</em> columns:</p><pre>%%time<br><br>roles = {&#39;target&#39;: &#39;Survived&#39;,<br>         &#39;drop&#39;: [&#39;PassengerId&#39;, &#39;Name&#39;, &#39;Ticket&#39;]}</pre><h3>Step 3. Create AutoML model from preset</h3><figure><img alt="TabularAutoML preset pipeline" src="https://cdn-images-1.medium.com/proxy/1*VdoX78-sv4wThpGxpCSmnQ.png" /><figcaption>Structure of our first model — Linear model, LigthGBM with expert params and LightGBM with Optuna optimized params are weighted averaged to create final predictions</figcaption></figure><p>To develop first LightAutoML model with the structure above, we use the TabularAutoML preset. In code, it looks like this:</p><pre>automl = TabularAutoML(task = task, <br>                       timeout = 600, # 600 seconds = 10 minutes<br>                       cpu_limit = 4, # Optimal for Kaggle kernels<br>                       general_params = {&#39;use_algos&#39;: [[&#39;linear_l2&#39;, <br>                                         &#39;lgb&#39;, &#39;lgb_tuned&#39;]]})</pre><p>Base algorithms, which are currently available to be in general_params use_algos :</p><ul><li>Linear model (called &#39;linear_l2&#39;)</li><li>LightGBM model with expert params based on dataset (&#39;lgb&#39;)</li><li>LightGBM with tuned params using Optuna (&#39;lgb_tuned&#39;)</li><li>CatBoost model with expert params (&#39;cb&#39;) and</li><li>CatBoost with params from Optuna (&#39;cb_tuned&#39;)</li></ul><p>As you can see, use_algos are lists in the list — this is the notation to create ML pipelines with as many levels of algorithms as you want. For example, [[&#39;linear_l2&#39;, &#39;lgb&#39;, &#39;cb&#39;], [&#39;lgb_tuned&#39;, &#39;cb&#39;]] stands for 3 algorithms on the first level and 2 on the second. After the second level will be fully trained, predictions from the 2 algorithms are weighted averaged to construct the final prediction. The full set of parameters (not only general ones), which can be provided for the TabularAutoML customization, can be found in its <a href="https://github.com/sberbank-ai-lab/LightAutoML/blob/master/lightautoml/automl/presets/tabular_config.yml">YAML config</a>.</p><p>To fit our TabularAutoML preset model on the train part of the dataset, we use the code below:</p><pre>oof_pred = automl.fit_predict(tr_data, roles = roles)</pre><p>As a result of fit_predict function, we receive Out-of-Fold (OOF for short) predictions. They are based on the inner CV of LightAutoML and can be used to calculate the model performance metrics on the train data.</p><h3>Step 4. Predict to validation data and check scores</h3><p>Now we have a trained model and we want to receive predictions for the validation data:</p><pre>valid_pred = automl.predict(valid_data)</pre><p>And as we have the ground truth labels for this object let’s check how good we are:</p><pre>def acc_score(y_true, y_pred):<br>    return accuracy_score(y_true, (y_pred &gt; 0.5).astype(int))</pre><pre>print(&#39;OOF acc: <strong>{}</strong>&#39;.format(acc_score(tr_data[&#39;Survived&#39;].values,      oof_pred.data[:, 0])))<br>print(&#39;VAL acc: <strong>{}</strong>&#39;.format(acc_score(valid_data[&#39;Survived&#39;].values, valid_pred.data[:, 0])))</pre><p>The results are pretty good and stable — <strong>84.4% accuracy for OOF and 83.2% for validation data in 2.5 minutes. </strong>But we want even more :)</p><h3>Step 5. Create LightAutoML model with time utilization</h3><p>Below we are going to create specific AutoML preset for TIMEOUT utilization (try to spend it as much as possible inside TIMEOUT boundary):</p><pre>automl = TabularUtilizedAutoML(task = task, <br>                       timeout = 600, # 600 seconds = 10 minutes<br>                       cpu_limit = 4, # Optimal for Kaggle kernels<br>                       general_params = {&#39;use_algos&#39;: [[&#39;linear_l2&#39;, <br>                                         &#39;lgb&#39;, &#39;lgb_tuned&#39;]]})</pre><p>It’s time to fit and get the better result:</p><pre>oof_pred = automl.fit_predict(tr_data, roles = roles)</pre><p>As you can see, the API is the same for both presets so you can easily check each of them without much coding.</p><h3>Step 6. Predict to validation data and check scores for utilized model</h3><p>Prediction API for TabularUtilizedAutoML is also the same:</p><pre>valid_pred = automl.predict(valid_data)</pre><p>And now we check scores:</p><pre>print(&#39;OOF acc: <strong>{}</strong>&#39;.format(acc_score(tr_data[&#39;Survived&#39;].values,      oof_pred.data[:, 0])))<br>print(&#39;VAL acc: <strong>{}</strong>&#39;.format(acc_score(valid_data[&#39;Survived&#39;].values, valid_pred.data[:, 0])))</pre><p>Wow! <strong>85.5% for OOF and 82.7% for validation data in less than 9 minutes. </strong>The validation score is a little bit lower here, but we have only 179 passengers in it. OOF score increase is more valuable here as we have 712 passengers in its calculation.</p><h3>Step 7. Retrain selected model on the full dataset and predict for the real test</h3><p>Now we know what model to use to receive good results on the Titanic dataset, so it’s time to retrain it on the whole dataset:</p><pre>automl = TabularUtilizedAutoML(task = task, <br>                       timeout = 600, # 600 seconds = 10 minutes<br>                       cpu_limit = 4, # Optimal for Kaggle kernels<br>                       general_params = {&#39;use_algos&#39;: [[&#39;linear_l2&#39;, <br>                                         &#39;lgb&#39;, &#39;lgb_tuned&#39;]]})<br>oof_pred = automl.fit_predict(train_data, roles = roles)<br>test_pred = automl.predict(test_data)</pre><h3>Step 8. Prepare submission for Kaggle</h3><p>As we already loaded sample submission file, the only thing we need to do is to insert our predictions into it and save the file:</p><pre>submission[&#39;Survived&#39;] = (test_pred.data[:, 0] &gt; 0.5).astype(int)<br>submission.to_csv(&#39;automl_utilized_600_f1_score.csv&#39;, index = False)</pre><h3>Step 9. Submit to Kaggle!!!</h3><p>Our prepared submit scores <strong>0.79665</strong> on Kaggle public leaderbord.</p><h3>Conclusion</h3><p>In this tutorial we created a step-by-step solution for the Titanic Survival competition using <a href="https://github.com/sberbank-ai-lab/LightAutoML">LightAutoML</a> — an open-source framework for fast, automatic ML model creation.</p><p>Full tutorial code is available in Kaggle kernels <a href="https://www.kaggle.com/alexryzhkov/lightautoml-titanic-love">here</a> (and <a href="https://www.kaggle.com/alexryzhkov/lightautoml-extreme-short-titanic-solution">here</a> for super-short version of the solution) — just give it a try on this dataset or any other one you have. It can surprise you :)</p><p>Stay tuned for more examples!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cce7da6f936" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/lightautoml-preset-usage-tutorial-2cce7da6f936">LightAutoML preset usage tutorial</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>