In this post we will learn Logistic Regression and real life example.
Logistic regression is a supervised machine learning which is used for classification problems or we can say it is used to find the probability of target variable which is the form in binary or discrete 0 and 1.
It represents by simple sigmoid function:
Output goes between 0 and 1.
Here the simple curve which is used to represent this sigmoid curve:
The middle value of this curve (0.5) is called the “threshold values”
It divided into the two categories, class1 and class2,
if value is less than 0.5 that means it assign to class1 and value set as 0.
And if the value is greater than 0.5 than it goes to the class2 and value set as 1.
# importing required librariesimport pandas as pdfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import accuracy_score# train and test datasettrain_data = pd.read_csv('train-data.csv')test_data = pd.read_csv('test-data.csv')print(train_data.head())# shape of the datasetprint('Shape of training data :',train_data.shape)print('Shape of testing data :',test_data.shape)# seperate the independent and target variable on training datax_train = train_data.drop(columns=['Income'],axis=1)y_train = train_data['Income']# seperate the independent and target variable on testing datax_test = test_data.drop(columns=['Income'],axis=1)y_test = test_data['Income']model = LogisticRegression()# fit the model with the training datamodel.fit(x_train,y_train)# coefficients of the trained modelprint('Coefficient of model :', model.coef_)# intercept of the modelprint('Intercept of model',model.intercept_)# predict the target on the train datasetpredict_train = model.predict(x_train)print('Target on train data',predict_train) # Accuray Score on train datasetaccuracy_train = accuracy_score(y_train,predict_train)print('accuracy_score on train dataset : ', accuracy_train)# predict the target on the test datasetpredict_test = model.predict(x_test)print('Target on test data',predict_test) # Accuracy Score on test datasetaccuracy_test = accuracy_score(y_test,predict_test)print('accuracy_score on test dataset : ', accuracy_test)