Leveraging Jupyter notebooks

Jupyter notebooks are arguably the most-used tool in Python scientific computing and data science projects. In this section, we will briefly discuss the basics of Jupyter notebooks as well as the reasons why it is a great tool for data analysis purposes. Then, we will consider the way PyCharm supports the usage of these notebooks.

We will be working with the code examples from the Chapter12/JupyterNotebooks folder of this book's code repository. In its requirements.txt file, we have Pandas, NumPy, Matplotlib, and Jupyter as the external libraries that need to be installed. Whether you are creating a new project or importing the folder into your PyCharm, go ahead and install those libraries in your environment.

Even though we will be writing code in Jupyter notebooks, it is beneficial to first consider a bare-bones program in a traditional Python script so that we can fully appreciate the advantages of using a notebook later on. Let's look at the main.py file and see how we can work with it. We can see that this file contains the same program from the previous section, where we randomly generate a dataset of three attributes (x, y, and z) and consider their correlation matrix:

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


# Generate sample data
x = np.random.rand(50,)
y = x * 2 + np.random.normal(0, 0.3, 50)
z = np.random.rand(50,)

df = pd.DataFrame({
'x': x,
'y': y,
'z': z
})

# Compute and show correlation matrix
corr_mat = df.corr()

plt.matshow(corr_mat)
plt.show()

In addition to this, we also have two extra lines of code to show a scatter plot of x and y:

# Plot x and y
plt.scatter(df['x'], df['y'])
plt.show()

Because of the way we generated these two data columns, the scatter plot will most likely produce a nice relationship. When the program is run, we would roughly obtain the following plot at the end:

Sample scatter plot

We will come back to this program during our discussions in the following subsections.

Now, for those who are unfamiliar with Jupyter notebooks, let's move on to our first subsection, where we will be discussing the fundamentals. If, on the other hand, you are ready to learn how to integrate Jupyter into PyCharm, you can skip to the Jupyter notebooks in PyCharm section of this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.77.71