Clustering methods to detect fraud

Fraud Detection in Python

Charlotte Werger

Data Scientist

Clustering: trying to detect patterns in data

Fraud Detection in Python

K-means clustering: using the distance to cluster centroids

Fraud Detection in Python

K-means clustering: using the distance to cluster centroids

Fraud Detection in Python

K-means clustering: using the distance to cluster centroids

Fraud Detection in Python

Fraud Detection in Python

Fraud Detection in Python

Fraud Detection in Python

K-means clustering in Python

# Import the packages
from sklearn.preprocessing import MinMaxScaler
from sklearn.cluster import KMeans

# Transform and scale your data X = np.array(df).astype(np.float)
scaler = MinMaxScaler() X_scaled = scaler.fit_transform(X)
# Define the k-means model and fit to the data kmeans = KMeans(n_clusters=6, random_state=42).fit(X_scaled)
Fraud Detection in Python

The right amount of clusters

Checking the number of clusters:

  • Silhouette method
  • Elbow curve
clust = range(1, 10) 
kmeans = [KMeans(n_clusters=i) for i in clust]

score = [kmeans[i].fit(X_scaled).score(X_scaled) for i in range(len(kmeans))]
plt.plot(clust,score) plt.xlabel('Number of Clusters') plt.ylabel('Score') plt.title('Elbow Curve') plt.show()
Fraud Detection in Python

The elbow curve

Fraud Detection in Python

Let's practice!

Fraud Detection in Python

Preparing Video For Download...