Posts

Showing posts from December, 2023

How to detect Credit Card Fraud Using Python Pandas

Image
  Detecting fraud in credit card transactions is an important application of Machine Learning. Given below is a step-by-step guide on how to approach fraud detection using Python (Pandas and Scikit-Learn) with the Credit Card Fraud Detection Dataset from Kaggle: Data source: Credit Card Fraud Detection Dataset  https://www.kaggle.com/mlg-ulb/creditcardfraud Step 1: Data Preprocessing Start by importing the necessary libraries and loading the dataset into a Pandas DataFrame. import pandas as pd # Load the dataset data = pd.read_csv('creditcard.csv') #replace with the downloaded file path # Explore the dataset print(data.head()) Output: Step 2: Data Exploration Understand the dataset by checking its structure, summary statistics, and class distribution (fraudulent vs. non-fraudulent transactions). # Check the dataset shape print (data.shape) # Check summary statistics #print(data.describe()) # Check class distribution print (data[ 'Class' ].value_counts()) Output: ( 284

Outlier detection Z-Score Method: Python Pandas

Image
To find outliers in a dataset, you can use various statistical methods and visualization techniques. Here’s a step-by-step approach to identify outliers and explain the process: Step 1: Understand the Data. Before identifying outliers, it’s important to have a good understanding of the dataset you’re working with. Familiarize yourself with the variables and their meanings, as well as any potential data collection issues. Step 2: Choose an Outlier Detection Method. There are several commonly used methods for outlier detection, including: Z-Score Method: Calculates the number of standard deviations away from the mean each data point is. Points beyond a certain threshold (e.g., 2 or 3 standard deviations) are considered outliers. IQR Method: Uses the Interquartile Range (IQR) to identify outliers. Points that fall below Q1–1.5 * IQR or above Q3 + 1.5 * IQR are considered outliers, where Q1 and Q3 represent the 25th and 75th percentiles, respectively. Mahalanobis Distance: Takes into ac

Mahalanobis distance among 5 points:

Image
  Mahalanobis distance among 5 points: To create a plot with five points and visualize the Mahalanobis distances between them, lets modify the above code as given in example below: import numpy as np import matplotlib. pyplot as plt from scipy. spatial import distance # Define the five points as arrays points = np.array([[1, 2], [4, 5], [2, 9], [7, 3], [5, 7]]) # Define the covariance matrix covariance = np.array([[2, 0], [0, 3]]) # Compute the pairwise Mahalanobis distances mahalanobis_distances = distance.cdist(points, points, 'mahalanobis', VI=np.linalg.inv(covariance)) # Create a scatter plot of the points plt.scatter(points[:, 0], points[:, 1], color='blue', label='Points') # Plot the Mahalanobis distances as lines for i in range(len(points)): for j in range(i + 1, len(points)): plt.plot([points[i, 0], points[j, 0]], [points[i, 1], points[j

K-means Clustering 3D Plot Swiss roll Dataset

Image
  K-means is a widely used clustering algorithm in machine learning and data mining. It is an unsupervised learning algorithm that aims to partition a given dataset into distinct groups or clusters based on similarity of data points. The algorithm is called “K-means” because it divides the data into K clusters, where K is a user-specified parameter. K-means aims to minimize the within-cluster sum of squares. The objective is to have the data points within each cluster as similar as possible, while keeping the clusters as distinct as possible. However, it’s important to note that K-means is sensitive to the initial random selection of cluster centers. Therefore, it’s often recommended to run the algorithm multiple times with different initializations and choose the clustering with the lowest inertia. Here’s an example of K-means clustering using Python and machine learning: import numpy as np import matplotlib. pyplot as plt from sklearn. cluster import KMeans from sklearn. dat