ML 分类问题逻辑回归2022-02-21

#分类问题逻辑回归
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler

m=15#Number of training examples
exam1=np.array([34.62366,30.286711,35.847409,60.182599,79.032736,35,55,51,75,52,58,77,67,47,92]).reshape(m,1)
exam2=np.array([78.024693,43.894998,72.902198,86.308552,75.344376,47,35,46,30,62,59,47,67,88,40]).reshape(m,1)
y=np.array([0,0,0,1,1,0,0,0,0,0,1,1,1,1,0]).reshape(m,1)

#寻找y是0和1各自对应的数组编号
num0=[]
num1=[]
for i in range(len(y)):
    if y[i]==0:
        num0.append(i)
    else:
        num1.append(i)


plt.scatter(exam1[num0],exam2[num0],color='red')
plt.scatter(exam1[num1],exam2[num1],color='blue')
plt.show()

#对x1和x2标准化
scaler=StandardScaler()
exam1=scaler.fit_transform(exam1)#必须是列向量
exam2=scaler.fit_transform(exam2)

plt.scatter(exam1[num0],exam2[num0],color='red')
plt.scatter(exam1[num1],exam2[num1],color='blue')
plt.show()

X0=np.ones((m,1))
X   for i in range(iteration):#迭代次数
        theta=theta-alpha*gradient
        gradient=gradientFunction(X,y,theta,m)
    return theta



alpha=0.01
iteration=40000

theta=gradient_descent(X,y,alpha,iteration)

print(theta)
print(costFunction(X,y,theta,m))

plt.scatter(exam1[num0],exam2[num0],color='red')
plt.scatter(exam1[num1],exam2[num1],color='blue')
plt.plot(exam1,(theta[0]-theta[1]*exam1)/theta[2])
plt.show()  =np.hstack((X0,exam1,exam2))

def sigmoid(z):
    return 1/(1+np.exp(-z))

def model(X,theta):
    return sigmoid(np.dot(X,theta))

def costFunction(X,y,theta,m):
    left=np.multiply(-y,np.log(model(X,theta)))
    right=np.multiply(1-y,np.log(1-model(X,theta)))
    return np.sum(left-right)/m

def gradientFunction(X,y,theta,m):
    return (np.dot(np.transpose(X),model(X,theta)-y))/m

def gradient_descent(X,y,alpha,iteration):
    #初始化theta
    theta=np.array([1,1,1]).reshape(3,1)#矩阵形式才可以转置
    gradient=gradientFunction(X,y,theta,m)#梯度初始化
    for i in range(iteration):#迭代次数
        theta=theta-alpha*gradient
        gradient=gradientFunction(X,y,theta,m)
    return theta



alpha=0.01
iteration=40000

theta=gradient_descent(X,y,alpha,iteration)

print(theta)
print(costFunction(X,y,theta,m))

plt.scatter(exam1[num0],exam2[num0],color='red')
plt.scatter(exam1[num1],exam2[num1],color='blue')
plt.plot(exam1,(theta[0]-theta[1]*exam1)/theta[2])
plt.show()  

你可能感兴趣的:(ML 分类问题逻辑回归2022-02-21)