# Predicting the Chance of Admission of a Student Based on His/her GRE and TOEFL Scores.

## Synopsis

The objective of this work is to predict the chance of admission of a student based on the student GRE and TOEFL scores. We will start by first trying to predict admission chance based on the student GRE score only and then proceed by considering both scores all together.

We will be using the “Admission_Predict_Ver1.1” data set that was gotten from Kaggle. We use MATLAB to build our machine learning algorithm (linear regression). The work focuses on:

1. Plot a scatter plot of the data set
2. compute and display initial cost
3. testing of the cost function
5. Plot the linear fit
6. Predict admission chance for a GRE score of of say 316 and 340
7. visualize the cost function

X = dat(:, 1); y = dat(:, 2);
m = length(y);

## Plot the Data

plot(X, y);
plot(X, y, ‘rx’, ‘MarkerSize’, 10);
xlabel(‘GRE score in 1000s’);
fprintf(‘Program paused. Press enter to continue.\n’);
pause;

## Compute and display initial cost

iterations = 25000;
alpha = 1.0;

J = computeCost1(X, y, theta);
fprintf(‘With theta = [0 ; 0]\nCost computed = %f\n’, J);
J = computeCost1(X, y, [-1 ; 2]);
fprintf(‘\nWith theta = [-1 ; 2]\nCost computed = %f\n’, J);

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

Output:

With theta = [0 ; 0]
Cost computed = 0.269432
Expected chance of admission(approx) is 0.26

With theta = [-1 ; 2]
Cost computed = 0.598547
Expected chance of admission(approx) is 0.59

theta = gradientDescent1(X, y, theta, alpha, iterations);
fprintf(‘%f\n’, theta);
fprintf(‘Expected theta values (approx)\n’);
fprintf(‘ -2.3874\n 9.8184\n\n’);

Output:

-2.387424
9.818487
Expected theta values (approx)
-2.3874
9.8184

## Plot the linear fit

hold on;
plot(X(:,2), X*theta, ‘-‘)
legend(‘Training data’, ‘Linear regression’)
hold off

predict1 = [1, 0.316] *theta;
fprintf(‘For gre = 316, we predict an admission chance of %f\n’,…
predict1);
predict2 = [1, 0.340] * theta;
fprintf(‘For gre = 340, we predict an admission chance of %f\n’,…
predict2);

Output:

For gre = 316, we predict an admission chance of 0.715218
For gre = 340, we predict an admission chance of 0.950861

So a student with a gre score of 316 will have a 71.5% chance of being admitted and a student with a gre score of 340 will have a 95.08% chance of being admitted .

## Visualizing the cost function

theta0_vals = linspace(-10, 10, 100);
theta1_vals = linspace(-1, 4, 100);
J_vals = zeros(length(theta0_vals), length(theta1_vals));
for i = 1:length(theta0_vals)
for j = 1:length(theta1_vals)
t = [theta0_vals(i); theta1_vals(j)];
J_vals(i,j) = computeCost1(X, y, t);
end
end

Output:

## Considering both GRE and TOEFL For the Prediction.

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,:)]’);

## Scale features

fprintf(‘Normalizing Features …\n’);

[X mu sigma] = featureNormalize1(X);
X = [ones(m, 1) X];

% Choose some alpha value
alpha1 = 0.0001;
alpha2 = 0.001;
alpha3 = 0.015;
alpha4 = 0.01;
num_iters = 1000;

% Init Theta and Run Gradient Descent
theta = zeros(3, 1);

[theta, J1]= gradientDescentMulti1(X, y, theta, alpha1, num_iters);

[theta, J2]= gradientDescentMulti1(X, y, theta, alpha2, num_iters);

[theta, J3] = gradientDescentMulti1(X, y, theta, alpha3, num_iters);

[theta, J4]= gradientDescentMulti1(X, y, theta, alpha4, num_iters);

% Plot the convergence graph
figure;
subplot(2,2,1);
plot(J1, ‘b’)
xlabel(‘Number of iterations’);
ylabel(‘Cost J’);
title(‘convergence graph for alpha = 0.0001’)
subplot(2,2,2);
plot(J2, ‘r’)
xlabel(‘Number of iterations’);
ylabel(‘Cost J’);
title(‘convergence graph for alpha = 0.001’)
subplot(2,2,3);
plot(J3, ‘k’)
xlabel(‘Number of iterations’);
ylabel(‘Cost J’);
title(‘convergence graph for alpha = 0.015’)
subplot(2,2,4);
plot(J4, ‘g’)

xlabel(‘Number of iterations’);
ylabel(‘Cost J’);
title(‘convergence graph for alpha = 0.01’)

fprintf(‘Theta computed from gradient descent: \n’);
fprintf(‘ %f \n’, theta);
fprintf(‘\n’);

Output:

## Estimate the chance of admission of a student with a gre = 300 and toefl = 99 Using Gradient Descent

admitchance = sum(X*theta)/100000; % You should change this

fprintf([‘chance of admission of a student with a gre score= 300 and toefl score = 99’ …

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

Output:

72.174000
6.921442
5.457525

chance of admission of a student with a gre score= 300 and toefl score = 99(using gradient descent):
0.360870

## Estimate the chance of admission of a student with a gre = 300 and toefl = 99 Using Normal Equations

X = data(:, 1:2);
y = data(:, 3);
m = length(y);
X = [ones(m, 1) X];
theta = normalEqn1(X, y);
fprintf(‘Theta computed from the normal equations: \n’);
fprintf(‘ %f \n’, theta);
fprintf(‘\n’);

% Estimate the chance of admission of a student with a gre score = 300 and toefl score = 99

admitchance = sum(X*theta)/100000; % You should change this

fprintf([‘chance of admission of a student with a gre = 300 and toefl = 99’ …

Output:

Theta computed from the normal equations:
-218.026755
0.613504
0.896000

chance of admission of a student with a gre = 300 and toefl = 99(using normal equations):
0.360870

So a student with a gre score of 300 and toefl score of 99 will have a 36.08% chance of being admitted.

# Deciding What to Try Next For Your ML System.

Suppose you are developing a ML system or trying to improve the performance of your ML system. One very important step is deciding what are the promising avenues to try next.

To explain this, suppose you are using linear regression to predict Diamond prices for example and let’s say you have implemented regularized linear regression. However, suppose that when you test your hypothesis on a new set of Diamond, you find out that it makes unacceptably large errors in its prediction. The question is what should you then try next in order to improve the learning algorithm.

Some of the things to think of could be to:

1. Try and get more training examples (even though it doesn’t usually work)
2. Try a smaller set of features (i.e., carefully selecting a small subset of them to prevent over fitting)
3. Try and get additional features (i.e., collecting more data to get more features)
5. Try other things like increasing or decreasing the regularization parameter.

The above list contains plausible things to think of in such a situation, but most of the time what people do is to base their judgment on personal intuition (i.e., they might decide to to get more training data or get smaller sets of features or decrease the regularization parameter etc) on a random setting which might be very time consuming.

Fortunately, there is a pretty simple technique that can help rule out some (half) of the things in the above list as being potentially promising things to pursue (use) which will help eliminate things on the above list that will not help. This is achieved through a ML diagnostic. A diagnostic is a test that you can run to gain insight what is or isn’t working with a learning algorithm and gain guidance as to how best to improve its performance. However, diagnostic can take time to implement, but doing so can be very good use of your time. Below are some of the diagnostics to consider in order to make a decision.

Evaluating a hypothesis:  You want to evaluate a hypothesis that has been learned from your algorithm. When you fit the parameters of your learning algorithm, you think about choosing the parameters that minimize the training error. One might think that getting a low value of training error might be a good think but just the fact that the hypothesis has low training error doesn’t mean it is necessarily a good hypothesis because it might be over fitting the data and therefore fails to generalize the new examples that are not in the training set. So how do you tell if the hypothesis might be over fitting? In problems with one feature, we could plot the hypothesis function to see what is happening but in general, for problems involving more features it becomes hard to plot what the hypothesis function looks like and so we need some other ways to evaluate the hypothesis. The standard way to evaluate a learned hypothesis is to split the data into two portions; a training set and a test set in the ratio 7:3.

A typical training/testing procedure of your learning algorithm will consist of:

1. learn the parameters from the training data.
2. then use the learned parameters to compute the test set error. Of course your definition of the test set error will depend on your learning algorithm. Suppose for example that the implementation of linear regression (without regularization) is badly over fitting the training set. In this case, we would expect the training error to be low and the test set error to be high.

Model selection and training/validation/test sets:     Suppose you’ll like to decide what degree of polynomial to fit to a data set, what features to include to give you a learning algorithm or suppose you’ll like to choose the regularization parameter of the learning algorithm. These are called model selection problems and how to go about this is very important. As mentioned earlier, just because a learning algorithm fits a training set well doesn’t mean it is a good hypothesis. More generally, this is why the training set error is not a good predictor of how well the hypothesis will do (generalize) on new examples not seen in the training set. Coming back to the model selection problem, let’s say you try to choose what degree polynomial to fit on your data. You could choose a linear function, quadratic function, cubic function all the way to a 10th degree polynomial and get some estimates of how well your fitted hypothesis will generalize to new examples. One thing you could do is:

1. first pick a polynomial of degree one (i.e., linear regression) and minimize the training error to get the corresponding parameter vector.
2. take a second model (i.e., a quadratic function) and fit that to your training set and get the corresponding parameter vector.
3. continue this way up to say a 10th parameter model and get their parameter vectors.
4. A thing to try next will be to take these parameters and look at their respective test set errors. That is take each parameter vector and measure their performance on the test set.

One thing to do next in order to select one of these models will be to see which of the models has the lowest test set error. Let say for example that one ended up choosing the fifth order polynomial. But how well does this model generalizes is the question we need to ask. One thing to do is to look at how well this model has done on the test set. But the problem with this is that this will not be a fair estimate of how well the hypothesis generalizes. The reason is that what we have done is to fit an extra parameter using the test set. That is the degree of the polynomial was chosen such that it gave the best possible performance on the test set and so the performance of the parameter vector corresponding to this model on the test set is likely going to be an overly optimistic estimate of our generalization error. So because we have fitted these parameter values on the test set, it is no longer fair to evaluate the hypothesis on the test set since the hypothesis is likely to do better on the test set than it would on new examples that it hasn’t seen before.

To address this problem in a model selection setting, we split the data set into 3 sets namely; training set, cross validation set (validation set) and the test set in the ratio 6:2:2 although it can vary. Next we define the train/validation/test errors. So instead of using the test set to select the model, we now use the validation set to select the model. So at this point, we carry out steps 1 to 4 and instead test the hypothesis on the validation set and then pick the hypothesis with the lowest cross validation error. Let say this time around that it is the polynomial of degree 4 that has the lowest validation error. What we’ve done is that we have fitted the parameter (degree of polynomial) and so it is no longer fitted to the test set thus saving apart the test set to measure (estimate) the generalization error of the model that was selected by this algorithm. In this case, we might generally expect the validation error to be lower than the test error because an extra parameter has been fitted to the validation set.

Diagnosing bias vs variance:   If you run a learning algorithm and it doesn’t do well as you’re hoping almost all the time then it could be because you either have a high bias problem or a high variance problem. And in this case, it is very important to figure out which of these problems is affecting the model. Knowing which of these problems is affecting the model will give us a clue on the promising ways of solving the problem. Recall the training and validation (test) errors mentioned above. A graph of these errors against the degree of the polynomial will provide a good way of accessing these problems. The plot of the test error will have some sought (not really) of a parabola shape and that of the train error will be decreasing as the degree of the polynomial increases. So for a polynomial of degree one, the training error will be too high whereas for a polynomial of say degree 10 the training error will be low. On the contrary, for a polynomial of degree one, the validation error will be high but for an intermediate degree (say 2) the validation error will also be intermediate (i.e., decrease) and when the degree is high (say 10), the validation error instead becomes very high. This curves give us a clue on how to distinguish whether the algorithm is suffering from a high variance or a high bias problem. Concretely, for the high bias case, we will realize that the validation and train errors are going to be high (where the validation error might slightly be higher than the training error). In contrast, for the high variance case, we will realize that the training error is going to be low (that is the algorithm is fitting the training set too well) whereas the validation error will be high.

Regularization and the variance/bias problem: Let now see how variance/bias is affected by regularization. Suppose you’re fitting a high order(degree) polynomial of say order 4. In order to prevent over fitting we use regularization. This is a modification of the hypothesis function such that it includes a regularization term with a regularization parameter to penalize high order parameters. The penalty on the parameters depends on the value of the regularization parameter. However, care should be take in choosing the value of the regularization parameter. If the regularization parameter is say 1000, then most of the parameters will be highly penalized rendering them approximately zero and the hypothesis function will become the standard hypothesis function(i.e., that of linear regression). On the other extreme, if we choose a small value of the regularization parameter (say approximately zero) then given that we are fitting a high order polynomial, this will just be the usual over fitting problem since little or no regularization was made. But an intermediate value of the regularization parameter will give a reasonable fit through the data.

So the question of how to automatically choose a good value for the regularization parameter is a central one. So how to choose this value can be achieved by considering a range of values say 0.01, 0.02,…, 10 that one might want to try. Based on the number of values that one might want to consider, one will end up with a corresponding models that you want to select among. For all the corresponding models, what one can do is the following:

1. for 0.01, minimize the hypothesis function to obtain the corresponding parameter.
2. for the second model with 0.02, again minimize the hypothesis function to get the corresponding parameter.
3. continue this way up to the last model with 10.
4. next we take all these parameters and use the validation set to evaluate them to pick which ever of these models has the lowest error on this set.
5. get the parameter corresponding to this model and look at how it does on the test set.
6. lastly, it is useful to see how the validation error and the training error vary as we vary the regularization parameter. To do this, a graph of the training and validation errors should be plotted against the regularization parameters. What we find is that for small values of the regularization parameter, we can fit the training set relatively well because no regularization is actually done. Thus you are just minimizing the square errors. So for small regularization values, we end with a small value of the train error whereas if the regularization value is large then we have a high bias problem. For the validation error, we may end up under fitting for large regularization values and if we have too small value then we may be over fitting. Once again, there would always be some intermediate regularization value (optimal value) that works just best in terms of having a small validation error or small test error.

Learning curve:    Learning curve (LC) is a diagnostic tool used to figure out if a learning algorithm may be suffering a bias problem or a variance problem or both. LCs are often a very useful thing to plot if either you want to sanity check that your algorithm is working correctly of if you want to improve the performance of the algorithm. To plot LCs, what we usually do is to plot the train error and the validation error without regularization as a function of the number of training examples.

We have seen above what we should do in selecting a good model but the central question from the beginning is “what next?” Recall the list we had earlier for the potential things to do. Now lets give situations under which we could adopt each points on that list:

1. Get more training examples when trying to fix high variance (i.e., if the validation error is greater than the training error.)
2. Try a smaller set of features when trying to fix high variance . If you have high bias then don’t waste time trying to select features.
3. Try getting additional features (not always) when tying to fix high bias problem.
4. Try adding polynomial features when trying to fix high bias problem.
5. Try increasing the regularization parameter to fix high bias problem.
6. Try decreasing the regularization parameter to fix high variance problem.

# The Journey Begins

Thanks for joining me!

Good company in a journey makes the way seem shorter. — Izaak Walton 