Import necessary libraries from sklearn.modelselection import traintestsplit from…
- User
Import necessary libraries
from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.ensemble import BaggingRegressor from sklearn.neural_network import MLPRegressor from sklearn.metrics import mean_squared_error, r2_score import numpy as np import matplotlib.pyplot as plt
Load the data
df = pd.read_csv('your_data.csv')
Define the features (X) and target (y)
X = df.drop('NPS', axis=1) y = df['NPS']
Split the data into training and testing sets (80% for training and 20% for testing)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Scale the data using StandardScaler
scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test)
Create a neural network model with bagging
bagging_model = BaggingRegressor(MLPRegressor(hidden_layer_sizes=(64, 32), max_iter=5000, learning_rate_init=0.01, activation='relu', solver='adam'), n_estimators=100)
Fit the model to the training data
bagging_model.fit(X_train, y_train)
Make predictions on the test data
y_pred_bagging = bagging_model.predict(X_test)
Evaluate the model
mse_bagging = mean_squared_error(y_test, y_pred_bagging) r2_bagging = r2_score(y_test, y_pred_bagging)
print(f'Bagging Mean Squared Error: {mse_bagging:.2f}') print(f'Bagging R^2 Score: {r2_bagging:.2f}')
Plot the results
plt.scatter(y_test, y_pred_bagging) plt.xlabel('Actual Values') plt.ylabel('Predicted Values') plt.title('Bagging Results') plt.show() mostre mais graficos
- Assistant
To visualize and evaluate the performance of your BaggingRegressor model further, you can create additional plots such as a residual plot, a histogram of the residuals, and a comparison of the predicted vs. actual values line plot. Here's how you can implement those:
# Residual plot residuals = y_test - y_pred_bagging plt.figure(figsize=(10, 6)) plt.scatter(y_pred_bagging, residuals) plt.axhline(y=0, color='r', linestyle='--') plt.xlabel('Predicted Values') plt.ylabel('Residuals') plt.title('Residuals vs. Predicted Values') plt.show() # Histogram of the residuals plt.figure(figsize=(10, 6)) plt.hist(residuals, bins=30, edgecolor='k', alpha=0.7) plt.xlabel('Residuals') plt.ylabel('Frequency') plt.title('Histogram of Residuals') plt.show() # Predicted vs. Actual line plot plt.figure(figsize=(10, 6)) plt.plot(y_test.values, label='Actual Values', color='blue', marker='o') plt.plot(y_pred_bagging, label='Predicted Values', color='red', linestyle='--', marker='x') plt.xlabel('Sample Index') plt.ylabel('NPS') plt.title('Predicted vs Actual Values') plt.legend() plt.show() - User
Residual plot
residuals = y_test - y_pred_bagging plt.figure(figsize=(10, 6)) plt.scatter(y_pred_bagging, residuals) plt.axhline(y=0, color='r', linestyle='--') plt.xlabel('Predicted Values') plt.ylabel('Residuals') plt.title('Residuals vs. Predicted Values') plt.show()
Histogram of the residuals
plt.figure(figsize=(10, 6)) plt.hist(residuals, bins=30, edgecolor='k', alpha=0.7) plt.xlabel('Residuals') plt.ylabel('Frequency') plt.title('Histogram of Residuals') plt.show()
Predicted vs. Actual line plot
plt.figure(figsize=(10, 6)) plt.plot(y_test.values, label='Actual Values', color='blue', marker='o') plt.plot(y_pred_bagging, label='Predicted Values', color='red', linestyle='--', marker='x') plt.xlabel('Sample Index') plt.ylabel('NPS') plt.title('Predicted vs Actual Values') plt.legend() plt.show() adicione mais garficos
- Assistant
- User
Import necessary libraries
from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.ensemble import BaggingRegressor from sklearn.neural_network import MLPRegressor from sklearn.metrics import mean_squared_error, r2_score import numpy as np import matplotlib.pyplot as plt
Load the data
df = pd.read_csv('your_data.csv')
- Assistant
- User
crie versao em portugues
- Assistant