Select Git revision
HelpView.js
Forked from an inaccessible project.
-
Mehdi Zaidi authoredMehdi Zaidi authored
ml_rn.py 2.22 KiB
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
import time
from sklearn.neural_network import MLPRegressor
def load_and_describe_data(file_path):
"""
Charge un fichier CSV.
:param file_path: Chemin du fichier CSV
:return: DataFrame Pandas
"""
df = pd.read_csv(file_path)
return df
df = load_and_describe_data('data_sup_0popularity.csv')
print(df.info())
def train_mlp(df):
# 1. Séparation des features et de la cible
X = df.drop(columns=["popularity", "id", "artists", "name", "release_date", "date_sortie", "duration_ms", "nom_artiste"])
y = df['popularity']
# 2. Séparation train/test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 3. Normalisation des features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# 4. Définition des hyperparamètres à tester
param_grid = {
'hidden_layer_sizes': [(50,), (100,), (100, 50), (100, 100)],
'activation': ['relu', 'tanh'],
'solver': ['adam', 'sgd'],
'learning_rate_init': [0.001, 0.01, 0.1],
'max_iter': [500]
}
# 5. Recherche des meilleurs hyperparamètres avec GridSearchCV
mlp = MLPRegressor(random_state=42)
grid_search = GridSearchCV(mlp, param_grid, cv=3, scoring='r2', n_jobs=-1, verbose=2)
grid_search.fit(X_train_scaled, y_train)
# 6. Affichage des meilleurs paramètres
print("Meilleurs paramètres :", grid_search.best_params_)
# 7. Prédictions avec le meilleur modèle
best_mlp = grid_search.best_estimator_
y_pred = best_mlp.predict(X_test_scaled)
# 8. Évaluation du modèle
mae = mean_absolute_error(y_test, y_pred)
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
r2 = r2_score(y_test, y_pred)
print(f"📊 MLPRegressor Optimisé - MAE: {mae:.2f}, RMSE: {rmse:.2f}, R²: {r2:.3f}")
# 9. Ajout des prédictions au DataFrame original
df.loc[X_test.index, 'pred_mlp'] = y_pred
print(df.head(40))
return df
train_mlp(df)