构造C++程序都会面临三个传参选择,传值、传指针还是传引用?这里参考了xinklabi和一些教材,对三种传参形式进行介绍和对比。
int func(Item item); // general int func(Item *item); // pass by pointer int func(Item &item); // pass by reference
构造C++程序都会面临三个传参选择,传值、传指针还是传引用?这里参考了xinklabi和一些教材,对三种传参形式进行介绍和对比。
int func(Item item); // general int func(Item *item); // pass by pointer int func(Item &item); // pass by reference
数学上,一个m×n的矩阵是一个由m行n列元素排列成的矩形阵列(元素可以是数字、符号或数学式)。如上所示是一个4×3的矩阵A,从左上角数起的第i行第j列上的元素成为第i, j项,通常记为Ai, j,比如上面的例子中A2, 3 = 7。而矩阵dimension的定义则是行数x列数。
在模式识别领域中,kNN (k-Nearest Neighbor,也称k-近邻算法或最近邻法)是在特征向量空间中从训练数据中找出k个最接近新输入数据的训练样本,利用找到的这k个样本来对新输入的数据进行预测。最近邻法采用向量空间模型来分类,概念相同样本,彼此的相似度高。打个简单的比方,你要了解一个人饮食口味,你可以去了解一下与这个人年龄相似、性别相似、地域相同的人们喜欢的饮食,通过他们的结果来预测这个人的喜好。为了更直观了解kNN,我们先来看一个简单的例子:
第一个图给定一堆数据样本,每一个样本有一个feature x和一个target y,现在给一个新的属性值x(new),求最可能的y(new)值。这里我们采取的策略就是从具有相同或相似的属性值入手,找到与x(new)最近的k个点,然后通过这k个点来预测y(new)。第二个图则是在此策略的基础上(k=1),将训练数据划分成不同的区域,这样每当输入一个新样本属性,我们可以很快预测出新样本目标值。
k-NN是一种基于实例的学习,采取局部近似和将所有计算推迟到分类之后的惰性学习。它是机器学习算法中最简单的之一,其训练过程很trivial,可以理解为将训练数据理解为一个look-up table,每当需要判断一个新样本时,再通过搜索找到最近邻的k个样本。
机器学习中有一对很重要的概念,那就是模型的复杂度和训练结果的过度拟合。理论上来讲,我们可以把算法训练到很完美地(0误差)拟合所有的训练数据(training data),但是这样的算法很有可能在测试数据(test data)上表现不佳,因为它违反了奥卡姆剃刀原则,当然也可以理解为矫枉过正。
在kNN算法中,当k=1,那么样本被简单分配给其最近邻的一个样本的类,它的表现就像单纯的去记忆每一个训练样本,能够完全地拟合所有的训练数据,这个模型是很complex的。当不断地增大k,模型的复杂度也逐渐下降。下面的图形显示的是当样本有两个feature时(横纵坐标),利用kNN算法进行分类(红蓝点)。我们可以看到当k不断增大,决策面也变得更加柔和,有些红点样本也被归入到蓝色区域中。Majority voting means less emphasis on individual points.
下面一张图红色曲线表示的是训练数据的预测误差随k值变化的趋势,对应绿色曲线表示的是训练数据的预测误差变化趋势。
可以看到,随着k不断增大,训练数据的预测误差先迅速增大然后趋于平稳,而测试数据的预测误差则体现为限降低,后平稳,最后略有回升。所以我们应该在中间部分取得合理的k值。
第一次接触机器学习还是在大三的时候,那个时候项目中因为需要预测电网中一段时间内电量消耗的情况,所以看了一点预测方面的算法,才知道原来CS不是修计算机的科学,而是如何让计算机更好的为人类所用的科学,因而决定弃EE从CS,走上码农的不归路。。。
其实自己去年9月就上过Machine Learning的课,当时的课程内容就是用Matlab仿真最基本的machine learning算法,而在后面的project中则通过用Weka解决一个特定的分类问题来分析各种算法之间的优劣。对machine learning的认识也只停留在一个很肤浅的程度。最近决定要选修系里的Large Scale in Machine Learning,所以决定在开课前先巩固一遍machine learning,一是梳理思路,希望相比之前能站在更高的一个角度来看待这个学科,二是记录一些自己学习过程中的理解重点、难点、易错点。
在了解Machine Learning前,我们不得不提到Artificial Intelligence(人工智能,简称AI)。这个术语是由当时麻省理工学院的约翰·麦卡锡在1956年的达特矛斯会议上提出的:人工智能就是要让机器的行为看起来就像是人所表现出的智能行为一样。关于如何判断机器是否能表现出人类一样的智能,参见图灵测试。
而在计算机范畴内的AI,我们可以理解为让计算机体现出像人类一样可以感知、学习的特征。比如让计算机进行象棋博弈,让计算机对新闻进行自动分类,让计算机通过分析以往的天气记录来预测未来的天气。。。为了达到这样的效果,有许许多多的算法及策略应运而生,随着研究的不断深入、细分,人工智能被分为三个领域,它们分别是Machine Learning, Pattern Recognition and Data Mining。这三个领域相互独立相互包含,有的时候针对一道具体问题的时候,很难把它划为某一特定领域,而且很多算法在三个领域中都适用。
机器学习的定义有两个注明的版本,一个是Arthur Samuel于1959年提出Machine Learning – Field of study that gives computers the ability to learn without being explicitly programmed,另一个则是Tom Mitchell于1998年提出Well-posed Learning Problem – A computer program is said to learn from experience E with respect to some task T and some performance measure P, if its performance on T, as measured by P, improves with experience E。简而言之,就是让计算机利用已有的经验进行自我学习,达到举一反三的目的。而目前我们生活中的许多地方都需要让计算机来体现这样的学习能力:
根据学习数据的不同,Machine Learning算法可以分为:
有监督学习,顾名思义,就是学习的过程有明确目标。比如给一堆电子邮件,我们可以让计算机分析(学习)每封邮件的发件人、发件人地址、发件时间、主题、内容长短、链接个数(这些称之为features或attributes)及该邮件是否为垃圾邮件(称为target或result),这样当给出新的一封邮件时,计算机根据以往信息对该邮件进行判断。
上面的例子是一个classification problem,因为是一个是非的判断问题,计算机要求输出一个离散的结果,若如果任务变成预测未来一段时间内的温度,由于输出在一个连续的取值范围里面,所以这个问题就变成了regression problem。
相对于上面的有监督学习,无监督学习则是需要学习的数据没有特定的target(可以理解为Data Mining)。比如前面新闻分类的例子,给一大堆新闻,根据他们的标题、内容、长短、时间、撰稿人等等信息多维度进行分析从而将新闻进行分类,而好的算法能让这些新闻的分类符合人类分类的习惯,比如财经类、体育类、娱乐类、民生类等等。
第三种学习也很常见,比如有许多病人的数据,但是只有其中一些记录了结果,或者在Flikr上面有很多图片,但是只有很少一部分有被贴上标签了,要对这些数据进行分析,就属于半监督学习。
如上图所示,在玩牌的过程中不断根据新进牌的情况而改变下注策略。
在计算机中,所有的数据都是存放在存储器中的。一般把存储器中的一个字节称为一个内存单元,不同的数据类型所占用的内存单元数不等。为了正确地访问这些内存单元,必须为每个内存单元编上号,根据一个内存单元的编号即可准确地找到该内存单元(内存单元的编号也叫做地址)。指针则是针对这个而产生,通过储存内存单元的地址从而访问储存的内容(理解“指针就是地址”)。 Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥ ♥
Data Structure: String
Algorithm: dynamic programming, DFS, backtrace, recursively
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥ ♥
Data Structure: String
Algorithm: recursively, iteratively, dynamic programming, top down
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥ ♥
Data Structure: HashMap
Algorithm:
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥
Data Structure: Array, Stack
Algorithm: one pass strategy
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: graph
Algorithm: BFS, DFS
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: String
Algorithm:
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: Array
Algorithm: one pass strategy
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: Array
Algorithm: one pass strategy
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: Linked List
Algorithm: divide and conquer
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: Linked List
Algorithm:
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥ ♥
Data Structure: Double Linked List, Hash Map
Algorithm:
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: Linked List
Algorithm: two pointers
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥
Data Structure: Linked List
Algorithm: two pointers
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: Linked List
Algorithm:
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥ ♥
Data Structure: Linked List
Algorithm: Hash
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: Array
Algorithm: bit manipulation
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥
Data Structure: Array
Algorithm: hash, bit manipulation
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥ ♥
Data Structure: Array
Algorithm: divide and conquer, dynamic programming
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥
Data Structure: Array
Algorithm: greedy algorithm, one pass strategy
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥
Data Structure: Array
Algorithm: one pass strategy, dynamic programming
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: matrix
Algorithm: dynamic programming, level order traversal, top-down, bottom-up
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥
Data Structure: ArrayList
Algorithm: level order traversal
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥
Data Structure: Array
Algorithm: level order traversal
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥
Data Structure: binary tree
Algorithm: recursively, BFS, level order traversal
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥
Data Structure: binary tree
Algorithm: recursively, BFS, level order traversal
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥
Data Structure: binary tree
Algorithm: recursively
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥ ♥
Data Structure: BST
Algorithm: recursively
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: binary search tree
Algorithm: dynamic programming, iteratively, recursively
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: String, Stack
Algorithm:
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥ ♥
Data Structure: Array, String
Algorithm: DFS, math
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥
Data Structure: String
Algorithm: one pass strategy
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥ ♥
Data Structure: String, HashMap, HashSet, Tier Tree
Algorithm:
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥ ♥
Data Structure: Array
Algorithm: two pointers
Read the rest of this entry »
Frequency: ♥ Difficulty: ♥ ♥
Data Structure: String
Algorithm:
Read the rest of this entry »