Kmeans Algorithm

K means Clustering – Introduction
We are given a data set of items, with certain features, and values for these features (like a vector). The task is to categorize those items into groups. To achieve this, we will use the kMeans algorithm; an unsupervised learning algorithm.

Here is an python code that simply divides the vector into group of clusters using kmeans algorithm.

import math
x=[185,170,168,179,182,188]
y=[72,56,60,68,72,77]

k=2

clust1=[“cluster1”]
clust2=[“cluster2”]

k1=[]
k2=[]
k1.append([x[0],y[0]])
k2.append([x[1],y[1]])

for i in range(len(x)):

c1=(x[i]-k1[0][0])**2
c2=(y[i]-k1[0][1])**2
print(c1)
print(c2)
print(math.sqrt((c1+c2)))
clust1.append(math.sqrt((c1+c2)))

for i in range(len(x)):
c11=(k2[0][0]-x[i])**2
c22=(k2[0][1]-y[i])**2
print(c11)
print(c22)

print(math.sqrt((c11+c22)))

clust2.append(math.sqrt((c11+c22)))

if clust1[i] > clust2[i]:

k2[0]=[(k2[0][0]+x[i])/2,(k2[0][1]+y[i])/2]
print(k2)
else:
k1[0]=[(k1[0][0]+x[i])/2,(k1[0][1]+y[i])/2]
print(k1)

print(k1)
print(k2)
print(clust1)
print(clust2)

Download code:- Github Link

Data Science

 

Hey there! I created this site to learn an explore new technology
so here is an example of  simple forward propagation algorithm

import numpy as np

input_data=np.array([2,3])
print(input_data)

weights={
‘node_0’:np.array([1,1]),
‘node_1’:np.array([-1,1]),
‘output’:np.array([2,-1])
}
node_0_value=(input_data * weights[‘node_0’]).sum()
node_1_value=(input_data * weights[‘node_1’]).sum()

hidden_layer_values=np.array([node_0_value,node_1_value])

output=(hidden_layer_values * weights[‘output’]).sum()
print(output)

Download Code:-Github Link