在逻辑回归中将连续数据转换为分类数据时出错

Error while converting continuous data to categorical data in Logistic Regression

我正在对我的数据集使用逻辑回归,它的目标变量为 0 和 1。我使用了 .replace() 函数并相应地替换了它们。

> data['target']=data['target'].replace({0:"No",1:"yes"})

代码运行很好。但是当我对数据建模时,

model_log=sm.Logit(data['target'],data.iloc[:,2:]).fit()

显示以下错误:

ValueError: Pandas data cast to numpy dtype of object. Check input data with np.asarray(data).

当您 select X 数据使用 iloc,it is return a pandas dataframe.According to statsmodel documentation,logit expect to X and y to be array_like. You need to cast the dataframe to required data type.You can use to_numpy 方法将数据帧转换为 numpy 数组时。

model_log=sm.Logit(data['target'].astype(float),data.iloc[:,2:].to_numpy()).fit()