DEV Community

Cover image for Start Your ML (Machine Learning) Journey in 10 Minutes with Jupyter on AWS EC2!
Ramkumar M N
Ramkumar M N

Posted on

Start Your ML (Machine Learning) Journey in 10 Minutes with Jupyter on AWS EC2!

Setting Up Jupyter Notebook on an Ubuntu Machine and Running a Simple Machine Learning Program

Jupyter Notebook is an excellent tool for data scientists and machine learning engineers to write and execute Python code interactively. In this blog, we will cover how to install Jupyter Notebook on an Ubuntu machine and create a simple machine learning program using Scikit-learn.

Step 1: Update and Upgrade Ubuntu Packages

Before installing Jupyter, it is always recommended to update the package list to ensure you have the latest software versions.

sudo apt update && sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Python and Pip

Jupyter Notebook requires Python. If you don’t have Python installed, you can install it using the following command:

sudo apt install python3 python3-pip -y
Enter fullscreen mode Exit fullscreen mode

Verify the installation:

python3 --version
pip3 --version
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Virtual Environment (Optional but Recommended)

Using a virtual environment helps to isolate dependencies for different projects.

sudo apt install python3-venv -y
mkdir ml
cd ml
python3 -m venv jupyter_env
source jupyter_env/bin/activate
Enter fullscreen mode Exit fullscreen mode

Step 4: Install Jupyter Notebook

Once inside the virtual environment (or globally if you skipped the previous step), install Jupyter using pip:

pip install jupyter
Enter fullscreen mode Exit fullscreen mode

Step 5: Start Jupyter Notebook

You can now start Jupyter Notebook using the following command:

# for localhost
jupyter notebook
http://127.0.0.1:8888/tree?token=adac8de88e2098f0d6dd324ed2663fdf414fbd88503eb717

# to access public ip
jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser --allow-root
http://<public ip>:8888/tree?token=adac8de88e2098f0d6dd324ed2663fdf414fbd88503eb717
Enter fullscreen mode Exit fullscreen mode

This will start a Jupyter Notebook server and open a new browser tab at http://localhost:8888/ where you can create and manage notebooks.

Step 6: Install Necessary Libraries for Machine Learning

To implement a simple machine learning model, install the required libraries:

pip install numpy pandas scikit-learn matplotlib
Enter fullscreen mode Exit fullscreen mode

Step 7: Create a Simple Machine Learning Program

Open a new Jupyter Notebook and add the following code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Generate some sample data
data_size = 100
X = np.linspace(0, 10, data_size).reshape(-1, 1)
y = 2 * X + np.random.normal(0, 1, (data_size, 1))

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create and train a linear regression model
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions
y_pred = model.predict(X_test)

# Evaluate the model
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

# Plot results
plt.scatter(X_test, y_test, color='blue', label='Actual Data')
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Predicted Line')
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.show()
Enter fullscreen mode Exit fullscreen mode

Step 8: Expected Output:

  • The Mean Squared Error (MSE) value will be printed in the console, indicating the error of the model.
  • A scatter plot displaying the actual data points (in blue) and the predicted regression line (in red).

Step 9: Repository with Working Code and Screenshots

Now you have successfully set up, accessed, and managed Jupyter Notebook on an EC2 instance! πŸš€

Final Output Screenshot of ML Program

Image description

Conclusion

By following these steps, you have successfully set up Jupyter Notebook on an Ubuntu machine and implemented a basic linear regression model using Scikit-learn. You can now explore more advanced machine learning algorithms and data science techniques within Jupyter Notebook.

Drop a comment if you have any questions or need help.

Follow Me for More!

Top comments (0)