0
0
SciPydata~30 mins

Least squares optimization in SciPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Least squares optimization
📖 Scenario: You are working as a data scientist helping a small business understand the relationship between advertising spending and sales. You have collected data on advertising budgets and sales figures. Your goal is to find the best straight line that fits this data using least squares optimization.
🎯 Goal: Build a simple least squares optimization model using scipy.optimize.least_squares to find the best line parameters (slope and intercept) that fit the sales data.
📋 What You'll Learn
Create arrays for advertising budgets and sales data
Define a residual function for the least squares method
Use scipy.optimize.least_squares to find the best slope and intercept
Print the optimized slope and intercept values
💡 Why This Matters
🌍 Real World
Least squares optimization is used in many fields like economics, engineering, and science to find the best fit model for data.
💼 Career
Data scientists and analysts often use least squares methods to build predictive models and understand relationships between variables.
Progress0 / 4 steps
1
Create the data arrays
Create two numpy arrays called advertising and sales with these exact values: advertising = [5, 10, 15, 20, 25] and sales = [7, 12, 14, 22, 27].
SciPy
Need a hint?

Use np.array([...]) to create numpy arrays with the given values.

2
Define the residual function
Define a function called residuals that takes params as input. Inside, unpack params into slope and intercept. Return the difference between the predicted sales (slope * advertising + intercept) and the actual sales.
SciPy
Need a hint?

The residuals function calculates how far off the predicted sales are from the actual sales for given slope and intercept.

3
Run least squares optimization
Import least_squares from scipy.optimize. Use least_squares with the residuals function and an initial guess [1, 0] for slope and intercept. Store the result in a variable called result.
SciPy
Need a hint?

Use least_squares(residuals, [1, 0]) to start with slope=1 and intercept=0.

4
Print the optimized parameters
Print the optimized slope and intercept from result.x using print(f"Slope: {result.x[0]:.2f}") and print(f"Intercept: {result.x[1]:.2f}").
SciPy
Need a hint?

Use print(f"Slope: {result.x[0]:.2f}") and print(f"Intercept: {result.x[1]:.2f}") to show results rounded to two decimals.