single
Simple Linear Regression
Swipe to show menu
Simple linear regression is a foundational statistical method that helps you understand the relationship between two continuous variables: one independent (predictor) and one dependent (response). The goal is to model the dependent variable as a linear function of the independent variable. The general form of the model is:
y=Ξ²0β+Ξ²1βx+Ξ΅where y is the dependent variable, x is the independent variable, Ξ²0β is the intercept, Ξ²1β is the slope, and Ξ΅ is the error term.
To fit a linear regression model in Python using scikit-learn, follow these key steps:
- Prepare your data: organize your predictor (x) and response (y) variables, typically in arrays or pandas Series;
- Import and instantiate the model: use
LinearRegressionfrom scikit-learn; - Fit the model: call the
fit()method with your data to estimate the best-fitting line; - Interpret the results: extract the slope (
coef_) and intercept (intercept_) to understand the relationship.
The slope (Ξ²1β) tells you how much y is expected to change for a one-unit increase in x. The intercept (Ξ²0β) represents the expected value of y when x is zero. By examining these parameters, you can assess both the direction and strength of the relationship.
Visualizing the regression line alongside the data points can help you interpret the model's fit and the nature of the relationship between the variables.
1234567891011121314151617181920212223242526272829import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression # Sample data: hours studied vs exam score hours_studied = np.array([1, 2, 3, 4, 5, 6]).reshape(-1, 1) exam_score = np.array([50, 55, 65, 70, 75, 80]) # Create and fit the linear regression model model = LinearRegression() model.fit(hours_studied, exam_score) # Extract slope and intercept slope = model.coef_[0] intercept = model.intercept_ print("Slope:", slope) print("Intercept:", intercept) # Predict values and plot the regression line predicted_scores = model.predict(hours_studied) plt.scatter(hours_studied, exam_score, color='blue', label='Actual Data') plt.plot(hours_studied, predicted_scores, color='red', label='Regression Line') plt.xlabel('Hours Studied') plt.ylabel('Exam Score') plt.title('Simple Linear Regression: Hours Studied vs Exam Score') plt.legend() plt.show()
Swipe to start coding
Fit a simple linear regression model to a dataset in the global scope.
- Initialize a
LinearRegressionmodel fromsklearn.linear_model. - Fit the model using
ad_spendas the predictor andsales_revenueas the response. Ensure you reshapead_spendto a 2D array utilizing.reshape(-1, 1)since scikit-learn requires 2D arrays for features. - Extract the slope utilizing
model.coef_[0]and assign it to theslopevariable. - Extract the intercept utilizing
model.intercept_and assign it to theinterceptvariable.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat