Logistic Regression in Python – Building a Classifier
Logistic Regression in Python – Building a Classifier
You don’t necessarily need to build a classifier from scratch. Building a classifier is complex and requires knowledge of fields such as statistics, probability theory, and optimization techniques. Several pre-built libraries are available that provide well-tested and highly effective implementations of these classifiers. We will use one such pre-built model from sklearn.
sklearn Classifier
Creating a Logistic Regression classifier from the sklearn toolkit is very simple and can be done with just one program statement, as shown in the figure.
In [22]: classifier = LogisticRegression(solver='lbfgs',random_state=0)
Once the classifier is created, you will feed your training data into the classifier so that it can adjust its internal parameters and prepare to predict your future data. To adjust the classifier, we run the following statement –
In [23]: classifier.fit(X_train, Y_train)
The classifier is now ready for testing. The following code shows the output of executing the above two statements –
Out[23]: LogisticRegression(C = 1.0, class_weight = None, dual = False,
fit_intercept=True, intercept_scaling=1, max_iter=100,
multi_class='warn', n_jobs=None, penalty='l2', random_state=0,
solver='lbfgs', tol=0.0001, verbose=0, warm_start=False))
Now, we are ready to test the classifier we created. We will deal with this in the next chapter.