Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Follow publication

Quickly Build and Deploy a Machine Learning App

Retin P Kumar
Geek Culture
Published in
10 min readDec 3, 2021

--

Photo by Andrea De Santis on Unsplash

Part 1: Building a model from scratch

!pip install “libraryname”
!pip install pandas
pip install -r requirements.txt
# data manipulation
import numpy as np
import pandas as pd

# data transfer
import joblib

# raw dataset
from sklearn.datasets import load_iris
iris = load_iris()
iris.keys()
iris.DESCR.split("\n")
# creating dataframe
data = pd.DataFrame(iris.data, columns=iris.feature_names)
# printing observations
print(f"The data has {data.shape[1]} features and {data.shape[0]} observations.")
# checking first 5 observations
data.head()
png
Iris dataset (image by author)
data = data.rename(columns={
'sepal length (cm)':'sepal_length',
'sepal width (cm)':'sepal_width',
'petal length (cm)':'petal_length',
'petal width (cm)':'petal_width'
})
data.head()
png
Mapped columns (image by author)
# save data if needed
data.to_csv('data.csv', index=False)
# explore target
iris.target
# add target to dataframe
data['Target'] = iris.target
data.head()
png
Datset with target (image by author)
# check target composition
data['Target'].value_counts()
0 50
1 50
2 50
Name: Target, dtype: int64
# check missing values
data.isnull().any()
sepal_length False
sepal_width False
petal_length False
petal_width False
Target False
dtype: bool
# split data
y = data['Target']
X = data.drop(columns=['Target'], axis=1)
# create train and test sets
from sklearn.model_selection import train_test_split
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.25, random_state=7)

print(train_X.shape, test_X.shape, train_y.shape, test_y.shape)
(112, 4) (38, 4) (112,) (38,)
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()

train_X_scaled = scaler.fit_transform(train_X) # scaling training data

#save scaler
joblib.dump(scaler, 'standard_scaler.sav')

test_X_scaled = scaler.transform(test_X) # scaling testing data
['standard_scaler.sav']
from sklearn.linear_model import LogisticRegression

model = LogisticRegression()
# fitting model
model.fit(train_X_scaled, train_y)
LogisticRegression()# Making predictions using test data
y_pred = model.predict(test_X_scaled)
# calculating accuracy
from sklearn.metrics import accuracy_score

accuracy = accuracy_score(test_y, y_pred)
accuracy
0.8947368421052632
# save model
joblib.dump(model, 'model.sav')
['model.sav']

Part 2: Creating an app for the model

image.png
Creating model file (image by author)
image.png
Creating scaler file (image by author)
image.png
Importing libraries (image by author)
image.png
Setting app page configuration (image by author)
image-2.png
Giving app title (image by author)
image.png
Creating app form (image by author)
image.png
Complete app (image by author)
streamlit run app.py

Starting with the “requirements.txt” file for installing libraries.

pip install pipreqs
pipreqs --encoding=utf8  --debug  "path/to/project"

Other 3 important files

mkdir -p ~/.streamlit/
echo "\
[general]\n\
email = \"your-email@domain.com\"\n\
" > ~/.streamlit/credentials.toml
echo "\
[server]\n\
headless = true\n\
enableCORS=false\n\
port = $PORT\n\
" > ~/.streamlit/config.toml
web: sh setup.sh && streamlit run app.py
python-3.8.8
python --version

Part 3: Deploying the Model

image-3.png
Creating new app in Heroku (image by author)
image.png
Giving app name (image by author)
image.png
Selecting deployment pipeline (image by author)
image.png
Enabling automatic deploy (image by author)
image.png
Deploying the app (image by author)
image.png
App successfully deployed (image by author)
image.png
Deployment error (image by author)
image.png
Final app (image by author)

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Retin P Kumar
Retin P Kumar

Responses (2)

Write a response