我想使用 GMM 对经典的鸢尾花数据集进行聚类。我从以下位置获取数据集:
https://gist.github.com/netj/8836201
到目前为止,我的程序如下:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.mixture import GaussianMixture as mix
from sklearn.cross_validation import StratifiedKFold
def main():
data=pd.read_csv("iris.csv",header=None)
data=data.iloc[1:]
data[4]=data[4].astype("category")
data[4]=data[4].cat.codes
target=np.array(data.pop(4))
X=np.array(data).astype(float)
kf=StratifiedKFold(target,n_folds=10,shuffle=True,random_state=1234)
train_ind,test_ind=next(iter(kf))
X_train=X[train_ind]
y_train=target[train_ind]
gmm_calc(X_train,"full",y_train)
def gmm_calc(X_train,cov,y_train):
print X_train
print y_train
n_classes = len(np.unique(y_train))
model=mix(n_components=n_classes,covariance_type="full")
model.means_ = np.array([X_train[y_train == i].mean(axis=0) for i in
xrange(n_classes)])
model.fit(X_train)
y_predict=model.predict(X_train)
print cov," ",y_train
print cov," ",y_predict
print (np.mean(y_predict==y_train))*100
我遇到的问题是当我尝试获取巧合数 y_predict=y_train 时,因为每次运行程序都会得到不同的结果。例如:
第一次运行:
full [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
full [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 2 2 2 2 2 2 2 2 2
2 2 2 0 2 2 2 2 2 2 2 2 2 2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0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1
1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2]
98.51851851851852
因此,正如您所见,每次运行的结果都不同。我在互联网上找到了一些代码:
https://scikit-learn.org/0.16/auto_examples/mixture/plot_gmm_classifier.html
但他们在完全协方差的情况下,训练集的准确度约为 82%。在这种情况下我做错了什么?
谢谢
更新:我发现在互联网示例中使用 GMM 而不是新的 GaussianMixture。我还发现,在示例中,GMM 参数以监督方式初始化:classifier.means_ = np.array([X_train[y_train == i].mean(axis=0) for i in xrange(n_classes)])
我已将修改后的代码放在上面,但每次运行它时它仍然会更改结果,但是使用库 GMM 不会发生这种情况。