Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Big Data Analysis with Python

You're reading from   Big Data Analysis with Python Combine Spark and Python to unlock the powers of parallel computing and machine learning

Arrow left icon
Product type Paperback
Published in Apr 2019
Publisher Packt
ISBN-13 9781789955286
Length 276 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Ivan Marin Ivan Marin
Author Profile Icon Ivan Marin
Ivan Marin
Sarang VK Sarang VK
Author Profile Icon Sarang VK
Sarang VK
Ankit Shukla Ankit Shukla
Author Profile Icon Ankit Shukla
Ankit Shukla
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Big Data Analysis with Python
Preface
1. The Python Data Science Stack 2. Statistical Visualizations FREE CHAPTER 3. Working with Big Data Frameworks 4. Diving Deeper with Spark 5. Handling Missing Values and Correlation Analysis 6. Exploratory Data Analysis 7. Reproducibility in Big Data Analysis 8. Creating a Full Analysis Report Appendix

Chapter 6: Business Process Definition and Exploratory Data Analysis


Activity 13: Carry Out Mapping to Gaussian Distribution of Numeric Features from the Given Data

  1. Download the bank.csv. Now, use the following commands to read the data from it:

    import numpy as np
    import pandas as pd
    import seaborn as sns
    import time
    import re
    import os
    import matplotlib.pyplot as plt
    sns.set(style="ticks")
    
    # import libraries required for preprocessing
    import sklearn as sk
    from scipy import stats
    from sklearn import preprocessing
    
    # set the working directory to the following
    os.chdir("/Users/svk/Desktop/packt_exercises")
    
    # read the downloaded input data (marketing data)
    df = pd.read_csv('bank.csv', sep=';')
  2. Identify the numeric data from the DataFrame. The data can be categorized according to its type, such as categorical, numeric (float, integer), date, and so on. We identify numeric data here because we can only carry out normalization on numeric data:

    numeric_df = df._get_numeric_data()
    numeric_df.head()

    The output is as follows:

    Figure 6.12: DataFrame

  3. Carry out a normality test and identify the features that have a non-normal distribution:

    numeric_df_array = np.array(numeric_df) # converting to numpy arrays for more efficient computation
    
    loop_c = -1
    col_for_normalization = list()
    
    for column in numeric_df_array.T:
        loop_c+=1
        x = column
        k2, p = stats.normaltest(x) 
        alpha = 0.001
        print("p = {:g}".format(p))
            
        # rules for printing the normality output
        if p < alpha:
            test_result = "non_normal_distr"
            col_for_normalization.append((loop_c)) # applicable if yeo-johnson is used
            
            #if min(x) > 0: # applicable if box-cox is used
                #col_for_normalization.append((loop_c)) # applicable if box-cox is used
            print("The null hypothesis can be rejected: non-normal distribution")
            
        else:
            test_result = "normal_distr"
            print("The null hypothesis cannot be rejected: normal distribution")

    The output is as follows:

    Figure 6.13: Normality test and identify the features

    Note

    The normality test conducted here is based on D'Agostino and Pearson's test (https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.normaltest.html), which combines skew and kurtosis to identify how close the distribution of the features is to a Gaussian distribution. In this test, if the p-value is less than the set alpha value, then the null hypothesis is rejected, and the feature does not have a normal distribution. Here, we look into each column using a loop function and identify the distribution of each feature.

  4. Plot the probability density of the features to visually analyze their distribution:

    columns_to_normalize = numeric_df[numeric_df.columns[col_for_normalization]]
    names_col = list(columns_to_normalize)
    
    # density plots of the features to check the normality
    columns_to_normalize.plot.kde(bw_method=3)

    The density plot of the features to check the normality is as follows:

    Figure 6.14: Plot of features

    Note

    Multiple variables' density plots are shown in the previous graph. The distribution of the features in the graph can be seen with a high positive kurtosis, which is not a normal distribution.

  5. Prepare the power transformation model and carry out transformations on the identified features to convert them to normal distribution based on the box-cox or yeo-johnson method:

    pt = preprocessing.PowerTransformer(method='yeo-johnson', standardize=True, copy=True)
    normalized_columns = pt.fit_transform(columns_to_normalize)
    normalized_columns = pd.DataFrame(normalized_columns, columns=names_col)

    In the previous commands, we prepare the power transformation model and apply it to the data of selected features.

  6. Plot the probability density of the features again after the transformations to visually analyze the distribution of the features:

    normalized_columns.plot.kde(bw_method=3)

    The output is as follows:

    Figure 6.15: Plot of features

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime