<jupyter_start><jupyter_text>a) K-Means clustering from scratch<jupyter_code>data.head()
data = data[['ApplicantIncome', 'LoanAmount']]
data = data.loc[:, ['ApplicantIncome', 'LoanAmount']]
X = data.values
sns.scatterplot(X[:,0], X[:, 1])
plt.xlabel('Income')
plt.ylabel('Loan')
plt.show()
def calculate_cost(X, centroids, cluster):
  sum = 0
  for i, val in enumerate(X):
    sum += np.sqrt((centroids[int(cluster[i]), 0]-val[0])**2 +(centroids[int(cluster[i]), 1]-val[1])**2)
  return sum
def kmeans(X, k):
  diff = 1
  cluster = np.zeros(X.shape[0])
  centroids = data.sample(n=k).values
  while diff:
    # for each observation
    for i, row in enumerate(X):
        mn_dist = float('inf')
        # dist of the point from all centroids
        for idx, centroid in enumerate(centroids):
            d = np.sqrt((centroid[0]-row[0])**2 + (centroid[1]-row[1])**2)
            # store closest centroid
            if mn_dist > d:
               mn_dist = d
               cluster[i] = idx
    new_centroids = pd.DataFrame(X).groupby(by=cluster).mean().values
     # if centroids are same then leave
    if np.count_nonzero(centroids-new_centroids) == 0:
        diff = 0
    else:
        centroids = new_centroids
  return centroids, cluster
cost_list = []
for k in range(1, 10):
    centroids, cluster = kmeans(X, k)
    # WCSS (Within cluster sum of square)
    cost = calculate_cost(X, centroids, cluster)
    cost_list.append(cost)
sns.lineplot(x=range(1,10), y=cost_list, marker='o')
plt.xlabel('k')
plt.ylabel('WCSS')
plt.show()
k = 4
centroids, cluster = kmeans(X, k)
sns.scatterplot(X[:,0], X[:, 1], hue=cluster)
sns.scatterplot(centroids[:,0], centroids[:, 1], s=100, color='y')
plt.xlabel('Income')
plt.ylabel('Loan')
plt.show()<jupyter_output><empty_output><jupyter_text>b) Hierarchical clustering (not from scratch)<jupyter_code># heirarchical clustering 
# data
a, b, c, d, e = (3,9), (3,8), (4,8), (2,9), (5,7)

f,g,h,i,j,k=(2,6), (3,6), (4,5), (1,7), (1,5), (5,4)
l,m,n,o,p,q=(6,9),(7,9),(8,7),(7,3),(7,2),(8,1)
data = np.array([a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q])

#data = np.array([(2,6),(3,7),(5,8),(5,5),(6,6),(2,2),(5,2),(7,3),(8,4),(10,6),(12,8)])  

# average link
avg_link_cluster = AgglomerativeClustering(n_clusters=3, linkage='average').fit(data)
plt.figure(figsize=(5,5))
plt.scatter(data[:,0], data[:,1], c=avg_link_cluster.labels_, cmap='rainbow')
plt.show()<jupyter_output><empty_output><jupyter_text>c) Gaussian mixture models clustering (not from scratch)<jupyter_code># GMM
from sklearn.datasets import make_moons
Xmoon, ymoon = make_moons(138, noise=.2, random_state=0)
plt.scatter(Xmoon[:, 0], Xmoon[:, 1]);
gmm = GaussianMixture(n_components=3, covariance_type="full", random_state=0)

from matplotlib.patches import Ellipse

def draw_ellipse(position, covariance, ax=None, **kwargs):
    """Draw an ellipse with a given position and covariance"""
    ax = ax or plt.gca()
    
    # Convert covariance to principal axes
    if covariance.shape == (2, 2):
        U, s, Vt = np.linalg.svd(covariance)
        angle = np.degrees(np.arctan2(U[1, 0], U[0, 0]))
        width, height = 2 * np.sqrt(s)
    else:
        angle = 0
        width, height = 2 * np.sqrt(covariance)
    
    # Draw the Ellipse
    for nsig in range(1, 4):
        ax.add_patch(Ellipse(position, nsig * width, nsig * height,
                             angle, **kwargs))
def plot_gmm(gmm, X, label=True, ax=None):
    ax = ax or plt.gca()
    labels = gmm.fit(X).predict(X)
    if label:
        ax.scatter(X[:, 0], X[:, 1], c=labels, s=40, cmap='viridis', zorder=2)
    else:
        ax.scatter(X[:, 0], X[:, 1], s=40, zorder=2)
    ax.axis('equal')
    
    w_factor = 0.2 / gmm.weights_.max()
    for pos, covar, w in zip(gmm.means_, gmm.covariances_, gmm.weights_):
        draw_ellipse(pos, covar, alpha=w * w_factor)
plot_gmm(gmm, Xmoon)<jupyter_output><empty_output><jupyter_text>d) DB Scan clustering (not from scratch) using pycaret library<jupyter_code>from pycaret.datasets import get_data
data = get_data('jewellery')

from pycaret.clustering import *
clu1 = setup(data, normalize=True, session_id=123)
kmeans = create_model('kmeans')
print(kmeans)
plot_model(kmeans)
dbscan = create_model("dbscan")
print(dbscan)
plot_model(dbscan)<jupyter_output><empty_output><jupyter_text>dbscan with sklearn<jupyter_code>from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
%matplotlib inline
#Determine centroids
centers = [[0.5, 2], [-1, -1], [1.5, -1]]
#Create dataset
X, y = make_blobs(n_samples=400, centers=centers, 
                  cluster_std=0.5, random_state=0)
#Normalize the values
X = StandardScaler().fit_transform(X)
plt.figure(figsize=(10,6))
plt.scatter(X[:,0], X[:,1], c=y, cmap='Paired')
from sklearn.cluster import DBSCAN
db = DBSCAN(eps=0.4, min_samples=20)
db.fit(X)
y_pred = db.fit_predict(X)
plt.figure(figsize=(10,6))
plt.scatter(X[:,0], X[:,1],c=y_pred, cmap='Paired')
plt.title("Clusters determined by DBSCAN")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17560879033854682, 'fraction_numerical': 0.03187487626212631, 'mean_word_length': 4.408993576017131, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '485030', 'license': 'MIT', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2020, 'n_words': 414, 'path': '255/clustering_hw/clustering_hw.ipynb', 'repo_name': 'austinwilson1224/SJSU', 'size': '5051'}