Hướng dẫn draw roc curve python
Logistic Regression is a statistical method that we use to fit a regression model when the response variable is binary. To assess how well a logistic regression model fits a dataset, we can look at the following two metrics: One way to visualize these two metrics is by creating a ROC curve, which stands for “receiver operating characteristic” curve. This is a plot that displays the sensitivity and specificity of a logistic regression model. The
following step-by-step example shows how to create and interpret a ROC curve in Python. First, we’ll import the packages necessary to perform logistic regression in Python: Next, we’ll import a dataset and fit a logistic regression model to it: Next, we’ll calculate the true positive rate and the false positive rate and create a ROC curve using the Matplotlib data visualization package: The more that the curve hugs the top left corner of the plot, the better the model does at classifying the data into categories. As we can see from the plot above, this logistic regression model does a pretty poor job of classifying the data into categories. To quantify this, we can calculate the AUC – area under the curve – which tells us how much of the plot is located under the curve. The closer AUC is to 1, the better the model. A model with an AUC equal to 0.5 is no better than a model that makes random classifications. Step 4: Calculate the AUCWe can use the following code to calculate the AUC of the model and display it in the lower right corner of the ROC plot: #define metrics
y_pred_proba = log_regression.predict_proba(X_test)[::,1]
fpr, tpr, _ = metrics.roc_curve(y_test, y_pred_proba)
auc = metrics.roc_auc_score(y_test, y_pred_proba)
#create ROC curve
plt.plot(fpr,tpr,label="AUC="+str(auc))
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.legend(loc=4)
plt.show() The AUC for this logistic regression model turns out to be 0.5602. Since this is close to 0.5, this confirms that the model does a poor job of classifying data. Related: How to Plot Multiple ROC Curves in Python Tôi đang cố gắng vẽ đồ thị ROC để đánh giá độ chính xác của mô hình dự đoán mà tôi đã phát triển trong Python bằng cách sử dụng các gói hồi quy logistic. Tôi đã tính tỷ lệ dương thực sự cũng như tỷ lệ dương tính giả; tuy nhiên, tôi không thể tìm ra cách vẽ những thứ này một cách chính xác bằng cách sử dụng Dưới đây là hai cách bạn có thể thử, giả sử
hay là thử
Đây là cách đơn giản nhất để vẽ đường cong ROC, được đưa ra một bộ nhãn sự thật mặt đất và xác suất dự đoán. Phần tốt nhất là, nó vẽ đường cong ROC cho TẤT CẢ các lớp, do đó bạn cũng có được nhiều đường cong trông gọn gàng
Đây là một đường cong mẫu được tạo bởi plot_roc_curve. Tôi đã sử dụng bộ dữ liệu chữ số mẫu từ scikit-learn để có 10 lớp. Lưu ý rằng một đường cong ROC được vẽ cho mỗi lớp. Tuyên bố miễn trừ trách nhiệm: Lưu ý rằng điều này sử dụng thư viện scikit-cốt truyện mà tôi đã xây dựng. Vẫn chưa rõ vấn đề ở đây là gì, nhưng nếu bạn có một mảng
Đường cong AUC cho phân loại nhị phân bằng matplotlib
Tải dữ liệu ung thư vú
Tách bộ dữ liệu
Mô hình
Sự chính xác
Đường cong AUC
Đây là mã python để tính toán đường cong ROC (dưới dạng biểu đồ phân tán):
Các câu trả lời trước cho rằng bạn thực sự đã tự tính toán TP/Sens. Đó là một ý tưởng tồi để thực hiện việc này một cách thủ công, thật dễ dàng để mắc lỗi với các tính toán, thay vì sử dụng chức năng thư viện cho tất cả những điều này. hàm plot_roc trong scikit_lean thực hiện chính xác những gì bạn cần: http://scikit-learn.org/urdy/auto_examples/model_selection/plot_roc.html Phần thiết yếu của mã là:
Tôi đã thực hiện một chức năng đơn giản có trong một gói cho đường cong ROC. Tôi mới bắt đầu thực hành học máy, vì vậy xin vui lòng cho tôi biết nếu mã này có vấn đề! Hãy xem tập tin github readme để biết thêm chi tiết! :) https://github.com/bc123456/ROC
Một biểu đồ roc mẫu được tạo bởi mã này |