Ⅰ.原始数据
i.i.训练集
i.ii.测试集
Ⅱ.python程序
import pandas as pd
import numpy as np
from sklearn.mixture import GaussianMixture
data=pd.read_excel('数据.xlsx',sheet_name='Sheet3')
x_train=np.array(data.iloc[:,1:]).T
gm=GaussianMixture(n_components=3,covariance_type='diag',max_iter=200,random_state=0).fit(x_train)
category=data.columns[1:]
train=gm.predict(x_train)
train_p=gm.predict_proba(x_train)
print('将训练集指标划分为3类:')
for i in range(len(x_train)):
print(category[i],train[i],train_p[i])
data2=pd.read_excel('数据.xlsx',sheet_name='Sheet4')
x_test=np.array(data2.iloc[:,1:]).T
category2=data2.columns[1:]
test=gm.predict(x_test)
test_p=gm.predict_proba(x_test)
print('\n按照训练集的聚类规则,测试集的预测结果为:')
for i in range(len(category2)):
print(category2[i],test[i],test_p[i])
Ⅲ.结果
将训练集指标划分为3类:
芹菜 2 [3.76856808e-44 0.00000000e+00 1.00000000e+00]
油菜 0 [1.00000000e+000 0.00000000e+000 6.81185442e-130]
黄瓜 0 [1.00000000e+00 0.00000000e+00 2.38771846e-28]
萝卜 0 [1.00000000e+000 0.00000000e+000 2.63343633e-256]
茄子 2 [5.3255621e-52 0.0000000e+00 1.0000000e+00]
西红柿 2 [1.05575102e-36 0.00000000e+00 1.00000000e+00]
土豆 0 [1.0000000e+00 0.0000000e+00 2.9303686e-42]
胡萝卜 0 [1.00000000e+00 0.00000000e+00 2.03496053e-62]
青椒 2 [2.09865547e-27 0.00000000e+00 1.00000000e+00]
尖椒 1 [4.30383770e-243 1.00000000e+000 5.26051736e-284]
豆角 1 [6.47028700e-219 1.00000000e+000 4.39092872e-209]
韭菜 2 [6.94093288e-32 0.00000000e+00 1.00000000e+00]
豆腐 0 [1.0000000e+00 0.0000000e+00 2.2037534e-26]
按照训练集的聚类规则,测试集的预测结果为:
鸡蛋 2 [2.06427711e-23 0.00000000e+00 1.00000000e+00]
菜籽油 0 [1.00000000e+000 0.00000000e+000 4.08827163e-226]
大豆油 0 [1.00000000e+000 0.00000000e+000 2.66272761e-124]
花生油 0 [1. 0. 0.]
芦柑 2 [6.68896463e-23 0.00000000e+00 1.00000000e+00]
苹果 0 [1.00000000e+00 0.00000000e+00 4.41565598e-82]
香蕉 2 [2.04319734e-15 0.00000000e+00 1.00000000e+00]
西瓜 0 [1.00000000e+00 0.00000000e+00 8.45239157e-35]
食用盐 0 [1.00000000e+000 0.00000000e+000 1.62179641e-166]
绵白糖 0 [1. 0. 0.]
白砂糖 0 [1.00000000e+000 0.00000000e+000 2.22931208e-227]
红糖 0 [1. 0. 0.]
酱油 0 [1.00000000e+00 0.00000000e+00 5.62525683e-98]
醋 0 [9.99999982e-01 0.00000000e+00 1.80833198e-08]
鲜牛奶 2 [7.82041121e-14 0.00000000e+00 1.00000000e+00]