Believe in Mathematics
This article is my notes on support vector machine for Lecture 7 and 8 of Machine Learning by Andrew Ng.
In a binary classification problem, we can use logistic regression
where \(g\) is the sigmoid function with a figure of it below.
Then given input \(x\), the model predicts \(1\) if and only if \(\theta^x \ge 0\), in which case \(h_\theta(x) = g(\theta^T x) \ge 0.5\); and it predicts \(0\) if and only if \(\theta^T x < 0\). Moreover, based on the shape of sigmoid function, if \(\theta^T x >> 0\), we are very confident that \(y=1\). Likewise, if \(\theta^T x << 0\), we are very confident that \(y=0\). Therefore, we hope that for the training set \(\{(x^{(i)}, y^{(i)})\}_{i=1}^m\), we can find such a \(\theta\) that \(\theta^T x^{(i)} >> 0\) if \(y^{(i)}=1\) and \(\theta^T x^{(i)} << 0\) if \(y^{(i)}=0\).
This article is my notes on generative model for Lecture 5 and 6 of Machine Learning by Andrew Ng. What we do in logistic regression using generalized linear model is that, we approximate \(P(y|x)\) using given data. This kind of learning algorithms is discriminative, in which we predict \(y\) based on the input features \(x\). On the contrary, generative model is to model \(P(x|y)\), the probability of the features \(x\) given class \(y\). In other words, we want to study how the features structure looks like given a class \(y\). If we also learn what \(P(y)\) is, we can easily recover \(P(y|x)\), for example, in the binary classification problem,
where \(P(x) = P(x|y=0)P(y=0) + P(x|y=1)P(y=1)\).
In this article, we are going to see a simple example of generative model on Gaussian discriminant analysis and Naive Bayes.
这次比赛跪了。。
第一题Repeated String Match
二分查找。首先算出最少需要多少次才有可能使得B是子字符串:必须要保证重复后的A的长度不小于B的长度。我们可以简单算出最少需要重复:
这题竟然WA了2次(因为typo),TLE一次(一开始用j=10i)。。。
class Solution(object):
def repeatedStringMatch(self, A, B):
"""
:type A: str
:type B: str
:rtype: int
"""
i = (len(A) + len(B) - 1) / len(A)
j = 2 * i
ans = -1
while i <= j:
k = (i+j) / 2
tmp = A * k
if tmp.find(B) != -1:
ans = k
j = k - 1
else:
i = k + 1
return ans
This article is a companion article to my another post Generalized Linear Model. In this article, I will implement some of the learning algorithms in Generalized Linear Model. To be more specific, I will do some examples on linear regression and logistic regression. With some effort, google search gives me some very good example data sets to work with. The datasets collected by Larry Winner is one of the excellent sets, which will be used in the article.
The implementations here use Python. Required 3rd party libraries are:
这次比赛做得还算可以,只是最后一题看错题目WA了3次😭
第一题Baseball Game
class Solution(object):
def calPoints(self, ops):
"""
:type ops: List[str]
:rtype: int
"""
points = []
for op in ops:
if op == 'C':
points.pop()
elif op == 'D':
points.append(points[-1]*2)
elif op == '+':
points.append(points[-1]+points[-2])
else:
points.append(int(op))
return sum(points)
This article on Generalized Linear Model (GLM) is based on the first four lectures of Machine Learning by Andrew Ng. But the structure of the article is quite different from the lecture. I will talk about exponential family of distributions first. Then I will discuss the general idea of GLM. Finally, I will try to derive some well known learning algorithms from GLM.
\(\eta\) is called the natural parameter and \(T(y)\) is called the sufficient statistic.
这次比赛对python不友好啊,第三题在比赛的时候一直TLE,比赛结束后用C++实现同样的算法就过了😭
第一题Longest Continuous Increasing Subsequence
扫描一遍数组即可:如果nums[i], nums[i+1], ..., nums[j-1]是当前最长严格递增子数组,下次扫描的时候可以直接从j开始。
class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = i = 0
while i < len(nums):
j = i + 1
while j < len(nums) and nums[j] > nums[j-1]:
j += 1
ans = max(ans, j-i)
i = j
return ans
这次比赛有点简单的样子。
第一题Second Minimum Node In a Binary Tree
我当时什么都没有想,简单地将树的所有值取下来排序取出第二最小值。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def findSecondMinimumValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
nums = set()
def dfs(root):
if not root:
return
nums.add(root.val)
dfs(root.left)
dfs(root.right)
dfs(root)
return -1 if len(nums)<2 else sorted(list(nums))[1]