新闻详情

新闻详情

首页 / 资讯中心 / 详情

理解机器学习的各种算法

发布时间:2026/11/28 8:18:46来源:尧图网络
理解机器学习的各种算法
所有算法回归和分类作为两种类型的算法都出现回归线性回归和逻辑回归的算法如何理解。线性回归和逻辑回归的区别线性回归一个参数一个值参数不断的变化值不断的变化。逻辑回归是基于先线性回归的基础上加一个sigmod函数的判断得出最终的一个分类结果0/1. 这就是线性回归和逻辑回归的区别。线性回归例子import numpy as np from sklearn.linear_model import LinearRegression # 准备数据 X np.array([[50], [60], [70], [80], [90], [100]]) # 面积 y np.array([150, 180, 210, 240, 270, 300]) # 房价 # 创建并训练模型 model LinearRegression() model.fit(X, y) # 查看学到的参数 print(权重 w , model.coef_[0]) # 3.0 print(偏置 b , model.intercept_) # 0.0 # 预测 75 平米 pred model.predict([[75]]) print(75 平米预测房价 , pred[0], 万元) # 225.0逻辑回归例子import numpy as np from sklearn.linear_model import LogisticRegression # 准备数据 X np.array([[1], [2], [3], [4], [5], [6], [7], [8]]) # 学习时长 y np.array([0, 0, 0, 1, 1, 1, 1, 1]) # 是否通过 # 创建并训练模型 model LogisticRegression() model.fit(X, y) # 查看学到的参数 print(权重 w , model.coef_[0][0]) print(偏置 b , model.intercept_[0]) # 预测 3.5 小时 prob model.predict_proba([[3.5]])[0][1] pred model.predict([[3.5]])[0] print(f3.5 小时通过概率 {prob:.3f}) print(f预测类别 {pred})如何理解参数中的 [**]prob和pred分别代表什么意思为什么prob有两个值岭回归import numpy as np from sklearn.linear_model import Ridge, LinearRegression # 准备数据 X np.array([[50, 1], [60, 2], [70, 3], [80, 4], [90, 5], [100, 6]]) # 面积, 房龄 y np.array([150, 180, 210, 240, 270, 300]) # 房价 # 普通线性回归 ols LinearRegression() ols.fit(X, y) print(普通线性回归:) print( 权重 , ols.coef_) print( 偏置 , ols.intercept_) # 岭回归 ridge Ridge(alpha1.0) # alpha 就是 λ ridge.fit(X, y) print(\n岭回归 (alpha1.0):) print( 权重 , ridge.coef_) print( 偏置 , ridge.intercept_) X_new np.array([[75, 3]]) print(普通线性回归预测:, ols.predict(X_new)[0]) print(岭回归预测:, ridge.predict(X_new)[0])Lasso回归import numpy as np from sklearn.linear_model import LinearRegression, Ridge, Lasso # 数据 X np.array([[50, 2, 1, 3, 10], [60, 2, 2, 5, 9], [70, 3, 3, 8, 8], [80, 3, 4, 10, 7], [90, 4, 5, 12, 6], [100, 4, 6, 15, 5], [110, 5, 7, 18, 4], [120, 5, 8, 20, 3], [130, 6, 9, 22, 2], [140, 6, 10, 25, 1]]) y np.array([150, 180, 210, 240, 270, 300, 330, 360, 390, 420]) # 普通线性回归 ols LinearRegression() ols.fit(X, y) print(普通线性回归权重:, np.round(ols.coef_, 2)) # 岭回归 ridge Ridge(alpha1.0) ridge.fit(X, y) print(岭回归权重: , np.round(ridge.coef_, 2)) # Lasso 回归 lasso Lasso(alpha0.1) lasso.fit(X, y) print(Lasso 权重: , np.round(lasso.coef_, 2))删除冗余特征的例子import numpy as np import pandas as pd from sklearn.linear_model import Lasso, LassoCV, LinearRegression from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_error np.random.seed(42) # 1. 生成数据 n 200 X pd.DataFrame({ 面积: np.random.rand(n) * 100 50, 房间数: np.random.randint(1, 6, n), 房龄: np.random.randint(0, 30, n), 楼层: np.random.randint(1, 30, n), 距离地铁: np.random.rand(n) * 10, 朝向: np.random.randint(0, 4, n), 装修年限: np.random.randint(0, 10, n), 绿化率: np.random.rand(n), 物业费: np.random.rand(n) * 5, 编号: np.arange(n), }) y (3.0 * X[面积] 5.0 * X[房间数] - 1.0 * X[房龄] 0.5 * X[楼层] - 2.0 * X[距离地铁] np.random.randn(n) * 5) print( * 50) print(原始特征数:, X.shape[1]) # 2. 手动粗筛10 - 8 X_8 X.drop(columns[编号, 朝向]) print(粗筛后特征数:, X_8.shape[1]) # 3. Lasso 细筛 X_train, X_test, y_train, y_test train_test_split( X_8, y, test_size0.2, random_state42 ) lasso_cv LassoCV(alphasnp.logspace(-3, 2, 50), cv5, random_state42) lasso_cv.fit(X_train, y_train) lasso Lasso(alphalasso_cv.alpha_).fit(X_train, y_train) print( * 50) print(Lasso 最优 alpha:, lasso_cv.alpha_) print(pd.DataFrame({特征: X_8.columns, 权重: lasso.coef_})) # 4. 手动删除权重为 0 的特征 selected_idx np.where(lasso.coef_ ! 0)[0] selected_features X_8.columns[selected_idx] X_selected X_8[selected_features] print( * 50) print(Lasso 保留特征:, list(selected_features)) print(最终特征数:, X_selected.shape[1]) # 5. 重新训练最终模型 X_train_sel, X_test_sel, y_train_sel, y_test_sel train_test_split( X_selected, y, test_size0.2, random_state42 ) final_model LinearRegression().fit(X_train_sel, y_train_sel) print( * 50) print(最终模型权重:) print(pd.DataFrame({特征: selected_features, 权重: final_model.coef_})) print(偏置:, final_model.intercept_) # 6. 评估 mse_final mean_squared_error(y_test_sel, final_model.predict(X_test_sel)) print( * 50) print(f最终模型测试集 MSE: {mse_final:.4f})
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

【外设】之大彩串口显示屏 2026/11/28 5:05:09

【外设】之大彩串口显示屏

大彩串口屏初步使用 1 .官网下载 STM32 屏幕 GUI 设计资料 http://www.gz-dc.com/category/typeid/4112 找到 STM32 Keil 工程,移植相关代码因项目而异进行移植,由于项目简单,本人只对用到的指令接口进行修改。 比如:注意事项&…

阅读更多 →
无法下载Windows系统iso文件 2026/11/25 23:02:13

无法下载Windows系统iso文件

当我遇到这个问题的时候,我打开了一个网站: 登录 然后我打算下载的时候: 突然那个官方的连接就可以下载了:

阅读更多 →
【清华代码熊】DeepSeek V4.1 Flash 后训练详解 2026/11/27 23:07:11

【清华代码熊】DeepSeek V4.1 Flash 后训练详解

📌 上期解析了 DeepSeek V4.1 Flash 模型架构改进,本期解析 DeepSeek V4.1 Flash 预训练/后训练技术: 🌟 预训练:45T 文本 多模态混合语料、直接训练 sparse attention(取消 DeepSeek V4 的 dense 冷启动&…

阅读更多 →
Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ... 2026/11/27 6:58:21

Shuffle-R1: Efficient RL framework for Multimodal Large Language Models via Data-centric Dynamic ...

文章主要内容和创新点 主要内容 本文聚焦于多模态大语言模型(MLLM)强化学习(RL)训练中的效率问题,提出了一个名为Shuffle-R1的框架。研究发现,当前RL训练存在两个关键缺陷: 优势值坍缩(Advantage Collapsing):批次中大多数优势值集中在零附近,导致有效梯度信号被淹…

阅读更多 →
PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction 2026/11/28 2:48:02

PRvL: Quantifying the Capabilities and Risks of Large Language Models for PII Redaction

一、文章主要内容总结 本文聚焦于利用大型语言模型(LLMs)实现个人身份信息(PII)脱敏的研究,旨在解决传统脱敏方法(如基于规则的系统、领域特定命名实体识别(NER)模型)泛化能力差、跨格式/跨语境适应性弱的问题。 研究通过全面评估多种LLM架构(包括密集型LLM(D-LLM…

阅读更多 →
LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model 2026/11/27 19:57:42

LLaVA-RE: Binary Image-Text Relevancy Evaluation with Multimodal Large Language Model

文章主要内容和创新点 主要内容 本文聚焦于二进制图像-文本相关性评估任务(判断图像与文本“相关”或“不相关”),针对该任务中文本格式多样、相关性定义随场景变化等挑战,提出了基于多模态大语言模型(MLLM)的解决方案LLaVA-RE。 模型设计:LLaVA-RE基于LLaVA 1.5架构,…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉