Book Image

Data Science with Python

By : Rohan Chopra, Aaron England, Mohamed Noordeen Alaudeen
Book Image

Data Science with Python

By: Rohan Chopra, Aaron England, Mohamed Noordeen Alaudeen

Overview of this book

Data Science with Python begins by introducing you to data science and teaches you to install the packages you need to create a data science coding environment. You will learn three major techniques in machine learning: unsupervised learning, supervised learning, and reinforcement learning. You will also explore basic classification and regression techniques, such as support vector machines, decision trees, and logistic regression. As you make your way through the book, you will understand the basic functions, data structures, and syntax of the Python language that are used to handle large datasets with ease. You will learn about NumPy and pandas libraries for matrix calculations and data manipulation, discover how to use Matplotlib to create highly customizable visualizations, and apply the boosting algorithm XGBoost to make predictions. In the concluding chapters, you will explore convolutional neural networks (CNNs), deep learning algorithms used to predict what is in an image. You will also understand how to feed human sentences to a neural network, make the model process contextual information, and create human language processing systems to predict the outcome. By the end of this book, you will be able to understand and implement any new data science algorithm and have the confidence to experiment with tools or libraries other than those covered in the book.
Table of Contents (10 chapters)

Chapter 4: Dimensionality Reduction and Unsupervised Learning

Activity 12: Ensemble k-means Clustering and Calculating Predictions


After the glass dataset has been imported, shuffled, and standardized (see Exercise 58):

  1. Instantiate an empty data frame to append each model and save it as the new data frame object labels_df with the following code:

    import pandas as pd

    labels_df = pd.DataFrame()

  2. Import the KMeans function outside of the loop using the following:

    from sklearn.cluster import KMeans

  3. Complete 100 iterations as follows:

    for i in range(0, 100):

  4. Save a KMeans model object with two clusters (arbitrarily decided upon, a priori) using:

    model = KMeans(n_clusters=2)

  5. Fit the model to scaled_features using the following:

  6. Generate the labels array and save it as the labels object, as follows:

    labels = model.labels_

  7. Store labels as a column in labels_df named after the iteration using the code:

    labels_df['Model_{}_Labels'.format(i+1)] = labels

  8. After labels have been generated for each of the 100 models (see Activity 21), calculate the mode for each row using the following code:

    row_mode = labels_df.mode(axis=1)

  9. Assign row_mode to a new column in labels_df, as shown in the following code:

    labels_df['row_mode'] = row_mode

  10. View the first five rows of labels_df


Figure 4.24: First five rows of labels_df
Figure 4.24: First five rows of labels_df

We have drastically increased the confidence in our predictions by iterating through numerous models, saving the predictions at each iteration, and assigning the final predictions as the mode of these predictions. However, these predictions were generated by models using a predetermined number of clusters. Unless we know the number of clusters a priori, we will want to discover the optimal number of clusters to segment our observations.

Activity 13: Evaluating Mean Inertia by Cluster after PCA Transformation


  1. Instantiate a PCA model with the value for the n_components argument equal to best_n_components (that is, remember, best_n_components = 6) as follows:

    from sklearn.decomposition import PCA

    model = PCA(n_components=best_n_components)

  2. Fit the model to scaled_features and transform them into the six components, as shown here:

    df_pca = model.fit_transform(scaled_features)

  3. Import numpy and the KMeans function outside the loop using the following code:

    from sklearn.cluster import KMeans

    import numpy as np

  4. Instantiate an empty list, inertia_list, for which we will append inertia values after each iteration using the following code:

    inertia_list = []

  5. In the inside for loop, we will iterate through 100 models as follows:

    for i in range(100):

  6. Build our KMeans model with n_clusters=x using:

    model = KMeans(n_clusters=x)


    The value for x will be dictated by the outer loop which is covered in detail here.

  7. Fit the model to df_pca as follows:

  8. Get the inertia value and save it to the object inertia using the following code:

    inertia = model.inertia_

  9. Append inertia to inertia_list using the following code:


  10. Moving to the outside loop, instantiate another empty list to store the average inertia values using the following code:

    mean_inertia_list_PCA = []

  11. Since we want to check the average inertia over 100 models for n_clusters 1 through 10, we will instantiate the outer loop as follows:

    for x in range(1, 11):

  12. After the inside loop has run through its 100 iterations, and the inertia value for each of the 100 models have been appended to inertia_list, compute the mean of this list, and save the object as mean_inertia using the following code:

    mean_inertia = np.mean(inertia_list)

  13. Append mean_inertia to mean_inertia_list_PCA using the following code:


  14. Print mean_inertia_list_PCA to the console using the following code:


  15. Notice the output in the following screenshot:
Figure 4.25: mean_inertia_list_PCA
Figure 4.25: mean_inertia_list_PCA