Search code examples
pythonmachine-learningscikit-learndata-sciencelogistic-regression

logistic regression using more than one predictor


I want to fit a logistic regression model that predicts Y using X1 and X2. What I know is that we use the following method:

x_train, x_test, y_train, y_test = train_test_split(X,Y,test_size)

and then

model = LogisticRegression()
model.fit(x_train,y_train)

To predict Y using X, I don't know how to train the data using more than one predictor. Any help, please?


Solution

  • If there are 2 features X1 and X2, then the training data X will have 2 columns. For example if data has 1000 X1 and 1000 X2, then the shape of X should be (1000 x 2)

    For example you have a csv file with 3 columns: X1, X2, y

    import pandas as pd
    from sklearn.metrics import accuracy_score
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LogisticRegression
    
    df = pd.read_csv('my_file.csv')
    X = df[['X1', 'X2']]
    Y = df['y']
    
    x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2)
    model = LogisticRegression()
    model.fit(x_train,y_train)
    
    y_pred = model.predict(x_test)
    
    acc = accuracy_score(y_test, y_pred)