Draw precision recall curve python
When using classification models in machine learning, two metrics we often use to assess the quality of the model are precision and recall. Show Precision: Correct positive predictions relative to total positive predictions. This is calculated as:
Recall: Correct positive predictions relative to total actual positives This is calculated as:
To visualize the precision and recall for a certain model, we can create a precision-recall curve. This curve shows the tradeoff between precision and recall for different thresholds. The following step-by-step example shows how to create a precision-recall curve for a logistic regression model in Python. Step 1: Import PackagesFirst, we’ll import the necessary packages: from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import precision_recall_curve import matplotlib.pyplot as plt Step 2: Fit the Logistic Regression ModelNext, we’ll create a dataset and fit a logistic regression model to it: #create dataset with 5 predictor variables
X, y = datasets.make_classification(n_samples=1000,
n_features=4,
n_informative=3,
n_redundant=1,
random_state=0)
#split dataset into training and testing set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3,random_state=0)
#fit logistic regression model to dataset
classifier = LogisticRegression()
classifier.fit(X_train, y_train)
#use logistic regression model to make predictions
y_score = classifier.predict_proba(X_test)[:, 1]
Step 3: Create the Precision-Recall CurveNext, we’ll calculate the precision and recall of the model and create a precision-recall curve: #calculate precision and recall
precision, recall, thresholds = precision_recall_curve(y_test, y_score)
#create precision recall curve
fig, ax = plt.subplots()
ax.plot(recall, precision, color='purple')
#add axis labels to plot
ax.set_title('Precision-Recall Curve')
ax.set_ylabel('Precision')
ax.set_xlabel('Recall')
#display plot
plt.show() The x-axis shows the recall and the y-axis shows the precision for various thresholds. Notice that as recall increases, precision decreases. This represents the tradeoff between the two metrics. To increase the recall of our model, the precision must decrease and vice versa. Additional ResourcesHow to Perform Logistic Regression in Python How do you make a precisionSet the figure size and adjust the padding between and around the subplots.. Create r, p and duplicate recall, i data points using numpy.. Create a figure and a set of subplots.. Plot the recall matrix in the range of r. ... . Plot the r and dup_r data points using plot() method.. To display the figure, use show() method.. How do you make a ROC curve in Python?How to plot a ROC Curve in Python?. Recipe Objective.. Step 1 - Import the library - GridSearchCv.. Step 2 - Setup the Data.. Step 3 - Spliting the data and Training the model.. Step 5 - Using the models on test dataset.. Step 6 - Creating False and True Positive Rates and printing Scores.. Step 7 - Ploting ROC Curves.. How do you plot a ROC curve?To plot the ROC curve, we need to calculate the TPR and FPR for many different thresholds (This step is included in all relevant libraries as scikit-learn ). For each threshold, we plot the FPR value in the x-axis and the TPR value in the y-axis. We then join the dots with a line. That's it!
|