Are you sure you want to delete this access key?
This project focuses on analyzing and categorizing customer sales data in a supermarket. It involves exploring the dataset, extracting useful features, visualizing relationships, and building a machine learning model.
The dataset used for this analysis is named "supermarket_sales - Sheet1.csv." It contains information about sales transactions, including features like 'Date,' 'Time,' 'Branch,' 'Product line,' 'Quantity,' 'Tax 5%,' 'Total,' 'Gross income,' 'Rating,' and more.
In this section, a machine learning model is developed using K-Means clustering to categorize customers based on their spending patterns.
from sklearn.model_selection import train_test_split
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# Fetching the 'cogs' (Cost of Goods) column
X = sales.iloc[:, -8].values.reshape(-1,1)
# Using the Elbow Method to find the optimal number of clusters (k)
wcss = []
for i in range(1, 11):
kmeans = KMeans(n_clusters=i, init='k-means++', random_state=42)
kmeans.fit(X)
wcss.append(kmeans.inertia_)
# Plotting the Elbow Method graph
plt.figure(figsize=(20,7))
plt.plot(range(1, 11), wcss)
plt.title('The Elbow Method')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
# Training the K-Means model on the dataset with k=5
kmeans = KMeans(n_clusters=5, init='k-means++', random_state=42)
y_kmeans = kmeans.fit_predict(X)
# Visualizing the clusters
plt.figure(figsize=(20,8))
plt.scatter(X[y_kmeans == 0], X[y_kmeans == 0], s=100, c='red', label='Lower Class')
plt.scatter(X[y_kmeans == 1], X[y_kmeans == 1], s=100, c='blue', label='Upper Lower Class')
plt.scatter(X[y_kmeans == 2], X[y_kmeans == 2], s=100, c='green', label='Medium Class')
plt.scatter(X[y_kmeans == 3], X[y_kmeans == 3], s=100, c='cyan', label='Upper Medium Class')
plt.scatter(X[y_kmeans == 4], X[y_kmeans == 4], s=100, c='magenta', label='Upper Class5')
plt.title('Clusters of customers')
plt.xlabel('Total expenditure')
plt.ylabel('COGs earned')
plt.legend()
plt.show()
Press p or to see the previous file or, n or to see the next file
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?
Are you sure you want to delete this access key?