import os
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import metrics
from sklearn import cluster
os.chdir(r'D:\projects\wordpress\ex49')
os.getcwd()
iris = datasets.load_iris()
X, y = iris.data, iris.target
np.random.seed(123)
n_clusters = 3
c = cluster.KMeans(n_clusters=n_clusters)
c.fit(X)
y_pred = c.predict(X)
print(y_pred[::8])
print(y[::8])
idx_0, idx_1, idx_2 = (np.where(y_pred == n) for n in range(3))
y_pred[idx_0], y_pred[idx_1], y_pred[idx_2] = 2, 0, 1
print(y_pred[::8])
print(metrics.confusion_matrix(y, y_pred))
N = X.shape[1]
fig, axes = plt.subplots(N, N, figsize=(12, 12), sharex=True, sharey=True)
colors = ["coral", "blue", "green"]
markers = ["^", "v", "o"]
for m in range(N):
    for n in range(N):
        for p in range(n_clusters):
            mask = y_pred == p
            axes[m, n].scatter(X[:, m][mask], X[:, n][mask],
                               marker=markers[p], s=30,
                               color=colors[p], alpha=0.25)

[crayon-6738e04b9b298996488700/]

fig.tight_layout()
plt.savefig("example49.png", dpi=100)
plt.show()
plt.close()

 

example49

 

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading