Python【逻辑回归】3d可视化

Catalog

基础补充

完整代码

import requests, re, pandas as pd, numpy as np, matplotlib.pyplot as mp
# 从网络下载数据
def download():
    url = 'https://blog.csdn.net/Yellow_python/article/details/81430620'
    header = {'User-Agent': 'Opera/8.0 (Windows NT 5.1; U; en)'}
    r = requests.get(url, headers=header)
    data = re.findall('<pre><code>([\s\S]+?)</code></pre>', r.text)[0].strip()
    df = pd.DataFrame([i.split(',') for i in data.split()], columns=['x1', 'x2', 'x3', 'y'])
    df['x1'] = pd.to_numeric(df['x1'])
    df['x2'] = pd.to_numeric(df['x2'])
    df['x3'] = pd.to_numeric(df['x3'])
    df['y'] = pd.to_numeric(df['y'])  # 正例和负例
    return df
# 数据处理
def handle(df):
    # 增加一列,并转换成矩阵,用以矩阵相乘
    df.insert(0, 'one', 1)
    matrix = df.as_matrix()
    # 纵向切分
    X = matrix[:, 0:-1]  # Ones, x1, x2, x3
    y = matrix[:, -1:]  # y
    return X, y
# sigmoid函数
sigmoid = lambda x: 1 / (1 + np.exp(-x))
# 梯度上升
def gradient_ascent(X, y):
    m = X.shape[1]
    theta = np.mat([[1]] * m)  # 初始化回归系数
    for i in range(9, 99999):
        alpha = 1 / i  # 步长(先大后小)
        h = sigmoid(X * theta)
        theta = theta + alpha * X.transpose() * (y - h)  # 最终梯度上升迭代公式
    return theta
# 数据可视化
def visualize(df, theta):
    from mpl_toolkits import mplot3d
    fig = mp.figure()
    ax = mplot3d.Axes3D(fig)  # 获取三维坐标轴
    # 散点图:正例和负例
    one = df[df['y'] == 1]
    zero = df[df['y'] == 0]
    ax.scatter(one.x1, one.x2, one.x3, s=99, c='g', label='one')
    ax.scatter(zero.x1, zero.x2, zero.x3, s=99, c='r', label='zero')
    # 平面图:决策边界
    x1 = np.arange(0, 11, 2)
    x2 = np.arange(0, 11, 2)
    x1, x2 = np.meshgrid(x1, x2)
    x3 = (theta[0, 0] + theta[1, 0] * x1 + theta[2, 0] * x2) / -theta[3, 0]  # 边界函数
    ax.plot_surface(x1, x2, x3, alpha=0.3, color='blue')
    # 展示图形
    mp.legend()
    mp.show()
# 主函数
def main():
    df = download()
    X, y = handle(df)
    theta = gradient_ascent(X, y)
    print(theta)
    visualize(df, theta)
# 执行
if __name__=='__main__':
    main()

这里写图片描述

数据源

1,1,23.85,1
1,1,-17.85,0
1,2,15.63,1
1,2,-5.63,0
1,3,21.78,1
1,3,-7.78,0
1,4,12.37,1
1,4,5.63,0
1,5,21.89,1
1,5,0.11,0
1,6,24.54,1
1,6,1.46,0
1,7,29.36,1
1,7,0.64,0
1,8,31.30,1
1,8,2.70,0
1,9,44.28,1
1,9,-6.28,0
1,10,36.84,1
1,10,5.16,0
2,1,17.37,1
2,1,-9.37,0
2,2,19.93,1
2,2,-7.93,0
2,3,20.19,1
2,3,-4.19,0
2,4,21.33,1
2,4,-1.33,0
2,5,14.81,1
2,5,9.19,0
2,6,38.82,1
2,6,-10.82,0
2,7,36.12,1
2,7,-4.12,0
2,8,22.07,1
2,8,13.93,0
2,9,36.83,1
2,9,3.17,0
2,10,26.66,1
2,10,17.34,0
3,1,27.00,1
3,1,-17.00,0
3,2,18.42,1
3,2,-4.42,0
3,3,22.52,1
3,3,-4.52,0
3,4,20.54,1
3,4,1.46,0
3,5,30.93,1
3,5,-4.93,0
3,6,26.31,1
3,6,3.69,0
3,7,24.11,1
3,7,9.89,0
3,8,22.32,1
3,8,15.68,0
3,9,40.12,1
3,9,1.88,0
3,10,47.57,1
3,10,-1.57,0
4,1,25.68,1
4,1,-13.68,0
4,2,13.67,1
4,2,2.33,0
4,3,28.97,1
4,3,-8.97,0
4,4,18.34,1
4,4,5.66,0
4,5,23.64,1
4,5,4.36,0
4,6,39.69,1
4,6,-7.69,0
4,7,26.22,1
4,7,9.78,0
4,8,24.09,1
4,8,15.91,0
4,9,41.94,1
4,9,2.06,0
4,10,25.83,1
4,10,22.17,0
5,1,13.26,1
5,1,0.74,0
5,2,11.49,1
5,2,6.51,0
5,3,25.02,1
5,3,-3.02,0
5,4,35.13,1
5,4,-9.13,0
5,5,28.02,1
5,5,1.98,0
5,6,33.58,1
5,6,0.42,0
5,7,35.80,1
5,7,2.20,0
5,8,42.28,1
5,8,-0.28,0
5,9,27.98,1
5,9,18.02,0
5,10,44.91,1
5,10,5.09,0
6,1,27.24,1
6,1,-11.24,0
6,2,12.39,1
6,2,7.61,0
6,3,27.02,1
6,3,-3.02,0
6,4,27.06,1
6,4,0.94,0
6,5,23.00,1
6,5,9.00,0
6,6,25.09,1
6,6,10.91,0
6,7,32.68,1
6,7,7.32,0
6,8,25.08,1
6,8,18.92,0
6,9,39.41,1
6,9,8.59,0
6,10,41.19,1
6,10,10.81,0
7,1,14.46,1
7,1,3.54,0
7,2,23.67,1
7,2,-1.67,0
7,3,25.48,1
7,3,0.52,0
7,4,16.91,1
7,4,13.09,0
7,5,22.76,1
7,5,11.24,0
7,6,28.63,1
7,6,9.37,0
7,7,43.19,1
7,7,-1.19,0
7,8,32.39,1
7,8,13.61,0
7,9,27.12,1
7,9,22.88,0
7,10,51.67,1
7,10,2.33,0
8,1,18.89,1
8,1,1.11,0
8,2,33.40,1
8,2,-9.40,0
8,3,33.44,1
8,3,-5.44,0
8,4,41.17,1
8,4,-9.17,0
8,5,29.02,1
8,5,6.98,0
8,6,34.91,1
8,6,5.09,0
8,7,23.35,1
8,7,20.65,0
8,8,45.50,1
8,8,2.50,0
8,9,48.18,1
8,9,3.82,0
8,10,42.55,1
8,10,13.45,0
9,1,14.79,1
9,1,7.21,0
9,2,32.93,1
9,2,-6.93,0
9,3,32.80,1
9,3,-2.80,0
9,4,27.62,1
9,4,6.38,0
9,5,41.45,1
9,5,-3.45,0
9,6,35.00,1
9,6,7.00,0
9,7,31.62,1
9,7,14.38,0
9,8,42.36,1
9,8,7.64,0
9,9,37.38,1
9,9,16.62,0
9,10,51.93,1
9,10,6.07,0
10,1,28.90,1
10,1,-4.90,0
10,2,38.20,1
10,2,-10.20,0
10,3,23.30,1
10,3,8.70,0
10,4,43.55,1
10,4,-7.55,0
10,5,33.59,1
10,5,6.41,0
10,6,38.74,1
10,6,5.26,0
10,7,43.31,1
10,7,4.69,0
10,8,42.59,1
10,8,9.41,0
10,9,39.31,1
10,9,16.69,0
10,10,38.59,1
10,10,21.41,0

猜你喜欢

转载自blog.csdn.net/Yellow_python/article/details/81430620
今日推荐