我真的可以使用提示来帮助我绘制决策边界以分离数据类.我通过Python NumPy创建了一些样本数据(来自高斯分布).在这种情况下,每个数据点是2D坐标,即由2行组成的1列向量.例如,
[ 1
2 ]
Run Code Online (Sandbox Code Playgroud)
假设我有2个类,class1和class2,我通过下面的代码为class1创建了100个数据点,为class2创建了100个数据点(分配给变量x1_samples和x2_samples).
mu_vec1 = np.array([0,0])
cov_mat1 = np.array([[2,0],[0,2]])
x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1, 100)
mu_vec1 = mu_vec1.reshape(1,2).T # to 1-col vector
mu_vec2 = np.array([1,2])
cov_mat2 = np.array([[1,0],[0,1]])
x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)
mu_vec2 = mu_vec2.reshape(1,2).T
Run Code Online (Sandbox Code Playgroud)
当我绘制每个类的数据点时,它看起来像这样:

现在,我想出了一个决策边界的等式来分离两个类,并希望将它添加到图中.但是,我不确定如何绘制此函数:
def decision_boundary(x_vec, mu_vec1, mu_vec2):
g1 = (x_vec-mu_vec1).T.dot((x_vec-mu_vec1))
g2 = 2*( (x_vec-mu_vec2).T.dot((x_vec-mu_vec2)) )
return g1 - g2
Run Code Online (Sandbox Code Playgroud)
我真的很感激任何帮助!
编辑:直觉(如果我的数学正确)我会期望决定边界在我绘制函数时看起来有点像这条红线...

我正在尝试在 scikit learn 中绘制逻辑回归的决策边界
features_train_df : 650 columns, 5250 rows
features_test_df : 650 columns, 1750 rows
class_train_df = 1 column (class to be predicted), 5250 rows
class_test_df = 1 column (class to be predicted), 1750 rows
Run Code Online (Sandbox Code Playgroud)
分类器代码;
tuned_logreg = LogisticRegression(penalty = 'l2', tol = 0.0001,C = 0.1,max_iter = 100,class_weight = "balanced")
tuned_logreg.fit(x_train[sorted_important_features_list[0:650]].values, y_train['loss'].values)
y_pred_3 = tuned_logreg.predict(x_test[sorted_important_features_list[0:650]].values)
Run Code Online (Sandbox Code Playgroud)
我得到了分类器代码的正确输出。
在网上得到这个代码:
code:
X = features_train_df.values
# evenly sampled points
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = …Run Code Online (Sandbox Code Playgroud)