简体   繁体   English

使用 sklearn 在 python 中执行逻辑回归分析

[英]Performing logistic regression analysis in python using sklearn

I am trying to perform a logistic regression analysis but I don't know which part am i mistaken in my code.我正在尝试执行逻辑回归分析,但我不知道我在代码中误会了哪一部分。 It gives error on the line logistic_regression.fit(X_train, y_train) .它在logistic_regression.fit(X_train, y_train)线上给出错误。 But it seems okay as i checked from different sources.但这似乎还可以,因为我从不同的来源进行了检查。 Can anybody help?有人可以帮忙吗? Here is my code:这是我的代码:

import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

df = pd.read_csv("/Users/utkusenel/Documents/Data Analyzing/data.csv", header=0, sep=";")
data = pd.DataFrame(df)

x = data.drop(columns=["churn"])  #features
y = data.churn  # target variable
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0)
logistic_regression = LogisticRegression()
logistic_regression.fit(X_train, y_train)

There are multiple problems here.这里有多个问题。

  1. Your first row of headers has a ';'您的第一行标题有一个';' at the end.在最后。 So it is going to read an extra column.所以它将读取一个额外的列。 You need to remove that ';'你需要删除那个';' after churn . churn后。
  2. The training data that you are trying to use here, X_train, is going to have multiple text/categorical columns.您在此处尝试使用的训练数据 X_train 将具有多个文本/分类列。 You need to convert these into numbers.您需要将这些转换为数字。 Check out OneHotEncoder here: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html and LabelEncoder here: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html Check out OneHotEncoder here: https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.OneHotEncoder.html and LabelEncoder here: https://scikit-learn.org/stable/modules/generated/sklearn.预处理.LabelEncoder.html

After you have converted your text and categorical data to numbers and removed the extra ';'在您将文本和分类数据转换为数字并删除额外';'之后separator, run your algorithm again.分隔符,再次运行你的算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM