""" File: SimpleNNMPLCbatrem.py Author : Eric Lafon for LASTIG Last Update: 07/07/2023…
- User
""" File: Simple_NN_MPLC_batrem.py Author : Eric Lafon for LASTIG Last Update: 07/07/2023 """
############## # Connection # ##############
Librairies
import geopandas as gpd import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split from sklearn.preprocessing import OneHotEncoder from sqlalchemy import create_engine
Informations de connexion
user = "admin" password = "admin" host = "127.0.0.1" dbname = "postgres" schema = "batrem" table_name = "batrem_44_test"
Connexion à la base de données
engine = create_engine(f'postgresql://{user}:{password}@{host}/{dbname}') conn = engine.connect()
Lecture de la table avec pandas
data = pd.read_sql_table(table_name, con=conn, schema=schema)
###########################
Préparation des données
###########################
Création du dataframe du jeu de données tabulaires
data = data[['id','NATURE', 'USAGE1', 'USAGE2','NB_LOGTS', 'NB_ETAGES','hauteur', 'superficie','perimetre','indicateur_forme','Granularite','convexite'
, 'bat_moy_s_500','bat_moy_s_125','bat_moy_s_250','bat_moy_h_500','bat_moy_h_250', 'bat_moy_h_125','Remarquable']] data.rename(columns=str.lower, inplace=True)Sélection des bâtiments remarquables + 3000 entités non-remarquables aléatoirement
remarquable = data[data['remarquable']== 1] non_remarquable = data[data['remarquable'] == 0] non_remarquable = non_remarquable.sample(n=3387, random_state=66) data=pd.concat([remarquable,non_remarquable]) data.sort_values(by="id", inplace=True) data.reset_index(drop=True, inplace=True)
Remplissage des valeurs manquantes
data = data.fillna(9999)
####################
Machine Learning
####################
Sélection du modèle
mplc=
Séparation des variables à prédire du reste du jeu de données
X = data.drop('remarquable', axis=1) y = data['remarquable']
#One Hot Encoder
Encodage des variables étiquettes en str
categorical_columns = ['nature', 'usage1', 'usage2'] X[categorical_columns] = X[categorical_columns].astype(str) encoder = OneHotEncoder(sparse=False, handle_unknown='ignore') X_encoded = encoder.fit_transform(X[categorical_columns]) X_encoded = pd.DataFrame(X_encoded, columns=encoder.get_feature_names_out(categorical_columns)) X = pd.concat([X.drop(categorical_columns, axis=1), X_encoded], axis=1)
Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
- Assistant
Training the model
Fit the model to the training data
mplc.fit(X_train, y_train)
Predict the labels for the test data
y_pred = mplc.predict(X_test)
Compute the accuracy of the model
accuracy = mplc.score(X_test, y_test) print("Accuracy:", accuracy)
Generate a classification report
classification_report = classification_report(y_test, y_pred) print("Classification Report:\n", classification_report)
- User
ca you provide me the right parameters for model selection ?
- Assistant
Sure! Here are some key parameters to consider for model selection in the MLPClassifier:
-
hidden_layer_sizes: This parameter determines the number of hidden layers and the number of neurons in each hidden layer. It is specified as a tuple, where each element represents the number of neurons in that particular hidden layer. For example, hidden_layer_sizes=(100, 50) represents a neural network with 2 hidden layers, the first with 100 neurons and the second with 50 neurons.
-
activation: This parameter specifies the activation function for the hidden layers. Common options include "relu" (rectified linear unit), "tanh" (hyperbolic tangent), and "logistic" (sigmoid). The choice of activation function depends on the problem and the data.
-
solver: This parameter determines the algorithm used for weight optimization. Common options include "adam" (stochastic gradient-based optimizer), "lbfgs" (quasi-Newton optimizer), and "sgd" (stochastic gradient descent). "adam" generally performs well in most cases.
-
alpha: This parameter determines the regularization strength. It penalizes complex models to avoid overfitting. A higher value of alpha increases the amount of regularization.
-
learning_rate: This parameter determines the learning rate schedule for weight updates. Common options include "constant" (constant learning rate), "invscaling" (gradually decreasing learning rate), and "adaptive" (keeps the learning rate constant as long as the training loss keeps decreasing).
-
max_iter: This parameter determines the maximum number of iterations for the solver to converge. It is important to set a sufficient number to ensure convergence.
You can experiment with different combinations of these parameters using grid search or random search to find the best performing model for your specific problem.
Remember that there is no "one-size-fits-all" solution, and the best model parameters may vary depending on the characteristics of your dataset.
-
- User
can you add these parameters in the code ?
- Assistant
Certainly! Here's an example of how you can add these parameters to the code:
# Initialize the MLPClassifier with the desired parameters mplc = MLPClassifier(hidden_layer_sizes=(100, 50), activation='relu', solver='adam', alpha=0.001, learning_rate='constant', max_iter=200) # Fit the model to the training data mplc.fit(X_train, y_train) # Predict the labels for the test data y_pred = mplc.predict(X_test) # Compute the accuracy of the model accuracy = mplc.score(X_test, y_test) print("Accuracy:", accuracy) # Generate a classification report classification_report = classification_report(y_test, y_pred) print("Classification Report:\n", classification_report)In this example, I have set the hidden_layer_sizes to (100, 50) to have 2 hidden layers with 100 and 50 neurons respectively. The activation function is set to 'relu', the solver is set to 'adam', the regularization strength (alpha) is set to 0.001, the learning_rate is set to 'constant', and the maximum number of iterations (max_iter) is set to 200.
Feel free to modify these parameter values according to your needs and the characteristics of your dataset.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.