Untitled
unknown
plain_text
6 months ago
1.7 kB
3
Indexable
# Import necessary libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures # Load the dataset dataset = pd.read_csv('/mnt/data/HeightVsWeight1.csv') # Adjust path if needed X = dataset.iloc[:, 0:1].values # Height (independent variable) y = dataset.iloc[:, 1].values # Weight (dependent variable) # Train a Linear Regression model linearReg = LinearRegression() linearReg.fit(X, y) # Train a Polynomial Regression model (Degree 4 for a smooth curve) polyReg = PolynomialFeatures(degree=4) X_poly = polyReg.fit_transform(X) polyLinearReg = LinearRegression() polyLinearReg.fit(X_poly, y) # Visualize the Linear Regression Results plt.scatter(X, y, color='red', label='Data Points') # Original data points plt.plot(X, linearReg.predict(X), color='blue', label='Linear Regression') # Linear regression line # Visualize the Polynomial Regression Results plt.plot(X, polyLinearReg.predict(X_poly), color='green', label='Polynomial Regression (Degree 4)') # Polynomial regression curve plt.title('Height vs Weight') plt.xlabel('Height') plt.ylabel('Weight') plt.legend() plt.show() # Predict weights for specific heights (replace with any heights you want to test) heights = [[60], [65], [70]] # Example heights predictions_linear = linearReg.predict(heights) predictions_poly = polyLinearReg.predict(polyReg.fit_transform(heights)) # Display predictions for height, pred_lin, pred_poly in zip(heights, predictions_linear, predictions_poly): print(f"Height: {height[0]} -> Linear Prediction: {pred_lin:.2f}, Polynomial Prediction: {pred_poly:.2f}")
Editor is loading...
Leave a Comment