def test_qda(): # QDA classification. # This checks that QDA implements fit and predict and returns # correct values for a simple toy dataset. clf = QuadraticDiscriminantAnalysis() y_pred = clf.fit(X6, y6).predict(X6) assert_array_equal(y_pred, y6) # Assure that it works with 1D data y_pred1 = clf.fit(X7, y6).predict(X7) assert_array_equal(y_pred1, y6) # Test probas estimates y_proba_pred1 = clf.predict_proba(X7) assert_array_equal((y_proba_pred1[:, 1] > 0.5) + 1, y6) y_log_proba_pred1 = clf.predict_log_proba(X7) assert_array_almost_equal(np.exp(y_log_proba_pred1), y_proba_pred1, 8) y_pred3 = clf.fit(X6, y7).predict(X6) # QDA shouldn't be able to separate those assert np.any(y_pred3 != y7) # Classes should have at least 2 elements with pytest.raises(ValueError): clf.fit(X6, y4)
class QDA(object): def __init__(self, priors=None, reg_param=0., store_covariance=False, tol=1.0e-4): """ :param priors: 分来优先级, array, 可选项, shape=[n_classes] :param reg_param: float, 可选项,将协方差估计正规化 :param store_covariance: boolean 如果为真,则计算并存储协方差矩阵到self.covariance_中 :param tol: 使用排序评估的阈值 """ self.model = QuadraticDiscriminantAnalysis( priors=priors, reg_param=reg_param, store_covariance=store_covariance, tol=tol) def fit(self, x, y): self.model.fit(X=x, y=y) def get_params(self, deep=True): return self.model.get_params(deep=deep) def predict(self, x): return self.model.predict(X=x) def predict_log_dict(self, x): return self.model.predict_log_proba(X=x) def predict_proba(self, x): return self.model.predict_proba(X=x) def score(self, x, y, sample_weight=None): return self.model.score(X=x, y=y, sample_weight=sample_weight) def set_params(self, **params): self.model.set_params(**params) def decision_function(self, x): # 将决策函数应用于样本数组。 return self.model.decision_function(X=x) def get_attribute(self): covariance = self.model.covariance_ # 每个种类的协方差矩阵, list of array-like of shape (n_features, n_features) means = self.model.means # 种类均值, array-like of shape (n_classes, n_features) priors = self.model.priors_ # 种类占比, 求和为1, array-like of shape (n_classes) rotations = self.model.rotations_ # n_k = min(n_features, number of elements in class k) list_array, # 高斯分布的旋转 scalings = self.model.scalings_ # list_array, 每个种类k,shape[n_k]的数组,包含高斯分布的缩放, # 如,旋转坐标系中的方差 classes = self.model.classes_ # array-like, shape(n_classes,), 不同种类标签 return covariance, means, priors, rotations, scalings, classes
def test_qda(): # QDA classification. # This checks that QDA implements fit and predict and returns # correct values for a simple toy dataset. clf = QuadraticDiscriminantAnalysis() y_pred = clf.fit(X6, y6).predict(X6) assert_array_equal(y_pred, y6) # Assure that it works with 1D data y_pred1 = clf.fit(X7, y6).predict(X7) assert_array_equal(y_pred1, y6) # Test probas estimates y_proba_pred1 = clf.predict_proba(X7) assert_array_equal((y_proba_pred1[:, 1] > 0.5) + 1, y6) y_log_proba_pred1 = clf.predict_log_proba(X7) assert_array_almost_equal(np.exp(y_log_proba_pred1), y_proba_pred1, 8) y_pred3 = clf.fit(X6, y7).predict(X6) # QDA shouldn't be able to separate those assert np.any(y_pred3 != y7) # Classes should have at least 2 elements assert_raises(ValueError, clf.fit, X6, y4)
# Put the result into a color plot Z = Z.reshape(xx.shape) plt.contour(xx, yy, Z, cmap=plt.cm.Blues) plt.xlabel('$x_1$') plt.ylabel('$x_2$') plt.grid() # In[51]: x, y = np.random.multivariate_normal(Mean, Cov, N).T x2, y2 = np.random.multivariate_normal(Mean2, Cov2, N).T X = np.r_[np.c_[x, y], np.c_[x2, y2]] Y = np.r_[np.ones((N, 1)), np.zeros((N, 1))] Y = Y.flatten() y_pred = clf.predict(X) y_pred2 = clf.predict_log_proba(X) score = y_pred2[:, 0] - y_pred2[:, 1] #TruePositive indi_TP = np.logical_and((y_pred == Y), (Y == 1)) #TrueNegative indi_TN = np.logical_and((y_pred == Y), (Y == 0)) #FalsePositive indi_FP = np.logical_and((y_pred != Y), (Y == 1)) #FalseNegative indi_FN = np.logical_and((y_pred != Y), (Y == 0)) TP = score[indi_TP] TN = score[indi_TN] FP = score[indi_FP] FN = score[indi_FN] _ = plt.hist(TP, bins=30, rwidth=0.9, label='TP', alpha=0.5)