here is the python code import numpy as np import pandas as pd from sklearn.linearmodel…
- User
here is the python code import numpy as np import pandas as pd from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score from sklearn.model_selection import train_test_split, cross_val_score from sklearn.model_selection import GridSearchCV
Load the dataset
data = pd.read_excel('Data20231118.xlsx', index_col=0, sheet_name='value')
Check statistical aspects of the dataframe
data.describe()
Check the type of each column
data.info()
Check column names
data.columns
Split the data into predictor variables (X) and target variables (y)
X = data[['Temperature', 'Humidity', 'Rainfall', 'AirIndex']] y = data[['Adm_0_5','Adm_6_11','Adm_12_17','Adm_18_49','Adm_50_64','Adm_65_higher']]
Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Create a Linear Regression model for each target variable
models = {} y_preds = {} mse = {} rmse = {} mae = {} r2 = {}
for column in y.columns: model = LinearRegression() model.fit(X_train, y_train[column]) models[column] = model
y_pred = model.predict(X_test) y_preds[column] = y_pred mse[column] = mean_squared_error(y_test[column], y_pred) rmse[column] = np.sqrt(mse[column]) mae[column] = mean_absolute_error(y_test[column], y_pred) r2[column] = r2_score(y_test[column], y_pred)Print evaluation metrics for each target variable
for column in y.columns: print('Evaluation metrics for', column) print('Mean Squared Error (MSE):', mse[column]) print('Root Mean Squared Error (RMSE):', rmse[column]) print('Mean Absolute Error (MAE):', mae[column]) print('R-squared:', r2[column]) print()
Hyperparameter Tuning using cross-validation
params = {'fit_intercept': [True, False], 'normalize': [True, False], 'copy_X': [True, False]}
best_params = {} best_score = {}
for column in y.columns: grid_search = GridSearchCV(models[column], params, cv=5) grid_search.fit(X_train, y_train[column])
best_params[column] = grid_search.best_params_ best_score[column] = grid_search.best_score_Print best parameters and score for each target variable
for column in y.columns: print('Best Parameters for', column) print('Best Parameters:', best_params[column]) print('Best Score (R-squared):', best_score[column]) print() I ask you later
- Assistant
Sure, feel free to ask any questions or clarify any doubts you may have later. I'm here to help!
- User
- Assistant
The error message you encountered is due to providing an invalid parameter 'normalize' for the LinearRegression() estimator. The valid parameters for LinearRegression are 'copy_X', 'fit_intercept', 'n_jobs', and 'positive'.
To resolve this issue, you can update the 'params' variable in your code to only include valid parameters for LinearRegression. For example:
params = {'fit_intercept': [True, False], 'copy_X': [True, False]}By removing 'normalize' from the 'params' dictionary, the GridSearchCV should run without errors.
- User
but now is cannot provide the result "print('Best Parameters:', best_params[column])". it just return 'Best Parameters: {'copy_X': True, 'fit_intercept': False}"
- Assistant
If you are only seeing the 'Best Parameters' output as
'Best Parameters: {'copy_X': True, 'fit_intercept': False}', it means that the GridSearchCV did not find any better parameters than the default ones for the selected target variable.In this case, the default parameters are
'copy_X': Trueand'fit_intercept': False. It suggests that these default parameters already provide the best fit for your linear regression model, and no further parameter tuning is necessary.If you want to explore different parameters for your LinearRegression model, you can add more options to the
paramsdictionary. For example:params = {'fit_intercept': [True, False], 'copy_X': [True, False], 'normalize': [True, False] }This will include the
'normalize'parameter in the search space. However, keep in mind that the'normalize'parameter is not a valid parameter for the LinearRegression model and may result in an error.