Python 尝试将Streamlight应用程序部署到Heroku时出现AttributeError
我有一个简单的Streamlight应用程序,其中包括存储为pickle文件的tranforms+estimator,用于预测。当我部署到本地主机时,该应用程序运行良好。部署到Heroku时,web布局工作正常,但预测应用程序生成错误AttributeError:“ColumnTransformer”对象没有属性“\u feature\u names\u in”。 我使用了下面的requirements.txt: numpy==1.17.2熊猫==0.25.1流线型==0.67.1枕头==7.2.0科学学习==0.23.2 由pipreqs生成 从对类似问题的公开回答中,我推断这可能是由于sklearn版本的不兼容性。但不知道如何纠正它 以下是来自Heruko的错误消息: AttributeError:“ColumnTransformer”对象没有“\u功能\u名称\u in”Python 尝试将Streamlight应用程序部署到Heroku时出现AttributeError,python,heroku,streamlit,Python,Heroku,Streamlit,我有一个简单的Streamlight应用程序,其中包括存储为pickle文件的tranforms+estimator,用于预测。当我部署到本地主机时,该应用程序运行良好。部署到Heroku时,web布局工作正常,但预测应用程序生成错误AttributeError:“ColumnTransformer”对象没有属性“\u feature\u names\u in”。 我使用了下面的requirements.txt: numpy==1.17.2熊猫==0.25.1流线型==0.67.1枕头==7.2
您是否可能正在尝试使用尚未安装的ColumnTransformer调用predict
属性_feature_names_in在fit_transform调用中设置。我有相同的sklearn版本,并且方法是存在的,所以版本不应该有问题,我解决了这个问题。结果表明,保存的模型的pickle文件已损坏。我重新生成了模型,部署工作正常。 感谢所有花时间回顾我问题的人。
阿波罗。谢谢你回答我的问题。存储为pickle文件的管道是管道安装列车数据后的结果。此管道加上其他文件:app.py、Procfile和requirements.txt已通过运行:streamlit run app.py在本地主机上进行了测试,它使用Predict生成的正确值运行文件。
Here is the code for app.py:
import pandas as pd
import numpy as np
import pickle
import streamlit as st
from PIL import Image
#from sklearn.preprocessing import OneHotEncoder
from sklearn.base import BaseEstimator, TransformerMixin
#from sklearn.impute import SimpleImputer
#from sklearn.pipeline import Pipeline
#from sklearn.preprocessing import MinMaxScaler
#from sklearn.compose import ColumnTransformer
import warnings
warnings.filterwarnings('ignore')
acc_ix, wt_ix, hpower_ix, cyl_ix = 4, 3, 2, 0
##custom class inheriting the BaseEstimator and TransformerMixin
class CustomAttrAdder(BaseEstimator, TransformerMixin):
def __init__(self, acc_and_power=True):
self.acc_and_power = acc_and_power # new optional variable
def fit(self, X, y=None):
return self # nothing else to do
def transform(self, X):
wt_and_cyl = X[:, wt_ix] * X[:, cyl_ix] # required new variable
if self.acc_and_power:
acc_and_power = X[:, acc_ix] * X[:, hpower_ix]
return np.c_[X, acc_and_power, wt_and_cyl] # returns a 2D array
return np.c_[X, wt_and_cyl]
def predict_mpg_web1(config,regressor):
if type(config)==dict:
df=pd.DataFrame(config)
else:
df=config
# Note the model is in the form of pipeline_m, including both transforms and the estimator
# The config is with Origin already in country code
y_pred=regressor.predict(df)
return y_pred
# this is the main function in which we define our webpage
def main():
# giving the webpage a title
#st.title("MPG Prediction")
st.write("""
# MPG Prediction App
based on a Random Forest Model built from
"http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data"
""")
# here we define some of the front end elements of the web page like
# the font and background color, the padding and the text to be displayed
html_temp = """
<div style ="background-color:yellow;padding:13px">
<h1 style ="color:black;text-align:center;">What is the mpg of my car? </h1>
</div>
"""
# this line allows us to display the front end aspects we have
# defined in the above code
st.markdown(html_temp, unsafe_allow_html = True)
# the following lines create dropdowns and nueemric sliders in which the user can enter
# the data required to make the prediction
st.sidebar.header('Set My Car Configurations')
Orig = st.sidebar.selectbox("Select Car Origin",("India", "USA", "Germany"))
Cyl = st.sidebar.slider('Cylinders', 3, 6, 8)
Disp = st.sidebar.slider('Displacement', 68.0, 455.0, 193.0)
Power = st.sidebar.slider('Horsepower', 46.0, 230.0, 104.0)
WT = st.sidebar.slider(' Weight', 1613.0, 5140.0, 2970.0)
Acc = st.sidebar.slider('Acceleration', 8.0, 25.0, 15.57)
MY = st.sidebar.slider('Model_Year', 70, 82, 76)
image = Image.open('car.jpg')
st.image(image, caption='MPG Prediction',
use_column_width=True)
st.subheader("Click the 'Predict' button below")
# loading the saved model
pickle_in = open('final_model.pkl', 'rb')
regressor=pickle.load(pickle_in)
result =""
# the below line ensures that when the button called 'Predict' is clicked,
# the prediction function defined above is called to make the prediction
# and store it in the variable result
# Set up the Vehicale configurations
vehicle={"Origin": [Orig], "Cylinders": [Cyl], "Displacement": Disp, "Horsepower": [Power],
"Weight":[WT], "Acceelation": [Acc], "Model Year": [MY]
}
if st.button("Predict"):
result = predict_mpg_web1(vehicle, regressor)
mpg=int(result[0])
st.success('The prediction is {}'.format(mpg))
if __name__=='__main__':
main()