[英]UFuncTypeError: ufunc 'matmul' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32') - 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")
I am using Streamlit to build a ML model that take user input.我正在使用 Streamlit 构建一个接受用户输入的 ML model。 In my main function it returns error
UFuncTypeError: ufunc 'matmul' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')
and trace back to pred = model.predict(user_input_prediction)
the main function will print out error1 but not error2在我的主要 function 中,它返回错误
UFuncTypeError: ufunc 'matmul' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')
and trace返回pred = model.predict(user_input_prediction)
主 function 将打印出 error1 但不会打印 error2
I was stuck in same kind of situation where the model was throwing various kinds of errors.我陷入了 model 抛出各种错误的同样情况。 But particularly for your case, let me tell what I tried:
但特别是对于你的情况,让我告诉我我尝试了什么:
This line of yours seems pretty well.你的这条线看起来还不错。
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)
Just try adding this line below your code只需尝试在您的代码下方添加此行
user_input_prediction = user_input_prediction.astype(np.float64)
Because here model was throwing error that your datatype is mismatch since inside all these values of features are in form of a matrix(numeric) so we need to convert it into floating values before doing any prediction.因为这里 model 抛出错误,即您的数据类型不匹配,因为在所有这些特征值内部都是矩阵(数字)的形式,所以我们需要在进行任何预测之前将其转换为浮点值。
Also try passing the user_input_prediction inside predict method as a list:还可以尝试将 predict 方法中的 user_input_prediction 作为列表传递:
preds = model.predict([user_input_prediction])
This worked for me, hope it'll work for you as well这对我有用,希望它也对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.