numpy dtype ='numeric'与带streamlit的字节/字符串数组不兼容

368yc8dk  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(153)
import numpy as np
import pickle
import streamlit

loaded_model=pickle.load(open("C:/Users/pranj/Desktop/titanic/titanic_trained_model.pkl",'rb'))

input_data=(3,0,22.000000,1,0,7.2500,0)

#channging the input data in numpy array
input_data_as_numpy_arr=np.asarray(input_data)

# reshape the array as we are predicting for one instance
input_data_reshaped=input_data_as_numpy_arr.reshape(1,-1)

prediction=loaded_model.predict(input_data_reshaped)
print(prediction)
if (prediction[0]==0):
    print("The person will die")
else:
    print("The person will not die")

字符串
如果你运行这段代码,它将运行没有任何错误。但是,当我尝试使用streamlit制作网站时,我得到了一个错误。

import numpy as np
import pickle
import streamlit as st

#loading the saved model
loaded_model=pickle.load(open("C:/Users/pranj/Desktop/titanic/titanic_trained_model.pkl",'rb'))

# creating a function
def diabetes_predection(input_data):
    
    #changing the input data in numpy array
    input_data_as_numpy_arr=np.asarray(input_data)

# reshape the array as we are predicting for one instance
    input_data_reshaped=input_data_as_numpy_arr.reshape(1,-1)
    prediction=loaded_model.predict(input_data_reshaped)
    print(prediction)
    if (prediction[0]==0):
        return "The person will die"       
    else:
        return "The person will not die"

def main():
    # giving title 
    st.title('Titanic Prediction Web App')
    
    # getting the input data from the users
    Pclass=st.text_input('Pclass 1st class/2nd class/3rd class')
    Sex=st.text_input('Sex Put 0 for male 1 for female')
    Age=st.text_input('Age')
    SibSp=st.text_input('SibSp')
    Parch=st.text_input('Parch')
    Fare=st.text_input('Fare')
    Embarked=st.text_input('Embarked Press 0 for Southampton,1 for Cherbourg,2 for Queenstown')
    
    #code for Prediction
    Survived=""
    
    # creating button for prediction
    if st.button('Survived Test Result'):
        Survived=diabetes_predection([Pclass,Sex,Age,SibSp,Parch,Fare,Embarked])
    
    st.success(Survived)
    
    
main()


我得到的错误是:
dtype ='numeric'与字节/字符串数组不兼容。将数据显式转换为数值。
有人能解释一下为什么会出现这个错误以及如何修复它吗?

tjrkku2a

tjrkku2a1#

st.text_input('param ')的返回类型是字符串。请确保将要素转换为兼容的数据类型。

相关问题