mle-interview
  • 面试指南
  • 数据结构与算法
    • 列表
      • 912. Sort an Array
      • 215. Kth Largest Element
      • 977. Squares of a Sorted Array
      • 605. Can Place Flowers
      • 59. Spiral Matrix II
      • 179. Largest Number
      • 31. Next Permutation
    • 二分查找
      • 704. Binary Search
      • 69. Sqrt(x)
      • 278. First Bad Version
      • 34. Find First and Last Position of Element in Sorted Array
      • 33. Search in Rotated Sorted Array
      • 81. Search in Rotated Sorted Array II
      • 162. Find Peak Element
      • 4. Median of Two Sorted Arrays
      • 1095. Find in Mountain Array
      • 240. Search a 2D Matrix II
      • 540. Single Element in a Sorted Array
      • 528. Random Pick with Weight
      • 1300. Sum of Mutated Array Closest to Target
      • 410. Split Array Largest Sum
      • 1044. Longest Duplicate Substring
      • *644. Maximum Average Subarray II
      • *1060. Missing Element in Sorted Array
      • *1062. Longest Repeating Substring
      • *1891. Cutting Ribbons
    • 双指针
      • 26. Remove Duplicate Numbers in Array
      • 283. Move Zeroes
      • 75. Sort Colors
      • 88. Merge Sorted Arrays
      • 167. Two Sum II - Input array is sorted
      • 11. Container With Most Water
      • 42. Trapping Rain Water
      • 15. 3Sum
      • 16. 3Sum Closest
      • 18. 4Sum
      • 454. 4Sum II
      • 409. Longest Palindrome
      • 125. Valid Palindrome
      • 647. Palindromic Substrings
      • 209. Minimum Size Subarray Sum
      • 5. Longest Palindromic Substring
      • 395. Longest Substring with At Least K Repeating Characters
      • 424. Longest Repeating Character Replacement
      • 76. Minimum Window Substring
      • 3. Longest Substring Without Repeating Characters
      • 1004. Max Consecutive Ones III
      • 1658. Minimum Operations to Reduce X to Zero
      • *277. Find the Celebrity
      • *340. Longest Substring with At Most K Distinct Characters
    • 链表
      • 203. Remove Linked List Elements
      • 19. Remove Nth Node From End of List
      • 876. Middle of the Linked List
      • 206. Reverse Linked List
      • 92. Reverse Linked List II
      • 24. Swap Nodes in Pairs
      • 707. Design Linked List
      • 148. Sort List
      • 160. Intersection of Two Linked Lists
      • 141. Linked List Cycle
      • 142. Linked List Cycle II
      • 328. Odd Even Linked List
    • 哈希表
      • 706. Design HashMap
      • 1. Two Sum
      • 146. LRU Cache
      • 128. Longest Consecutive Sequence
      • 73. Set Matrix Zeroes
      • 380. Insert Delete GetRandom O(1)
      • 49. Group Anagrams
      • 350. Intersection of Two Arrays II
      • 299. Bulls and Cows
      • *348. Design Tic-Tac-Toe
    • 字符串
      • 242. Valid Anagram
      • 151. Reverse Words in a String
      • 205. Isomorphic Strings
      • 647. Palindromic Substrings
      • 696. Count Binary Substrings
      • 28. Find the Index of the First Occurrence in a String
      • *186. Reverse Words in a String II
    • 栈与队列
      • 225. Implement Stack using Queues
      • 54. Spiral Matrix
      • 155. Min Stack
      • 232. Implement Queue using Stacks
      • 150. Evaluate Reverse Polish Notation
      • 224. Basic Calculator
      • 20. Valid Parentheses
      • 1472. Design Browser History
      • 1209. Remove All Adjacent Duplicates in String II
      • 1249. Minimum Remove to Make Valid Parentheses
      • *281. Zigzag Iterator
      • *1429. First Unique Number
      • *346. Moving Average from Data Stream
    • 优先队列/堆
      • 692. Top K Frequent Words
      • 347. Top K Frequent Elements
      • 973. K Closest Points
      • 23. Merge K Sorted Lists
      • 264. Ugly Number II
      • 378. Kth Smallest Element in a Sorted Matrix
      • 295. Find Median from Data Stream
      • 767. Reorganize String
      • 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
      • 895. Maximum Frequency Stack
      • 1705. Maximum Number of Eaten Apples
      • *1086. High Five
    • 深度优先DFS
      • 二叉树
      • 543. Diameter of Binary Tree
      • 101. Symmetric Tree
      • 124. Binary Tree Maximum Path Sum
      • 226. Invert Binary Tree
      • 104. Maximum Depth of Binary Tree
      • 951. Flip Equivalent Binary Trees
      • 236. Lowest Common Ancestor of a Binary Tree
      • 987. Vertical Order Traversal of a Binary Tree
      • 572. Subtree of Another Tree
      • 863. All Nodes Distance K in Binary Tree
      • 1110. Delete Nodes And Return Forest
      • 230. Kth Smallest element in a BST
      • 98. Validate Binary Search Tree
      • 235. Lowest Common Ancestor of a Binary Search Tree
      • 669. Trim a Binary Search Tree
      • 700. Search in a Binary Search Tree
      • 108. Convert Sorted Array to Binary Search Tree
      • 450. Delete Node in a BST
      • 938. Range Sum of BST
      • *270. Closest Binary Search Tree Value
      • *333. Largest BST Subtree
      • *285. Inorder Successor in BST
      • *1485. Clone Binary Tree With Random Pointer
      • 回溯
      • 39. Combination Sum
      • 78. Subsets
      • 46. Permutation
      • 77. Combinations
      • 17. Letter Combinations of a Phone Number
      • 51. N-Queens
      • 93. Restore IP Addresses
      • 22. Generate Parentheses
      • 856. Score of Parentheses
      • 301. Remove Invalid Parentheses
      • 37. Sodoku Solver
      • 图DFS
      • 126. Word Ladder II
      • 212. Word Search II
      • 79. Word Search
      • 399. Evaluate Division
      • 1376. Time Needed to Inform All Employees
      • 131. Palindrome Partitioning
      • 491. Non-decreasing Subsequences
      • 698. Partition to K Equal Sum Subsets
      • 526. Beautiful Arrangement
      • 139. Word Break
      • 377. Combination Sum IV
      • 472. Concatenated Words
      • 403. Frog Jump
      • 329. Longest Increasing Path in a Matrix
      • 797. All Paths From Source to Target
      • 695. Max Area of Island
      • 341. Flatten Nested List Iterator
      • 394. Decode String
      • *291. Word Pattern II
      • *694. Number of Distinct Islands
      • *1274. Number of Ships in a Rectangle
      • *1087. Brace Expansion
    • 广度优先BFS
      • 102. Binary Tree Level Order Traversal
      • 103. Binary Tree Zigzag Level Order Traversal
      • 297. Serialize and Deserialize Binary Tree
      • 310. Minimum Height Trees
      • 127. Word Ladder
      • 934. Shortest Bridge
      • 200. Number of Islands
      • 133. Clone Graph
      • 130. Surrounded Regions
      • 752. Open the Lock
      • 815. Bus Routes
      • 1091. Shortest Path in Binary Matrix
      • 542. 01 Matrix
      • 1293. Shortest Path in a Grid with Obstacles Elimination
      • 417. Pacific Atlantic Water Flow
      • 207. Course Schedule
      • 210. Course Schedule II
      • 787. Cheapest Flights Within K Stops
      • 444. Sequence Reconstruction
      • 994. Rotting Oranges
      • 785. Is Graph Bipartite?
      • *366. Find Leaves of Binary Tree
      • *314. Binary Tree Vertical Order Traversal
      • *269. Alien Dictionary
      • *323. Connected Component in Undirected Graph
      • *490. The Maze
    • 动态规划
      • 70. Climbing Stairs
      • 72. Edit Distance
      • 377. Combination Sum IV
      • 1335. Minimum Difficulty of a Job Schedule
      • 97. Interleaving String
      • 472. Concatenated Words
      • 403. Frog Jump
      • 674. Longest Continuous Increasing Subsequence
      • 62. Unique Paths
      • 64. Minimum Path Sum
      • 368. Largest Divisible Subset
      • 300. Longest Increasing Subsequence
      • 354. Russian Doll Envelopes
      • 121. Best Time to Buy and Sell Stock
      • 132. Palindrome Partitioning II
      • 312. Burst Balloons
      • 1143. Longest Common Subsequence
      • 718. Maximum Length of Repeated Subarray
      • 174. Dungeon Game
      • 115. Distinct Subsequences
      • 91. Decode Ways
      • 639. Decode Ways II
      • 712. Minimum ASCII Delete Sum for Two Strings
      • 221. Maximal Square
      • 1277. Count Square Submatrices with All Ones
      • 198. House Robber
      • 213. House Robber II
      • 1235. Maximum Profit in Job Scheduling
      • 740. Delete and Earn
      • 87. Scramble String
      • 1140. Stone Game II
      • 322. Coin Change
      • 518. Coin Change II
      • 1048. Longest String Chain
      • 44. Wildcard Matching
      • 10. Regular Expression Matching
      • 32. Longest Valid Parentheses
      • 1043. Partition Array for Maximum Sum
      • *256. Paint House
      • 926. Flip String to Monotone Increasing
      • *1062. Longest Repeating Substring
      • *1216. Valid Palindrome III
    • 贪心
      • 56. Merge Intervals
      • 621. Task Scheduler
      • 135. Candy
      • 376. Wiggle Subsequence
      • 55. Jump Game
      • 134. Gas Station
      • 1005. Maximize Sum Of Array After K Negations
      • 406. Queue Reconstruction by Height
      • 452. Minimum Number of Arrows to Burst Balloons
      • 738. Monotone Increasing Digits
    • 单调栈
      • 739. Daily Temperatures
      • 503. Next Greater Element II
      • 901. Online Stock Span
      • 85. Maximum Rectangle
      • 84. Largest Rectangle in Histogram
      • 907. Sum of Subarray Minimums
      • 239. Sliding Window Maximum
    • 前缀和
      • 53. Maximum Subarray
      • 523. Continuous Subarray Sum
      • 304. Range Sum Query 2D - Immutable
      • 1423. Maximum Points You Can Obtain from Cards
      • 1031. Maximum Sum of Two Non-Overlapping Subarrays
    • 并查集
      • 684. Redundant Connection
      • 721. Accounts Merge
      • 547. Number of Provinces
      • 737. Sentence Similarity II
      • *305. Number of Islands II
    • 字典树trie
      • 208. Implement Trie
      • 211. Design Add and Search Words Data Structure
      • 1268. Search Suggestions System
      • *1166. Design File System
      • *642. Design Search Autocomplete System
    • 扫描线sweep line
      • 253. Meeting Room II
      • 1094. Car Pooling
      • 218. The Skyline Problem
      • *759. Employee Free Time
    • tree map
      • 729. My Calendar I
      • 981. Time Based Key-Value Store
      • 846. Hand of Straights
      • 480. Sliding Window Median
      • 318. Count of Smaller Numbers After Self
    • 数学类
      • 50. Pow(x, n)
      • *311. Sparse Matrix Multiplication
      • 382. Linked List Random Node
      • 398. Random Pick Index
      • 29. Divide Two Integers
    • 设计类
      • 1603. Design Parking System
      • 355. Design Twitter
      • 1396. Design Underground System
      • *359. Logger Rate Limiter
      • *353. Design Snake Game
      • *379. Design Phone Directory
      • *588. Design In-Memory File System
      • *1244. Design A Leaderboard
    • SQL
  • 机器学习
    • 数学基础
    • 评价指标
    • 线性回归
    • 逻辑回归
    • 树模型
    • 深度学习
    • 支持向量机
    • KNN
    • 无监督学习
    • k-means
    • 强化学习 RL
    • 自然语言处理 NLP
    • 大语言模型 LLM
    • 机器视觉 CV
    • 多模态 MM
    • 分布式机器学习
    • 推荐系统
    • 异常检测与风控
    • 模型解释性
    • 多任务学习
    • MLops
    • 特征工程
    • 在线学习
    • 硬件 cuda/triton
    • 产品case分析
    • 项目deep dive
    • 机器学习代码汇总
  • 系统设计
    • 面向对象设计
      • 电梯设计
      • 停车场设计
      • Unix文件系统设计
    • 系统设计
      • 设计社交网站Twitter
      • 设计视频网站Youtube
      • 短网址系统
      • 爬虫系统
      • 任务调度系统
      • 日志系统
      • 分布式缓存
      • 广告点击聚合系统
      • webhook
    • 机器学习系统设计
      • 推荐系统
      • 搜索引擎
      • Youtube视频推荐
      • Twitter推荐
      • 广告点击预测
      • 新闻推送推荐
      • POI推荐
      • Youtube视频搜索
      • 有害内容检测
      • 大模型RAG
      • 大模型Agent
      • 信贷风控
      • 朋友推荐
      • 去重复性/版权检测
      • 情感分析
      • 目标检测
      • 问答系统
      • 知识图谱问答
  • 行为面试
    • 领导力法则
    • 问答举例
  • 案例分享
    • 准备工作
    • 面试小抄
    • 面试之后
Powered by GitBook
On this page
  • 0. 基础假设
  • 1. 最小二乘法 Ordinary least squares
  • 2. 最大似然法 Maximum likelihood estimation
  • 3. 最大后验法与正则 Maximum a posteriori estimation
  • L1正则
  • L2正则
  • 4. 贝叶斯线性回归与高斯过程回归
  • 5. 问答
  • 6. 代码
  • 参考
  1. 机器学习

线性回归

0. 基础假设

  • Linearity: There is a linear relationship between the independent variables(X) and the dependent variables (y)

  • Independence of Errors: Independence assumes that there is no relationship or correlation between the errors (residuals) of different observations.

  • Normality of Errors: The residuals of the linear regression model are assumed to be normally distributed.

  • Homoscedasticity (Constant Variance of Errors): Homoscedasticity assumes that the variability of the errors (residuals) is constant across all levels of the independent variables.

  • No Multicollinearity: No Multi-collinearity between features

1. 最小二乘法 Ordinary least squares

最小二乘法对线性回归的基础假设是:误差(residual)符合正态分布。

最小二乘法的理念是承认观测误差的存在,误差是围绕真值上下波动的,从几何角度看,当平方误差最小时即为真值。

具体应用时,算法目标是求线性方程中未知权重w和b,经验风险最小化时的权重认为是最优的。为了简化,我们经常在推导中忽略b,因为b可以认为存在一个常数列x=1对应的权重w的一个分量,不必单独另求。

以下向量定义为列向量,注意不同步骤需要清楚定义谁被看作是谁的函数

y=wTx+by=w^Tx +by=wTx+b

这里,有一个转置T,是为了求两个向量的点积。

w=argminwLoss(w)w=\mathop {argmin} _{w}Loss(w)w=argminw​Loss(w)

Loss function:

L(w)=1n∑i=1n(y^−y)2L(w)=\frac{1}{n}\sum_{i=1}^{n}{(\hat{y}-y)^2}L(w)=n1​i=1∑n​(y^​−y)2

从标准量中计算其梯度(只有一个样本,多样本加连加符号):

∂L(w)∂wj=(y^−y)xj\frac{\partial{L(w)}}{\partial{w_j}} = {(\hat{y} - y)}{x_j}∂wj​∂L(w)​=(y^​−y)xj​

梯度是让损失函数下降的方向。先看单个参数,就决定了下一步的大小(学习率还会拦一道)和方向(也就是正负)。对多个特征需要的多参数,主要是决定了不同参数大小的对比,也就成了多向量空间中的方向。对于多个样本来说,可以认为每个样本都会让参数"优化"移动一次。 不同参数的移动大小,可以说决定了这个参数的重要程度。因为本身需要norm,让这个移动更纯粹一些。

其矩阵形式如下:

L=(y−XW)T(y−XW)L= (y-XW)^{T}(y-XW)L=(y−XW)T(y−XW)
L=yTy−yTXW−WTXTy+WTXTXWL=y^Ty-y^TXW-W^TX^Ty+W^TX^TXWL=yTy−yTXW−WTXTy+WTXTXW

继续化简需要用到矩阵求导的几个规律:

∂L∂w=2XTXw−2XTy\frac{\partial{L}}{\partial{w}}=2X^TXw-2X^Ty∂w∂L​=2XTXw−2XTy
  • 损失函数为凸函数,可知道全局最优解的取值为导数等于0处的极值

  • 解析解的存在需要满足满秩矩阵。加L2 norm之后有了先验约束,则不需要

  • 梯度下降的优化方法:梯度决定了参数的变化方向。模型中每一个参数都看做是损失函数的自变量。结合逻辑回归、深度学习的前向反向传播综合理解

Normal Equation: 解析解-> 导数为0:

W=(XTX)−1XTyW=(X^TX)^{-1}X^TyW=(XTX)−1XTy

2. 最大似然法 Maximum likelihood estimation

最小二乘与最大似然等价,同属于频率派(Frequentists);最大后验则属于贝叶斯派(Bayesians)

什么是似然(likelihood)?在似然中,所有数据对(x, y) 的集合,被看成是一个随机变量X,但X已经被观察到了,已经固定。以最常见的抛硬币模型举例,硬币出现正面或反面就是一个随机变量,一个正常硬币出现正面的概率为1/2。而似然的定义是给定一个硬币,我们抛了10次之后出现了1次正面,出现1/10正面这个事件已经被观察到了并固定,这个硬币是正常硬币的可能性就被称为似然。

w,b=argmax(logP(x∣w))w,b=argmax(logP(x|w))w,b=argmax(logP(x∣w))

在线性回归中,贝叶斯公式可以写成:

P(w∣X)=P(X∣W).P(w)P(X)P(w|X)=\frac{P(X|W).P(w)}{P(X)}P(w∣X)=P(X)P(X∣W).P(w)​
  • P(X|w)是似然函数,

  • P(w)被称为先验(prior),是发生在我们观察样本之前,例如硬币抛出之前对硬币的假设。

  • P(w|X)就称为后验概率(posterior)

最大似然:

wMLE=argmaxwP(x∣w)w_{MLE}=\mathop{argmax}_w P(x|w)wMLE​=argmaxw​P(x∣w)

最大似然需要假设分布,对于一般线性回归的假设即是: 误差是一个高斯分布。(注意区分后验概率中的w本身是一个高斯分布)

关于变量x的高斯分布公式:

P(x)=12πσexp(−(x−μ)22σ2)P(x)=\frac{1}{\sqrt{2\pi}\sigma}exp(-\frac{(x-\mu)^2}{2\sigma^2})P(x)=2π​σ1​exp(−2σ2(x−μ)2​)

误差满足高斯分布,而误差是真值和预测的差值,所以:

P(y^−xw)∼N(0,σ2)P(\hat{y}-xw) \sim N(0,\sigma^2)P(y^​−xw)∼N(0,σ2)

由于误差是均值为0,方差$\sigma^2$的高斯分布,则y的预测值是满足均值为wx,方差为$\sigma^2$的高斯分布。在似然中的x其实代表的是一个样本,即(x,y)数据对,而在特征已知的情况下,似然函数可以进一步表示为:

P(y^∣x,w)P(\hat{y}|x,w)P(y^​∣x,w)

因此,似然也可以表达成一个高斯分布

P(y∣x,w)∼N(wTx,σ2)P(y|x,w) \sim N(w^Tx,\sigma^2)P(y∣x,w)∼N(wTx,σ2)

可以转化为:

P(y∣x,w)=12πσexp(−(y−wTx)22σ2)P(y|x,w)=\frac{1}{\sqrt{2\pi}\sigma}exp(-\frac{(y-w^Tx)^2}{2\sigma^2})P(y∣x,w)=2π​σ1​exp(−2σ2(y−wTx)2​)

3. 最大后验法与正则 Maximum a posteriori estimation

贝叶斯认为变量w也是一个随机变量,随机变量自带一个概率分布。因此在观察数据之前,我们可以提前根据经验预估一个w的分布,这个预估就是先验,并成为一个对参数的约束,即为最大后验法。

wMAP=argmaxwP(x∣w)P(w)w_{MAP}=\mathop{argmax}_w P(x|w)P(w)wMAP​=argmaxw​P(x∣w)P(w)

L1与L2理论涉及到朗格朗日优化。

w=argmax(P(w∣X))=argmax(P(X∣w).P(w))w=argmax(P(w|X))=argmax(P(X|w).P(w))w=argmax(P(w∣X))=argmax(P(X∣w).P(w))

从损失函数的角度,加了一个针对w大小的惩罚。

L1正则

  • 假设w的先验满足拉普拉斯分布

  • 在损失函数中引入了绝对值,绝对值的导数是不连续的。可采用坐标下降法优化,即每次更新一个w的分量

L2正则

  • 假设w的先验服从零均值正态分布

  • 解析解变为

w=(XTX+λI)−1XTYw=(X^TX+\lambda I)^{-1}X^TYw=(XTX+λI)−1XTY

4. 贝叶斯线性回归与高斯过程回归

贝叶斯线性回归要预测出一个关于y的分布来。稍微拓展一下贝叶斯线性回归到高斯过程回归(Gaussian Process Regression)

贝叶斯线性回归最大的不同在于预测的不是关于w的点估计,而是w的分布,即w的期望和协方差。

5. 问答

  • pros:

    • explainable method

    • interpretable results by its output coefficients

    • faster to train than other machine learning models

  • cons:

    • assumes linearity between inputs and outputs

    • sensitive to outliers

    • can under fit with small, high-dimensional data

  • 为什么需要对数值类型特征进行归一化

    • 使用梯度下降优化的模型,归一化容易更快通过梯度下降找到最优解,包括线性回归、逻辑回归、支持向量机、神经网络。

  • 如何判断是否该用线性回归模型

    • EDA画图

    • 检查预测值与真实值的residual残差是否为高斯分布

  • Lasso regression如何导致参数稀疏

    • 可用于特征筛选

    • 原因: 直观上可以从图像切点理解,数学上可以通过朗格朗日优化解释

  • 异常点如何处理

    • 检测并去除

    • Huber Loss is a loss function that combines MSE and MAE. It is less sensitive to outliers than a squared error loss and has the advantage of not needing to remove outlier data from the dataset

  • 共线性问题 Multi-collinearity

    • 自变量之间由于存在高度相关关系(correlation)而使模型的权重参数估计失真或难以估计准确的一种特性

    • 共线性不影响模型的预测而是影响对模型的解释

    • 可以尝试l2正则;可以使用PCA,将特征转为独立的变量

    • feature selection, ridge regression, PCA

6. 代码

梯度下降

  • 对多元函数的参数求∂偏导数,把求得的各个参数的偏导数以向量的形式写出来,就是梯度

  • 从几何意义上讲,梯度就是函数变化增加最快的方向。减梯度就是朝着减小的方向

def update_weights(w, b, x, y, learning_rate):
    w_grad = 0
    b_grad = 0
    N = len(x)
    for i in range(N):
        w_grad += -2 * x[i] * (y[i] - (w * x[i] + b))
        b_grad += -2 * (y[i] - (w * x[i] + b))

    w -= (w_grad / float(N)) * learning_rate
    b -= (b_grad / float(N)) * learning_rate
    return w, b

牛顿法

  • 在现有极小点估计值的附近对 f(x) 做二阶泰勒展开,进而找到极小点的下一个估计值

# https://www.stat.cmu.edu/~cshalizi/350/lectures/26/lecture-26.pdf

线性回归

# 2018.01.26 我学习机器学习的第一天写的代码,learning_rate too large cause the gradient explosion
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)

class LinearRegression(object):
    def __init__(self):
        self.w = None
        self.b = None

    def fit(self, x, y):
        batch_size, feature_size = x.shape
        self.w = np.random.random((feature_size, 1))
        self.b = np.random.random(1)
        self._gradient_descent(x, y)

    def eval(self):
        pass

    def predict(self, x):
        y_hat = np.dot(x, self.w) + self.b
        return y_hat

    def plot(self, x, y, y_pred):
        plt.plot(x, y, 'r-')
        plt.plot(x, y_pred, 'b.')
        plt.show()

    def _gradient_descent(self, x, y, learning_rate=0.0001, epoch=100):
        for i in range(epoch):
            y_hat = self.predict(x)  # => batch_size * 1
            loss = np.dot((y_hat - y).T, (y_hat - y))
            w_gradient = np.dot(x.T, x).dot(self.w) - np.dot(x.T, (y - self.b))
            b_gradient = self.b - np.mean(y - np.dot(x, self.w))
            self.w = self.w - learning_rate * w_gradient
            self.b=self.b - learning_rate * b_gradient
            print('step:', i, 'Loss:', loss[0][0])

    def __str__(self):
        return 'weights\t:%s\n bias\t:%f\n' % (self.w, self.b)

def generate_data():
    '''generate the examples to implement linear regression by numpy'''
    x = np.linspace(1, 30, num=30).reshape(-1, 3)
    y = np.linspace(1, 10, num=10) + np.random.random(10)
    y = y.reshape(-1, 1)
    return x,y

if __name__ == "__main__":
    x, y = generate_data()
    lr = LinearRegression()
    lr.fit(x, y)
    y_hat = lr.predict(x)
    lr.plot(x, y, y_hat)

参考

Previous评价指标Next逻辑回归

Last updated 11 days ago

wikipedia-linear regression
stat.cmu.edu/~cshalizi/mreg/15/lectures/04/lecture-04
Courses/ml/ppt/04_LinearRegression
为什么L1和L2正则化可防止过拟合 - 大龙的文章 - 知乎
5.1 - Ridge Regression
Gradient Descent Algorithm — a deep dive
https://web.vu.lt/mif/a.buteikis/wp-content/uploads/PE_Book/3-2-OLS.html