<jupyter_start><jupyter_text>First 2 dimensions<jupyter_code>x = [v[0] for v in iris_data.data]
y = [v[1] for v in iris_data.data]

# Create a trace
trace = go.Scatter(
    x = x,
    y = y,
    mode = 'markers'
)

layout= go.Layout(
    title= 'Iris Dataset',
    hovermode= 'closest',
    xaxis= dict(
        title= 'sepal length (cm)',
        ticklen= 5,
        zeroline= False,
        gridwidth= 2,
    ),
    yaxis=dict(
        title= 'sepal width (cm)',
        ticklen= 5,
        gridwidth= 2,
    ),
    showlegend= False
)

data = [trace]

fig= go.Figure(data=data, layout=layout)
plot(fig)<jupyter_output><empty_output><jupyter_text>Scatter plot matrix<jupyter_code>import pandas as pd
df = pd.DataFrame(iris_data.data,
                         columns=['sepal length (cm)',
                                  'sepal width (cm)',
                                  'petal length (cm)',
                                  'petal width (cm)'])

df['class'] = [iris_data.target_names[i] for i in iris_data.target]
df.head()
fig = ff.create_scatterplotmatrix(df, index='class', diag='histogram', size=10, height=800, width=800)
plot(fig)<jupyter_output><empty_output><jupyter_text>3D Chart<jupyter_code># Creating data for the plotly
trace1 = go.Scatter3d(
    # Extracting data based on label
    x=[x[0][0] for x in zip(iris_data.data, iris_data.target) if x[1] == 0],
    y=[x[0][2] for x in zip(iris_data.data, iris_data.target) if x[1] == 0],
    z=[x[0][3] for x in zip(iris_data.data, iris_data.target) if x[1] == 0],
    mode='markers',
    marker=dict(
        size=12,
        line=dict(
            color='rgba(217, 217, 217, 0.14)',
            width=0.5
        ),
        opacity=0.8
    )
)

trace2 = go.Scatter3d(
    # Extracting data based on label
    x=[x[0][0] for x in zip(iris_data.data, iris_data.target) if x[1] == 1],
    y=[x[0][2] for x in zip(iris_data.data, iris_data.target) if x[1] == 1],
    z=[x[0][3] for x in zip(iris_data.data, iris_data.target) if x[1] == 1],
    mode='markers',
    marker=dict(
        color='rgb(#3742fa)',
        size=12,
        symbol='circle',
        line=dict(
            color='rgb(204, 204, 204)',
            width=1
        ),
        opacity=0.9
    )
)

trace3 = go.Scatter3d(
    # Extracting data based on label
    x=[x[0][0] for x in zip(iris_data.data, iris_data.target) if x[1] == 2],
    y=[x[0][2] for x in zip(iris_data.data, iris_data.target) if x[1] == 2],
    z=[x[0][3] for x in zip(iris_data.data, iris_data.target) if x[1] == 2],
    mode='markers',
    marker=dict(
        color='rgb(#ff4757)',
        size=12,
        symbol='circle',
        line=dict(
            color='rgb(104, 74, 114)',
            width=1
        ),
        opacity=0.9
    )
)


data = [trace1, trace2, trace3]

# Layout settings
layout = go.Layout(
    scene = dict(
        xaxis = dict(
            title= 'sepal length (cm)'),
        yaxis = dict(
            title= 'petal length (cm)'),
        zaxis = dict(
            title= 'petal width (cm)'),),
)


fig = go.Figure(data=data, layout=layout)
plot(fig)<jupyter_output><empty_output><jupyter_text>Hyperparameters<jupyter_code>from sklearn.cluster import KMeans
KMeans?
from sklearn.datasets.samples_generator import make_blobs
X, y = make_blobs(n_samples=500, centers=3, n_features=3, cluster_std=[2.1, 1.5, 3.7], random_state=442)
k_means = KMeans(n_clusters=3)
y_pred = k_means.fit_predict(X)
print(y_pred)
k_means = KMeans(n_clusters=3, init='random', n_init=1, max_iter=1)
y_pred = k_means.fit_predict(X)
print(y_pred)<jupyter_output>[2 0 2 2 1 1 2 1 2 1 2 0 1 1 0 1 0 2 2 2 2 1 1 2 1 0 1 0 0 2 0 0 0 1 2 0 0
 1 1 2 1 2 2 2 2 2 2 0 2 2 0 0 1 0 1 1 2 2 0 1 1 1 1 2 1 2 0 1 1 2 0 0 1 1
 1 1 1 0 1 1 2 1 2 1 2 2 0 2 1 2 2 0 2 1 0 0 2 0 0 2 1 1 0 1 0 1 1 0 0 2 1
 0 1 2 0 2 1 1 1 1 1 2 1 2 2 2 1 0 1 0 2 1 2 2 1 2 2 0 1 2 2 1 0 2 2 2 2 2
 2 1 0 0 1 2 1 0 2 1 1 0 0 2 1 1 1 1 0 1 2 0 0 2 2 1 1 2 1 2 1 1 0 2 1 0 1
 1 1 0 2 2 1 2 1 0 0 1 0 0 1 0 2 0 2 2 2 1 0 1 2 2 1 2 2 0 2 1 0 0 1 1 2 0
 1 0 1 2 1 2 0 2 1 1 0 1 2 0 1 0 2 2 2 1 2 2 1 0 0 1 0 0 2 1 2 1 2 1 2 2 1
 2 1 2 1 1 1 0 2 2 2 2 1 1 1 0 0 0 0 0 0 0 2 0 1 1 2 2 2 2 0 2 0 0 0 1 2 0
 2 1 2 2 1 1 2 2 1 1 2 0 1 2 0 2 0 0 2 0 0 1 1 1 1 1 2 2 1 0 0 2 2 0 0 2 0
 1 0 2 0 1 2 1 1 2 0 2 2 0 0 0 2 1 2 2 1 2 1 2 2 0 2 1 2 2 0 2 2 1 2 0 2 2
 2 2 0 2 1 1 0 1 2 0 2 2 0 2 0 0 2 0 1 1 1 2 2 0 1 1 1 2 2 1 2 1 1 1 1 2 0
 2 1 2 2 2 1 0 1 0 0 1 0 1 0 1 1 1 0 1 0 1 2 2 1 1 2 1 1 0 2 1 1 2 0 1 2 1
 2 2 1 0 2 2 2 0 2 2 1 2 2 1 1 1 1 1 1 0 2 2 1 1 1 2 0 0 1 1 2 1 0 1 0 1 0
 1 2 0 2 0 0 2 1 1 2 2 2 [...]<jupyter_text>Implement algorithm for one variable<jupyter_code>X = np.array([1,2,3,2,1,3,9,8,11,12,10,11,14,25,26,24,30,22,24,27])
trace1 = go.Scatter(
    x=X,
    y=[0 for num in X],
    mode='markers',
    name='Data',
    marker=dict(
        size=12
    )
)

layout = go.Layout(
    title='1D vector',
)
traces = [trace1]

fig = go.Figure(data=traces, layout=layout)

plot(fig)<jupyter_output><empty_output><jupyter_text>KMeans for one variable<jupyter_code>import numpy as np
X = np.array([1,2,3,2,1,3,9,8,11,12,10,11,14,25,26,24,30,22,24,27])
n_clusters = 3
c_centers = np.random.choice(X, n_clusters)
print(c_centers)
deltas = np.array([np.abs(point - c_centers) for point in X])
deltas
deltas.argmin(1)
c_centers = np.array([X[np.where(deltas.argmin(1) == i)[0]].mean() for i in range(3)])
print(c_centers)
def Kmeans_1D(X, n_clusters, random_seed=442):
    
    # Randomly choose random indexes as cluster centers
    rng = np.random.RandomState(random_seed)
    i = rng.permutation(X.shape[0])[:n_clusters]
    c_centers = X[i]
    
    # Calculate distances between each point and cluster centers
    deltas = np.array([np.abs(point - c_centers) for point in X])
    
    # Get labels for each point
    labels = deltas.argmin(1)

    while True:

        # Calculate mean of each cluster
        new_c_centers = np.array([X[np.where(deltas.argmin(1) == i)[0]].mean() for i in range(n_clusters)])

        # Calculate distances again
        deltas = np.array([np.abs(point - new_c_centers) for point in X])
    
        # Get new labels for each point
        labels = deltas.argmin(1)

        # If there's no change in centers, exit
        if np.all(c_centers == new_c_centers):
            break
        c_centers = new_c_centers

    return c_centers, labels
c_centers, labels = Kmeans_1D(X, 3)
print(c_centers, labels)
trace1 = go.Scatter(
    x=X,
    y=[0 for num in X],
    mode='markers',
    name='Data',
    marker=dict(
        size=12
    )
)

trace2 = go.Scatter(
    x = c_centers,
    y = [0 for num in X],
    mode='markers',
    name = 'Cluster centers',
    marker = dict(
        size=12,
        color = ('rgb(122, 296, 167)'))
)

layout = go.Layout(
    title='1D vector',
)
traces = [trace1, trace2]

fig = go.Figure(data=traces, layout=layout)

plot(fig)<jupyter_output><empty_output><jupyter_text>KMeans with multiple variables  Reading dataset as numpy array<jupyter_code>from numpy import genfromtxt
wholesales_data = genfromtxt('Wholesale customers data.csv', delimiter=',', skip_header=1)
print(wholesales_data[:5])
wholesales_data.shape
wholesales_data_norm = wholesales_data / np.linalg.norm(wholesales_data)
print(wholesales_data_norm[:5])
import pandas as pd
df = pd.DataFrame(wholesales_data_norm,
                         columns=['Channel',
                                  'Region',
                                  'Fresh',
                                  'Milk',
                                  'Grocery',
                                  'Frozen',
                                  'Detergents_Paper',
                                  'Delicassen'])
df.head(10)
fig = ff.create_scatterplotmatrix(df, diag='histogram', size=7, height=1200, width=1200)
plot(fig)
df.corr()
import seaborn as sns; sns.set()
ax = sns.heatmap(df.corr(), annot=True)
# Creating data for the plotly
trace1 = go.Scatter3d(
    # Extracting data based on label
    x=df['Grocery'],
    y=df['Detergents_Paper'],
    z=df['Milk'],
    mode='markers',
    marker=dict(
        size=12,
        line=dict(
            color='rgba(217, 217, 217, 0.14)',
            width=0.5
        ),
        opacity=0.8
    )
)

# Layout settings
layout = go.Layout(
    scene = dict(
        xaxis = dict(
            title= 'Grocery'),
        yaxis = dict(
            title= 'Detergents_Paper'),
        zaxis = dict(
            title= 'Milk'),),
)

data = [trace1]

fig = dict(data=data, layout=layout)
plot(fig)
df = df[[col for col in df.columns if col not in ['Channel', 'Region']]]
df.head(10)
df.values
def Kmeans_nD(X, n_clusters, random_seed=442):
    
    # Randomly choose random indexes as cluster centers
    rng = np.random.RandomState(random_seed)
    i = rng.permutation(X.shape[0])[:n_clusters]
    c_centers = X[i]
    
    # Calculate distances between each point and cluster centers
    deltas = np.array([[np.linalg.norm(i - c) for c in c_centers] for i in X])
    
    # Get labels for each point
    labels = deltas.argmin(1)

    while True:

        # Calculate mean of each cluster
        new_c_centers = np.array([X[np.where(deltas.argmin(1) == i)[0]].mean(axis=0) for i in range(n_clusters)])

        # Calculate distances again
        deltas = np.array([[np.linalg.norm(i - c) for c in new_c_centers] for i in X])
    
        # Get new labels for each point
        labels = deltas.argmin(1)

        # If there's no change in centers, exit
        if np.array_equal(c_centers, new_c_centers):
            break
        c_centers = new_c_centers

    return c_centers, labels
X = df.values
random_seed = 442
n_clusters = 3
rng = np.random.RandomState(random_seed)
i = rng.permutation(X.shape[0])[:n_clusters]
c_centers = X[i]
c_centers
deltas = np.array([[np.linalg.norm(i - c) for c in c_centers] for i in X])
deltas
labels = deltas.argmin(1)
labels
new_c_centers = np.array([X[np.where(deltas.argmin(1) == i)[0]].mean(axis=0) for i in range(n_clusters)])
new_c_centers
centers, labels = Kmeans_nD(df[['Grocery', 'Detergents_Paper']].values, 3)
labels
df['labels'] = labels
# Creating data for the plotly
trace1 = go.Scatter(
    # Extracting data based on label
    x=df[df['labels'] == 0]['Grocery'],
    y=df[df['labels'] == 0]['Detergents_Paper'],
    mode='markers',
    name='clust_1',
    marker=dict(
        size=12,
        line=dict(
            color='rgba(217, 217, 217, 0.14)',
            width=0.5
        ),
        opacity=0.8
    )
)

trace2 = go.Scatter(
    # Extracting data based on label
    x=df[df['labels'] == 1]['Grocery'],
    y=df[df['labels'] == 1]['Detergents_Paper'],
    mode='markers',
    name='clust_2',
    marker=dict(
        color='rgb(#3742fa)',
        size=12,
        symbol='circle',
        line=dict(
            color='rgb(204, 204, 204)',
            width=1
        ),
        opacity=0.9
    )
)

trace3 = go.Scatter(
    # Extracting data based on label
    x=df[df['labels'] == 2]['Grocery'],
    y=df[df['labels'] == 2]['Detergents_Paper'],
    mode='markers',
    name='clust_3',
    marker=dict(
        color='rgb(#ff4757)',
        size=12,
        symbol='circle',
        line=dict(
            color='rgb(104, 74, 114)',
            width=1
        ),
        opacity=0.9
    )
)


data = [trace1, trace2, trace3]

# Layout settings
layout = go.Layout(
    scene = dict(
        xaxis = dict(
            title= 'Grocery'),
        yaxis = dict(
            title= 'Detergents_Paper'),
    )
)


fig = go.Figure(data=data, layout=layout)
plot(fig)
df.groupby('labels').mean()
from scipy.spatial import distance
a = (1,2,3)
b = (4,5,6)
dst = distance.euclidean(a,b)
dst
distance.euclidean?<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1317855923523387, 'fraction_numerical': 0.07348924547627177, 'mean_word_length': 2.388374783111625, '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': 89, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '489655', 'license': 'MIT', 'n_tokens_mistral': 5211, 'n_tokens_neox': 4451, 'n_words': 1349, 'path': 'Ch05/Chapter05.ipynb', 'repo_name': 'umitmertcakmak/Mastering_Numerical_Computing_with_NumPy', 'size': '11716'}