K-Means Clustering Algorithm From Scratch In Python | ML Algorithms From Scratch

preview_player
Показать описание
In this video, we'll demystify the K-Means Clustering algorithm by implementing it from scratch in Python. Dive deep into the inner workings of K-Means as we build the algorithm step by step.

Code:

============================
Do you want to learn from me?
============================

📱 Grow with us:

⌚Time Stamps⌚

00:00 - Intro
00:34 - Code Demo
01:55 - Creating a Class & Constructor
10:58 - Kmeans Intuition
32:05 - Running the Code
Рекомендации по теме
Комментарии
Автор

this channel is a hidden gem
very soon going to be popular

tejasarya
Автор

I recently came across your YouTube channel and I am amazed to see the amount of efforts you are putting to explain every single concept with all the minute details.. have followed so many videos.. This is the best out of the lot!! Keep up the good work.. Thanks alot.

seemaaswani
Автор

I’m a great fan of your content, sir, and I truly appreciate the value it brings to learners worldwide in the fields of Machine Learning and Deep Learning. I have a small request: as your videos are watched by people from various backgrounds, it would be incredibly helpful if more of the content were delivered in English. This would make it easier for a broader audience, like myself, to follow along and fully benefit from your teachings. I hope that, from your upcoming series on PyTorch and Generative AI, we might see more content in English. Thank you very much for considering this suggestion!

mgopalakrishnan
Автор

This youtube channel is really hidden gem !!!!
Awesome Explanation

etudemath
Автор

Cant believe I paid for that faltu ds ineuron course when there was this gem here all along. Thankyou for all your efforts, im learning so much everyday.

sanaayakurup
Автор

Your way of teaching is soo good I must say.
The way you have explained this concept is really appreciable.
Thank you for that.

Anushkaa_Thakur
Автор

33:27

Thanks a lot sir !! I did it !!

import random
import numpy as np
import matplotlib.pyplot as plt

class KMeans_:
def __init__(self, number_of_clusters=2, max_iterations=100):
self.number_of_clusters = number_of_clusters
self.max_iterations = max_iterations
self.centroids = None

def calculate_centroid_distance(self, row):
# calculate the distance from the row to the centroid
distances = []
for cent in self.centroids:
curr_row_cent_dist = np.sqrt(
np.dot(
row - cent,
row - cent
)
)

return distances


def assign_cluster_group(self, x):
# this will have decided cluster group for the point
cluster_group = []
for row in x:
distances =
# find nearest centroid from the point
min_distance = min(distances)
index_position =

cluster_group = np.array(cluster_group)
return cluster_group

def move_centroids(self, x, cluster_group):
# new centroids to move will be returned from here
new_centroids = []
cluster_types = np.unique(cluster_group)
for cls_type in cluster_types:
# get the cluster type's data . and calculate its mean
cls_detail = x[cluster_group == cls_type]
new_cont = cls_detail.mean(axis=0)

new_centroids = np.array(new_centroids)
return new_centroids

def fit_predict(self, x):
random_index = random.sample(
range(x.shape[0]),
self.number_of_clusters
)
self.centroids = x[random_index]
for i in range(self.max_iterations):
# assign cluster groups
cluster_group = self.assign_cluster_group(x)
old_centroids = self.centroids
# move centroids
self.centroids = self.move_centroids(x, cluster_group)
# check finish, if both centroids are same now (not moving)
if (old_centroids == self.centroids).all():
break
return cluster_group

def calculate_wcss(self, x, cluster_group):
wcss = 0
for i in
cluster_points = x[cluster_group == i]
centroid = self.centroids[i]
# square and add the distance within the clusters
wcss += np.sum((cluster_points - centroid) ** 2)
return wcss

def find_appropriate_no_of_clusters(self, x, max_no_of_clusters):
wcss = []
for no_clusters in range(1, max_no_of_clusters + 1):
self.number_of_clusters = no_clusters
cluster_group = self.fit_predict(x)
wcss_value = self.calculate_wcss(x, cluster_group)
wcss.append(wcss_value)
plt.plot(range(1, max_no_of_clusters + 1), wcss)
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.title('Elbow Method For Optimal k')
plt.show()

Your videos are like a gold mine for all us .

ayushkhaire_dev
Автор

You are the best sir .I finally understood this algorithm.

sofiamalik
Автор

Awesome, felt so happy while coding along with you! You are one of my great educators!

somanathking
Автор

Sir always outperforms so the channel is. Unique & no. 1. Thanku very very much for 'The Great Videos'.

siyays
Автор

Sir I am following your channel and learning a lot. Sir Ek request hai kindly create a project on Pycharm because in industry we have work on it and we require a understanding of it.

ajaykushwaha
Автор

Sir ek request hai, ek baar ap ML project life cycle explain ker sakte hain, q ki bahot confusion hai.
Data import --> Featuring engineering --> feature selection tak sab theek hai uske baad confusion hai. Some people say PCA come at last, some say before like this. Require your help on this.
As usual great teaching, awesome explanation. Sir deep learning k playlist mein Regression and classification project kindly add kijiye. I saw you video on DL. App ne keha tha ki ek time baad ML ka performance stable ho jata hai unlike DL. so I want to learn ANN so that same ML problem ko Regression and classification ko best possible way mein solve ker sakein.

ajaykushwaha-jemw
Автор

Sir please make a video one the series roadm map of ML algorithms... k phly knsa algorithm seekhy or ese kn knsa seekhy series vise Supervised ka to pta chl gya Un-Supervised ka ni pta lg ra or Basic Neural network kha se kry start please Sir aik video is pr b bnaa dy guide krdy ... i am sure in future many beginners ask for this video to you!

GamerBoy-iijc
Автор

Plz also upload videos on Concept Learning (Find S-Algorithm & many more)

aounhaider
Автор

hello Sir hope you're doing great, i started watchng your videos 2 months ago and havent got these type of explanation anywhere else, sir im working as an intern and they asked me to learn how to create a voice synthesizer, can you guide how to do that?

aameenmd
Автор

Sir, Could you please help me in this. I have a dataframe with multiple wells having data along with DEPTH as one column. Now i want to create a flag column into the dataframe based on depth falling in any of the interval (for each well 3-4 intervals are there) for that particular well mentioned in an excel sheet. How can i do this? Thank you in advance.

coloringwithmaisha
Автор

Bounce kr gya, how much experience need to code like this

ErSonuSinghh