You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Logistic Regression is sensitive towards scale of data.
Dataset's features should be standardized for better performance ( mean = 0 and variance = 1 )
StandardScaler helps to standardize the dataset features.
Important: fit on train set and transform on train and test set ( Never apply fit on test set )
Important: No need to apply standardization on target vector ( Only apply on X i.e. feature matrix )
Standardize Data:
# Create instance for standard scaler:scaler=StandardScaler()
# Fit on train set:scaler.fit(X_train)
# Transform on train and test set:X_train=scaler.transform(X_train)
X_test=scaler.transform(X_test)
Logistic Regression Model:
# Create model instance: ( We can tune hyperparameters if required ) model=LogisticRegression()
# Fit the model: ( Model is learning the relationship between X features and y labels )model.fit(X_train, y_train)
Classification:
# Check classification for one observation:print(f'Prediction: model.predict(X_test[0].reshape(1, -1))[0]')
# Check classification probability:print(f'Probability: model.predict_proba(X_test[0].reshape(1, -1))')
Measure Model Performance:
model.score(X_test, y_test)
Metrics for Classification Model: ( Confusion Matrix )
scaler=StandardScaler()
# Fit on train set:scaler.fit(X_train)
# Transform on train and test set:X_train=scaler.transform(X_train)
X_test=scaler.transform(X_test)
Logistic Regression Model:
model=LogisticRegression(solver='liblinear', multi_class='ovr', random_state=42)
# Fit the model:model.fit(X_train, y_train)
Measure Model Performance:
model.score(X_test, y_test)
# Check attributes of model: ( You will get 4 different coefficient matrices )model.intercept_# You will find 4 intercepts 1 for each classmodel.coef_
Classifications
# Check classification probability for one observation:model.predict_proba(X_test[0:1])
model.predict(X_test[0:1])