- import pandas as pd
- from sklearn.model_selection import train_test_split
- from sklearn.compose import ColumnTransformer
- from sklearn.preprocessing import OneHotEncoder
- from sklearn.linear_model import LinearRegression
- from sklearn.metrics import mean_squared_error
- # Load the data
- regression_data = pd.read_excel('your_file.xlsx', sheet_name='Regression Data')
- # Separate features (X) and target variable (y)
- X = regression_data.drop(columns=['charges'])
- y = regression_data['charges']
- # Split the data into train and test sets (80% train, 20% test)
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
- # Identify categorical columns
- categorical_cols = [col for col in X_train.columns if X_train[col].dtype == 'object']
- # Apply one-hot encoding to categorical columns
- preprocessor = ColumnTransformer(
- transformers=[('cat', OneHotEncoder(), categorical_cols)],
- remainder='passthrough')
- # Fit and transform the training data
- X_train_encoded = preprocessor.fit_transform(X_train)
- # Transform the test data
- X_test_encoded = preprocessor.transform(X_test)
- # Train the model
- model = LinearRegression()
- model.fit(X_train_encoded, y_train)
- # Predict on the test set
- y_pred = model.predict(X_test_encoded)
- # Evaluate the model
- mse = mean_squared_error(y_test, y_pred)
- print("Mean Squared Error:", mse)
[text] B
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- B
- Paste Code
- 27 Apr-2024
- 1.35 Kb
You can Share it:
Latest Code Pastes