I did a very simple linear regression program for learning and I wanted to use streamline to deploy it.
My code looks a bit like this this:
#separate features and target variables
X = df['Number of Claims'] #X is a number between 0 and 124
Y = df['Total payment']
# standardize the data attributes
standardized_X = preprocessing.scale(X)
#standardize the target attribute
standardized_Y = preprocessing.scale(Y)
X_train, X_test, y_train, y_test = train_test_split(standardized_X, standardized_Y, test_size=0.25, random_state=42)
#reshape as data has a single feature
X_train = X_train.reshape(-1,1)
y_train = y_train.reshape(-1,1)
X_test = X_test.reshape(-1,1)
# Model initialization
regression_model = LinearRegression()
# Fit the data(train the model)
regression_model.fit(X_train, y_train)
autosweden = regression_model.fit(X_train, y_train)
# Predict
y_predicted = regression_model.predict(X_test)
joblib.dump(regression_model, 'autosweden.pkl')
it works without issues but when I want to deploy it in Streamlit and I load the .pkl file. I get a different value which I understand because I am not scaling the value from I get from streamline slide. how could I fix this issue in Streamlit? would even be possible?
here the streamline code:
import streamlit as st
import joblib
import numpy as np
from sklearn import preprocessing
model = joblib.load('autosweden.pkl')
# interface
X = st.slider('Select number of Claims:', 0, 124)
# predict button
if st.button('Predict'):
X_reshaped = np.array([X]).reshape(-1, 1)
predicted_payment = model.predict(X_reshaped)
st.markdown(f'prediction is: {predicted_payment}')