Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

numpy

Uploaded by

pari.jasbeer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

numpy

Uploaded by

pari.jasbeer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Linear Regression Example


Linear Regression is used to predict a continuous target variable based on input
features.
python
Copy code

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

# Input data: Hours studied


X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)

# Output data: Exam scores


y = np.array([2, 4, 6, 8, 10])

# Create and fit the model


model = LinearRegression()
model.fit(X, y)

# Predict for new values


predictions = model.predict(X)

# Plot the results


plt.scatter(X, y, color='blue', label='Data Points')
plt.plot(X, predictions, color='red', label='Regression Line')
plt.xlabel('Hours Studied')
plt.ylabel('Exam Score')
plt.legend()
plt.show()

# Print the slope (coefficient) and intercept of the line


print("Slope (Coefficient):", model.coef_[0])
print("Intercept:", model.intercept_)

# Predict a new value


new_hours = np.array([[6]])
predicted_score = model.predict(new_hours)
print("Predicted Score for 6 Hours of Study:", predicted_score[0])

Output:
yaml
Copy code
Slope (Coefficient): 2.0
Intercept: 0.0
Predicted Score for 6 Hours of Study: 12.0
Explanation:
 This example trains a simple linear regression model to predict exam
scores based on study hours.
 The graph shows the data points and the fitted regression line.
 The slope (2.0) indicates that for each additional hour studied, the score
increases by 2.

You might also like