<jupyter_start><jupyter_text>**Heirarchical Clustering**<jupyter_code>import time as time
import scipy.cluster.hierarchy as sch
import mpl_toolkits.mplot3d.axes3d as p3
from sklearn.cluster import AgglomerativeClustering

X = df[['qty','revenue']].values
X.shape
ac = AgglomerativeClustering(n_clusters=4, affinity='euclidean', linkage='ward', memory=None)
ac
y_ac = ac.fit_predict(X)
y_ac
df_y_ac = pd.DataFrame(y_ac)
df_y_ac.head()
fig = plt.figure(figsize=(13,11))
plt.scatter(X[y_ac == 0,0], X[y_ac == 0,1],  c ='red', label='Cluster 1')
plt.scatter(X[y_ac == 1,0], X[y_ac == 1,1],  c ='orange', label='Cluster 2')
plt.scatter(X[y_ac == 2,0], X[y_ac == 2,1],  c ='green', label='Cluster 3')
plt.scatter(X[y_ac == 3,0], X[y_ac == 3,1],  c ='black', label='Cluster 4')
plt.title('Cluster of Transactions and Revenue')
plt.xlabel('Count of Transactions')
plt.ylabel('Revenue')
plt.legend()
plt.show();<jupyter_output><empty_output><jupyter_text>* Cluster 1: Quantity [0-7], Revenue [0-125] * Cluster 2: Quantity [2-20], Revenue [125-375]* Cluster 3: Quantity [6-25], Revenue [400-1100]* Cluster 4: Quantity [12-25], Revenue [1200+]<jupyter_code>df = pd.concat([df, pd.get_dummies(df['region'])], axis=1)
df.head()
X = df[['qty','revenue','East','North','West','South']].values
X.shape
# #############################################################################
# Compute clustering
print("Compute unstructured hierarchical clustering...")
st = time.time()
ward = AgglomerativeClustering(n_clusters=4, linkage='ward').fit(X)
elapsed_time = time.time() - st
label = ward.labels_
print("Elapsed time: %.2fs" % elapsed_time)
print("Number of points: %i" % label.size)

# #############################################################################
# Plot result
fig = plt.figure(figsize=(21,13))
ax = p3.Axes3D(fig)
ax.view_init(37, -37)
for l in np.unique(label):
    ax.scatter(X[label == l, 0], X[label == l, 1], 
               color=plt.cm.jet(np.float(l) / np.max(label + 1)),
               s=30, edgecolor='k')
plt.title('Without connectivity constraints (time %.2fs)' % elapsed_time)
plt.legend()<jupyter_output>Compute unstructured hierarchical clustering...
{'fraction_non_alphanumeric': 0.2456221198156682, 'fraction_numerical': 0.03870967741935484, 'mean_word_length': 6.188741721854305, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '76011', 'license': 'MIT', 'n_tokens_mistral': 893, 'n_tokens_neox': 790, 'n_words': 166, 'path': 'machinelearning/ml_python_clustering_1.ipynb', 'repo_name': 'futureseadev/hgwxx7', 'size': '2170'}