Machine Learning – AppDividend https://appdividend.com Latest Code Tutorials Wed, 02 Oct 2019 17:27:26 +0000 en-US hourly 1 https://wordpress.org/?v=5.2.4 https://appdividend.com/wp-content/uploads/2017/08/cropped-ApDivi-32x32.png Machine Learning – AppDividend https://appdividend.com 32 32 TensorFlow Variables and Placeholders Tutorial With Example https://appdividend.com/2019/02/06/tensorflow-variables-and-placeholders-tutorial-with-example/ https://appdividend.com/2019/02/06/tensorflow-variables-and-placeholders-tutorial-with-example/#respond Wed, 06 Feb 2019 22:43:10 +0000 http://localhost/wordpress/?p=4602 TensorFlow Variables, Constant, and Placeholders Tutorial With Example

TensorFlow Variables and Placeholders Tutorial With Example is today’s topic. TensorFlow is an open source machine learning framework developed by Google which can be used to the build neural networks and perform a variety of all machine learning tasks. TensorFlow works on data flow graphs where nodes are the mathematical operations, and the edges are the data […]

The post TensorFlow Variables and Placeholders Tutorial With Example appeared first on AppDividend.

]]>
TensorFlow Variables, Constant, and Placeholders Tutorial With Example

TensorFlow Variables and Placeholders Tutorial With Example is today’s topic. TensorFlow is an open source machine learning framework developed by Google which can be used to the build neural networks and perform a variety of all machine learning tasks. TensorFlow works on data flow graphs where nodes are the mathematical operations, and the edges are the data in the for tensors, hence the name Tensor-Flow.

Tensors

A tensor is a central unit of data in TensorFlow. It consists of primitive values stored in the shape of a multidimensional array. The number of dimensions a tensor has is called its rank.

A rank 0 tensor is just a scalar. To keep things simple, we can say that a tensor in TensorFlow is instead a fancy name of an array and now we call dimension number as rank. One dimensional array or list is rank one tensor, and two-dimensional array or list is two rank tensor.

TensorFlow Variables

When we train the model, we need to assign some weights and biases throughout the session.

TensorFlow variables can hold the values of biases and weights throughout the session.

You need to one thing keep in mind thatTensorFlow variables need to be initialized.

In TensorFlow variables are of great use when we are training models. As constants, we have to call a constructor to initialize a variable; the initial value can be passed in as an argument.

Variables can easily be added to the computational graph by calling a constructor.

TensorFlow Placeholder

TensorFlow placeholders are initially empty and are used to feed in the actual training examples.

If we want to inject the data into a computation graph, we have to use the mechanism named as a placeholder. Placeholders are bound inside some expressions. The syntax of the placeholder is following.

placeholder(dtype, shape=None, name=None)

Placeholders allow us to not to provide the data in advance for operations and computation graphs, and the data can be added in runtime from external sources as we train the Machine Learning models.

TensorFlow Placeholder does need to declare as a float32 datatype within an optional shape parameter.

TensorFlow Variables and Placeholders Tutorial

Okay, we have covered enough theory, let’s see some practical example of TensorFlow Variables and Placeholders in Python Jupyter Notebook.

You can find the guide about how to install TensorFlow on Mac on this article.

Also, you can find the basics of TesorFlow post.

Now, fire up the Jupyter Notebook and import the TensorFlow.

import tensorflow as tf

You can run the cell by keyboard shortcut Ctrl + Enter.

In the next cell, we will write the following code.

sess = tf.InteractiveSession()

The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. We do not need to write that with tf.Session() as sess code whenever we need to perform some operations. 

Once we run the above code, we do not need to start the session again for that Jupyter Notebook file.

Now, let’s define a random tensor using the following code.

tensorA = tf.random_uniform((4, 4), 1, 2)
tensorA

Here, we have defined the 4*4 matrix between the value 1 and 2. The values are random between 1 to 2.

When we try to display the tensorA, we will get the following output.

 

TensorFlow Variables and Placeholders Tutorial

Here, you can see that the datatype of tensorA is float32.

Now, in the next step, we will define a TensorFlow variable called tensor_var_A.

tensor_var_A = tf.Variable(initial_value=tensorA)

Okay, now run the tensor_var_A variable.

sess.run(tensor_var_A)

You will get an error like below.

 

TensorFlow Variables Tutorial With Example

So, the error is saying that FailedPreconditionError: Attempting to use uninitialized value Variable.

That means, we need to first initialize the TensorFlow variable and then we can run that variable.

So, let’s do that first. Write the following code in the next cell.

init = tf.global_variables_initializer()

Run the above cell and then write the following code in the next cell.

sess.run(init)

Run the above cell, and all the variables are initialized. Now, we write that failed code again, and now you can see the 4*4 matrix.

sess.run(tensor_var_A)

See the output below.

 

Tensors in TensorFlow

Now, let’s create a TensorFlow Placeholder Example.

Define one placeholder using the following code in the next cell.

tfph = tf.placeholder(tf.float32, shape=(None, 5))

The above code creates a TensorFlow placeholder, and its datatype is float32, and here None is the placeholder’s initial value of data. As time goes and our machine learning mode starts training, the data is filled in the placeholder. But, at the starting point, it is None.

We can use another example of TensorFlow Placeholder, which is the following code.

a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')
c = tf.add(a, b, name='c')
sess.run(c, feed_dict={a: 2.1, b: 1.9})

Here, we have defined two placeholders and then create the third node to add both placeholders and run the operation. Remember, we are using Interactive Session. The output is following.

 

TensorFlow Variables and Placeholders Tutorial Example For Beginners

So, this is how you can create TensorFlow Variables and Placeholders.

Finally, TensorFlow Variables and Placeholders Tutorial With Example is over.

The post TensorFlow Variables and Placeholders Tutorial With Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/02/06/tensorflow-variables-and-placeholders-tutorial-with-example/feed/ 0
Tensorflow Basics Tutorial | Getting Started With Tensorflow https://appdividend.com/2019/02/04/tensorflow-basics-tutorial-example-getting-started-with-tensorflow/ https://appdividend.com/2019/02/04/tensorflow-basics-tutorial-example-getting-started-with-tensorflow/#respond Mon, 04 Feb 2019 18:22:42 +0000 http://localhost/wordpress/?p=4480 Tensorflow Tutorial With Example From Scratch

In this article, we will see Tensorflow Basics Tutorial With Example | Getting Started With Tensorflow. Machine learning is the complex discipline. But implementing the machine learning models is far less daunting and difficult than it used to be, thanks to the machine learning frameworks—such as Google’s TensorFlow: that ease the process of acquiring data, training models, serving […]

The post Tensorflow Basics Tutorial | Getting Started With Tensorflow appeared first on AppDividend.

]]>
Tensorflow Tutorial With Example From Scratch

In this article, we will see Tensorflow Basics Tutorial With Example | Getting Started With Tensorflow. Machine learning is the complex discipline. But implementing the machine learning models is far less daunting and difficult than it used to be, thanks to the machine learning frameworks—such as Google’s TensorFlow: that ease the process of acquiring data, training models, serving predictions, and future results.

TensorFlow is an open source library for numerical computation and large-scale machine learning. TensorFlow bundles together the slew of machine learning and deep learning (also known as neural networking) models and algorithms and makes them useful by way of the common metaphor. It uses Python to provide the convenient front-end API for building applications with the framework while executing those applications in high-performance C++.

Tensorflow Basics Tutorial

For this example, I am using Jupyter Notebook to perform some Tensorflow practicals. So, if your notebook has not installed the Tensorflow library then you can install it using Anaconda Navigator and find the environment section of Anaconda Navigator. You will see something like this.

 

Tensorflow Basics Tutorial

Here, if a tensorflow package is uninstalled on your machine, then you can install it from here.

Now, launch a Jupyter Notebook and create a new book and verify that you have installed the tensorflow using the following code in the first cell of Jupyter Notebook.

import tensorflow as tf
tf.__version__

Run the cell using the Ctrl + Enter command and see the output.

 

Install Tensorflow on Jupyter Notebook

Tensorflow Basic Syntax

Let’s define the Tensorflow constants using the following code. You need to write that code in the next cell.

app = tf.constant('app')
type(app)

Here, I have defined the app tensorflow constant and also we have checked the type of that constant. Run the cell See the below output.

 

Tensorflow Basic Syntax

Let’s define one more tensorflow constant and see its datatype as well.

dividend = tf.constant('dividend')
type(dividend)

See the output below.

 

Tensorflow Constants

Now, print the constant and see the output.

print(app)

See the output below.

 

Print Tensorflow constant

See the datatype of the constant is String. The object is Tensor.

The tf.session in Tensorflow

You can not run any operation in Tensorflow without using the sessions. If you try to add any integers or concat the strings, you need to start a session and then add the operational code and then run that operation. Otherwise, nothing will have happened. Let’s concat the above two strings we have defined earlier. See the following example.

with tf.Session() as sess:
    result = sess.run(app + dividend)
print(result)

Here, we have used the tf.Session() function to start a session and perform the concat operation between two strings and store that output in the result variable and then print that variable. See the output.

 

Sessions in Tensorflow

Here, in the output represents the byte literal.

Let’s perform the addition operation computation in Tensorflow. Write the following code in the next cell.

a = tf.constant(10)
b = tf.constant(10)
with tf.Session() as sess:
    result = sess.run(a + b)
print(result)

Here, we have defined the two constants, which are integers and then we have performed the addition operation inside the tensorflow session and store the output in result variable and display that variable. See the following output.

 

Perform Operation in Tensorflow Session

Create Matrix using Tensorflow

We can create a Matrix using Tensorflow functions like, we have created using NumPy library.

We can create the matrix using tf.fill() method provided by the tensorflow framework. See the following example.

mat = tf.fill((5,5), 21)
with tf.Session() as sess:
    op = sess.run(mat)
op

See the output below.

 

Create Matrix using Tensorflow

In the above code, we have created the 5*5 matrix with the 21 values filled inside each element. Remember, we need to perform any operation inside the tensorflow sessions.

So these are some basics of Tensorflow.

Finally, Tensorflow Basics Tutorial | Getting Started With Tensorflow is over.

The post Tensorflow Basics Tutorial | Getting Started With Tensorflow appeared first on AppDividend.

]]>
https://appdividend.com/2019/02/04/tensorflow-basics-tutorial-example-getting-started-with-tensorflow/feed/ 0
How To Build Simple Model In Tensorflow https://appdividend.com/2019/01/30/how-to-build-simple-model-in-tensorflow/ https://appdividend.com/2019/01/30/how-to-build-simple-model-in-tensorflow/#respond Wed, 30 Jan 2019 09:53:58 +0000 http://localhost/wordpress/?p=4197 Create a Tensorflow Model in Python

In this tutorial, I will show you How To Build Simple Model In Tensorflow. Tensorflow Framework is the popular framework to design a neural network in Machine Learning. Tensorflow is created at Google. It is an open source machine learning framework for everyone. TensorFlow is an open source library for high-performance numerical computation. Tensorflow has a flexible […]

The post How To Build Simple Model In Tensorflow appeared first on AppDividend.

]]>
Create a Tensorflow Model in Python

In this tutorial, I will show you How To Build Simple Model In Tensorflow. Tensorflow Framework is the popular framework to design a neural network in Machine Learning. Tensorflow is created at Google. It is an open source machine learning framework for everyone. TensorFlow is an open source library for high-performance numerical computation. Tensorflow has a flexible architecture that allows easy deployment of calculation across a variety of platforms like CPUs, GPUs, TPUs, and from desktops to clusters of servers to mobile and edge devices. It is initially developed by researchers and engineers from the Google Brain team within Google’s AI organization, and it comes with strong support for machine learning and deep learning, and the flexible numerical computation core is used across many other scientific domains.

How To Build Simple Model In Tensorflow

If we want to work with Tensorflow then first, we need to define the tensorflow graph. In this model, we will define two tensors or nodes, and then we add those nodes. Now, let’s dive into what is tensors.

Tensors in Tensorflow

TensorFlow, as the name indicates, is the framework to define and run computations involving tensors. The tensor is the generalization of vectors and matrices to potentially higher dimensions. Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes.

When writing a TensorFlow program, the primary object you manipulate and pass around is the tf.Tensor. The tf.Tensor object represents a partially defined computation that will eventually produce a value. TensorFlow programs work by first building a graph of tf.Tensor objects, detailing how each tensor is computed based on the other available tensors and then by running parts of this graph to achieve the desired results.

TensorFlow programs use the tensor data structure to represent all data only tensors are passed between operations in the computation graph. You can think of the TensorFlow tensor as an n-dimensional array or list.

For example, a scaler is a tensor, a vector is a tensor, and a matrix is a tensor. A tensor has a rank, a shape, and a static type so that a tensor can be represented as the multidimensional array of numbers.

Tensorflow Example

Okay, now, if you do not know how to install and configure the Tensorflow on virtualenv then check out my post on how to install tensorflow on this blog.

Okay, now we will create a file called tflow.py and write the following code inside that file.

# app.py

import tensorflow as tf
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

X = tf.placeholder(tf.int32, name='X')
Y = tf.placeholder(tf.int32, name='Y')
add = tf.add(X, Y, name='add')

with tf.Session() as session:
    result = session.run(add, feed_dict={
        X: [19, 1, 2],
        Y: [21, 4, 3]
    })
print(result)

In the above code, first, we have imported the tensorflow and os module from the Python library.

We have used the os module’s environ attribute to disable the logs which are unnecessary for this demo model. If you want that log in the output, then you can comment that statement.

Then we have created the computational graph with two nodes using a placeholder and defined their datatypes and names.

So, we have two tensors up to now which is X and Y.

Then we are adding those tensors using tensorflow’s add method and get the third node which is an add node.

If we want to execute operation in the Graph, we need to use sessions.

We can create a session in tensorflow using with tf.Session as session code.

Next step is that we have called the run method on the session, which takes the two arguments.

  1. add node
  2. data

We have used the dictionary data, which has two nodes, X and Y. So we will add those and generate a new node with the session.

Go to the terminal and run the following command.

python3 tflow.py

The output of the above code is following.

 

How To Build Simple Model In Tensorflow

So, we have built a tensorflow model, which can add two nodes and gives the output node.

Here, the computation is to add the nodes and nothing complicated. But in real life application, there are lots of variables and iterables to go through and finally predict the future value.

Finally, How To Build Simple Model In Tensorflow Tutorial With Example is over.

The post How To Build Simple Model In Tensorflow appeared first on AppDividend.

]]>
https://appdividend.com/2019/01/30/how-to-build-simple-model-in-tensorflow/feed/ 0
How To Install Tensorflow on Mac Tutorial From Scratch https://appdividend.com/2019/01/29/how-to-install-tensorflow-on-mac-tutorial-from-scratch/ https://appdividend.com/2019/01/29/how-to-install-tensorflow-on-mac-tutorial-from-scratch/#respond Tue, 29 Jan 2019 22:28:23 +0000 http://localhost/wordpress/?p=4178 Install TensorFlow

In this tutorial, we will see How To Install Tensorflow on Mac. For this tutorial, you must have installed Python 3 in your mac machine. If not then go to python.org website and install version 3 of Python. Other then that, you need to have an editor like Visual Studio Code. You can always welcome with PyCharm IDE as well. […]

The post How To Install Tensorflow on Mac Tutorial From Scratch appeared first on AppDividend.

]]>
Install TensorFlow

In this tutorial, we will see How To Install Tensorflow on Mac. For this tutorial, you must have installed Python 3 in your mac machine. If not then go to python.org website and install version 3 of Python. Other then that, you need to have an editor like Visual Studio Code. You can always welcome with PyCharm IDE as well. I am using VSCode and have installed Python extension.

How To Install Tensorflow on Mac

I have installed version 3 of the Python. You can check it by going to the terminal and see the output by python3 -v command.

 

How To Install Tensorflow on Mac

If you don’t have still Python 3 and other packages like Otherwise, install Python, the pip package manager, and Virtualenv then you can follow the below steps.

First, Install using the Homebrew package manager.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then add a global path. Modify and add the following line inside .bash_profile or .zshrc file.

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

The hit the one by one following command.

brew update
brew install python  # Python 3
sudo pip3 install -U virtualenv

I have already created a Virtual Environment, but you can also create by following steps.

Create a new virtual environment by choosing a Python interpreter and making a ./pythonenv directory to hold it. Type the following command then.

virtualenv --system-site-packages -p python3 ./pythonenv

Go inside that folder.

cd pythonenv

Activate the virtual environment using a shell-specific command.

source bin/activate

If you have installed virtual environment perfectly then virtualenv is active, your shell prompt is prefixed with (pythonenv). See the below image.

 

Install Tensorflow on Mac

Install packages within a virtual environment without affecting the host system setup. Start by upgrading pip. See the following command to upgrade pip.

pip install --upgrade pip

After that, you can see all the packages by typing the following command.

pip list

 

Create a virtual environment

You can see that, I have already installed the TensorFlow package.

Install the TensorFlow pip package

If your system has not tensorflow package then you can install it using the following command.

pip install tensorflow

Basically, package dependencies are already installed, if you have not the newer version of tensorflow then you can upgrade using the following command.

pip install --upgrade tensorflow

So, finally, the Tensorflow is installed on your machine.

Let’s check if it has installed or not.

Create a new folder inside the pythonenv folder called tflow and inside that, create a new file called tflow.py and add the following code inside it.

import tensorflow as tf
import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

hello = tf.constant('Kudos, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

In the above code, we have imported the tensorflow as well as the os module.

If you don’t import the os module and use the environ function then it will give us a warning and avoid the warning, we have applied the setting export TF_CPP_MIN_LOG_LEVEL=2.

For more information, you can check out this StackOverflow link.

Finally, run the tflow.py file using the following command and see the output.

python3 tflow.py

 

Install the TensorFlow pip package

If you have seen like this then congratulations!! You have successfully installed Tensorflow on your mac.

Finally, How To Install Tensorflow on Mac Tutorial From Scratch is over.

The post How To Install Tensorflow on Mac Tutorial From Scratch appeared first on AppDividend.

]]>
https://appdividend.com/2019/01/29/how-to-install-tensorflow-on-mac-tutorial-from-scratch/feed/ 0
How To Prepare Your Dataset For Machine Learning in Python https://appdividend.com/2018/07/23/prepare-dataset-for-machine-learning-in-python/ https://appdividend.com/2018/07/23/prepare-dataset-for-machine-learning-in-python/#comments Mon, 23 Jul 2018 22:50:45 +0000 http://localhost/wordpress/?p=1369 How to Preprocess Data For Machine Learning in Python

How To Prepare Dataset For Machine Learning in Python. Machine Learning is all about train your model based on current data to predict future values. So we need the proper amounts to train our model. So in real life, we do not always have the correct data to work with. If the data is not processed […]

The post How To Prepare Your Dataset For Machine Learning in Python appeared first on AppDividend.

]]>
How to Preprocess Data For Machine Learning in Python

How To Prepare Dataset For Machine Learning in Python. Machine Learning is all about train your model based on current data to predict future values. So we need the proper amounts to train our model. So in real life, we do not always have the correct data to work with. If the data is not processed correctly, then we need to prepare it and then start training our model. So in this post, we will see step by step to transform our initial data into Training and Test data. For this example, we use python libraries like scikit learn, numpyand pandas.

Prepare Dataset For Machine Learning in Python

We use the Python programming language to create a perfect dataset. For preparing a dataset, we need to perform the following steps.

#Steps To Prepare The Data.

  1. Get the dataset and import the libraries.
  2. Handle missing data.
  3. Encode categorical data.
  4. Splitting the dataset into the Training set and Test set.
  5. Feature Scaling, if all the columns are not scaled correctly.

So, we will be all the steps on the dataset one by one and prepare the final dataset on which we can apply regression and different algorithms.

#1: Get The Dataset.

Okay, now we are going to use Indian Liver Patient data. So we first prepare the complete dataset for this kind of data. I am putting the link here to download the data. Remember, this is not a real dataset; this is just the demo dataset. It looks like the actual dataset.

Download File: patientData

Now, we need to create a project directory. So let us build using the following command.

mkdir predata

Now go into the directory.

cd predata

We need to move the CSV file inside this folder.

Now, open the Anaconda Navigator software. If you are new to Anaconda, then please check out this How To Get Started With Machine Learning In Python. After opening Navigator, you can see a screen like below.

How To Prepare Your Dataset For Machine Learning in Python

 

Now, launch the Spyder application and navigate to your project folder. You can see, we have already moved the patientData.csv file so that you can see that file over there.

Okay, now we need to create one Python file called datapre.py and start importing the mathematical libraries.

Write the following code inside the datapre.py file. So, your file looks like this. Remember, we are using Python 3

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 25 18:52:15 2018

@author: your name
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

Now, select code of three import statements and hit the command + enter and you can see at the right side down; the code is running successfully.

That means we have successfully imported the libraries. If you found any error then possibly the numpy,  pandas, or matplotlib library is missing. So you need to install that, and that is it.

#2: Handle Missing Data.

In real-time, missing the data happens quite a lot. If you are finding the real-time data set like for the patients, then there is always missing the data. To train the model correctly, we need to fill the data somehow. Otherwise, the model will mispredict the values. Luckily libraries are already available to do that; we need to use the proper function to do that. Now, in our dataset, there is missing data, so we need to fill the data with either mean values or to use some other algorithms. In this example, we are using MEAN to supply the values. So let us do that.

But first, let us divide the dataset into our X and Y axis.

Okay, now write the following code after importing the libraries.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 25 18:52:15 2018

@author: krunal
"""
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset = pd.read_csv('patientData.csv')

Now, select the following line and hit the command + enter.

dataset = pd.read_csv('patientData.csv')

Okay, so we have included our initial dataset, and you can see here.

Prepare Dataset For Machine Learning

 

Here, you can see that if the value is empty, then the nan is displaying. So we need to change it with the MEAN values. So let us do that.

Write the following code.

X = dataset.iloc[:, :-1].values
Y = dataset.iloc[:, 3].values

So, here in the X, we have selected the first four columns and leave the fifth column. It will be our Y.

Remember, indexes are starting from 0. So -1 means the last column. So we are selecting all the columns except for the last column.

For Y, we have explicitly selected the fourth column, and the index is 3.

Okay, now we need to handle the missing data. We will use a library Scikit learn.

Write the following code.

...

from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])

So, here, we have to use the Imputer module to use the strategy ‘mean’ and fill the missing values with the mean values. Run the above lines and type the X in the Console. You can see something like below. Here, columns 1 and 2 have missing values, but we have written 1:3 because the upper bound is excluded; that is why we have taken 1 and 3, and it is working fine. Finally, transform the whole column values which have NaN values, and now we have got the filled values.

Machine Learning Dataset Preparation

 

Here, you can see that the mean values of that particular column fill the missing values.

So, we have handled the missing data. Now, head over to the next step.

#3: Encode Categorical data.

In our dataset, there are two categorical columns.

  1. Gender
  2. Liver Disease

So, we need to encode these two columns of data.

# Encode Categorical Data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()
labelencoder_Y = LabelEncoder()
Y = labelencoder_Y.fit_transform(Y)

Here, we have encoded the values of the first column. Now, here, we have only two cases for the first column, and that is Female and Male. Now, after the transform, the values are 1 for Female, and 0 for Male.

Run the above line and see the changes in categorical data. So, here for Female, it is and Male is 0. It has created one more column and replaces Male and Female according to 1 and 0. That is why it becomes from 3 columns to 4 columns.

Python Machine Learning Dataset Preparation

 

#4: Split the dataset into Training Set and Test Set.

Now, generally, we split the data with a ratio of 70% for the Training Data and 30% to test data. For our example, we divided into 80% for training data and 20% for the test data.

Write the following code inside the Spyder.

# Split the data between the Training Data and Test Data

from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2
                                                    ,random_state = 0)

Run the code, and you can get the four more variables. So, we have a total of seven variables.

Training model in Machine Learning

So, here, we have split the both Axis X and Y into X_train and X_test

Y-axis becomes Y_train and Y_test.

So, you have 80% data on the X_train and Y_train and 20% data on the X_test and Y_test.

#5:  Feature Scaling

In a general scenario, machine learning is based on Euclidean Distance. Here for the column Albumin and Age column has an entirely different range of values. So we need to convert those values and make it under the range of values. That is why this is called feature scaling. We need to scale the values for the Age column. So let us scale the X_train and X_test.

# Feature Scaling

from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

Here, we do not need for Y because it is already scaled. Now run the above code and hit the following command.

Feature Scaling in Machine Learning

 

Here, we can see that all the values are appropriately scaled and also you can check the X_test variable as well.

So, we have successfully cleared and prepared the data.

Here is the final code of our datapre.py.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 25 18:52:15 2018

@author: krunal
"""

# Importing Libraries

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing Dataset

dataset = pd.read_csv('patientData.csv')
X = dataset.iloc[:, :-1].values
Y = dataset.iloc[:, 3].values

# Handing Missing Dataset

from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])

# Encode Categorical Data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder_X = LabelEncoder()
X[:, 0] = labelencoder_X.fit_transform(X[:, 0])
onehotencoder = OneHotEncoder(categorical_features = [0])
X = onehotencoder.fit_transform(X).toarray()
labelencoder_Y = LabelEncoder()
Y = labelencoder_Y.fit_transform(Y)

# Split the data between the Training Data and Test Data

from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2
                                                    ,random_state = 0)

# Feature Scaling

from sklearn.preprocessing import StandardScaler
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

So, we have successfully Prepare Dataset For Machine Learning in Python.

Machine Learning has very complex computations. It depends on how you get the data and in which condition. Based on the condition of the data, you will start to preprocess the data and split the data into the Train and Test model.

Finally, Prepare Dataset For Machine Learning in Python is over. Thanks for taking it.

The post How To Prepare Your Dataset For Machine Learning in Python appeared first on AppDividend.

]]>
https://appdividend.com/2018/07/23/prepare-dataset-for-machine-learning-in-python/feed/ 3
How To Get Started With Machine Learning In Python https://appdividend.com/2018/04/07/how-to-get-started-with-machine-learning-in-python/ https://appdividend.com/2018/04/07/how-to-get-started-with-machine-learning-in-python/#respond Sat, 07 Apr 2018 21:40:16 +0000 http://localhost/wordpress/?p=352 How To Get Started With Machine Learning In Python

Hello Devs, Today I will guide you How To Get Started With Machine Learning In Python. In this tutorial, you will learn to install the Python3, Jupyter Notebook, Scikit Learn, and other useful Python libraries step by step. Also if you do not want to install all the packages one by one, then you can also install […]

The post How To Get Started With Machine Learning In Python appeared first on AppDividend.

]]>
How To Get Started With Machine Learning In Python

Hello Devs, Today I will guide you How To Get Started With Machine Learning In Python. In this tutorial, you will learn to install the Python3, Jupyter Notebook, Scikit Learn, and other useful Python libraries step by step. Also if you do not want to install all the packages one by one, then you can also install only one software called Anaconda. It will install Python3 and other I guess 150 packages that will help you to build your development environment very smoothly and you do not even think about installing all the dependencies.

What Is Machine Learning?

Machine Learning is the application of Artificial Intelligence (AI) that provides systems the ability to learn and improve from the previous experience without being explicitly programmed automatically. Machine learning focuses on the development of computer programs that can access the data and use it to determine themselves for future making decisions.

The process of learning begins with the measurements or observations of data, such as examples, direct experience, or instruction, to look for patterns in data and make better decisions in the future based on the models that we can provide. The primary focus is to allow the computers to learn automatically without human intervention or assistance and adjust actions accordingly.

Machine Learning is the art of computing, in which we need first to prepare the data, if not in well-formed condition and then use that accurate data to train the models. The models are built on different algorithms, which the programmers need to write in programming language and our case it is Python. Based on the previous output of the data and algorithmic logic, Machine starts to learn themselves, and the accuracy of the future values are accurate more and more based on the vast amount of data that we have collected.

Machine Learning WorkFlow

The necessary steps are following.

  1. We need to ask our selves, what are the main problems that can solve machine learning.
  2. After identifying the problem, we need to start the preparing for the data. If the data is not well organized than we need to organize it very well so that we can train the model based on accurate data.
  3. Next step is to select the algorithm that can solve the problem. There are so many algorithms out there to work with, but we need to identify the perfect algorithm to meet our requirement.
  4. After selecting the Algorithm, we need to train the model based on that.
  5. The last step is to test that model to predict the future values.

Get Started With Machine Learning In Python

The first thing we will do is to install the Python 3 and other machine learning libraries that we can use to develop and train the models. I am using Mac, but if you are the windows user, then you can also follow up this tutorial although installation instructions might be different on Windows platform. You can easily google for that, not a big deal. Once you install all the libraries, you are ready to go with this tutorial.

There are lots of ways, you can install all the dependencies, but I am showing two methods from them

  1. Install Anaconda package that covers all the installation libraries. You need to launch the Navigator and open the Jupyter Notebook, and you are good to go.
  2. We will install all the dependencies one by one on Mac.

Method: 1 Install Dependencies Using Anaconda.

Step 1: Install Anaconda

Go to the downloads directory https://www.anaconda.com/download/#macos

For Windows users, choose your platform’s executables and download the package and install it on your machine.

During the installation, you have an option that tells us that, whether or not you need to install Visual Studio Code or not. Guys, if you have not downloaded already then, please download and install it on your machine.

Step 2: Open Anaconda Navigator

After installing, we need to open the application from Launchpad called Anaconda-Navigator.

Anaconda Navigator

I have already installed VSCode, so do not need to install it from here. But almost our interface looks same.

Step 3: Launch Jupyter Notebook.

It will start a server at default PORT: 8888. Also, it will open the browser window and take to this local URL: http://localhost:8888/tree, Please do not close the terminal that opens with Jupyter Notebook otherwise our local server will stop working and we will not be able to run the Jupyter Notebook. So we all set up to start Machine Learning.

Method 2: Install Individual Libraries.

First, check your Python version using the following command.

python

On my MacBook, I have preinstalled Python, and its version is 2.7. But we need the different version, and that is Python 3. Do not uninstall old version because otherwise, your some of the mac applications will be stopped. Instead, pull another version of Python by the following command.

brew install python3

Okay, so it will install the Python3, and now you can check it using the following command.

python3

Now, you can see that Python3 is installed on our machine.

Finally, install the following packages using Pip. It is the Python, package manager.

python3 -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose

So it will install all the dependencies. Now, go to your terminal and hit the following command to start Jupyter server.

jupyter notebook

Now, switch to the browser and type this URL: http://localhost:8888/tree

You can see that our Jupyter Notebook server is running correctly.

Jupyter Notebook

Getting Started With Jupyter Notebook.

So, now we need to create one notebook.

Go into your project folder, and in my case it is AnacondaProjects. It is empty so press new drop-down in the upper right corner and select python3. You will see something like below.

FirstNotebook

I have renamed the title to FirstML. Yours would be untitled. My Browser URL is like this: http://localhost:8888/notebooks/AnacondaProjects/FirstML.ipynb Your URL will be different based on your folder structure.

Markdown Editor

If you have used Github before then, I guess you are familiar with Markdown Language. Markdown is a lightweight markup language with plain text formatting syntax. It is designed so that it can be converted to HTML and many other formats using a tool by the same name.

In Github, we are writing this language inside readme.md file. So that the project can be described very well and it can be documented very well.

In Jupyter Notebook, when we need to write something that is not code, we can change it to via drop-down and select the markdown instead and write some text in markdown language. For example, If we need to write H1 text in markdown, then we can write it using the following syntax.

# Hello Machine Learning

So it will print as an H1 text. Same if we define two hashes then H2 text and go on.

Code In Jupyter Notebook.

We can write the code and run the code like following.

First, if your dropdown says markdown then change it to code and then write the code. After that, to run that code type shortcut shift + enter(return)

Python3 Tutorial

You will get an output like the above image. Great!!. So we have set up all correctly.

We will start working with data in the next tutorial. How To Get Started With Machine Learning In Python tutorial is over. This is a just basic overview of machine learning and installing the dependencies related to machine learning. Stay tuned for the next machine learning tutorial.

 

The post How To Get Started With Machine Learning In Python appeared first on AppDividend.

]]>
https://appdividend.com/2018/04/07/how-to-get-started-with-machine-learning-in-python/feed/ 0