Docsity

Logistic regression Machine learning, Lab Reports of Machine Learning

Toggle sidebar
bg1
LOGISTIC REGRESSION 1)What is logistic regresssion 2) What is sigmoid function - what it does in logistic
regresssion 3) Why cant we use linear regresssion in problems where the output label is discrete
In [26]:
In [27]:
In [28]:
Out[27]:
Customer Spending Card Coupon
01 2.291 1 0
12 3.215 1 0
23 2.135 1 0
34 3.924 0 0
45 2.528 1 0
... ... ... ... ...
95 96 3.318 0 0
96 97 2.421 1 0
97 98 6.073 0 0
98 99 2.630 1 0
99 100 3.411 0 1
100 rows ร— 4 columns
Out[28]:
100
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
df=pd.read_excel\
('C:/Users/kriti/OneDrive/Desktop/machine Learning/experiments/Simmons.xls')
df
#Find the length of the dataset
len(df)
1
2
3
4
1
2
3
1
2
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Logistic regression Machine learning and more Lab Reports Machine Learning in PDF only on Docsity!

LOGISTIC REGRESSION 1)What is logistic regresssion 2) What is sigmoid function - what it does in logistic regresssion 3) Why cant we use linear regresssion in problems where the output label is discrete In [26]: In [27]: In [28]: Out[27]: Customer Spending Card Coupon 0 1 2.291 1 0 1 2 3.215 1 0 2 3 2.135 1 0 3 4 3.924 0 0 4 5 2.528 1 0 ... ... ... ... ... 95 96 3.318 0 0 96 97 2.421 1 0 97 98 6.073 0 0 98 99 2.630 1 0 99 100 3.411 0 1 100 rows ร— 4 columns Out[28]: 100 import pandas as pd import numpy as np import seaborn as sns import matplotlib.pyplot as plt df = pd.read_excel
('C:/Users/kriti/OneDrive/Desktop/machine Learning/experiments/Simmons.xls') df #Find the length of the dataset len(df)

In [5]: In [6]: Customer Spending Card Coupon 0 1 2.291 1 0 1 2 3.215 1 0 2 3 2.135 1 0 3 4 3.924 0 0 4 5 2.528 1 0 Out[5]: Customer Spending Card Coupon count 100.000000 100.000000 100.000000 100. mean 50.500000 3.333790 0.500000 0. std 29.011492 1.741298 0.502519 0. min 1.000000 1.058000 0.000000 0. 25% 25.750000 2.059000 0.000000 0. 50% 50.500000 2.805500 0.500000 0. 75% 75.250000 4.468250 1.000000 1. max 100.000000 7.076000 1.000000 1. Out[6]: (100, 4) #print the first five records print(df.head( 5 )) #Describe your dataset df.describe() # Find the shape of the dataset df.shape

In [10]: In [11]: Out[9]: Spending Card 0 2.291 1 1 3.215 1 2 2.135 1 3 3.924 0 4 2.528 1 ... ... ... 95 3.318 0 96 2.421 1 97 6.073 0 98 2.630 1 99 3.411 0 100 rows ร— 2 columns Out[10]: 0 0 1 0 2 0 3 0 4 0 .. 95 0 96 0 97 0 98 0 99 1 Name: Coupon, Length: 100, dtype: int x y # from sklearn library import model_selection module and train_test_split function from sklearn.model_selection import train_test_split #Split the data into random train and test subsets #random_state is the seed used by the random number generator; x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.33, random_sta

In [13]: Out[12]: LogisticRegression() Out[13]: array([1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0], dtype=int64) #Logistic Regression (aka logit) classifier. Import LogisticRegression from sklearn. from sklearn.linear_model import LogisticRegression # create the object of class LogisticRegression logmodel = LogisticRegression() #fit-Fit the model according to the given training data. logmodel.fit(x_train, y_train) # do the predictions on x_test predictions = logmodel.predict(x_test) predictions

Out[16]: Actual value predicted values 80 1 1 84 1 0 33 0 0 81 1 1 93 1 1 17 0 1 36 0 0 82 0 0 69 0 0 65 0 1 92 1 1 39 0 0 56 1 0 52 0 0 51 0 0 32 1 1 31 0 1 44 0 0 78 1 1 10 0 0 2 0 0 73 1 0 97 0 0 62 0 1 19 1 0 35 0 0 94 0 0 27 1 0 46 0 0 38 0 0 67 1 1 99 1 0 54 0 0 1 d

Confusion matrix is a table which describes the performance of a prediction model. A confusion matrix contains the actual values and predicted values. we can use these values to calculate the accuracy score of the model. In [18]: precision recall f1-score support 0 0.73 0.80 0.76 20 1 0.64 0.54 0.58 13 accuracy 0.70 33 macro avg 0.68 0.67 0.67 33 weighted avg 0.69 0.70 0.69 33 Out[18]: array([[16, 4], [ 6, 7]], dtype=int64) # from sklearn.meterics import a function called classification_report to see the pe from sklearn.metrics import classification_report print(classification_report(y_test, predictions)) #Create a confusion matrix by importing the function confusion_matrix from sklearn.metrics import confusion_matrix,accuracy_score confusion = confusion_matrix(y_test, predictions) confusion