逻辑回归分类Sklearn的make_moon数据集

直接上代码

from sklearn.datasets import make_moons
from numpy import *
import numpy as np
import operator
from os import listdir
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
#创建数据集
x, y = make_moons(n_samples=200, noise=0.15, random_state=0)
# Max-Min标准化
# 建立MinMaxScaler对象
# minmax = preprocessing.MinMaxScaler()
# # 标准化处理
# data_minmax = minmax.fit_transform(x)
# MaxAbs标准化
# # 建立MinMaxScaler对象
minmax= preprocessing.MaxAbsScaler()
# 标准化处理
data_minmax  = minmax.fit_transform(x)

m, n = shape(data_minmax)
w = ones((1,1))

data_minmax = np.insert(data_minmax, 0, values=w, axis=1)

# print(data_maxabs)
x_train, x_test, y_train, y_test = train_test_split(data_minmax, y, test_size=0.25)


def sigmoid(inX):
    return 1.0/(1+exp(-inX))
def stocGradAscent1(dataMatrix, classLabels, numIter=150):
    m,n = shape(dataMatrix)
    weights = ones(n)   #initialize to all ones
    for j in range(numIter):
        dataIndex = range(m)
        for i in range(m):
            alpha = 4/(1.0+j+i)+0.0001    #apha decreases with iteration, does not
            randIndex = int(random.uniform(0,len(dataIndex)))#go to 0 because of the constant
            h = sigmoid(sum(dataMatrix[randIndex]*weights))
            error = classLabels[randIndex] - h
            weights = weights + alpha * error * dataMatrix[randIndex]
            del(list(dataIndex)[randIndex])
    return weights
def gradAscent(dataMatIn, classLabels):
    dataMatrix = mat(dataMatIn)             #convert to NumPy matrix
    labelMat = mat(classLabels).transpose() #convert to NumPy matrix
    m,n = shape(dataMatrix)
    alpha = 0.001
    maxCycles = 500
    weights = ones((n,1))
    for k in range(maxCycles):              #heavy on matrix operations
        h = sigmoid(dataMatrix*weights)     #matrix mult
        error = (labelMat - h)              #vector subtraction
        weights = weights + alpha * dataMatrix.transpose()* error #matrix mult
    return weights
def plotBestFit(weights):
    import matplotlib.pyplot as plt
    # dataMat,labelMat=loadDataSet()
    dataArr = array(x_train)
    n = shape(dataArr)[0]
    xcord1 = []; ycord1 = []
    xcord2 = []; ycord2 = []
    for i in range(n):
        if int(y_train[i])== 1:
            xcord1.append(dataArr[i,1]); ycord1.append(dataArr[i,2])
        else:
            xcord2.append(dataArr[i,1]); ycord2.append(dataArr[i,2])
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
    ax.scatter(xcord2, ycord2, s=30, c='green')
    x = arange(-3.0, 3.0, 0.1)
    y = (-weights[0] - weights[1] * x) / weights[2]
    # -weights[0]
    ax.plot(x, y)
    plt.xlabel('X1'); plt.ylabel('X2');
    plt.show()

c = stocGradAscent1(array(x_train), y_train)
plotBestFit(c)

运行结果
逻辑回归分类Sklearn的make_moon数据集_第1张图片

你可能感兴趣的:(逻辑回归分类Sklearn的make_moon数据集)