Book Image

Python Machine Learning Cookbook

By : Prateek Joshi, Vahid Mirjalili
Book Image

Python Machine Learning Cookbook

By: Prateek Joshi, Vahid Mirjalili

Overview of this book

Machine learning is becoming increasingly pervasive in the modern data-driven world. It is used extensively across many fields such as search engines, robotics, self-driving cars, and more. With this book, you will learn how to perform various machine learning tasks in different environments. We’ll start by exploring a range of real-life scenarios where machine learning can be used, and look at various building blocks. Throughout the book, you’ll use a wide variety of machine learning algorithms to solve real-world problems and use Python to implement these algorithms. You’ll discover how to deal with various types of data and explore the differences between machine learning paradigms such as supervised and unsupervised learning. We also cover a range of regression techniques, classification algorithms, predictive modeling, data visualization techniques, recommendation engines, and more with the help of real-world examples.
Table of Contents (19 chapters)
Python Machine Learning Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Plotting 3D scatter plots


In this recipe, we will learn how to plot 3D scatterplots and visualize them in three dimensions.

How to do it…

  1. Create a new Python file, and import the following packages:

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
  2. Create the empty figure:

    # Create the figure
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
  3. Define the number of values that we should generate:

    # Define the number of values
    n = 250
  4. Create a lambda function to generate values in a given range:

    # Create a lambda function to generate the random values in the given range
    f = lambda minval, maxval, n: minval + (maxval - minval) * np.random.rand(n)
  5. Generate X, Y, and Z values using this function:

    # Generate the values
    x_vals = f(15, 41, n)
    y_vals = f(-10, 70, n)
    z_vals = f(-52, -37, n)
  6. Plot these values:

    # Plot the values
    ax.scatter(x_vals, y_vals, z_vals, c='k', marker='o')
    ax.set_xlabel('X axis')
    ax.set_ylabel('Y axis')
    ax.set_zlabel('Z axis')
    
    plt.show...