0_(机器学习)逻辑回归介绍

模型简介

逻辑回归(logistic回归)即对数几率回归,它虽然被称作"回归",但却是一种用于二分类的分类方法。逻辑回归是通过分析一个样本被分为各个类的概率比较后得出该样本最有可能属于的类的一种分类方法。

逻辑回归公式推导

训练、测试过程简述

代码实现

python 复制代码
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
import math
import numpy as np
'''定义sigmoid函数'''
def sigmoid(x):
    if x>0:
        return 1.0/(1.0+np.exp(-x))
    else:
        return np.exp(x)/(1.0+np.exp(x))

class LogisticRegression:
    def __init__(self,learning_rate=0.01,num_iterations=1000):
        self.learning_rate=learning_rate
        self.num_iterations=num_iterations
        self.weights=None
        self.bias=None

    def fit(self,X,y,weights,bias=0):
        #num_samples是样本的数量,num_features是样本特征的数量
        num_samples,num_features=X.shape
        self.weights=weights
        self.bias=bias
        for _ in range(self.num_iterations):
            y_pred=[]
            for x in X:
                dot=np.dot(x, self.weights)
                linear_model = dot + self.bias
                y_pred.append(sigmoid(linear_model))
            y_pred=np.array(y_pred)
            dw = (1 / num_samples) * np.dot(X.T, (y_pred - y))
            db = (1 / num_samples) * np.sum(y_pred - y)
            self.weights -= self.learning_rate * dw
            self.bias -= self.learning_rate * db

    def predict_prob(self, X):
        y_pred = []
        for x in X:
            dot = np.dot(x, self.weights)
            linear_model = dot + self.bias
            y_pred.append(sigmoid(linear_model))
        y_pred = np.array(y_pred)
        return y_pred

    def predict(self, X, threshold=0.5):
        y_pred_prob = self.predict_prob(X)
        y_pred = np.zeros_like(y_pred_prob)
        y_pred[y_pred_prob >= threshold] = 1
        return y_pred
    def calculate_accuracy(self,y_pred,y_test):
        sum=0
        for p,t in zip(y_pred,y_test):
            if p==t:
                sum+=1
        return float(sum)/float(len(y_pred))

if __name__ == "__main__":
    l=LogisticRegression()
    breast_cancer = load_breast_cancer()
    X = breast_cancer.data
    y = breast_cancer.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
    num_samples, num_features = X.shape
    #w = np.ones(num_features)
    w = np.zeros(num_features)
    l.fit(X_train,y_train,w)

    y_pred=l.predict(X_test)
    print(y_pred)
    print(y_test)
    print("准确率:",l.calculate_accuracy(y_pred,y_test))
相关推荐
可编程芯片开发4 分钟前
基于PSO粒子群优化PI控制器的无刷直流电机最优控制系统simulink建模与仿真
人工智能·算法·simulink·pso·pi控制器·pso-pi
cpp_25015 分钟前
P8448 [LSOT-1] 暴龙的土豆
数据结构·c++·算法·题解·洛谷
lcj25115 分钟前
深入理解指针(4):qsort 函数 & 通过冒泡排序实现
c语言·数据结构·算法
fie88897 分钟前
基于MATLAB的转子动力学建模与仿真实现(含碰摩、不平衡激励)
开发语言·算法·matlab
Tadas-Gao12 分钟前
深度学习与机器学习的知识路径:从必要基石到独立范式
人工智能·深度学习·机器学习·架构·大模型·llm
唐梓航-求职中14 分钟前
编程大师-技术-算法-leetcode-1472. 设计浏览器历史记录
算法·leetcode
_OP_CHEN17 分钟前
【算法基础篇】(五十八)线性代数之高斯消元法从原理到实战:手撕模板 + 洛谷真题全解
线性代数·算法·蓝桥杯·c/c++·线性方程组·acm/icpc·高斯消元法
wukangjupingbb25 分钟前
Gemini 3和GPT-5.1在多模态处理上的对比
人工智能·gpt·机器学习
明月照山海-25 分钟前
机器学习周报三十四
人工智能·机器学习
唐梓航-求职中25 分钟前
编程大师-技术-算法-leetcode-355. 设计推特
算法·leetcode·面试