0
0
SciPydata~30 mins

Fitting custom models in SciPy - Mini Project: Build & Apply

Choose your learning style9 modes available
Fitting Custom Models with SciPy
📖 Scenario: You work as a data analyst for a small company that collects data on how the temperature affects the sales of ice cream. You want to find a simple mathematical model that fits the sales data so you can predict future sales based on temperature.
🎯 Goal: Build a custom mathematical model and fit it to the given temperature and sales data using SciPy's curve fitting tools. Then, display the fitted model's predicted sales for the given temperatures.
📋 What You'll Learn
Create a dataset of temperatures and corresponding ice cream sales.
Define a custom model function that relates temperature to sales.
Use SciPy's curve_fit function to find the best parameters for the model.
Print the predicted sales using the fitted model.
💡 Why This Matters
🌍 Real World
Fitting custom models helps businesses understand relationships in their data, like how temperature affects sales, so they can make better decisions.
💼 Career
Data scientists and analysts often fit models to data to predict outcomes and find trends, which is essential in many industries.
Progress0 / 4 steps
1
Create the temperature and sales data
Create two lists called temperatures and sales with these exact values: temperatures = [15, 18, 21, 24, 27, 30] and sales = [100, 150, 200, 250, 300, 350].
SciPy
Need a hint?

Use square brackets to create lists and separate values with commas.

2
Define a custom linear model function
Define a function called linear_model that takes three parameters: x, a, and b. The function should return the value of a * x + b.
SciPy
Need a hint?

Define a function using def and return the expression a * x + b.

3
Fit the linear model to the data
Import curve_fit from scipy.optimize. Use curve_fit with the linear_model function, temperatures, and sales to find the best parameters. Store the parameters in a variable called params.
SciPy
Need a hint?

Use from scipy.optimize import curve_fit and call curve_fit(linear_model, temperatures, sales).

4
Print the predicted sales using the fitted model
Use the fitted parameters in params to calculate predicted sales for each temperature in temperatures by calling linear_model. Store the results in a list called predicted_sales. Then print predicted_sales.
SciPy
Need a hint?

Use a list comprehension to apply linear_model to each temperature with the fitted parameters, then print the list.