绘制非线性决策边界

时间:2018-11-29 21:09:09

标签: python matlab matplotlib machine-learning logistic-regression

  • 数据可以在这里找到:ex2data2.txt
  • 我不确定应该使用什么调用plt.contour()来重现此内容。
  • 相关的Matlab函数调用为:

    contour(u, v, z, [0, 0], 'LineWidth', 2)
    
  • 我正在尝试绘制非线性逻辑回归的决策边界,如下图所示 enter image description here

    import scikitplot.plotters as skplt
    import matplotlib.pyplot as plt
    import numpy as np 
    import pandas as pd
    from sklearn.linear_model import LogisticRegression
    from sklearn.datasets import make_classification
    from sklearn import metrics
    from ggplot import *
    import time
    
    def mapFeature(X1, X2, df=True):
        """
            X1, X2: dtype = pd.DataFrame, float, int 
                either a single value or a vector of values
            df    : dtype = boolean
                whether it's a single scalar value or a vector of values
            ----------
            Return: dtype = m row vector or m x n vector of feature values
                Calculates each feature and returns its value
        """ 
        # add a column of ones for intercept parameter
        out = pd.DataFrame({'1':np.ones(X1.size)})
        # max 6th degree polynomial
        for i in range(1,7):
            for j in range(i+1):
                # all the combinations of polynomials up to 7th degree 
                value = (X1**(i-j))*(X2**j)
                col_name = 'X1**{} * X2**{}'.format(i-j, j)
                # When we give a vector with only one dimension, we need to specify 
                # whether to add it as a column or a row. 0 denotes adding a row, 
                # and 1 would be a column. 
                if df:
                    out = out.join(pd.DataFrame({col_name: value}))
                else:
                    out = out.join(pd.DataFrame({col_name: value}, index=[0]))
        return out
    if __name__ == '__main__':
    
        data = pd.read_csv('ex2data2.txt', header=None,
                            names=['Test1', 'Test2', 'Pass'])
    
        X = data.iloc[:, :2]
        y = data.iloc[:,2]
        X = mapFeature(X.iloc[:,0], X.iloc[:,1])
        clf = LogisticRegression().fit(X, y)
        theta = clf.coef_
    
        u = np.linspace(start, end, 30)
        v = np.linspace(start, end, 30)
        uu, vv = np.meshgrid(u, v)
        z = np.zeros((30, 30))
        for i in range(30):
            for j in range(30):
                z[i,j] = mapFeature(u[i], v[i], df=False).values.dot(theta.T)
    
        plt.contour(uu, vv, z, [0])
        plt.show()
    

0 个答案:

没有答案