Lesson Introduction

Machine learning! You’ve probably heard this term a lot. But what exactly is it? Think of it as teaching a computer to learn from data and make decisions or predictions based on that data. This is like teaching a child to recognize different objects by showing them examples.

In this lesson, our goal is to understand the basics of a machine learning project. We’ll generate data, visualize it, and understand the relationships within it.

Data Generation

Let’s start by generating some data. In real-life projects, the first step is to collect data, but we'll create synthetic (fake) data for our learning purposes using NumPy.

Why random data? It simulates different scenarios and creates a controlled environment for learning. Don't worry, in the end of this course we will work with the real data as well.

We'll use NumPy to generate areas of houses (in square feet) and their prices:

Python
import numpy as np

# Set random seed for reproducibility
np.random.seed(42)

# Generate synthetic data
num_samples = 100
area = np.random.uniform(500, 3500, num_samples)  # House area in square feet
area = np.round(area, 2)  # Round to 2 decimal places

# Assume a linear relationship: price = base_price + (area * price_per_sqft)
base_price = 50000
price_per_sqft = 200
noise = np.random.normal(0, 25000, num_samples)  # Adding some noise
price = base_price + (area * price_per_sqft) + noise
price = np.round(price, 2)  # Round to 2 decimal places

# Display a few generated data points for verification
print("Area (sq ft):", area[:5])  # Area (sq ft): [1623.62 3352.14 2695.98 2295.98  968.06]
print("Price ($):", price[:5])  # Price ($): [376900.18 712952.82 591490.02 459506.78 238120.2]

Real-life example: Imagine you want to predict house prices in your neighborhood. The area of the house affects the price. We simulate this by creating a simple linear relationship but add noise to make it realistic.

Let's break down the data generation:

  1. Generate House Areas: Creates 100 random house areas between 500 and 3500 square feet.

  2. Define Price Relationship:

    • Base price: A constant starting price.
    • Price per square foot: A fixed price per unit area.
    • Noise: Adds variability to simulate real-world data.
  3. Calculate Prices: Computes the final prices based on the area, base price, price per square foot, and added noise.

This method creates a realistic dataset with variable house prices based on their areas.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal