【问题标题】:UFuncTypeError: ufunc 'matmul' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32') - StreamlitUFuncTypeError: ufunc 'matmul' 不包含具有签名匹配类型的循环 (dtype('<U32'), dtype('<U32')) -> dtype('<U32') - Streamlit
【发布时间】:2020-12-17 00:17:35
【问题描述】:
#Linear Regression Model 
@st.cache(allow_output_mutation=True)
def linearRegression(X_train, X_test, y_train, y_test):
    model = LinearRegression()
    model.fit(X_train,y_train)
    score = model.score(X_test, y_test)*100

    return score , model      

#User input for the model
def user_input():
    bedrooms = st.slider("Bedrooms: ", 1,15)
    bathrooms = st.text_input("Bathrooms: ")
    sqft_living = st.text_input("Square Feet: ")
    sqft_lot = st.text_input("Lot Size: ")
    floors = st.text_input("Number Of Floors: ")
    waterfront = st.text_input("Waterfront? For Yes type '1',  For No type '0': ")
    view = st.slider("View (A higher score will mean a better view) : ", 0,4)
    condition = st.slider("House Condition (A higher score will mean a better condition): ", 1,5)
    yr_built = st.text_input("Year Built: ")
    yr_reno = st.text_input("A Renovated Property? For Yes type '1',  For No type '0': ")
    zipcode = st.text_input("Zipcode (5 digit): ")
    year_sold = st.text_input("Year Sold: ")
    month_sold = st.slider("Month Sold: ", 1,12)
   
    user_input_prediction = np.array([bedrooms,bathrooms,sqft_living, sqft_lot,floors,waterfront,view,condition,yr_built,yr_reno,zipcode,year_sold,month_sold]).reshape(1,-1)
    
    return(user_input_prediction)

#Main function


            if(st.checkbox("Start a Search")):
                user_input_prediction = user_input()
                st.write('error1')
                pred = model.predict(user_input_prediction)
                st.write('error2')
                if(st.button("Submit")):
                    st.text("success")
                    
                 

我正在使用 Streamlit 构建一个接受用户输入的 ML 模型。在我的主函数中,它返回错误UFuncTypeError: ufunc 'matmul' did not contain a loop with signature matching types (dtype('&lt;U32'), dtype('&lt;U32')) -&gt; dtype('&lt;U32') 并回溯到pred = model.predict(user_input_prediction),主函数将打印出error1 但不是error2

【问题讨论】:

    标签: python machine-learning linear-regression streamlit


    【解决方案1】:

    我陷入了模型抛出各种错误的相同情况。 但特别是对于你的情况,让我告诉我我尝试了什么:

    你的这条线看起来不错。

    user_input_prediction = np.array([bedrooms,bathrooms,sqft_living, sqft_lot,floors,waterfront,view,condition,yr_built,yr_reno,zipcode,year_sold,month_sold]).reshape(1,-1)
    

    只需尝试在您的代码下方添加这一行?

    user_input_prediction = user_input_prediction.astype(np.float64)
    

    因为这里的模型抛出了你的数据类型不匹配的错误,因为所有这些特征值都是矩阵(数字)的形式,所以我们需要在进行任何预测之前将其转换为浮点值。

    还可以尝试将 predict 方法中的 user_input_prediction 作为列表传递:

    preds = model.predict([user_input_prediction])
    

    这对我有用,希望它也对你有用?

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2023-04-03
      • 2021-11-23
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2016-08-06
      相关资源
      最近更新 更多