【零基础机器学习】手撕逻辑回归代码

【零基础机器学习】手撕逻辑回归代码🤔


更多代码: gitee主页:https://gitee.com/GZHzzz
博客主页CSDN:https://blog.csdn.net/gzhzzaa

写在前面

  • 本文主要面向零基础入门机器学习,希望大家相互学习,共同进步!😎

show me code,no bb

  • 手撕版本逻辑回归
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
# data_create
def create_date():
    iris = load_iris()
    df = pd.DataFrame(iris.data, columns=iris.feature_names)
    df['label'] = iris.target  
    df['test']='0'
    df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label','test']
    data = np.array(df.iloc[:100, [0,1,-2]])
    return data[:,:2],data[:,-1]
X,y=create_date()
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3)
class logisticRegressionClassifier:
    def __init__(self,max_iter=200,learning_rate=0.01):
        self.max_iter=max_iter
        self.learning_rate=learning_rate

    def sigmoid(self,x):
        return 1/(1+np.exp(-x))

    def fit (self,X,y):
        X = np.hstack((np.ones((X.shape[0],1)),X))
        self.weight= np.zeros((X.shape[1],1))  
        y = np.expand_dims(y,axis =1)
        # training
        for iter_ in range(self.max_iter):
            h = self.sigmoid(np.dot(X,self.weight))
            error = y-h
            self.weight = self.weight+self.learning_rate * np.dot(X.T, error)
    def predict(self,x):
        x = np.hstack((np.ones(x.shape[0],1),x))
        pred = self.sigmoid(np.dot(x, self.weight))
        if pred>0.5:
            return 1
        else:
            return 0
        
    def score(self,X_test,y_test):
        right = 0
        X_test = np.hstack((np.ones((X_test.shape[0],1)),  X_test))
        res = np.dot(X_test , self.weight)
        for (result,y) in zip(res,y_test):
            if (result>0 and y==1) or (result < 0 and y == 0):
                right+=1
        return right / len(X_test)


  • 训练:
instance =logisticRegressionClassifier()
instance.fit(X_train,y_train)
  • 画图
x_points = np.arange(4,8)
y_ = -(instance.weight[1]*x_points + instance.weight[0])/instance.weight[2]
plt.plot(x_points,y_)
plt.scatter(X[:50,0],X[:50,1],label = '0')
plt.scatter(X[50:,0],X[50:,1],label = '1')
plt.legend()

在这里插入图片描述

  • sklearn版本逻辑回归
from sklearn.linear_model import LogisticRegression
quick = LogisticRegression(max_iter=200)
quick.fit(X_train,y_train)
#画图
x_ponits = np.arange(4, 8)
y_ = -(quick.coef_[0][0]*x_ponits + quick.intercept_)/quick.coef_[0][1]
plt.plot(x_ponits, y_)

plt.plot(X[:50, 0], X[:50, 1], 'o', color='blue', label='0')
plt.plot(X[50:, 0], X[50:, 1], 'o', color='orange', label='1')
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()

在这里插入图片描述

  • 所有代码均可直接运行!😎

写在最后

十年磨剑,与君共勉!
更多代码gitee主页:https://gitee.com/GZHzzz
博客主页CSDN:https://blog.csdn.net/gzhzzaa

  • Fighting!😎

在这里插入图片描述

while True:
	Go life

在这里插入图片描述

谢谢点赞交流!(❁´◡`❁)

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北郭zz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值