def draw(test, predic, girlMat, boyMat, girl_train, boy_train, i, j): actual = transdata(test) predictions = predic #得到ROC曲线和曲线下AUC面积 false_positive_rate, true_positive_rate, thresholds = roc_curve( actual, predictions) roc_auc = auc(false_positive_rate, true_positive_rate) #画出ROC曲线 plt.subplot(221) plt.title('Receiver Operating Characteristic') plt.plot(false_positive_rate, true_positive_rate, lw=1, label='AUC = %0.2f' % roc_auc) plt.legend(loc='lower right') plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck') plt.xlim([-0.05, 1.05]) plt.ylim([-0.05, 1.05]) plt.ylabel('True Positive Rate') plt.subplot(222) w, m1, m2 = calFisher(i, j, girl_train, boy_train) w0 = gender_prod * m1 + (1 - gender_prod) * m2 x = girlMat.iloc[:, i].append(boyMat.iloc[:, i]) y = girlMat.iloc[:, j].append(boyMat.iloc[:, j]) x_min, x_max = x.min() - 1.5, x.max() + 1.5 y_min, y_max = y.min() - 1.5, y.max() + 1.5 h = 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) z = np.zeros(xx.shape, dtype='int') for k in range(xx.shape[0]): for g in range(xx.shape[1]): data = [xx[k, g], yy[k, g]] y = (w * np.mat(data).T) - w0 if y > 0: z[k, g] = 1 Colors = [] #不同性别用不同颜色区分(男:蓝,女:橘) for t in range(test.shape[0]): m = test.iloc[t, -2] if m == 'm': Colors.append('blue') if m == 'w': Colors.append('orange') cmap_light = ListedColormap(['#AAAAFF', '#F0E68C']) plt.pcolormesh(xx, yy, z, cmap=cmap_light) x1 = test.iloc[:, i].tolist() y1 = test.iloc[:, j].tolist() plt.scatter(x1, y1, c=Colors) plt.xlabel('height') plt.ylabel('weight') plt.title('boundary line of Fisher') plt.subplot(223) z2 = np.zeros(xx.shape, dtype='int') girl_mean = np.array(bs.calMean(girl_train, i, j)) print(girl_mean) mean_g = [girl_mean[0, 0], girl_mean[0, 1]] boy_mean = np.array(bs.calMean(boy_train, i, j)) mean_b = [boy_mean[0, 0], boy_mean[0, 1]] mean_bg = [mean_g, mean_b] girl_cov = np.mat(bs.calCov(i, j, girl_train, mean_g)) boy_cov = np.mat(bs.calCov(i, j, girl_train, mean_b)) cov_bg = [girl_cov, boy_cov] print(mean_bg) print(cov_bg) for k in range(xx.shape[0]): for g in range(xx.shape[1]): data = pd.DataFrame([xx[k, g], yy[k, g]]).iloc[:, 0] print(data) g_r = bs.calGauss(data, mean_bg[0], cov_bg[0]) b_r = bs.calGauss(data, mean_bg[1], cov_bg[1]) if g_r >= b_r: z2[k, g] = 1 plt.contourf(xx, yy, z2, cmap=cmap_light) plt.scatter(x1, y1, c=Colors) plt.xlabel('height') plt.ylabel('weight') plt.title('boundary line of Bayes') plt.subplot(224) plt.ylim(35, 90) # plt.contour(xx, yy, z) x3 = np.arange(x_min + 8, x_max - 8, h) y4 = (w0 - w[0, 0] * x3) / w[0, 1] y5 = w[0, 1] / w[0, 0] * x3 plt.plot(x3, y4, color='green') plt.plot(x3, y5, color='green') plt.contour(xx, yy, z2, color='y') plt.scatter(x1, y1, c=Colors) plt.xlabel('height') plt.ylabel('weight') plt.title('boundary line of two classfiers') plt.show()