forked from leeamen/gaussian_mixture_model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xigua.py
54 lines (43 loc) · 1.17 KB
/
xigua.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
#coding:utf8
import numpy as np
import os
import sys
from mltoolkits import *
import logging
import mygmm
logger = logging.getLogger(sys.argv[0])
logger.setLevel(logging.DEBUG)
if __name__ == '__main__':
logger.debug('start...')
params = {}
params['cluster_num'] = 3
#2元高斯混合模型
params['attr_num'] = 2
#迭代次数
params['iters'] = 200
#加载数据
train_file = './data/xiguashuju.txt'
train_data = np.genfromtxt(train_file, delimiter = ',', dtype = np.float)
logger.debug('训练样本:%s', train_data)
#训练样本x
train_x = train_data
#创建模型
gmm = mygmm.MyGMM(params)
#自己加载初始mu点
# mu0 = [0.403, 0.237]
# mu1 = [0.714, 0.346]
# mu2 = [0.532, 0.472]
#gmm.LoadMu({0:mu0, 1:mu1, 2:mu2})
#开始训练模型
gmm.Train(train_x)
#预测样本聚类
pred_y = gmm.Predict(train_x)
logger.debug('pred:\n%s', pred_y)
#显示
import matplotlib.pyplot as plt
markers = ['o', '*', '.', '+']
colors = ['red', 'yellow', 'blue', 'black']
for j in range(0, len(train_x)):
plt.plot(train_x[j][0], train_x[j][1], color = colors[pred_y[j]], marker = markers[pred_y[j]])
plt.show()