LinearRegressor with TensorFlow

Now it’s time to train our first model. Before that let me tell you what Tensor Flow is.
TensorFlow is a free and open-source software library for dataflow and differentiable programming across a range of tasks. It is a symbolic math library, and is also used for machine learning applications such as neural networks. Now let’s jump to our ML model. In the last post we had loaded our linear data and also defined our features and labels.

Oh! Before proceeding we have to randomise our data. If Randomisation is not done our model may by-hard outputs rather than improving itself. To randomise data we do following

Now we’ll configure a linear regression model using LinearRegressor. We’ll train this model using the GradientDescentOptimizer, which implements Mini-Batch Stochastic Gradient Descent (SGD). The learning_rate argument controls the size of the gradient step. We do gradient clipping using ‘clip_gradients_by_norm’. Gradient clipping is done to make sure the magnitude of the gradients do not become too large during training, which can cause gradient descent to fail.

Now we configure our Linear Regressor with features and optimizer

Defining a Input Function

To import our data into our LinearRegressor, we need to define an input function, which instructs TensorFlow how to preprocess the data, as well as how to batch, shuffle, and repeat it during model training.

To do this First, we’ll convert our pandas feature data into a dict of NumPy arrays. We can then use the TensorFlow Dataset API to construct a dataset object from our data, and then break our data into batches of batch_size, to be repeated for the specified number of epochs (num_epochs).

So the complete code for input function will be this

code for input function

Next Step is to Train our model

We can now call train() on our linear_regressor to train the model. We’ll wrap my_input_fn in a lambda so we can pass in my_feature and targets as arguments , and to start, we’ll train for 100 steps

Next lets Predict and evaluate our model

Hope all of you are understanding the topic. If you have any doubts the just comment I will get back to you soon. In the next post we will see how good our model is working. Note that this is very basic Linear Regression problem in further post I will be covering more complex one’s.

Link to my previous post : https://machinelearningpower.home.blog/2019/03/30/our-data-with-tensorflow-ml/

Link to my Blog : http://machinelearningpower.home.blog

Our Data with TensorFlow (ML)

Now we have imported our data so next step is to see what is our data made of. Before that let me give you a brief explanation about what are Features and Labels.

Features : Any Value in our data which is used/helpful in making predictions or any values in our data based on we can make good predictions are know as features. There can be one or many features in our data. They are usually represented by ‘x’.

Labels : Values which are to predicted are called Labels or Target values. These are usually represented by ‘y’.

Getting to know your Data

Before staring to write any code you should know what your aim/result. To know what your data is made of we will use some commands. let’s start

We can use Data_Frame.head() command to see some top rows and columns of our data.

Data_frame.head() to see top rows and columns of our data

We can also use Data_Frame.tail() command to see some bottom rows and columns of our data. Data_Frame.describe() shows some useful stats about our data.

Data_Frame.tail() command
Data_Frame.describe()

Deciding our Features and Labels

Here I am going to use only one Feature which is total_rooms and our label which we are going to predict will be median_house_value. Now lets define our features and labels.

In order to import our training data into TensorFlow, we need to specify what type of data each feature contains. There are two main types of data we’ll use in this and future exercises: Categorical Data and Numeric Data

In TensorFlow, we indicate a feature’s data type using a construct called a feature column. Feature columns store only a description of the feature data; they do not contain the feature data itself.

To start, we’re going to use just one numeric input feature, total_rooms. The following code pulls the total_rooms data from our Data_Frame and defines the feature column using numeric_column, which specifies its data is numeric.(when using more than one feature use a comma to separate different features)

Defining Labels

>>targets = Data_Frame[“median_house_value”] Now we have also defined our Labels.

In the next post we will start writing our LinearRegressor model. Note that i have changed the data, it’s not the same data which I used in my previous blog.

Links

Link for the data : https://download.mlcc.google.com/mledu-datasets/california_housing_train.csv

Link for my previous Blog Importing Data for ML : https://machinelearningpower.home.blog/2019/03/30/importing-data-ml/

Importing Data ML

First you need to have data, so download your data from any online CSV Data providing website. I will list some for you at the end of this post (Our first ML model will be linear regression so download linear data). Oh What is Linear Data now. Linear data structures are in which all the data are arranged in sequence manner.for example:linked list,stack queue. Let me show you a pictorial example.

Linear Data

In a Linear Data a line can be drawn drawn through pictorial representation of data and that line passes close to or on those data points. This line is called the line of best fit.

So now to import data in Jupiter Notebook open Jupiter Notebook and click on NEW and select Python 3 from the drop down box. Now in the Python cell type following commands.

Command to load data

Here I am loading a CSV(comma separated values) file from my desktop. Here I am assigning my data name as ” Data_Frame” and the pandas command to load data is “pd.read_csv” . In the the double quotation(remember you can also use single quotation instead to double) is my path where the data is stored on my computer. Here “r” is used to treat the data as raw data.

Now data has been successfully loaded into our working cell. Remember this is not the only way of loading csv data but this is the most preferred way.

In the next blog I will be teaching you how to know your data what are features and labels and how can you decide what are your features and lables

LINK’S

Link to download data : http:// https://archive.ics.uci.edu/ml/datasets.html?format=&task=reg&att=&area=&numAtt=&numIns=&type=&sort=nameUp&view=table

Link to the data I am using : http:// https://www.kaggle.com/andonians/random-linear-regression

Link to my previous post : https://machinelearningpower.home.blog/2019/03/29/setting-up-environment-for-ml/

For any doubts comment !!

Setting up environment for ML

Now lets download and Install all the required software/packages to get started with our ML model.

First Download latest version of Python from https://www.python.org/downloads/ . Download according to your operating system. Second Download a IDLE to write our code. There are many IDLE’s but I suggest you to Anaconda. Download Anaconda from here https://www.anaconda.com/distribution/ . Open Anaconda and download Jupiter Notebook.

Now you are all set up to start coding your ML model.

You have to Install some Libraries into Anaconda. Open the Anaconda Prompt and type these commands to install required libraries. Pandas Library :
pandas is a software library written for the Python programming language for data manipulation and analysis. In particular, it offers data structures and operations for manipulating numerical tables and time series.

< conda install -c anaconda pandas > type this in conda prompt to install pandas(without the brackets).

NumPy Library :
NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

< conda install -c anaconda numpy > type this in conda prompt to install pandas(without the brackets).

That’s it for now if there are any other packages required I will guide those installation during our model.

STAY WITH ME TO LEARN MACHINE LEARNING also share this blog with your friends.

Data Science

Data science is a field that uses scientific methods, processes, algorithms and systems to extract knowledge and insights from structured and unstructured data. Now a days we see the word AI in almost every Gadget we use be it Mobile phones, Laptops or Digital camera etc. So what exactly is AI, oh c’mon it is Artificial Intelligence!! Is it really that simple. Lets find out, in all of my blogs I am going to teach you what is Data Science, Machine learning, Deep Learning and Neural Networks. So stay with me….

Machine Learning

Machine learning is the scientific study of algorithms and statistical models that computer systems use to effectively perform a specific task without using explicit instructions, relying on patterns and inference instead. It is seen as a subset of artificial intelligence. For example if your boss asks you to write a code/program which differentiates between a apple and a banana, that does not sound difficult, but think how many lines of code would it take to write a program which makes efficient prediction and after all the hard work what if your boss gives a GREEN apple. Now you are screwed because you have programmed the machine that apples are red. Such tasks are very very easy with the help if machine learning.

OH! C’MON I AM NOT GOING TO BORE YOU ALL WITH DEFINITIONS, WE WILL START WRITING OUR FIRST MACHINE LEARNING MODEL FROM MY NEXT POST

Through out my posts I will be assuming that you have no prior knowledge of any programming languages and do not know anything about Machine Learning . You must have basic Math skills and logical thinking skills. Now lets get started.