Java线程间同步(诡异的IllegalMonitorStateException )

  前两天去面试,被问到了一个线程同步的问题,两个线程依次输出1……100,一个线程只输出奇数,一个只输出偶数。之前工作中没写过线程同步的代码,只知道使用object的wait()和notify()方法可以实现线程同步,之前也看过线程池实现的代码,用的也是wait()和notify()。 面试过程中没写出来,于是想回来学习下多线程的同步,然后就有了今天这诡异的事。
Continue reading

高效工程师系列(二) 花时间学习新技能

  工作中很容易被各种必须完成的事压得喘不过气来。我指导过的那些人,通常都是刚开始工作的人经常感觉到他们的工作任务清单在持续增长,而且进度远远的落在了后面,他们总是花很长时间追赶进度而不是花足够的时间开发能让他们更高效的新技能。
Continue reading

西屋时间胶囊

  时间胶囊是是一种用来把某些东西存放到未来再打开的容器,因其具备跨越时间的属性,所以被称为时间胶囊。历史上最著名的时间胶囊是在1939年世博会被埋下的西屋时间胶囊 是准备在6939年打开的,除了一些当时的代表性物品,还有爱因斯坦、物理学家密立根、著名作家托马斯·曼流给未来的三段话,现摘录并翻译如下。
Continue reading

关于《火影忍者 博人传》的一点想法

  作为一个海贼迷(对 我就是海贼迷),我还是抽空看完了火影忍者的最新剧场版————《博人传》,客观的评价,剧情还是很有吸引力的,毕竟从一个完整的故事背景发展出来的剧情都不会差到哪去。中国动漫只适合小孩子看,我觉得原因就是缺乏贯穿全故事的主线,火影忍者这个故事的主线就是一个从小到大都是loser的人最终实现自己成为火影的梦想。《海贼王》也有一个主线,这里不再讨论。其实火影的这个主线已经完结,那么这个剧场版还演什么? 我估计还是为了钱,把鸣人的儿子博人搬上来了,没错 七代火影的儿子,四代火影的孙子————就是此剧场版的主角。。。

Continue reading

萌妹子Python入门指北(三)

  前两篇网站我简单介绍了python环境的安装和基本的变量及运算。到目前为止,我们没办法用python做任何事,所以这篇文章我会介绍python的判断和循环语句,据说顺序、判断、循环可以解决计算机中的任何问题。 我为什么不介绍顺序呢!因为很简单,其实就是python的每行代码按顺序执行。 其实python预发是相当容易看懂的,本文我会将示例代码翻译成汉语方便大家理解(翻译后的代码是不能执行的哦)。

Continue reading

萌妹子Python入门指北(一)

《萌妹子Python入门指导》系列,以下简称萌妹子系列是教没有任何编程基础的妹子如何去写python代码,最终实现一些小工具的开发,请Python大牛们直接绕道。如果有想学习python的同学,也可以持续关注本系列。 本人在某互联网公司做运维,虽然python学的不是很好,但足以教一个完全不懂python的人,也希望在撰文的过程中提升自己的能力。

这是本系列第一堂课,主要介绍python为何物,以及python基础环境的安装,如果你已了解和安装了python,可直接跳过本文。

Continue reading

给CS专业大学生的推荐书籍

       刚刚大学毕业整一个月。大学这几年,尤其是后两年,看了很多书,现在在我豆瓣读书主页有149本的读书量了,其中有很多杂书,但也不乏好书,我就结合自己的体会从中给大家推荐一些。

       我个人大学期间参加ACM,所以计算机书籍会更偏向算法。另外因为开始对科学、数学有兴趣, 所以也会有一些这方面的书籍。为了拓宽知识面,我也看过一些心理学哲学的书籍,但本人并未在这些领域有所深入的研究。接下来我的读书重点会在英语、数学、机器学习等方面。 

       下面这些书只是结合我自身的情况推荐的,并不一定合理,可以适当参考下。推荐书目我会细化的学年,请大家根据自己的情况去阅读。

Continue reading

Github 配置SSH keys教程


   不知道什么原因,CSDN博客保存的时候会丢失图片和格式,请可以戳这里到我有道云笔记分享

   


   今天特地把Github上的教程Github Generating SSH keys翻译成中文(并非全文翻译),因为自己的水平有限,有错误还请指出。

   SSH key 是为了确认你是项目的管理者或拥有者,然后可以不使用密码使用git。当然使用github的时候要用SSH链接,用https的话就无所谓了。  https可以clone任何人的project,但SSH只能clone属于你的,你必须是这个项目的拥有者。 另外SSH在push的时候不需要输入用户名和密码,方便了用户的使用,前提是你先得在github上添加你的SSH key。  

   因为CSDN Code项目也是使用Git,而且和Github差不多,因此CSDN Code也可以参考此教程。


第一步:检查SSH keys


首先,需要检查你的电脑上是否存在SSH keys,打开你的电脑终端,输入以下命令:

ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist



如果你已经有了SSH key,你会看到以下的默认文件。



  • id_dsa.pub

  • id_ecdsa.pub

  • id_ed25519.pub

  • id_rsa.pub

然后你就可以直接跳过第二步直接进入第三步


第二步:创建一个新SSH key




要生成新的SSH密钥,打开终端输入以下命令,并确输入你的E-mail。默认设置是首选,因此当系统会提示“请输入要保存的重要文件:”你,只需按Enter键继续就可以了。


ssh-keygen -t rsa -C "your_email@example.com" # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. # Enter file in which to save the key (/home/you/.ssh/id_rsa):




接下来要求你输入密码,这个密码是使用SSH链接 push的时候用的,可以选择设,如果不想设 按Enter就可以了


然后如果输出下面内容,表示key创建成功

# Your identification has been saved in /home/you/.ssh/id_rsa. # Your public key has been saved in /home/you/.ssh/id_rsa.pub. # The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com



接下来把你刚刚创建的key添加到ssh-agent,执行以下两条命令。

# start the ssh-agent in the background eval "$(ssh-agent -s)" # Agent pid 59566 ssh-add ~/.ssh/id_rsa



第三步:把ssh key添加到github上


运行如下命令,拷贝key到剪切板. key的文件名是 id_dsa.pubid_ecdsa.pub 或者 id_ed25519.pub. 或者直接用gedit等编辑器打开这些文件复制即可。

sudo apt-get install xclip # Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`) xclip -sel clip < ~/.ssh/id_rsa.pub # Copies the contents of the id_rsa.pub file to your clipboard


然后打开你github设置页面,里面有个SSH keys  如图



然后点击Add SSH key  把刚刚复制的粘贴进去就可以了。



第四步:测试


为了确保一切工作正常,可以测试一下是否可以正常链接到github上。测试时,可能会让你输入密码,


打开你的终端,然后输入:

ssh -T git@github.com # Attempts to ssh to github


你可能会看到这样的错误信息:

... Agent admitted failure to sign using the key. debug1: No more authentication methods to try. Permission denied (publickey).


可参阅帮助文章


您可能会看到这样的警告:

# The authenticity of host 'github.com (207.97.227.239)' can't be established. # RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. # Are you sure you want to continue connecting (yes/no)?


不用担心,这是正常的。直接输入“yes”。

# Hi username! You've successfully authenticated, but GitHub does not # provide shell access.


如果该用户名是你的,你已经成功设置SSH密钥!


如果仍然无法链接到github,你可以参考这些文章。


如果你从HTTPS切换到SSH,你现在需要更新远程存储库的URL。欲了解更多信息,请参见更改远程的URL


大学三年总结



前两天拿到阿里巴巴和美团的offer,算是快走过人生的一个阶段了,应该准备走向社会了。现在的自己,可能不会被当做一个反面教材,不过以前就不好说了。回想自己的大学生活,第一年挂科无数(其实是挂了学分最高的几门课),最终成绩排名专业倒数第五,现在的我学习成绩也不怎么样,这就是我与奖学金无缘的原因了。然后也在学生会混过,交了一些朋友,但也浪费了不少的时间,还好,后来学生会没再继续干下去。


Continue reading

Leetcode Binary Tree Postorder Traversal(面试题推荐)


此题来自leetcode   https://oj.leetcode.com/problems/binary-tree-postorder-traversal/


Given a binary tree, return the postorder traversal of its nodes’ values.


For example:

Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3


return [3,2,1].


Note: Recursive solution is trivial, could you do it iteratively?


   非递后续归遍历二叉树,肯定得用到栈。先序遍历很好写,但后续遍历就不是那么容易了。

   只需要设置个指针pre,指向最后输出的那个节点就行了,只要判断cur指针指向的是上次输出节点的父节点,且cur无其他未遍历的节点,这个时候就把cur节点输出即可,然后更改pre。原理是要遍历当前节点,其所有子节点都必须遍历完,因为肯定是先左后右,所以只需一个指针保持前一次输出的结果即可。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> v;
        if (NULL == root)
            return v;
        stack<TreeNode*> s;
        s.push(root);
        TreeNode * pre = root;
        while (!s.empty()) {
            TreeNode * cur = s.top();
            if ((NULL == cur->left && NULL == cur->right) || (pre == cur->left || pre == cur->right)) {
                s.pop();
                v.push_back(cur->val);
                pre = cur;
            }
            else {
                if (cur->right)
                    s.push(cur->right);
                if (cur->left)
                    s.push(cur->left);
            }
        }
        return v;
    }
};

Leetcode Single Number II (面试题推荐)


    还记得《剑指offer》和《编程之美》等书上多次出现的找一个数组中只出现一次的数那个题吗?

    leetcode也有这道题 链接here  相信大家都知道用异或在O(n)的时间复杂度内求出的方法,这里不再赘述。

下面就是上题的升级版

Given an array of integers, every element appears three times except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

   给你一个整数数组,每个元素出现了三次,但只有一个元素出现了一次,让你找出这个数,要求线性的时间复杂度,不使用额外空间。

   这样就不能使用异或的方法了,把所有元素以后得到的就是所有元素的异或值了,对我们没有任何帮助。

   这个问题依旧使用位运算来解决,不过更加复杂。

   我们用三个变量one two three,分别存储二进制未分别出现一次 两次 三次的位。

class Solution {
public:
    int singleNumber(int A[], int n) {
        int one = 0, two = 0, three = 0;
        for(int i = 0; i < n; i++)
        {
            three = two & A[i];                    //出现三次的位肯定是由出现两次的得到的
            two = two | ones & A[i];          //出现两次的肯定是出现一次的得到的,不过还有原有的所以要或
            one = one | A[i];                       //计算出现一次的位  
            two = two & ~three;               //去掉二进制中出现三次的位
            ones = one & ~three;               //去掉二进制中出现三次的位</span>
        }
        return one;                                     //最终twos three都为0,one就是我们要的答案
    }
};


Leetcode Copy List with Random Pointer(面试题推荐)


给大家推荐一道leetcode上的面试题,这道题的具体讲解在《剑指offer》的P149页有思路讲解,如果你手头有这本书,建议翻阅。

题目链接
here

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

RandomList中,每个节点不光有一个next指针,而且每个链表节点都有一个random指针,随机指向链表中的一个节点,让你复制这个链表,节点的C++定义如下。

/**
 * Definition for singly-linked list with a random pointer.
 * struct RandomListNode {
 *     int label;
 *     RandomListNode *next, *random;
 *     RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
 * };
 */

     看到这个题目,估计很多人的第一反应是,先把这个单链表复制出来,然后挨个找random指向的节点。仔细想想这样效率很低,复制链表很快,但确定每个节点的random指针就不容易了,用这种方法的话,每找一个random指针,都必须遍历一次链表。时间复杂度O(n^2)。

    可能也有人可能会想到时间换空间的方法,用hash把时间复杂度降到O(n)。

    当然,还有更省时省空间的方法。

     大概思路就是,在原链表的基础上,把每个节点复制一份加的原来节点的后面,然后设置好新节点random指针,在把所有的新节点从原链表中分离出来,构成一个新链表,这个链表就是我们要的原链表的拷贝。

    下面有三个函数,第一个明显就是复制新节点并把其加入到被复制节点的后面。第二个,因为新节点的random指针还是指向旧节点的,要把它指向新节点,很简单,因为每个节点的新节点都是在原来的节点之后的。 第三个函数,把新节点从原链表中抽离,构成一个新链表。

   这种方法的好处就是,时间复杂度只有O(n),而且我们不需要额外的空间。

class Solution {
public:
    void CloneNode(RandomListNode *head) {
        RandomListNode * p = head;
        while (NULL != p) {
            RandomListNode * CloneNode = new RandomListNode(p->label);
            CloneNode->next = p->next;
            CloneNode->random = p->random;
            p->next = CloneNode;
            p = CloneNode->next;
        }
    }
    
    void ConnectRandomNode(RandomListNode * head) {
        RandomListNode * p = head;
        while (NULL != p) {
            RandomListNode *pclone = p->next;
            if (NULL != pclone->random) {
                pclone->random = pclone->random->next;
            }
            p = pclone->next;
        }
    }
    RandomListNode *copyRandomList(RandomListNode *head) {
        if (NULL == head)
            return head;
        CloneNode(head);
        ConnectRandomNode(head);
        RandomListNode *pnode = head;
        RandomListNode *pclonehead = pnode->next;
        RandomListNode *pclonenode = pnode->next;
        while (NULL != pnode) {
            pnode->next = pclonenode->next;
            pnode = pnode->next;
            if (NULL != pnode){
                 pclonenode->next = pnode->next;
                 pclonenode = pclonenode->next;
            }
        }
        return pclonehead;
    }
    
};


Stanford 机器学习练习 Part 3 Neural Networks: Representation


   从神经网络开始,感觉自己慢慢跟不上课程的节奏了,一些代码好多参考了别人的代码,而且,让我现在单独写也不一定写的出来了。学习就是一件慢慢积累的过程,两年前我学算法的时候,好多算法都完全看不懂,但后来,看的多了,做的多了,有一天就茅塞顿开。所有的困难都是一时的,只要坚持下去,一切问题都会解决的。没忍住发了点鸡汤文。

    关于神经网络,我表示现在的理解就是每一层都每个神经元都是依靠logistics regression得出的,所以必须先要掌握logistics regression。关于如何训练这个模型的算法(比如 反向传播算法),我也在这周的课程中慢慢学。其中涉及到好多数据处理的技巧,这些都是得通过练习才能学会的。

    下面是我参考别人代码写的第四周神经网络的编程练习,仅作为参考。

关于lrCostFunction,我直接用了上周练习中CostFunction的代码,没想到竟然也可以通过。

lrCostFunction.m

function [J, grad] = lrCostFunction(theta, X, y, lambda)
%LRCOSTFUNCTION Compute cost and gradient for logistic regression with 
%regularization
%   J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
%   theta as the parameter for regularized logistic regression and the
%   gradient of the cost w.r.t. to the parameters. 

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly 
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta
%
% Hint: The computation of the cost function and gradients can be
%       efficiently vectorized. For example, consider the computation
%
%           sigmoid(X * theta)
%
%       Each row of the resulting matrix will contain the value of the
%       prediction for that example. You can make use of this to vectorize
%       the cost function and gradient computations. 
%
% Hint: When computing the gradient of the regularized cost function, 
%       there're many possible vectorized solutions, but one solution
%       looks like:
%           grad = (unregularized gradient for logistic regression)
%           temp = theta; 
%           temp(1) = 0;   % because we don't add anything for j = 0  
%           grad = grad + YOUR_CODE_HERE (using the temp variable)
%

h = sigmoid(X*theta);  
J = m^-1 * sum(((-1) * y.*log(h)).-((1- y).*log(1 - h)));
theta(1) = 0;
tmp = lambda/(2*m)*sum(theta.^2);
J = J + tmp;
grad = m^-1 * ((h.-y)'*X)' + lambda/m * theta; 


% =============================================================

grad = grad(:);

end


oneVsAll.m

function [all_theta] = oneVsAll(X, y, num_labels, lambda)
%ONEVSALL trains multiple logistic regression classifiers and returns all
%the classifiers in a matrix all_theta, where the i-th row of all_theta 
%corresponds to the classifier for label i
%   [all_theta] = ONEVSALL(X, y, num_labels, lambda) trains num_labels
%   logisitc regression classifiers and returns each of these classifiers
%   in a matrix all_theta, where the i-th row of all_theta corresponds 
%   to the classifier for label i

% Some useful variables
m = size(X, 1);
n = size(X, 2);

% You need to return the following variables correctly 
all_theta = zeros(num_labels, n + 1);

% Add ones to the X data matrix
X = [ones(m, 1) X];

% ====================== YOUR CODE HERE ======================
% Instructions: You should complete the following code to train num_labels
%               logistic regression classifiers with regularization
%               parameter lambda. 
%
% Hint: theta(:) will return a column vector.
%
% Hint: You can use y == c to obtain a vector of 1's and 0's that tell use 
%       whether the ground truth is true/false for this class.
%
% Note: For this assignment, we recommend using fmincg to optimize the cost
%       function. It is okay to use a for-loop (for c = 1:num_labels) to
%       loop over the different classes.
%
%       fmincg works similarly to fminunc, but is more efficient when we
%       are dealing with large number of parameters.
%
% Example Code for fmincg:
%
%     % Set Initial theta
%     initial_theta = zeros(n + 1, 1);
%     
%     % Set options for fminunc
%     options = optimset('GradObj', 'on', 'MaxIter', 50);
% 
%     % Run fmincg to obtain the optimal theta
%     % This function will return theta and the cost 
%     [theta] = ...
%         fmincg (@(t)(lrCostFunction(t, X, (y == c), lambda)), ...
%                 initial_theta, options);
%
for k=1:num_labels
    initial_theta = zeros(n + 1, 1);
    options = optimset('GradObj', 'on', 'MaxIter', 50);
    [theta] = fmincg (@(t)(lrCostFunction(t, X, (y == k), lambda)),initial_theta, options);
    all_theta(k,:) = theta';
end
% =========================================================================

end

predictOneVsAll.m

function p = predictOneVsAll(all_theta, X)
%PREDICT Predict the label for a trained one-vs-all classifier. The labels 
%are in the range 1..K, where K = size(all_theta, 1). 
%  p = PREDICTONEVSALL(all_theta, X) will return a vector of predictions
%  for each example in the matrix X. Note that X contains the examples in
%  rows. all_theta is a matrix where the i-th row is a trained logistic
%  regression theta vector for the i-th class. You should set p to a vector
%  of values from 1..K (e.g., p = [1; 3; 1; 2] predicts classes 1, 3, 1, 2
%  for 4 examples) 

m = size(X, 1);
num_labels = size(all_theta, 1);

% You need to return the following variables correctly 
p = zeros(size(X, 1), 1);

% Add ones to the X data matrix
X = [ones(m, 1) X];

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned logistic regression parameters (one-vs-all).
%               You should set p to a vector of predictions (from 1 to
%               num_labels).
%
% Hint: This code can be done all vectorized using the max function.
%       In particular, the max function can also return the index of the 
%       max element, for more information see 'help max'. If your examples 
%       are in rows, then, you can use max(A, [], 2) to obtain the max 
%       for each row.
%       
[c,i] = max(sigmoid(X * all_theta'), [], 2);
p = i;

% =========================================================================


end

predict.m

function p = predict(Theta1, Theta2, X)
%PREDICT Predict the label of an input given a trained neural network
%   p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
%   trained weights of a neural network (Theta1, Theta2)

% Useful values
m = size(X, 1);
num_labels = size(Theta2, 1);

% You need to return the following variables correctly 
p = zeros(size(X, 1), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned neural network. You should set p to a 
%               vector containing labels between 1 to num_labels.
%
% Hint: The max function might come in useful. In particular, the max
%       function can also return the index of the max element, for more
%       information see 'help max'. If your examples are in rows, then, you
%       can use max(A, [], 2) to obtain the max for each row.
%

X = [ones(m, 1) X];
z2 = Theta1 * X';
a2 = sigmoid(z2);
a2 = [ones(1, m);a2];
z3 = Theta2 * a2;
a3 = sigmoid(z3);
output =a3';
[c,i] = max(output, [], 2);
p = i;

% =========================================================================

end

成功与失败——《异类》读后感


    看完《异类》,才知道成功不是想象中那么简单。

    知道这本书是因为那个一万小时定律,这个定律大概是说 一个人只要坚持专注于某一领域有一万个小时,那么他就能成为该领域的专家。这个理论给了我信心,我只要坚持十年,说不定就可以完成自己梦想,相对于整个人生来说,十年的时间并不算多。两天时间断断续续看完,改变了我对成功的看法。

   开篇讲到马太效应,凡是有的,要加给他,交他有余,没有的连他所有的也要夺过来。因为此话出自《新约 马太福音》,所以该效应被称为马太效应。举个简单好吃的栗子,越优秀的学生越容易获得更好的资源,从而变的更优秀。作者先是讲了优秀球队队员出生月份的关系,揭示了成功其实和出生月份有很大的关系,这很大程度取决于该地区的选拔时间,也许在中国你会发现,高考考的好的很大一部分都是在高考几个月出生的。推而广之,讲到了计算机时代非常成功的一些人,比如比尔盖茨 比尔乔伊 乔布斯 ,揭示了他们的成功和机遇有关,正好他们出生的年份都是在上世纪65年左右,他们的成长正好在计算机发展的浪潮上,现在看今天那些互联网的大佬,不都是站在互联网的浪潮上才起家的嘛!
如果他们没有生在这个时代,他们的名字也就不会为大家所熟知。

   然后就是很出名的10000小时定律了,书中指出:“人们眼中的天才之所以卓越非凡,并非天资超人一等,而是付出了持续不断的努力。只要经过1万小时的锤炼,任何人都能从平凡变成超凡。”他将此称为“一万小时定律”。要成为某个领域的专家,需要10000小时,按比例计算就是:如果每天工作八个小时,一周工作五天,那么成为一个领域的专家至少需要五年。这就是一万小时定律,也是书中的核心观念,这很符合中国熟能生巧的说法。

   我们普遍的看法是,聪明的人肯定比较有出息,此书揭示了这个误区。也许你想在某一领域获得一定成就,首要条件就是你不能太笨,但是否越聪明越好? 其实不然,书中讲到,爱因斯坦的智商是150,世界上智商比他高的大有人在,但可以说几乎全部的人获得的成就远不如他。智商越高,带来的优势也就越不明显,类似边际效应。 书中举了大量的例子来说明智商不是成功决定性的一环,但正常的智商确是必不可少的。书中讲到几个智商非常高的人,但其一生也没多少成就,通过大量的数据调查显示,即便一个人有很高的智商,其未来的发展也会很大程度上受父母、环境的影响。在我看了,环境的影响才是最重要的。

    第一部分将的成功,那么第二部分讲的就是失败了。成功的人都是一样的成功,失败的人各有各的失败。书中再次举例,失败有时候就是各种小问题的积累,量变引起了质变。并且再次说出了环境的重要性,这恰好与我们传统观念中的“逆境出人才”相悖。我想可能的原因是这样,逆境中出的人才往往被大家作为新闻,甚至是饭后的谈资,让大家更为熟知,因此给大家一种逆境中出的人才多的心理。

   书中提出成功的三个要素为:环境,个人努力,机遇。而在我们传统挂念中,往往将努力放在了第一位,而忽视了环境。中学时候,我们拼了命想考一个好学校,不久是看重好学校有好的环境吗!现在很多父母,只知道督促自己的孩子努力,但自己却起不到榜样的作用,整天无所事事抽烟酗酒,孩子在这样的环境中如何成长!!

 


Stanford 机器学习练习 Part 2 Logistics Regression


以下是我学习Andrew Ng machine learning 课程时logistic regression的相关代码,仅作为参考,因为是初学,暂时没办法做出总结。

sigmoid.m

function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
%   J = SIGMOID(z) computes the sigmoid of z.
% You need to return the following variables correctly 
g = zeros(size(z));
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
%               vector or scalar).
g = (1 + e.^(-z)).^(-1);
% =============================================================
end


predict.m

function p = predict(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic 
%regression parameters theta
%   p = PREDICT(theta, X) computes the predictions for X using a 
%   threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)

m = size(X, 1); % Number of training examples

% You need to return the following variables correctly
p = zeros(m, 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned logistic regression parameters. 
%               You should set p to a vector of 0's and 1's
%
val = sigmoid(X*theta);
for i=1:m
   if val(i)>0.5
       p(i) = 1;
   else
       p(i) = 0;
   end

% =========================================================================


end

mapFeature.m

function out = mapFeature(X1, X2)
% MAPFEATURE Feature mapping function to polynomial features
%
%   MAPFEATURE(X1, X2) maps the two input features
%   to quadratic features used in the regularization exercise.
%
%   Returns a new feature array with more features, comprising of 
%   X1, X2, X1.^2, X2.^2, X1*X2, X1*X2.^2, etc..
%
%   Inputs X1, X2 must be the same size
%
degree = 6;
out = ones(size(X1(:,1)));
for i = 1:degree
    for j = 0:i
        out(:, end+1) = (X1.^(i-j)).*(X2.^j);
    end
end

end


costFunction.m

function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
%   J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
%   parameter for logistic regression and the gradient of the cost
%   w.r.t. to the parameters.

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly 
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%

h = sigmoid(X*theta);  
J = m^-1 * sum(((-1) * y.*log(h)).-((1- y).*log(1 - h)));  
grad = m^-1 * ((h.-y)'*X)'; 

% =============================================================

end


costFunctionReg.m

function [J, grad] = costFunctionReg(theta, X, y, lambda)
%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
%   J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
%   theta as the parameter for regularized logistic regression and the
%   gradient of the cost w.r.t. to the parameters. 

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly 
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta
h = sigmoid(X*theta);  
J = m^-1 * sum(((-1) * y.*log(h)).-((1- y).*log(1 - h)));
theta(1) = 0;
tmp = lambda/(2*m)*sum(theta.^2);
J = J + tmp;
grad = m^-1 * ((h.-y)'*X)' + lambda/m * theta; 

% =============================================================

end


plotData.m

% Create New Figure
figure; hold on;

% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
%               2D plot, using the option 'k+' for the positive
%               examples and 'ko' for the negative examples.
%
pos = find(y==1); neg = find(y == 0);
% Plot Examples
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...
'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...
'MarkerSize', 7);


我知道的大数据



      随着科技的发展,信息的收集也越来越容易,再加上摩尔定律,大的数据量处理也成为了可能。

什么是大数据,也许你有几千个人的基本信息、也许你有数百条购物记录,但这都不不是大数据,大数据至少在千万的数据量上。

     大数据有什么作用?其实数据中是包含各种规律的,互联网时代的数据以不在那么直观,再加上超大的数据量,人工已经很难从中找到规律或者关联了,但这并不意味着这些规律联系永远无法被发掘出来,事实上,计算机的飞速发展和机器学习数据挖掘相关理论的发展为挖掘其规律带来了可能,建立适当的模型,我们就可以发掘出其中一部分关联规律,但我相信并不是所有的,可能任何数据中都有其隐藏的深层规律。理论在逐步完善,科技也在飞速发展,我相信未来这些问题都会被一一解决的。

       也许很多人对大数据的作用没有直观的了解,举几个简单的例子。看过《大数据时代》的人可能都知道这个例子,如果你有超市所有的购物记录的话,你可能会发现,很多人在买尿布时都会买啤酒,你将这两者摆在一起,很容易提升其销量。另外,还有好多类似的例子,这里我再说一下推荐系统,如果你常在当当或者亚马逊买书的话,你会看到网站会推荐一些书,很多时候,你要买的下一本书就在其中,这并不是网站猜的准,其实是根据大量的数据进行分析得来的。比如1万个买了《机器学习》的用户中有1000个又买了《机器学习导论》,这要比买其它书的比例高的多,那么如果你买了《机器学习》,系统就会认为你非常有可能会买《机器学习导论》,这时候就把这本书推荐给你。
这只是根据用户购买行为进行分析的推荐方法,还有对商品类别进行分析的推荐方法,可能准确率会更高些。事实上,亚马逊有三分之一的订单来着于它的推荐系统。

       我感觉,大数据会变革非常多的行业,实际上已经变革了很多行业了。

       传统的交通行业首先应当被变革。你应该注意到街上的红绿灯了,每天每个时段间隔时间都是一样的,但事实上,每天不同手段过往的车流量并不是一样的,适当改变红绿间隔时间,也许会大大降低汽车尾气的排放量并节省能源。这并不是很难实现,只要在现有红绿灯上加装传感器统计不同时段的车流量即可。统计所有易发交通事故的地方,然后在那些易发地适当的做些措施,可能会挽救很多人的生命。听说智能交通已经有人在做了。 另外,大数据也可以用来做基础设施的选址的依据,可节约很多成本,甚至规划公交路线,这也不是不可能。

       现在人们所能收集的数据只占所有数据中非常小的一部分,能发掘的信息也是少部分,以后可能随着传感器的大量应用,我们会获取的前所未有的数据,这些数据将帮助我们过上更优的生活,甚至解决一些人类目前没有解决的问题,我感觉以后就是人工智能和大数据的天下了。

14年7月总结


       从搬完宿舍以来,过的一天不如一天,甚至每天早上6点钟自然醒的能力都开始慢慢丧失了,而且白天还比较嗜睡,一躺下没两个小时肯定不够。这两天也基本上没学什么东西,浪费了好多时间,感觉心里老是毛毛的,也有一丝的愧疚。就以这个状态,怎么去参加秋招。。。


       再回想前一段时间,复习操作系统那一周最充实了,不光只看了操作系统,还看了《饥饿游戏》三部曲,另外,开始看Andrew
Ng机器学习公开课,初次尝试使用 番茄土豆这个时间管理软件,每天45分钟的番茄至少有7个。虽然操作系统上课从来也没听过,但一周的复习还是很有效果的,感觉考挺好的,虽然只考了63分,不过考的轻松加愉快。

       考完操作系统,离开了我住了三年的宿舍,搬到9公寓暗无天日的地方,开始很不爽,首先搬宿舍那么累,再来环境比原来差多了。可后来想想,宿舍对我而言已经沦落到只是一个睡觉的地方了,何必在意呢!
况且,要老是为一些小事而影响到自己的心情,那我们活着还有什么乐趣。搬宿舍并不仅意味着坏处,任何事都有对立的一面,搬过来也离同级其他人比较近了。一直以来,与其说计算机学院把11级大部分人孤立了,倒不如说是11级大部分人把我们孤立了,搬过去倒结束了这格局。

      考完操作系统,以放松的心态,开始了这一段时间最浑浑噩噩的状态了,就是一种不正常的心态。再来就是操作系统课设了,找了一个很靠谱的搭档,果然省事的多,结对编程,两天搞定课设,报告就是他的事了,表示课设做的很是轻松愉快。

       三件事打乱了我原有的生活状态,搬宿舍、操作系统课设,再加上搬教室。课设让我本该自己学习的时间大多数都变成了和别人扯淡,搬教室让我没了网,严重影响到学习,以至于我机器学习的课程落下了一周,直到上周六才补交了coursera上week
2的编程练习(还是参考别人代码),这两天才在做week 1和week 2的review question,因为晚做,让我损失了20%的分数,说来这应该“得益于”我不定期的拖延。

      再来就是自学的有关机器学习的课程了。之前就买了《机器学习》《机器学习实战》两本书,也看过一部分Andrew Ng coursera上的视频,但收获甚微。一来就是自己没认真看认真记,二来就是没有任何练习。上个月《机器学习》又开课了,强迫自己跟着看,并把后面的习题做了。越来越觉得有些事,只要认真去做了,有时候很简单。对于我来说,学习机器学习的路还很漫长,需要脚踏实地一步步来。另外,我想,我也该尝试把刚学完的线性回归这一部分总结出来了,一来是看我的理解程度,二来希望对其他人有所帮助。


      这段时间也并不是整天无所事事,也看了几本书,其中不乏非常好的书。
     重读吴军的《数学之美》,如果加上浏览的一遍,这应该已经是第四遍了,又理解了不少的东西,再次加深了好书经得起多次读印象。再来就是
唐纳德的《研究之美》,大师的手笔,如果读懂想必收获颇丰
。可惜,一堆无聊了符号,加上难懂的推理定义,让我失去兴趣,只能硬着头皮看过一遍。还有一本比较难懂的《图灵的秘密》,其实开始还行,一些有趣的推论证明,比如读此书我知道了任何证明√2是无理数……,但后来,再次出现了对我来说晦涩难懂的定理推论什么的,至此此书被搁置一旁,然后才了解自己的数学有多弱。(一共搁置了三本书了《图灵的秘密》《时间简史》《暗淡蓝点:展望人类的太空家园》
      加上自己在学机器学习的课程,开始慢慢感觉到数学的重要性,程序只是实现工具,其本质还是数学。高中和大学的课程让我丧失了对数学的兴趣,所以我数学比较差,势必今后要花大量的时间精力去恶补数学。现在才体会到数学这东西,学再好都不为过。我觉得,对于计算机专业的学生来说,数学应该是必备的技能,可惜学校没让我们了解到数学在计算机学科中的重要性,更没培养我们对于数学的兴趣。所以我在这推荐看一下吴军的《数学之美》。
      除了这两本难懂的书外,也看了两本小说。 《饥饿游戏》,这本书给我的现实意义就是让我决心放弃唯一看的一类型的电影——科幻片。加上之前看了《云图》及电影,《安德的游戏》及电影。对比电影和小说,电影删减了好多东西,甚至修改了原剧情,表示开始有点反感小说改编电影了。不过倒是可以理解,那么长的小说压缩两个小时,不改改也不大可能。至此,不再看电视剧,不再看综艺节目,不再看电影,不再看动漫。
        一本《达芬奇密码》,差点一改我对基督教的看法,果然一家之言不可信,任何事都得有理有据才能相信,艺术是艺术,现实是现实,如果有人在看这本书,最好区分小说和现实。
       《岛》,我感觉是这两个月来看过最好的一本小说了,有非常吸引人的情节,与《达芬奇密码》单纯给人的好奇感相比,《岛》给我更人性化的吸引力。

        今天看完了《生命中不能承受只轻》,其实书名吸引我看了这本书,我就想知道生命中不能承受之轻是什么,不过我没找到答案。豆瓣书评说,米兰•昆德拉的书不是一遍就能懂的,但我没打算最近在看一遍该书。其他人的感悟,生命中总会有挫折、困难、压力压着我们,倒让我们更现实,如果有一天,这些都不见了,生命却给你一种飘飘然的感觉,却也变得无趣了。压力、挫折、困难就是生命之重,而解脱是生命之轻,这种轻却常让我们难以承受。

       关于我看过的更多的书,请戳这 here

      其实动手写之前,感觉没什么要写的,写完了才觉得自己什么都没写,加上去年对区预赛的总结,和对13年的总结,这只是我的第三篇总结。看过一些博客,才觉得自己应该慢慢学着去总结,一点点的积累进步,最终都会有成果的,刘未鹏写了8年博客,至少每月一篇,将所有博客一总结,就是《暗时间》了。



Stanford 机器学习练习 Part 1 Linear Regression



warmUpExercise.m

function A = warmUpExercise()
%WARMUPEXERCISE Example function in octave
%   A = WARMUPEXERCISE() is an example function that returns the 5x5 identity matrix
    A = [];

% ============= YOUR CODE HERE ==============
% Instructions: Return the 5x5 identity matrix 
%               In octave, we return values by defining which variables
%               represent the return values (at the top of the file)
%               and then set them accordingly. 
    A = eye(5);
% ===========================================
end

computeCost.m

function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
%   J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
%   parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly 
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
%               You should set J to the cost.
J = sum((X*theta - y).^2) / (2*m);
% =========================================================================
end

plotData.m

function plotData(x, y)
%PLOTDATA Plots the data points x and y into a new figure 
%   PLOTDATA(x,y) plots the data points and gives the figure axes labels of
%   population and profit.
% ====================== YOUR CODE HERE ======================
% Instructions: Plot the training data into a figure using the 
%               "figure" and "plot" commands. Set the axes labels using
%               the "xlabel" and "ylabel" commands. Assume the 
%               population and revenue data have been passed in
%               as the x and y arguments of this function.
%
% Hint: You can use the 'rx' option with plot to have the markers
%       appear as red crosses. Furthermore, you can make the
%       markers larger by using plot(..., 'rx', 'MarkerSize', 10);

figure; % open a new figure window
plot(x, y, 'rx', 'MarkerSize', 5);
xlabel("x");
ylabel("y");
% ============================================================
end

gradientDescent.m

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
%   theta = GRADIENTDESENT(X, y, theta, alpha, num_iters) updates theta by 
%   taking num_iters gradient steps with learning rate alpha
% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);

for iter = 1:num_iters
    % ====================== YOUR CODE HERE ======================
    % Instructions: Perform a single gradient step on the parameter vector
    %               theta. 
    %
    % Hint: While debugging, it can be useful to print out the values
    %       of the cost function (computeCost) and gradient here.
    %
    temp = 0;
    temp = temp + alpha/m * X' * (y - X * theta);
    theta = theta + temp;
    % ============================================================
    % Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);
end
end

ex1.m

%% Machine Learning Online Class - Exercise 1: Linear Regression

%  Instructions
%  ------------
% 
%  This file contains code that helps you get started on the
%  linear exercise. You will need to complete the following functions 
%  in this exericse:
%
%     warmUpExercise.m
%     plotData.m
%     gradientDescent.m
%     computeCost.m
%     gradientDescentMulti.m
%     computeCostMulti.m
%     featureNormalize.m
%     normalEqn.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%
% x refers to the population size in 10,000s
% y refers to the profit in $10,000s
%

%% Initialization
clear ; close all; clc

%% ==================== Part 1: Basic Function ====================
% Complete warmUpExercise.m 
fprintf('Running warmUpExercise ... \n');
fprintf('5x5 Identity Matrix: \n');
warmUpExercise()

fprintf('Program paused. Press enter to continue.\n');
pause;


%% ======================= Part 2: Plotting =======================
fprintf('Plotting Data ...\n')
data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples

% Plot Data
% Note: You have to complete the code in plotData.m
plotData(X, y);

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =================== Part 3: Gradient descent ===================
fprintf('Running Gradient Descent ...\n')

X = [ones(m, 1), data(:,1)]; % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters

% Some gradient descent settings
iterations = 1500;
alpha = 0.01;

% compute and display initial cost
computeCost(X, y, theta)

% run gradient descent
theta = gradientDescent(X, y, theta, alpha, iterations);

% print theta to screen
fprintf('Theta found by gradient descent: ');
fprintf('%f %f \n', theta(1), theta(2));

% Plot the linear fit
hold on; % keep previous plot visible
plot(X(:,2), X*theta, '-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure

% Predict values for population sizes of 35,000 and 70,000
predict1 = [1, 3.5] *theta;
fprintf('For population = 35,000, we predict a profit of %f\n',...
    predict1*10000);
predict2 = [1, 7] * theta;
fprintf('For population = 70,000, we predict a profit of %f\n',...
    predict2*10000);

fprintf('Program paused. Press enter to continue.\n');
pause;

%% ============= Part 4: Visualizing J(theta_0, theta_1) =============
fprintf('Visualizing J(theta_0, theta_1) ...\n')

% Grid over which we will calculate J
theta0_vals = linspace(-10, 10, 100);
theta1_vals = linspace(-1, 4, 100);

% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));

% Fill out J_vals
for i = 1:length(theta0_vals)
    for j = 1:length(theta1_vals)
      t = [theta0_vals(i); theta1_vals(j)];    
      J_vals(i,j) = computeCost(X, y, t);
    end
end


% Because of the way meshgrids work in the surf command, we need to 
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');

% Contour plot
figure;
% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))
xlabel('\theta_0'); ylabel('\theta_1');
hold on;
plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);


featureNormalize.m

function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X 
%   FEATURENORMALIZE(X) returns a normalized version of X where
%   the mean value of each feature is 0 and the standard deviation
%   is 1. This is often a good preprocessing step to do when
%   working with learning algorithms.

% You need to set these values correctly
X_norm = X;
m = size(X, 2);
mu = zeros(1, size(X, 2));
mu = mean(X);
sigma = std(X);
for i = 1:m
    X_norm(:,i) = (X(:,i).-mu(i))./sigma(i);
end
% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
%               of the feature and subtract it from the dataset,
%               storing the mean value in mu. Next, compute the 
%               standard deviation of each feature and divide
%               each feature by it's standard deviation, storing
%               the standard deviation in sigma. 
%
%               Note that X is a matrix where each column is a 
%               feature and each row is an example. You need 
%               to perform the normalization separately for 
%               each feature. 
%
% Hint: You might find the 'mean' and 'std' functions useful.
%       









% ============================================================

end

computeCostMulti.m

function J = computeCostMulti(X, y, theta)
%COMPUTECOSTMULTI Compute cost for linear regression with multiple variables
%   J = COMPUTECOSTMULTI(X, y, theta) computes the cost of using theta as the
%   parameter for linear regression to fit the data points in X and y
% Initialize some useful values
m = length(y); % number of training examples
% You need to return the following variables correctly 
J = 0;
% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
%               You should set J to the cost.
J = 1/(2*m) * ( X * theta - y)' * (X*theta - y);
% =========================================================================
end


gradientDescentMulti.m

function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
%   theta = GRADIENTDESCENTMULTI(x, y, theta, alpha, num_iters) updates theta by
%   taking num_iters gradient steps with learning rate alpha

% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);

temp = zeros(feature_number,1); 

for iter = 1:num_iters
    temp = alpha/m * X' * (y - X*theta);
    theta = theta + temp;
    J_history(iter) = computeCostMulti(X, y, theta); 
    % ====================== YOUR CODE HERE ======================
    % Instructions: Perform a single gradient step on the parameter vector
    %               theta. 
    %
    % Hint: While debugging, it can be useful to print out the values
    %       of the cost function (computeCostMulti) and gradient here.
    %
    % ============================================================
    % Save the cost J in every iteration    
    J_history(iter) = computeCostMulti(X, y, theta);

end

end

normalEqn.m

function [theta] = normalEqn(X, y)
%NORMALEQN Computes the closed-form solution to linear regression 
%   NORMALEQN(X,y) computes the closed-form solution to linear 
%   regression using the normal equations.

theta = zeros(size(X, 2), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the code to compute the closed form solution
%               to linear regression and put the result in theta.
%
% ---------------------- Sample Solution ----------------------
theta = pinv(X' * X) * X' * y;
% -------------------------------------------------------------
% ============================================================

end

ex1_multi.m

%% Machine Learning Online Class
%  Exercise 1: Linear regression with multiple variables
%
%  Instructions
%  ------------
% 
%  This file contains code that helps you get started on the
%  linear regression exercise. 
%
%  You will need to complete the following functions in this 
%  exericse:
%
%     warmUpExercise.m
%     plotData.m
%     gradientDescent.m
%     computeCost.m
%     gradientDescentMulti.m
%     computeCostMulti.m
%     featureNormalize.m
%     normalEqn.m
%
%  For this part of the exercise, you will need to change some
%  parts of the code below for various experiments (e.g., changing
%  learning rates).
%

%% Initialization

%% ================ Part 1: Feature Normalization ================

%% Clear and Close Figures
clear ; close all; clc

fprintf('Loading data ...\n');

%% Load Data
data = load('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);

% Print out some data points
fprintf('First 10 examples from the dataset: \n');
fprintf(' x = [%.0f %.0f], y = %.0f \n', [X(1:10,:) y(1:10,:)]');

fprintf('Program paused. Press enter to continue.\n');
pause;

% Scale features and set them to zero mean
fprintf('Normalizing Features ...\n');

[X mu sigma] = featureNormalize(X);

% Add intercept term to X
X = [ones(m, 1) X];


%% ================ Part 2: Gradient Descent ================

% ====================== YOUR CODE HERE ======================
% Instructions: We have provided you with the following starter
%               code that runs gradient descent with a particular
%               learning rate (alpha). 
%
%               Your task is to first make sure that your functions - 
%               computeCost and gradientDescent already work with 
%               this starter code and support multiple variables.
%
%               After that, try running gradient descent with 
%               different values of alpha and see which one gives
%               you the best result.
%
%               Finally, you should complete the code at the end
%               to predict the price of a 1650 sq-ft, 3 br house.
%
% Hint: By using the 'hold on' command, you can plot multiple
%       graphs on the same figure.
%
% Hint: At prediction, make sure you do the same feature normalization.
%

fprintf('Running gradient descent ...\n');

% Choose some alpha value
alpha = 0.01;
num_iters = 400;

% Init Theta and Run Gradient Descent 
theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);

% Plot the convergence graph
figure;
plot(1:numel(J_history), J_history, '-b', 'LineWidth', 2);
xlabel('Number of iterations');
ylabel('Cost J');

% Display gradient descent's result
fprintf('Theta computed from gradient descent: \n');
fprintf(' %f \n', theta);
fprintf('\n');

% Estimate the price of a 1650 sq-ft, 3 br house
% ====================== YOUR CODE HERE ======================
% Recall that the first column of X is all-ones. Thus, it does
% not need to be normalized.
price = 0; % You should change this


% ============================================================

fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
         '(using gradient descent):\n $%f\n'], price);

fprintf('Program paused. Press enter to continue.\n');
pause;

%% ================ Part 3: Normal Equations ================

fprintf('Solving with normal equations...\n');

% ====================== YOUR CODE HERE ======================
% Instructions: The following code computes the closed form 
%               solution for linear regression using the normal
%               equations. You should complete the code in 
%               normalEqn.m
%
%               After doing so, you should complete this code 
%               to predict the price of a 1650 sq-ft, 3 br house.
%

%% Load Data
data = csvread('ex1data2.txt');
X = data(:, 1:2);
y = data(:, 3);
m = length(y);

% Add intercept term to X
X = [ones(m, 1) X];

% Calculate the parameters from the normal equation
theta = normalEqn(X, y);

% Display normal equation's result
fprintf('Theta computed from the normal equations: \n');
fprintf(' %f \n', theta);
fprintf('\n');


% Estimate the price of a 1650 sq-ft, 3 br house
% ====================== YOUR CODE HERE ======================
price = 0; % You should change this


% ============================================================

fprintf(['Predicted price of a 1650 sq-ft, 3 br house ' ...
         '(using normal equations):\n $%f\n'], price);