利用sklearn包裡的birch演算法,以iris資料集,聚類結果視覺化
**如下
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_blobs
from sklearn.cluster import birch
import urllib.request
import sys
from sklearn.manifold import tsne
import pandas as pd
# import ybirch
with open("f:\tabtad\downloads\iris\iris.txt", 'r') as f:#開啟資料檔案
line = f.readlines()
# data = urllib.request .urlopen(target_url),encoding = 'utf-8'
xlist =
# labels =
for data in line:
# line = line.decode()
row = data.strip().split(",")#切詞,並將資料變成浮點形式
row = list(map(float,row))
del row[0]
x = np.array(xlist)#轉為numpy的矩陣形式
print(xlist)
print(x)
#未使用birch之前的資料情況
plt.scatter(x[:,0], x[:,1],x[:,2], marker = 'o')
plt.show()
# print(labels)
nrow = len(xlist)
ncol = len(xlist[0])
print ("number of rows of data = " + str(len(xlist)) + '\n')
sys.stdout.write("number of columns of data = " + str(len(xlist[1])) + '\n')
#y = make_blobs(n_samples=150, n_features=4, cluster_std=[0.4, 0.3, 0.4, 0.3])
# ##設定birch函式,訓練函式
model = birch(n_clusters = 3,threshold = 0.4)
y_pred = model.fit_predict(x)
#print(y_pred)
# 輸出標籤下樣本數目
r1 = pd.series(model.labels_).value_counts()
print(r1)#統計各個類別的數目
# 繪圖
plt.scatter(x[:,0], x[:,1],x[:,2], c= y_pred)
plt.show()
from sklearn import metrics
print("calinski_harabasz score",metrics.calinski_harabasz_score(x,y_pred))
# # help(birch)
# # print(len(y_pred))
結果視覺化
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt def add layer inputs,input size,output size,activation function none weight...
結果視覺化
1 import tensorflow as tf 2import numpy as np 3import matplotlib.pyplot as plt 4def add layer inputs,in size,out size,activation function none 5 weigh...
航空公司客戶聚類結果的T sne視覺化
本文主要承接上文,以tsne的方式,側面驗證聚類的效果。流程大致為 1.用sklearn對資料進行tsne降維 2.用matplotlib進行資料視覺化和資料探索。上次寫到航空公司客戶的rfm價值分析,即抽取航空公司2012年4月1日至2014年3月31日的資料,構建出客戶關係長度l 消費時間間隔r...