def dbscan(): X, s = sample_vecs(credit) db = dbscan_func(X,11,4.4) f, axes = plt.subplots(1, 2) scatter(X[:, 0], X[:, 1], ax=axes[0], hue=s) scatter(X[:, 0], X[:, 1], ax=axes[1], hue=db) results = precision_recall_fscore_support(db, s,average = "binary") print("precision:", results[0]) print("recall:", results[1]) print("f1:", results[2]) plt.show()
def gmm(): X, s = sample_vecs(credit, ratio=2) gmm = GaussianMixture(n_components=2, random_state=0).fit(X) gmm_labels = gmm.predict(X) if sum(gmm_labels) > len(gmm_labels) / 2: gmm_labels = 1 - gmm_labels f, axes = plt.subplots(1, 2) scatter(X[:, 0], X[:, 1], ax=axes[0], hue=s) scatter(X[:, 0], X[:, 1], ax=axes[1], hue=gmm_labels) results = precision_recall_fscore_support(gmm_labels, s,average = "binary") print("precision:", results[0]) print("recall:", results[1]) print("f1:", results[2]) plt.show()
def kmeans(): X,s = sample_vecs(credit, ratio = 100) kmeans = KMeans(n_clusters=2, random_state = 0).fit(X) kmeans_labels = kmeans.labels_ if sum(kmeans_labels) > len(kmeans_labels)/2: kmeans_labels = 1 - kmeans_labels f, axes = plt.subplots(1, 2) scatter(X[:,0], X[:,1], ax=axes[0],hue=s) scatter(X[:,0], X[:,1], ax=axes[1], hue=kmeans_labels) results = precision_recall_fscore_support(kmeans_labels,s,average = "binary") print("precision:",results[0]) print("recall:",results[1]) print("f1:",results[2]) plt.show()
def fcm(): X,s = sample_vecs(credit, ratio = 7) fcm = FCM(n_clusters=2, m=2.5).fit(X) fcm_labels = cutoff(fcm.u,0.6) #fcm_labels = fcm.u.argmax(axis = 1) if sum(fcm_labels) > len(fcm_labels)/2: fcm_labels = 1 - np.array(fcm_labels) f, axes = plt.subplots(1, 2) scatter(X[:, 0], X[:, 1], ax=axes[0], hue=s) scatter(X[:, 0], X[:, 1], ax=axes[1], hue=fcm_labels) results = precision_recall_fscore_support(fcm_labels, s,average = "binary") print("precision:", results[0]) print("recall:", results[1]) print("f1:", results[2]) plt.show()
def spectral(): warnings.filterwarnings('ignore') X, s = sample_vecs(credit,ratio = 3) distances = dist(X,X) spec = SpectralClustering(n_clusters=2, affinity='nearest_neighbors', random_state=0) spec_labels = spec.fit(distances).labels_ f, axes = plt.subplots(1, 2) scatter(X[:, 0], X[:, 1], ax=axes[0], hue=s) scatter(X[:, 0], X[:, 1], ax=axes[1], hue=spec_labels) results = precision_recall_fscore_support(spec_labels, s,average = "binary") print("precision:", results[0]) print("recall:", results[1]) print("f1:", results[2]) plt.show() return spec_labels
def c_mean_cluster_graph(Ehull, Form_eng): df = pd.DataFrame({'x': Ehull, 'y': Form_eng}) kmeans = KMeans(n_clusters=6) kmeans.fit(df) labels = kmeans.predict(df) z = pd.concat([Ehull, Form_eng], join='outer', axis=1) fcm = FCM(n_clusters=6) fcm.fit(z) fcm_labels = fcm.u.argmax(axis=1) f, axes = plt.subplots(1, 2, figsize=(11, 5)) scatter(Ehull, Form_eng, ax=axes[0], hue=labels) plt.title('fuzzy-c-means algorithm') scatter(Ehull, Form_eng, ax=axes[1], hue=fcm_labels) plt.show()
def Scatterplot(self): cmap = sns.cubehelix_palette(rot=-.2, as_cmap=True) sns.set() ax = sns.scatter(x="COD", y="TSS", hue="Temp", size="Q", palette=cmap, sizes=(10, 200), data=self)
def fuzzy(X): # X = np.array([(1,1),(1,2),(1,3),(2,2),(10,10),(100,100),(101,101),(102,102)]) # print(X.type) # fit the fuzzy-c-means fcm = FCM(n_clusters=4) fcm.fit(X) # outputs fcm_centers = fcm.centers fcm_labels = fcm.u.argmax(axis=1) print(fcm_labels) # plot result # %matplotlib inline f, axes = plt.subplots(1, 2, figsize=(11, 5)) scatter(X[:, 0], X[:, 1], ax=axes[0]) scatter(X[:, 0], X[:, 1], ax=axes[1], hue=fcm_labels) scatter(fcm_centers[:, 0], fcm_centers[:, 1], ax=axes[1], marker="s", s=200) plt.show() show_clusters(fcm_labels, X)
n_bins = 3 # use 3 bins for calibration_curve as we have 3 clusters here centers = [(-5, -5), (0, 0), (5, 5)] X, _ = make_blobs(n_samples=n_samples, n_features=3, cluster_std=1.0, centers=centers, shuffle=False, random_state=42) # fit the fuzzy-c-means fcm = FCM(n_clusters=3) fcm.fit(X) # outputs fcm_centers = fcm.centers fcm_labels = fcm.u.argmax(axis=1) print(len(X)) print(len(fcm_labels)) # plot result f, axes = plt.subplots(1, 2, figsize=(11, 5)) scatter(X[:, 0], X[:, 1], ax=axes[0]) scatter(X[:, 0], X[:, 1], ax=axes[1], hue=fcm_labels) scatter(fcm_centers[:, 0], fcm_centers[:, 1], ax=axes[1], marker="s", s=200) plt.show()
def plot(centers, labels, X): f, axes = plt.subplots(1, 2, figsize=(11, 5)) scatter(X[:, 0], X[:, 1], ax=axes[0]) scatter(X[:, 0], X[:, 1], ax=axes[1], hue=labels) scatter(centers[:, 0], centers[:, 1], ax=axes[1], marker="s", s=200) plt.show()
# plt.scatter(subcategoryGet, labelGet) # plt.show() testData = np.array(datasetTest) # print(testData) testDataGet = testData[:, 0:2].astype(float) # print(testDataGet) fcm = FCM(n_clusters=3) fcm.fit(cscGet) fcm_centers = fcm.centers fcm_labels = fcm.u.argmax(axis=1) f, axes = plt.subplots(1, 2, figsize=(11, 5)) scatter(cscGet[:, 0], cscGet[:, 1], ax=axes[0]) scatter(cscGet[:, 0], cscGet[:, 1], ax=axes[1], hue=fcm_labels) scatter(fcm_centers[:, 0], fcm_centers[:, 1], ax=axes[1], marker="*", s=200) closest_centroid = [] for x in range(len(testDataGet)): diff = fcm_centers - testDataGet[x, :] # print(diff) dist = np.sqrt(np.sum(diff**2, axis=-1)) # print(dist) closest_centroid.append(fcm_centers[np.argmin(dist), ]) print(fcm_centers) for x in range(len(closest_centroid)): print(closest_centroid[x])
fcm.fit(z) df = pd.DataFrame({ 'x':x, 'y':y}) kmeans = KMeans(n_clusters=5) kmeans.fit(df) labels = kmeans.predict(df) centroids = kmeans.cluster_centers_ fcm_labels = fcm.u.argmax(axis=1) f, axes = plt.subplots(1, 2, figsize=(11,5)) scatter(x, y, ax=axes[0],hue = labels) plt.title('fuzzy-c-means algorithm') scatter(x, y, ax=axes[1], hue=fcm_labels) #scatter(fcm_centers[:,0], fcm_centers[:,1], ax=axes[1],marker="s",s=200) plt.show() colors = ['b', 'orange', 'g', 'r', 'c', 'm', 'y', 'k', 'Brown', 'ForestGreen'] #cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(z, 5, 2, error=0.005, maxiter=1000, init=None) fig1, axes1 = plt.subplots(3, 3, figsize=(8, 8)) fpcs = [] for ncenters, ax in enumerate(axes1.reshape(-1), 2): cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(
df1 = np.array(data_clean_df['MerkAsus']).astype(int) df2 = np.array(data_clean_df['MerkLenovo']).astype(int) #impor library yang dibutuhkan from fcmeans import FCM from matplotlib import pyplot as plt from seaborn import scatterplot as scatter #centers = [(0, 0), (5, 5)] data_array = np.array(data_clean_df).astype(int) # fit the fuzzy-c-means fcm = FCM(n_clusters=2) fcm.fit(data_array) # outputs fcm_centers = fcm.centers fcm_labels = fcm.u.argmax(axis=1) #dalam bentuk array center = np.array(fcm_centers) #plot untuk menampilkan result f, axes = plt.subplots(1, 2, figsize=(11, 5)) scatter(df1, df2, ax=axes[0]) scatter(df1, df2, ax=axes[0], hue=fcm_labels) scatter(center[:, 0], center[:, 1], ax=axes[1], marker="s", s=200) plt.title('Plot Merk Asus dan Merk Lenovo') plt.show()
hue='cluster', palette="hot_r") plt.scatter(x=kmeans.cluster_centers_[:, 0], y=kmeans.cluster_centers_[:, 1], marker='x', s=2000) sns.relplot(data=X, hue=kmeans.labels_, x='Male', y='Female') from fcmeans import FCM from sklearn.datasets import make_blobs from matplotlib import pyplot as plt from seaborn import scatterplot as scatter # fit the fuzzy-c-means fcm = FCM(n_clusters=6) fcm.fit(X) # outputs fcm_centers = fcm.centers fcm_labels = fcm.u.argmax(axis=1) f, axes = plt.subplots(1, 2, figsize=(11, 5)) scatter(X["Female"], X["Male"], ax=axes[0]) scatter(X["Female"], X["Male"], ax=axes[1], hue=fcm_labels) scatter(fcm_centers["Female"], fcm_centers["Male"], ax=axes[1], marker="s", s=200) plt.show()