A blog for computer science passionates.

Thursday, 11 June 2020

Hello Friends,
Welcome to passionforcs!!!

In previous article, we have seen what is cost function? if you haven't checked please find to get the detail about the cost function and make it clear.

In this article, we are going to learn formula of cost function. How to calculate cost function?

Let's briefly revise once what is cost function? cost function is the function to check whether the model works well or not. To check whether the model performs well or not we have hypothesis, i.e. our prediction. Now the question is How to get hypothesis?


Now, you can get your prediction through the above equation. But here the question is what is theta (ϴ)?

So the answer is, ϴ is an additional parameter.
Now we have one more question, i.e. why? what is the need of the additional parameter?
Let me give you an example so you can get easily,

Consider our previous example, stock price prediction now we just analyze the stock or share price and make our prediction. We can set an input feature vector or simply consider one input feature only here, the input is opening price, closing price, high and low price etc. etc. but additional parameter that we need in this example is our money i.e. how much amount you want to invest? through which we can get how much profit or loss I will have? this investment is additional parameter we can say because it depends on us i.e. investor.

NOTE: Human brain predict like the above example.

Now, let's consider the formula of cost function.
Cost function for linear regression

the above formula is the formula of cost function of linear regression. This is also known as squared error cost function.
Here, 1/2 * m -  m is number of samples in data or training set. 1/2 is used because when we take derivative of cost function to update the parameter during gradient descent at that time square (2 in the power) get cancelled with 1/2 multiplier thus derivation is cleaner.

Sigma (𝚺) - represents the sum.

hϴ(x(i)) - hypothesis prediction of input x at ith index. (hypothesis value of h(x) if we use single value)

y(i) - the actual label value for the input x at ith index.

Just subtract actual value i.e. y from hypothesis prediction (predicted value) i.e. h(x) and square the achieved result.

This is just the calculation of cost function with MSE i.e. Mean Squared Error. We will learn more with example.

Till then try to get some cost by putting some values of x and theta.

Stay Safe Anywhere. :)

Wednesday, 27 May 2020

Hello Friends!!!
Welcome to passionforcs,

Once upon a time I played a game especially puzzle and I tried to solve it. And of course, I solved :) (Kidding) but I solved, and my friend asked me how much time did you spent to solve this puzzle? And I got stuck & went in deep thinking of Machine Learning.

When we train our model, at that time also we should think about how our model performs? we can do this by using cost function to check whether our model performs well or not. Our hypothesis is valid and true so that the model gets the appropriate prediction or not.
In simple words the main usage of the cost function is after getting the prediction, how far or close our predicted values are from the actual values (i.e. label y).

Let's first prove these words by using the simple example.

Suppose our model predict a price of a share A, So the price of the share A for today is around 410 this is the prediction and the actual value of the share A is 415 so the difference between the actual value and predicted value is 5, but if our predicted value is 450 and actual price is 400 of the share A then the difference between these two is 50.

When the difference is 5 we can say our model works fine for the share A, but when the difference is too high such as 50, our model doesn't predict well.

This is the cost function. When we build a model, we have to check the cost function to find whether our model performs well or not. A cost of a good model should be minimum.
Difference between predicted and actual value of Share Price
NOTE: THIS FIGURE IS NOT PERFECT, THIS IS JUST AN EXAMPLE TO SHOW THE ACTUAL CONCEPT.

So, through the above figure, we can get that blue dot is actual value, red square is predicted values and dark blue triangle is the difference but if difference is too big, the model doesn't predict well.

Hope you enjoy this article, Stay Home, Stay Safe.


Tuesday, 19 May 2020

Hello Freinds!!!
Welcome to passionforcs,

In this article, we are going to learn one more flow breaking (jumping) statement i.e. continue. continue statement is another flow breaking statement.
continue statement skips an iteration, when continue occurs control directly transfers to the condition for the next iteration.
How Continue Statement Works?
In the above example, we can see that when the value of i divides by 2 and gets the reminder 0, continues the loop i.e. skips that iteration otherwise it prints the value of i. when the value of i equals 6 the loop terminates and at last it prints Bye...Bye...

We have one more form of continue statement i.e. continue with label. same as break with label. For example,


In the above example the values of i and j multiplied only for odd numbers when for even numbers it skips the iteration. Try these examples to get more clarity. Happy Coding... :)

Tuesday, 5 May 2020

Hello Friends!!!

Welcome to passionforcs,
In this article, we are going to learn flow breaking statements. In Java, there are three different flow breaking statements such as, break, continue, and return.

Lets' understand the break statement is used to break the entire code block. We use break statement with switch statements to break the case statement. Even we can use the break statement to break the loop and we can use a break like goto (in C programming). Let's understand more about the different types of a break statement.

break with a switch statement that we have seen in one of the previous articles.

The second form of break statement is break the looping statements. The break statement is used to break the loop, it immediate terminates the loop and control moves to the next statement.
Syntax,
break
For example,

The third form of a break statement is to use the break statement like goto statement in C programming. Java does not support the goto statement but it works the way goto statement works.
Syntax,
break label;
Here label is any valid label name.

For example,
Refer the above code and you can get that break label works when condition returns true. i.e. when condition is true, break statement break the labeled statement and control goes to the next statement.
Try these examples and let me know whether you get the break statement or not. Till then Happy Coding...:) In the next article we will learn about continue the next jumping statement.

Saturday, 2 May 2020

Hello friends!!!
Welcome to passionforcs,

In this article, we are going to discuss different flavors of for loop, in the previous article we have already seen that what is looping and how many looping statements are there? and basic syntax of looping statements but we can use for loop differently and we can play with the same syntax of for loop let's understand How?

As we know the main syntax of for loop is something like,
for(init;condition;increment/decrement)
{
   //code block
}

If we want to make for loop infinite we can use the syntax below
for(;;)
{
    //code block
}

If we have a counter variable and is already initialized we can use the syntax below
for(;condition;increment/decrement)
{
    //code block
}

If we want to use for loop like while loop we can use the below syntax
for(;condition;)
{
    //code block
    //increment/decrement
}
when we use the above syntax the counter variable should have to be initialized and we have to add increment or decrement inside the loop

So, we can use for loop with different syntax. try to perform different program with these different syntaxes, Happy Coding... :)

Monday, 20 April 2020

Hello Friends!!!
Welcome to passionforcs,

In this article, we are going to learn Iterative statements in Java. Iterative statements are generally known as looping statements. Looping statements are used to create a chain to perform the same task. such as if we want to print PassionForCS for five times, we can print the same sentence simply by using System.out.println() for five times or use iterative statements.

Basically, there are three different looping statements:
  • for
  • while
  • do-while
Here for and while both statements are entry control loop and do-while loop is exit control loop.

What is the entry control loop?

The entry control loop means when the control enters into the loop it checks for the condition whether the condition is true or false if the condition is true the control enters into the loop.

What is the exit control loop?

The exit control loop means when the control exits from the loop it checks for the condition whether the condition is true or false if the condition is true the control enters into the loop second time. while working with the exit control loop, it executes once without checking the condition.

Let's explore more, how do entry control and exit control loop works?
Flowchart of Entry Control & Exit Control Loop

Now we are going to learn for, while & do-while loop with syntax:

for loop: Generally, this loop is used when we know about the number of iterations. In this loop we have initialization
i.e. in a single statement.
Syntax:
for(initialization;condition;increment/decrement operation){
    //code block
}

Example,

while loop: Generally, this loop is used when we are unaware with the number of iterations. In while loop we have to initialize a variable before it checks for the condition in while loop and increment/decrement operation is done in side the loop.
Syntax:
while(condition){
    //code block
    //increment/decrement operation
}

Example,


Here I use the same example to give you an illustration that how for and while loop is different and how do their syntax work?

do-while loop: this loop is an exit control loop so when we don't know the number of iterations but we want to execute the loop at least once at that time we can use do-while loop. when we are working with do-while loop we have to initialize a variable before it is used and then it increase or decrease its' value and then checks for the condition. If the condition is true loop will be executed.
Syntax:
do{
    //code block
    //increment/decrement operation
}while(condition);

Example,

One more looping statement is there in Java programming is known as for-each loop and it especially designed and work for arrays and collections. It is introduced after the 5th version of Java (J2SE 5.0).
Syntax:
for(<dataType> varNm : <Array>|<Any Collection>)
{
    //code block
}

Example,

You can see here in the syntax we use only for in this loop, we can say that it is an extended version of for loop. in this loop there is no need for increment/decrement operations. this loop iterates for each element of collection or an array.
We will explore more examples of different looping statements.. till then that's it and in this pandemic,  learn new things but Stay Home, Stay Safe and Be Happy. :)




Tuesday, 7 April 2020

Hello Friends!!!
Welcome to passionforcs,

Today I am going to share decision making statements :)
Make Firm Decision & Draw Your Path
Image Src: pixabay

as you are already familiar with decision making. So, let's start with the decision making statements.
There are different decision making statements available in Java such as,

Statement

Usage

Syntax  

How to use? Example

simple if

To make a simple decision on the basis of single condition. if the condition is true, it returns a result otherwise it doesnot return anything.

 if(condition){
    //code block
}

int age=18;
if(age>=18){
    System.out.print("Eligible for vote");
}

if-else    

Used to make two way decision i.e.  if the condition is true, it returns an appropriate answer and if condition is false, it returns the statements of else block.

if(condition){
    //if code block
}
else{
    //else code block
}

int age=15;
if(age>=18){
    System.out.println("Eligible for vote");
}
else{
    System.out.println("Not Eligible for vote");
}

else-if ladder (Multiple if else)

When we have multiple conditions we can work with else-if ladder. When we work on else-if ladder, else part is optional.

if(condition-1){
    //code block
}
else if(condition-2){
    //code block
}
else if(condition-n){
    //code block
}
else{
    //code block
}

int percent=70;
if(percent>=70){
    System.out.println("Distinction");
}
else if(percent>=60 && percent<70){
    System.out.println("First class");
}
else if(percent>=50 && percent<60){
    System.out.println("Second class");
}
else if(percent>=40 && percent<50){
    System.out.println("Pass");
}
else if(percent<40){
    System.out.println("Fail");
}

Nested if-else

It is known as nested as its’ syntax is like that. It means if inside if let’s understand through its syntax.

if(condition){

    if(condition){

        //code block

    }

    else{

       //code block

    }

}

else{

    if(condition){

       //code block

    }

    else{

       //code block

    }

}

int a=5,b=2;

if(a!=b){

    if(a>b){

        System.out.println(a+” is max”);

    }

    else{

       System.out.println(b+” is max”);

    }

}

else{

   System.out.println(“Both are same”);

}

switch - case

Generally, it is not a pure decision making statement, it uses an expression for int and char data types only and based on expression it chooses its label and executes code block. But if no label matches, it goes to default and perform default code block. Here, we use break statement it is not compulsory every time it depends on situation that we need to add break or continue type of jumping statement or not. Even if we have same values for multiple case we can write different cases together and at last we can add it’s code block

switch(expression){

   case label-1:

      //code block-1

    break;

    case label-2:

      //code block-2

    break;

    case label-n:

      //code block-n

    break;

    default:

      //default code block

}

int a=3;

switch(a){

    case 1:

        System.out.println(“ONE”);

    break;

    case 2:

        System.out.println(“TWO”);

    break;

    case 3:

        System.out.println(“THREE”);

    break;

    default:

        System.out.println(“Invalid Input”);

    break;      //break is optional in default.

}