-
Notifications
You must be signed in to change notification settings - Fork 0
/
K_means
81 lines (63 loc) · 1.85 KB
/
K_means
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
"""K-means6.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1xXA-GMf0Kh8KSaabFYKooatEV1X3R5CP
"""
from copy import deepcopy
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams['figure.figsize'] = (16, 9)
plt.style.use('ggplot')
from google.colab import drive
drive.mount('/content/drive/')
!ls
!ls "/content/drive/My Drive/CSE499.A/onlyADC.csv"
!ls
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_csv('/content/drive/My Drive/CSE499.A/onlyADC+Datapoints.csv')
#data1 = pd.read_csv('/content/drive/My Drive/CSE499.A/datapoints.csv')
#data.rename(index=str, columns={"237.39": "Voltage"})
#data1.rename(index)
print(data.shape)
data.head()
#Getting the values and plotting it
f2 = data['237.39'].values
f1 = data['1'].values
X = np.array(list(zip(f1[:524287],f2[:524287])))
#plt.scatter(f1,f2,c='violet',s=7)
np.shape(X)
s_f1 = f1[:524287]
s_f2 = f2[:524287]
def dist(a, b, ax=1):
return np.linalg.norm(a - b, axis=ax)
import random
#number of clusters
low = np.amin(f2)
high= np.amax(f2)
k = 4
# X coordinates of random centroids
C_y = np.random.uniform(low=low,high=high,size=k)
#if np.isnan(C_x):
#C_x1 = np.nan_to_num(C_x)
# Y coordinates of random centroids
C_x = np.random.uniform(low=2.0,high=524287.0,size=k)
#if np.isnan(C_x):
#C_y1 = np.nan_to_num(C_y)
C=np.array(list(zip(C_x,C_y)),dtype = np.int)
print(C)
from sklearn.cluster import KMeans
# Number of clusters
kmeans = KMeans(n_clusters=6)
# Fitting the input data
kmeans=kmeans.fit(X)
# Getting the cluster labels
labels = kmeans.predict(X)
# Centroid values
centroids = kmeans.cluster_centers_
print(centroids)
plt.scatter(centroids[:,0],centroids[:,1],c='red',s=200,alpha = 0.5)
plt.scatter(s_f1, s_f2, c='green', s=7)