- import matplotlib.pyplot as plt
- # Given dataset
- features = [2, 4, 5, 6]
- animal_types = [6, 5, 9, 10]
- # Step 1: Calculate the means of the features and animal types
- mean_x = sum(features) / len(features)
- mean_y = sum(animal_types) / len(animal_types)
- # Step 2: Calculate the slope (m) and intercept (b) manually
- numerator = sum([(features[i] - mean_x) * (animal_types[i] - mean_y) for i in range(len(features))])
- denominator = sum([(features[i] - mean_x) ** 2 for i in range(len(features))])
- slope = numerator / denominator
- intercept = mean_y - (slope * mean_x)
- # Step 3: Predict the animal type for feature = 8
- feature_to_predict = 8
- predicted_animal_type = slope * feature_to_predict + intercept
- # Step 4: Plot the data points and the regression line
- plt.scatter(features, animal_types, color='blue', label='Data points')
- plt.plot(features, [slope * x + intercept for x in features], color='red', label='Regression line')
- plt.scatter([feature_to_predict], [predicted_animal_type], color='green', label=f'Prediction for feature={feature_to_predict}\nPredicted type={predicted_animal_type:.2f}')
- plt.xlabel('Feature')
- plt.ylabel('Animal Type')
- plt.legend()
- plt.title('Linear Regression: Feature vs Animal Type')
- plt.grid(True)
- plt.show()
- # Output the slope, intercept, and predicted value
- print(f"Slope (m): {slope}")
- print(f"Intercept (b): {intercept}")
- print(f"Predicted Animal Type for Feature = {feature_to_predict}: {predicted_animal_type}")