import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import DBSCAN
from sklearn.cluster import KMeans
def draw_pic(data,result):
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
markers=['or','og','ob','ok','oy','om']
for i,d in enumerate(data):
plt.plot(d[0],d[1],markers[result[i]])
plt.title('鸢尾花')
plt.show()
def example():
print ("----------ing-------------")
choose = 1
if choose == 0:
iris_data = load_iris()
data= iris_data.data[:]
else:
data = np.genfromtxt("kmeans.txt",delimiter=" ")
model = DBSCAN(eps=1.5,min_samples=4)
model.fit(data)
result = model.fit_predict(data)
print("result:",result)
print("model_labels:",model.labels_)
draw_pic(data,result)
print ("----------end-------------")
def example1():
print ("----------ing-------------")
x1,y1 =datasets.make_circles(n_samples=2000,factor = 0.5,noise = 0.05)
x2,y2 =datasets.make_blobs(n_samples=1000,centers=[[1.3,1.1]],cluster_std=[[.1]])
x=np.concatenate((x1,x2))
plt.scatter(x[:,0],x[:,1],marker='o')
plt.show()
y_ = KMeans(3).fit_predict(x)
plt.scatter(x[:,0],x[:,1],c=y_)
plt.show()
#y_ = DBSCAN(eps=0.2,min_samples=50).fit_predict(x)
y_ = DBSCAN(eps=0.15).fit_predict(x)
plt.scatter(x[:,0],x[:,1],c=y_)
plt.show()
print ("----------end-------------")
def main():
example()
example1()
main()