<jupyter_start><jupyter_text>Random initialization of Cluster Centroids.<jupyter_code>def cluster_centroids_random_initializations(x , k = 2):
    c_x = np.random.uniform(np.min(x) , np.max(x)  , size = k )
    c = np.array(c_x).T
    
    return c

def random_clusters( x , k ):
    length = len(x)
    
    c = pd.DataFrame()
    
    centroid = cluster_centroids_random_initializations(x , k = k)
    centroid = np.array(sorted(centroid))
      
    ind = []
    ccc = []
    for i in range(length):
        distance =  [np.linalg.norm(x[i] - c)**2 for c in centroid]
        min_dist = np.min(distance)
        ind.append(i)
        ccc.append(np.linalg.norm(centroid[distance.index(min_dist)])) 
    c["index"] = ind
    c["closest_cc"] = ccc
    return c , centroid<jupyter_output><empty_output><jupyter_text>Creating different Clusters using cluster centroids.<jupyter_code>def clusters( x  , centroids):
    length = len(x)
    
    c = pd.DataFrame()

    centroid = centroids
        
    ind = []
    ccc = []
    label = []
    
    for i in range(length):
        distance =  [np.linalg.norm(x[i] - c)**2 for c in centroid]
        min_dist = np.min(distance)
        ind.append(i)
        ccc.append(np.linalg.norm(centroid[distance.index(min_dist)])) 
        label.append(distance.index(min_dist))
    c["index"] = ind
    c["closest_cc"] = ccc
    c["label"] = label
    return c , centroid<jupyter_output><empty_output><jupyter_text>Moving Centroids.<jupyter_code>def moving_centroids(x ,clusters,centroids):
    clusters["x"] = x
    new_centroids = []
    for i in centroids:
        new_centroids.append(np.mean(clusters["x"][clusters["closest_cc"] == i]))
    return np.array(new_centroids).T<jupyter_output><empty_output><jupyter_text>K means<jupyter_code>def kmean(x ,k = 2 , n_iters = 100 , randm_clust_iters = 10):
    outputs = pd.DataFrame()
    pred_y = []
    for col in x.columns.values:
        for i in range(randm_clust_iters):
            clust , centroids =  random_clusters(x[col] , k = k) 
            
        for iters in range(n_iters):
            
            new_centroids = moving_centroids(x[col] , clust , centroids )
            if sum(new_centroids) == sum(centroids):
                break
            centroids =  new_centroids
            clust , c = clusters(x[col] , centroids)
        outputs[col] = clust["label"]
    for i in range(len(outputs)):
        pred_y.append(pd.DataFrame(np.array(outputs)[i].tolist()).mode()[0][0])
    return pred_y<jupyter_output><empty_output><jupyter_text>Output<jupyter_code>pred_y = kmean(df_pd , k = 3 , n_iters = 50 , randm_clust_iters=20)<jupyter_output><empty_output><jupyter_text>Checking ouput or predicted clusters with original dependent variable of iris data set.<jupyter_code>print("confusion matrix :")
print(confusion_matrix(iris.target , pred_y))
print("accuracy score :")
print(accuracy_score(iris.target , pred_y))
df_pd["predict_y"] = pred_y
df_pd.head()
plt.figure(1)
plt.subplot(211)
plt.scatter( df_pd["sepal width (cm)"],df_pd["sepal length (cm)"] ,
                   c = df_pd["predict_y"] ,s = 10)
plt.xlabel("sepal width")
plt.ylabel("sepal length")
plt.show()

plt.figure(1)
plt.subplot(212)
plt.scatter(df_pd['petal width (cm)'] , df_pd['petal length (cm)'] , 
                  c = df_pd["predict_y"] , s = 10 )
plt.xlabel("petal width")
plt.ylabel("petal length")
plt.show()<jupyter_output><empty_output><jupyter_text>K means using sklearn<jupyter_code>df_ = pd.DataFrame(df_array , columns = col_names , index = None)
from sklearn.cluster import KMeans
algo = (KMeans(n_clusters=3))
algo.fit(df_)
pred_algo = algo.predict(df_)
print("confusion matrix : ")
print(confusion_matrix(iris.target , pred_algo))
print("accuracy score : ")
print(accuracy_score(iris.target , pred_algo))<jupyter_output>confusion matrix : 
[[ 0 50  0]
 [48  0  2]
 [14  0 36]]
accuracy score : 
0.24
{'fraction_non_alphanumeric': 0.13819676342152581, 'fraction_numerical': 0.011559208836372977, 'mean_word_length': 3.430034129692833, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '129497', 'license': 'MIT', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1452, 'n_words': 262, 'path': 'KmeansBareImplementation.ipynb', 'repo_name': 'kushal9090/Kmeans_bare_implementation', 'size': '3893'}