[text] J

Viewer

  1. import pandas as pd
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.linear_model import LinearRegression
  4. from sklearn.metrics import mean_squared_error
  5.  
  6. # Load the data
  7. regression_data = pd.read_excel('your_file.xlsx', sheet_name='Regression Data')
  8.  
  9. # Separate features (X) and target variable (y)
  10. X = regression_data.drop(columns=['charges'])
  11. y = regression_data['charges']
  12.  
  13. # Split the data into train and test sets (80% train, 20% test)
  14. X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
  15.  
  16. # Train the model
  17. model = LinearRegression()
  18. model.fit(X_train, y_train)
  19.  
  20. # Predict on the test set
  21. y_pred = model.predict(X_test)
  22.  
  23. # Evaluate the model
  24. mse = mean_squared_error(y_test, y_pred)
  25. print("Mean Squared Error:", mse)

Editor

You can edit this paste and save as new:


File Description
  • J
  • Paste Code
  • 27 Apr-2024
  • 800 Bytes
You can Share it: