開始使用免費開始

檢視詐欺與非詐欺的比例

在本章中,你會使用 creditcard_sampledata.csv,這是一個包含信用卡交易資料的資料集。所幸,詐欺交易在這些交易中屬於「極少數」。

然而,Machine Learning 演算法通常在各類別於資料集中大致均衡時效果最佳。若詐欺案例太少,模型可學習的辨識資訊就不多。這種情況稱為「類別不平衡」(class imbalance),也是詐欺偵測的主要挑戰之一。

現在來探索這個資料集,並觀察這個類別不平衡的問題。

本練習屬於課程

Fraud Detection in Python

檢視課程

練習說明

  • 將 pandas 以 pd 匯入,讀取信用卡資料並指派給 df。這一步已替你完成。
  • 使用 .info() 列印 df 的資訊。
  • 使用 .value_counts() 取得 Class 欄中的詐欺與非詐欺交易筆數,並將結果指派給 occ。
  • 計算資料集中詐欺交易占所有交易的比例。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import pandas and read csv
import pandas as pd
df = pd.read_csv("creditcard_data.csv")

# Explore the features available in your dataframe
print(df.____)

# Count the occurrences of fraud and no fraud and print them
occ = df['____'].____()
print(occ)

# Print the ratio of fraud cases
print(occ / ____)
編輯並執行程式碼