House price prediction streamlit app (with lgbm pipeline backend) returns error on local host

Hello everyone, I trained a LGBM model (pipeline-based) for house price prediction. When I use the saved model in google colab for predicting the price of a new house, it works fine. But when I use it in sreamlit, it returns an error. The error seems to be related to creating the dataframe of the new house features. I have searched and tried different solutions but each one returns an error, while the same code in colab works fine.

I guess the problem is in the “make_column_transform” part of the “PipelineModel.ipynb” file. (Since I doubted if the problem is about the dataframe created in the “”; I used several methods for this purpose which can be observed in the “” file. But each method faced with an error.)

The files are available on my Github:

PipelineModel.ipynb —> training and saving the pipeline, based on lgbm and make-column-transformer. The transformer applies oneHotEncoder on the Address column.

finalized_pipe_model.joblib —> final model for using in the backend of the web app

newHousePrediction.ipynb —> using the final model with a completely new house and predicting the price —> using the final model as backend of the streamlit web app

As I run the file, the following error is returned:

AttributeError: ‘str’ object has no attribute ‘transform’
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\streamlit\scriptrunner\”, line 557, in _run_script
exec(code, module.dict)
File “”, line 43, in
predictionX = model.predict(X)
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\sklearn\”, line 602, in predict
Xt = transform.transform(Xt)
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\sklearn\”, line 295, in wrapped
data_to_wrap = f(self, X, *args, **kwargs)
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\sklearn\”, line 1014, in transform
Xs = self._call_func_on_transformers(
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\sklearn\”, line 823, in _call_func_on_transformers
return Parallel(n_jobs=self.n_jobs)(jobs)
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\sklearn\utils\”, line 67, in call
return super().call(iterable_with_config)
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\joblib\”, line 1863, in call
return output if self.return_generator else list(output)
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\joblib\”, line 1792, in _get_sequential_output
res = func(*args, **kwargs)
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\sklearn\utils\”, line 129, in call
return self.function(*args, **kwargs)
File “C:\Users\b_sad\anaconda3\envs\test\Lib\site-packages\sklearn\”, line 1283, in _transform_one
res = transformer.transform(X, **params.transform)

Hi @Beheshte_Sadeghi_Sab . You need to mention req.txt file name as requirements.txt. After changing the filename just reboot the app. Hope it works

Happy Streamlit-ing :balloon:

1 Like

I tried your and it worked.

I used these lib versions.


Thanks for your guidance, but it didn’t work

Could you please tell me your version of pandas and seaborn? As I applied the modifications you said, an error related to pandas came up which did not come up before.

Pandas is already included in the streamlit==1.31.1. What I have is pandas==1.5.2

I don’t use seaborn. I am only testing the model.

Prediction code.

import streamlit as st
import pandas as pd
import joblib

model = joblib.load('finalized_pipe_model.joblib')

# Columns = ['Area', 'Room', 'Parking', 'Warehouse', 'Elevator', 'Address']
df = pd.read_csv('housePrice.csv')

Address = st.selectbox("Address", df.Address.unique())
Area = st.number_input("Area", 30, 20000000000)
Room = st.number_input("Number of rooms", 0, 5)
Parking ="Parking", ["Yes", "No"]) == "Yes"
Elevator ="Elevator", ["Yes", "No"]) == "Yes"
Warehouse ="Warehouse", ["Yes", "No"]) == "Yes"

if st.button('Predict'):
    output = {
        'Area': [Area],
        'Room': [Room],
        'Parking': [Parking],
        'Warehouse': [Warehouse],
        'Elevator': [Elevator],
        'Address': [Address]
    X = pd.DataFrame(output)
    predictionX = model.predict(X)
    st.success(f'The price of this house is {predictionX}!') 

Sample output