UFuncTypeError:ufunc'matmul'不包含签名匹配类型为(dtype('<U32'),dtype('<U32'))-> dtype('<U32')的循环-Streamlit

时间:2020-08-28 04:20:24

标签: python machine-learning linear-regression streamlit

#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('<U32'), dtype('<U32')) -> dtype('<U32')并追溯到pred = model.predict(user_input_prediction),主函数将输出error1但不会输出error2

1 个答案:

答案 0 :(得分:0)

我遇到了模型抛出各种错误的情况。 但特别是对于您的情况,让我告诉您我尝试了什么:

你的这条线看起来不错。

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])

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