0% found this document useful (0 votes)
26 views

Logistic Regression

This code implements logistic regression to predict survival on the Titanic dataset. It loads training and test data, separates the features and target, fits a logistic regression model to the training data, makes predictions on both training and test data, and calculates the accuracy of those predictions. Key steps include loading data, separating features and target, fitting a logistic regression model, predicting on train and test data, and calculating accuracy scores.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Logistic Regression

This code implements logistic regression to predict survival on the Titanic dataset. It loads training and test data, separates the features and target, fits a logistic regression model to the training data, makes predictions on both training and test data, and calculates the accuracy of those predictions. Key steps include loading data, separating features and target, fitting a logistic regression model, predicting on train and test data, and calculating accuracy scores.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

'''

The following code is for Logistic Regression

'''

# importing required libraries


import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# read the train and test dataset


train_data = pd.read_csv('train-data.csv')
test_data = pd.read_csv('test-data.csv')

print(train_data.head())

# shape of the dataset


print('Shape of training data :',train_data.shape)
print('Shape of testing data :',test_data.shape)

# Now, we need to predict the missing target variable in the test data
# target variable - Survived

# seperate the independent and target variable on training data


train_x = train_data.drop(columns=['Survived'],axis=1)
train_y = train_data['Survived']

# seperate the independent and target variable on testing data


test_x = test_data.drop(columns=['Survived'],axis=1)
test_y = test_data['Survived']

'''
Create the object of the Logistic Regression model
You can also add other parameters and test your code here
Some parameters are : fit_intercept and penalty
Documentation of sklearn LogisticRegression:

https://scikit-learn.org/stable/modules/generated/
sklearn.linear_model.LogisticRegression.html

'''
model = LogisticRegression()

# fit the model with the training data


model.fit(train_x,train_y)

# coefficeints of the trained model


print('Coefficient of model :', model.coef_)

# intercept of the model


print('Intercept of model',model.intercept_)

# predict the target on the train dataset


predict_train = model.predict(train_x)
print('Target on train data',predict_train)

# Accuray Score on train dataset


accuracy_train = accuracy_score(train_y,predict_train)
print('accuracy_score on train dataset : ', accuracy_train)

# predict the target on the test dataset


predict_test = model.predict(test_x)
print('Target on test data',predict_test)

# Accuracy Score on test dataset


accuracy_test = accuracy_score(test_y,predict_test)
print('accuracy_score on test dataset : ', accuracy_test)

You might also like