### What is Regression?

In statistical modeling, regression analysis is a statistical process for estimating the relationships among variables. It includes many techniques for modeling and analyzing several variables when the focus is on the relationship between a dependent variable and one or more independent variables (or ‘predictors’). Regression is a predictive modeling analysis technique. It estimates a relationship between the dependent and an independent variable.

Use of Regression:

• Determine the strength of predictors.
• Forecasting an effect.
• Trend forecasting.

Linear Regression:

Linear regression is a basic and commonly used type of predictive analysis.  The overall idea of regression is to examine two things, it does a set of predictor variables do a good job in predicting an outcome (dependent) variable?  in Which variables, in particular, are significant predictors of the outcome variable, and in what way do they–indicated by the magnitude and sign of the beta estimates–impact the outcome variable?  These regression estimates are used to explain the relationship between one dependent variable and one or more independent variables.  The simplest form of the regression equation with one dependent and one independent variable is defined by the formula y = c + b*x, where y = estimated dependent variable score, c = constant, b = regression coefficient, and x = score on the independent variable.

Linear Regression Selection Criteria:

1. Classifiaction & Regression capabalities.
2. Data quality.
3. Computational complexity.
4. Comprehensive & transport.

When will we use Linear Regression?

• Evaluating trends & sales estimates.
• Analyzing the impact of price changes.
• Assessment of risk in financial services and insurance domain.

for example, a group of creative Tech enthusiasts started a company in Silicon Valley. This start-up — called Banana — is so innovative that it has been growing constantly since 2016. You, the wealthy investor, would like to know whether to put your money on Banana’s success in the next year or not. Let’s assume that you don’t want to risk a lot of money, especially since the stakes are high in Silicon Valley. So you decide to buy a few shares, instead of investing in a big portion of the company.

Well, you can definitely see the trend. Banana is growing like crazy, kicking up their stock price from 100 dollars to 500 in just three years. You only care about how the price is going to be like in the year 2021 because you want to give your investment some time to blossom along with the company. Optimistically speaking, it looks like you will be growing your money in the upcoming years. The trend is likely not to go through a sudden, drastic change. This leads to you hypothesizing that the stock price will fall somewhere above the \$500 indicator.

Here’s an interesting thought. Based on the stock price records of the last couple of years you were able to predict what the stock price is going to be like. You were able to infer the range of the new stock price (that doesn’t exist on the plot) for a year that we don’t have data for (the year 2021). Well — kinda.

What you just did is infer your model (that head of yours) to generalize — predict the y-value for an x-value that is not even in your knowledge. However, this is not accurate in any way. You couldn’t specify what exactly is the stock price most likely going to be. For all you know, it is probably going to be above 500 dollars.

Here is where Linear Regression (LR) comes into play. The essence of LR is to find the line that best fits the data points on the plot so that we can, more or less, know exactly where the stock price is likely to fall in the year 2021.

Let’s examine the LR-generated line (in red) above, by looking at the importance of it. It looks like, with just a little modification, we were able to realize that Banana’s stock price is likely to be worth a little bit higher than \$600 by the year 2021. Obviously, this is an oversimplified example. However, the process stays the same. Linear Regression as an algorithm relies on the concept of lowering the cost to maximize the performance. We will examine this concept, and how we got the red line on the plot next.

Finding the best fit line:

To check the goodness of fit we use the R-squared method.

What is the R-squared method?

R-squared value is a statistical measure of how close the data to the fitted linear regression line. It is also known as COD(coefficient of determination), or the coefficient of multiple determination.

### What are overfitting and underfitting?

Overfitting: Good performance on the training data, poor generalization to other data.

Underfitting: Poor performance on the training data & poor generalization to other data.

Linear Regression with python:

1.Importing required libraries:

`import numpy as np from sklearn.linear_model import LinearRegression`

2. Provide data:

`x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1)) y = np.array([5, 20, 14, 32, 22, 38])`
`print(x)`
`print(y)`` `

```Output:
>>> print(x)
[[ 5]
[15]
[25]
[35]
[45]
[55]]
>>> print(y)
[ 5 20 14 32 22 38]```

3. Create a model and fit it:

`model = LinearRegression().fit(x, y) `

4. Get Result:

`>> r_sq = model.score(x, y)`
`>>> print('coefficient of determination:', r_sq) coefficient of determination: 0.715875613747954 `

5. Predict response:

``````>>> y_pred = model.predict(x)
>>> print('predicted response:', y_pred, sep='\n')
predicted response:
[ 8.33333333 13.73333333 19.13333333 24.53333333 29.93333333 35.33333333]
``````

Learnbay provides industry accredited data science courses in Bangalore. We understand the conjugation of technology in the field of Data science hence we offer significant courses like Machine learning, Tensor Flow, IBM Watson, Google Cloud platform, Tableau, Hadoop, time series, R and Python. With authentic real-time industry projects. Students will be efficient by being certified by IBM. Around hundreds of students are placed in promising companies for data science roles. Choosing Learnbay you will reach the most aspiring job of present and future.
Learnbay data science course covers Data Science with Python, Artificial Intelligence with Python, Deep Learning using Tensor-Flow. These topics are covered and co-developed with IBM.

``````
``````

### Data Science is Important!

Data Science is not only required to only become Data scientists but also to become eligible for the other technical jobs outside the field of DS. Know how!

## Random Forest Model: Introduction

Random Forest Model is also a classification model with the combination of the decision tree. The random forest model algorithm is a supervised classification algorithm. As the name suggests, this algorithm creates the forest with several trees. … In the same way in the random tree classifier, the higher the number of trees in the forest gives the high the accuracy results. If you know the Random forest algorithm is a supervised classification algorithm.
The random forest model follows an ensemble technique. It involves constructing multi decision trees at training time. Its prediction is based on mode for classification and means for regression tree. It helps to reduce the overfitting of the individual decision tree. There are many possibilities for the occurrence of overfitting.

### Random Forest Model Algorithm: Working

We can understand the working of the Random Forest algorithm with the help of following steps −

• Step 1 − First, start with the selection of random samples from a given dataset. Do sampling without replacement.

Sampling without replacement stats that the training data split into several small samples and then the result we get is a combination of all the data set. If we have 1000 features in a data set the splitting will happen with 10 features each in a small training data and all split training data contains equal no of features. The result is based on which training data has the highest value.

• Step 2 − Next, this algorithm will construct a decision tree for every sample. Then it will get the prediction result from every decision tree.
• Step 3 − In this step, voting will be performed for every predicted result.
• Based on ‘n’ samples… ‘n’ tree is built
• Each record is classified based on the n tree
• The final class for each record is decided based on voting

Step 4 − At last, select the most voted prediction result as the final prediction result.

### What is the Out of Bag score in Random Forests?

Out of bag (OOB) score is a way of validating the Random forest model. Below is a simple intuition of how is it calculated followed by a description of how it is different from the validation score and where it is advantageous.

For the description of the OOB score calculation, let’s assume there are five DTs in the random forest ensemble labeled from 1 to 5. For simplicity, suppose we have a simple original training data set as below.

OOB Error Rate Computation Steps

• Sample left out (out-of-bag) in Kth tree is classified using the Kth tree
• Assume j cases are misclassified
• The proportion of time that j is not equal to true class averaged over all cases is the OOB error rate.

Variable importance of RF:

It stats about the feature that is most useful for the random forest model by which we can get the high accuracy of the model with less error.

• Random Forest computes two measures of Variable Importance
• Mean Decrease in Accuracy
• Mean Decrease in Gini
• Mean Decrease in Accuracy is based on permutation
• Randomly permute values of a variable for which importance is to be computed in the OOB sample
• Compute the Error Rate with permuted values
• Compute decrease in OOB Error rate (Permuted- Not permuted)
• Average the decrease overall the trees
• Mean Decrease in Gini is computed as a “total decrease in node impurities from splitting on the variable averaged over all trees”.

Finding the optimal values using grid-search cv:

It stats the optimal values of the splitting decision tree that how many trees to be split within the model.

Measuring RF model performance by Confusion Matrix:

A confusion matrix is a table that is often used to describe the performance of a classification model (or “classifier”) on a set of test data for which the true values are known. It allows the visualization of the performance of an algorithm. It tells about how many true values are true.

Random Forest with python:

Importing the important libraries–

`import pandas as pd import numpy as np from sklearn import preprocessing from sklearn.model_selection import train_test_split from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import confusion_matrix from sklearn.metrics import accuracy_score from sklearn import svm from sklearn.metrics import roc_curve, auc import matplotlib.pyplot as plt from sklearn.externals.six import StringIO from IPython.display import Image from sklearn.tree import export_graphviz `

```dummy_df = pd.read_csv("bank.csv", na_values =['NA']) temp = dummy_df.columns.values[0] temp print(dummy_df)```

## Data Pre-Processing:

`columns_name = temp.split(';') data = dummy_df.values print(data) print(data.shape) contacts = list() for element in data: contact = element[0].split(';') contacts.append(contact)`

`contact_df = pd.DataFrame(contacts,columns = columns_name) print(contact_df) def preprocessor(df): res_df = df.copy() le = preprocessing.LabelEncoder()`

` ``encoded_df = preprocessor(contact_df) #encoded_df = preprocessor(contacts) x = encoded_df.drop(['"y"'],axis =1).values y = encoded_df['"y"'].values`

## Split the data into Train-Test¶

`x_train, x_test, y_train, y_test = train_test_split(x,y,test_size =0.5)`

## Build the Decision Tree Model

```# Decision tree with depth = 2 model_dt_2 = DecisionTreeClassifier(random_state=1, max_depth=2) model_dt_2.fit(x_train, y_train) model_dt_2_score_train = model_dt_2.score(x_train, y_train) print("Training score: ",model_dt_2_score_train) model_dt_2_score_test = model_dt_2.score(x_test, y_test) print("Testing score: ",model_dt_2_score_test) #y_pred_dt = model_dt_2.predict_proba(x_test)[:, 1] #Decision tree```

`model_dt = DecisionTreeClassifier(max_depth = 8, criterion ="entropy") model_dt.fit(x_train, y_train) y_pred_dt = model_dt.predict_proba(x_test)[:, 1]`

## Graphical Representation of Tree

`plt.figure(figsize=(6,6)) dot_data = StringIO() export_graphviz(model_dt, out_file=dot_data, filled=True, rounded=True, special_characters=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) Image(graph.create_png())`

## Performance Metrics

```fpr_dt, tpr_dt, _ = roc_curve(y_test, y_pred_dt) roc_auc_dt = auc(fpr_dt, tpr_dt) predictions = model_dt.predict(x_test) # Model Accuracy print (model_dt.score(x_test, y_test)) y_actual_result = y_test[0] for i in range(len(predictions)): if(predictions[i] == 1): y_actual_result = np.vstack((y_actual_result, y_test[i]))```

## Recall

`#Recall y_actual_result = y_actual_result.flatten() count = 0 for result in y_actual_result: if(result == 1): count=count+1 print ("true yes|predicted yes:") print (count/float(len(y_actual_result)))`

## Area Under the Curve

`plt.figure(1) lw = 2 plt.plot(fpr_dt, tpr_dt, color='green', lw=lw, label='Decision Tree(AUC = %0.2f)' % roc_auc_dt) plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title('Area Under Curve') plt.legend(loc="lower right") plt.show()`

## Confusion Matrix

```print (confusion_matrix(y_test, predictions)) accuracy_score(y_test, predictions) import itertools from sklearn.metrics import confusion_matrix def plot_confusion_matrix(model, normalize=False): # This function prints and plots the confusion matrix. cm = confusion_matrix(y_test, model, labels=[0, 1]) classes=["Success", "Default"] cmap = plt.cm.Blues title = "Confusion Matrix" if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] cm = np.around(cm, decimals=3) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.tight_layout() plt.ylabel('True label') plt.xlabel('Predicted label')```

`plt.figure(figsize=(6,6)) plot_confusion_matrix(predictions, normalize=False) plt.show()`

### Pruning of the tree¶

`from sklearn.tree._tree import TREE_LEAF`

`def prune_index(inner_tree, index, threshold): if inner_tree.value[index].min() < threshold: # turn node into a leaf by "unlinking" its children inner_tree.children_left[index] = TREE_LEAF inner_tree.children_right[index] = TREE_LEAF # if there are shildren, visit them as well if inner_tree.children_left[index] != TREE_LEAF: prune_index(inner_tree, inner_tree.children_left[index], threshold) prune_index(inner_tree, inner_tree.children_right[index], threshold)`

`print(sum(model_dt.tree_.children_left < 0)) # start pruning from the root prune_index(model_dt.tree_, 0, 5) sum(model_dt.tree_.children_left < 0)`

`#It means that the code has created 17 new leaf nodes #(by practically removing links to their ancestors). The tree, which has looked before like`

`from sklearn.externals.six import StringIO from IPython.display import Image from sklearn.tree import export_graphviz import pydotplus plt.figure(figsize=(6,6)) dot_data = StringIO() export_graphviz(model_dt, out_file=dot_data, filled=True, rounded=True, special_characters=True) graph = pydotplus.graph_from_dot_data(dot_data.getvalue()) Image(graph.create_png())`

Learnbay provides industry accredited data science courses in Bangalore. We understand the conjugation of technology in the field of Data science hence we offer significant courses like Machine learning, Tensor Flow, IBM Watson, Google Cloud platform, Tableau, Hadoop, time series, R and Python. With authentic real-time industry projects. Students will be efficient by being certified by IBM. Around hundreds of students are placed in promising companies for data science roles. Choosing Learnbay you will reach the most aspiring job of present and future.
Learnbay data science course covers Data Science with Python, Artificial Intelligence with Python, Deep Learning using Tensor-Flow. These topics are covered and co-developed with IBM.

## Human Activity recognition:

In this case study, we design a model by which a smartphone can detect its owner’s activity precisely. Human activity recognition with a smartphone is a very famous ML project. It is a wellness approach for a human.  Human activity is a very exciting project for AI.

Most of the smartphones have two smart sensors accelerometer and gyroscope, which is an IoT sensor. With the help of the IoT devices captures the activity of a human. The data of human activity collected through the IoT sensor. The two smartphone sensors are accelerometer and gyroscope. Accelerometer collects the data of mobile movement such as move landscape and portrait when playing mobile games and gyroscope measure the rotational movement.

An example that a smartphone has an android app that reads the accelerometers and gyroscope which can predict the human activity that he/she walking normally, walking upstairs, walking downstairs, laying down, sitting all these are the human activities.  Some of the accelerometer and gyroscope measures heart rate, calories burned, etc. by reading all the human activities these tells how much work have done in a day by the human this is also the area of the internet of things(IoT).

### Working of Human task project:

1. Human activity recognition: With the help of sensors we collect the data of body movement which is captured by the smartphone. Movements are often indoor activities such as walking, walking upstairs, walking downstairs, lying down, sitting and standing. The data have recorded for the prediction of the data.

2. Data set collection of activity: The data was collected from the 30 volunteers aged between 19 to 48 performing the activities mentioned above while wearing a smartphone on waist. The example video is given below to understand Subject performing the activities and the movement data was labeled manually.

3. Human Activity Recognition Using Smartphones Data Set: The experiments have been carried out with a group of 30 volunteers within an age bracket of 19-48 years. Each person performed six activities (WALKING, WALKING_UPSTAIRS, WALKING_DOWNSTAIRS, SITTING, STANDING, LAYING) wearing a smartphone (Samsung Galaxy S II) on the waist. Using its embedded accelerometer and gyroscope, we captured 3-axial linear acceleration and 3-axial angular velocity at a constant rate of 50Hz. The experiments have been video-recorded to label the data manually. The obtained dataset has been randomly partitioned into two sets, where 70% of the volunteers were selected for generating the training data and 30% the test data. The sensor signals (accelerometer and gyroscope) were pre-processed by applying noise filters and then sampled in fixed-width sliding windows of 2.56 sec and 50% overlap (128 readings/window). The sensor acceleration signal, which has gravitational and body motion components, was separated using a Butterworth low-pass filter into body acceleration and gravity. The gravitational force is assumed to have only low-frequency components, therefore a filter with 0.3 Hz cutoff frequency was used. From each window, a vector of features was obtained by calculating variables from the time and frequency domain.

• There are “train” and “test” folders containing the split portions of the data for modeling (e.g. 70%/30%).
• There is a “txt” file that contains a detailed technical description of the dataset and the contents of the unzipped files.
• There is a “txt” file that contains a technical description of the engineered features.

The contents of the “train” and “test” folders are similar (e.g. folders and file names), although with differences in the specific data they contain.

Load  set data and process it:

Important libraries to import for data processing

`#start with some necessary imports import numpy as np import pandas as pd from google.colab import files uploaded = files.upload()`

google.colab used to fetch the data from the collaborator files.

` train_data = pd.read_csv("train.csv") train_data.head()`

we select the training data set for the modeling.

`train_data.Activity.value_counts() train_data.shape`

The above function defines how many rows and columns the dataset have.

` train_data.describe()  `

It describes that there are (8 rows and 563 columns) with all the features of the data. For numeric data, the result’s index will include `count``mean``std``min``max` as well as lower, `50` and upper percentiles. By default the lower percentile is `25` and the upper percentile is `75`. The `50` percentile is the same as the median.

` uploaded = files.upload() test_data = pd.read_csv('test.csv') test_data.head()`

Here we read the csv file to analyze the data set and the operation which is supposed to be programmed. head()
shows the first 5 rows with their respective columns so here we have (5 rows and 563 columns).

`# suffling data from sklearn.utils import shuffle`
`# test = shuffle(test) train_data = shuffle(train_data)`

Shuffling data serves the purpose of reducing variance and making sure that models remain general and overfit less.
The obvious case where you’d shuffle your data is if your data is sorted by their class/target. Here, you will want to shuffle to make sure that your training/test/validation sets are representative of the overall distribution of the data.

`# separating data inputs and output lables trainData = train_data.drop('Activity' , axis=1).values trainLabel = train_data.Activity.values`
`testData = test_data.drop('Activity' , axis=1).values testLabel = test_data.Activity.values print(testLabel)`

By using the above code we separate the input and output, here it determines the human activities which are captured by the IoT device. The human activities walking, standing, walking upstairs, walking downstairs, sitting and lying down are got separated to optimize the result.

`# encoding labels from sklearn import preprocessing`
`encoder = preprocessing.LabelEncoder()`
`# encoding test labels encoder.fit(testLabel) testLabelE = encoder.transform(testLabel)`
`# encoding train labels encoder.fit(trainLabel) trainLabelE = encoder.transform(trainLabel)`

Holds the label for each class. encode categorical features using a one-hot or ordinal encoding scheme. It can also be used to transform non-numerical labels (as long as they are hashable and comparable) to numerical labels.

`# applying supervised neural network using multi-layer preceptron import sklearn.neural_network as nn mlpSGD = nn.MLPClassifier(hidden_layer_sizes=(90,) \ , max_iter=1000 , alpha=1e-4 \ , solver='sgd' , verbose=10 \ , tol=1e-19 , random_state=1 \ , learning_rate_init=.001) `
`mlpADAM = nn.MLPClassifier(hidden_layer_sizes=(90,) \ , max_iter=1000 , alpha=1e-4 \ , solver='adam' , verbose=10 \ , tol=1e-19 , random_state=1 \ , learning_rate_init=.001) ``nnModelSGD = mlpSGD.fit(trainData , trainLabelE)`
`y_pred = mlpSGD.predict(testData).reshape(-1,1) #print(y_pred) from sklearn.metrics import classification_report print(classification_report(testLabelE, y_pred))  `

`import matplotlib.pyplot as plt import seaborn as sns fig = plt.figure(figsize=(32,24)) ax1 = fig.add_subplot(221) ax1 = sns.stripplot(x='Activity', y=sub_01.iloc[:,0], data=sub_01, jitter=True) ax2 = fig.add_subplot(222) ax2 = sns.stripplot(x='Activity', y=sub_01.iloc[:,1], data=sub_01, jitter=True) plt.show() `

`fig = plt.figure(figsize=(32,24)) ax1 = fig.add_subplot(221) ax1 = sns.stripplot(x='Activity', y=sub_01.iloc[:,2], data=sub_01, jitter=True) ax2 = fig.add_subplot(222) ax2 = sns.stripplot(x='Activity', y=sub_01.iloc[:,3], data=sub_01, jitter=True) plt.show()`

` `