from sklearn.linearmodel import LogisticRegression from sklearn.ensemble import…
- User
from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC from sklearn.model_selection import cross_val_score
models = { 'Logistic Regression': LogisticRegression(), 'Random Forest': RandomForestClassifier(), 'SVM': SVC(probability=True) }
Validation croisée pour chaque modèle
for name, model in models.items(): scores = cross_val_score(model, X_train, y_train, cv=5) print(f"{name} Accuracy: {scores.mean():.2f} (+/- {scores.std() * 2:.2f})")
Methode 1 ↳ 18 cells hidden Methode 2
id dur proto service state spkts dpkts sbytes dbytes
0 1 0.000011 udp - INT 2 0 496 0
1 2 0.000008 udp - INT 2 0 1762 0
2 3 0.000005 udp - INT 2 0 1068 0
3 4 0.000006 udp - INT 2 0 900 0
4 5 0.000010 udp - INT 2 0 2126 0rate ... ct_dst_sport_ltm ct_dst_src_ltm is_ftp_login \0 90909.0902 ... 1 2 0
1 125000.0003 ... 1 2 0
2 200000.0051 ... 1 3 0
3 166666.6608 ... 1 3 0
4 100000.0025 ... 1 3 0ct_ftp_cmd ct_flw_http_mthd ct_src_ltm ct_srv_dst is_sm_ips_ports
0 0 0 1 2 0
1 0 0 1 2 0
2 0 0 1 3 0
3 0 0 2 3 0
4 0 0 2 3 0attack_cat label
0 Normal 0
1 Normal 0
2 Normal 0
3 Normal 0
4 Normal 0[5 rows x 45 columns]
Dimensions du dataset
print("Dimensions du dataset:", data.shape)
Types de données
print(data.dtypes)
Vérifier les valeurs manquantes
print(data.isnull().sum())
Statistiques descriptives
print(data.describe())
Dimensions du dataset: (82332, 45) id int64 dur float64 proto object service object state object spkts int64 dpkts int64 sbytes int64 dbytes int64 rate float64 sttl int64 dttl int64 sload float64 dload float64 sloss int64 dloss int64 sinpkt float64 dinpkt float64 sjit float64 djit float64 swin int64 stcpb int64 dtcpb int64 dwin int64 tcprtt float64 synack float64 ackdat float64 smean int64 dmean int64 trans_depth int64 response_body_len int64 ct_srv_src int64 ct_state_ttl int64 ct_dst_ltm int64 ct_src_dport_ltm int64 ct_dst_sport_ltm int64 ct_dst_src_ltm int64 is_ftp_login int64 ct_ftp_cmd int64 ct_flw_http_mthd int64 ct_src_ltm int64 ct_srv_dst int64 is_sm_ips_ports int64 attack_cat object label int64 dtype: object id 0 dur 0 proto 0 service 0 state 0 spkts 0 dpkts 0 sbytes 0 dbytes 0 rate 0 sttl 0 dttl 0 sload 0 dload 0 sloss 0 dloss 0 sinpkt 0 dinpkt 0 sjit 0 djit 0 swin 0 stcpb 0 dtcpb 0 dwin 0 tcprtt 0 synack 0 ackdat 0 smean 0 dmean 0 trans_depth 0 response_body_len 0 ct_srv_src 0 ct_state_ttl 0 ct_dst_ltm 0 ct_src_dport_ltm 0 ct_dst_sport_ltm 0 ct_dst_src_ltm 0 is_ftp_login 0 ct_ftp_cmd 0 ct_flw_http_mthd 0 ct_src_ltm 0 ct_srv_dst 0 is_sm_ips_ports 0 attack_cat 0 label 0 dtype: int64 id dur spkts dpkts sbytes
count 82332.000000 82332.000000 82332.000000 82332.000000 8.233200e+04
mean 41166.500000 1.006756 18.666472 17.545936 7.993908e+03
std 23767.345519 4.710444 133.916353 115.574086 1.716423e+05
min 1.000000 0.000000 1.000000 0.000000 2.400000e+01
25% 20583.750000 0.000008 2.000000 0.000000 1.140000e+02
50% 41166.500000 0.014138 6.000000 2.000000 5.340000e+02
75% 61749.250000 0.719360 12.000000 10.000000 1.280000e+03
max 82332.000000 59.999989 10646.000000 11018.000000 1.435577e+07dbytes rate sttl dttl sload \count 8.233200e+04 8.233200e+04 82332.000000 82332.000000 8.233200e+04
mean 1.323379e+04 8.241089e+04 180.967667 95.713003 6.454902e+07
std 1.514715e+05 1.486204e+05 101.513358 116.667722 1.798618e+08
min 0.000000e+00 0.000000e+00 0.000000 0.000000 0.000000e+00
25% 0.000000e+00 2.860611e+01 62.000000 0.000000 1.120247e+04
50% 1.780000e+02 2.650177e+03 254.000000 29.000000 5.770032e+05
75% 9.560000e+02 1.111111e+05 254.000000 252.000000 6.514286e+07
max 1.465753e+07 1.000000e+06 255.000000 253.000000 5.268000e+09... ct_src_dport_ltm ct_dst_sport_ltm ct_dst_src_ltm is_ftp_login \count ... 82332.000000 82332.000000 82332.000000 82332.000000
mean ... 4.928898 3.663011 7.456360 0.008284
std ... 8.389545 5.915386 11.415191 0.091171
min ... 1.000000 1.000000 1.000000 0.000000
25% ... 1.000000 1.000000 1.000000 0.000000
50% ... 1.000000 1.000000 3.000000 0.000000
75% ... 4.000000 3.000000 6.000000 0.000000
max ... 59.000000 38.000000 63.000000 2.000000ct_ftp_cmd ct_flw_http_mthd ct_src_ltm ct_srv_dst \count 82332.000000 82332.000000 82332.000000 82332.000000
mean 0.008381 0.129743 6.468360 9.164262
std 0.092485 0.638683 8.543927 11.121413
min 0.000000 0.000000 1.000000 1.000000
25% 0.000000 0.000000 1.000000 2.000000
50% 0.000000 0.000000 3.000000 5.000000
75% 0.000000 0.000000 7.000000 11.000000
max 2.000000 16.000000 60.000000 62.000000is_sm_ips_ports labelcount 82332.000000 82332.000000
mean 0.011126 0.550600
std 0.104891 0.497436
min 0.000000 0.000000
25% 0.000000 0.000000
50% 0.000000 1.000000
75% 0.000000 1.000000
max 1.000000 1.000000[8 rows x 41 columns]
import matplotlib.pyplot as plt import seaborn as sns from sklearn.preprocessing import LabelEncoder
Encoder les variables catégorielles
categorical_cols = data.select_dtypes(include=['object']).columns data[categorical_cols] = data[categorical_cols].apply(LabelEncoder().fit_transform)
Heatmap des corrélations
plt.figure(figsize=(14, 12)) sns.heatmap(data.corr(), annot=False, cmap='coolwarm') plt.title('Heatmap des corrélations') plt.show()
Remplacer les valeurs manquantes par la moyenne de chaque colonne
data.fillna(data.mean(), inplace=True)
from sklearn.preprocessing import StandardScaler
Séparation des caractéristiques et de la cible
X = data.drop('label', axis=1) y = data['label']
Normalisation des caractéristiques
scaler = StandardScaler() X_scaled = scaler.fit_transform(X)
/usr/local/lib/python3.10/dist-packages/sklearn/linear_model/_logistic.py:460: ConvergenceWarning: lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in: https://scikit-learn.org/stable/modules/preprocessing.html Please also refer to the documentation for alternative solver options: https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression n_iter_i = _check_optimize_result( /usr/local/lib/python3.10/dist-packages/sklearn/linear_model/_logistic.py:460: ConvergenceWarning: lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in: https://scikit-learn.org/stable/modules/preprocessing.html Please also refer to the documentation for alternative solver options: https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression n_iter_i = _check_optimize_result( /usr/local/lib/python3.10/dist-packages/sklearn/linear_model/_logistic.py:460: ConvergenceWarning: lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in: https://scikit-learn.org/stable/modules/preprocessing.html Please also refer to the documentation for alternative solver options: https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression n_iter_i = _check_optimize_result( /usr/local/lib/python3.10/dist-packages/sklearn/linear_model/_logistic.py:460: ConvergenceWarning: lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.
Increase the number of iterations (max_iter) or scale the data as shown in: https://scikit-learn.org/stable/modules/preprocessing.html Please also refer to the documentation for alternative solver options: https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression n_iter_i = _check_optimize_result(
Logistic Regression Accuracy: 0.95 (+/- 0.00) Random Forest Accuracy: 1.00 (+/- 0.00) SVM Accuracy: 0.99 (+/- 0.00)
- Assistant
It looks like you have provided a detailed notebook script to perform the preprocessing steps and training of different machine learning models on a dataset. Let's break down the steps you have implemented and ensure everything is clear and correct.
-
Initial Dataset Overview:
- We display the first few rows of the dataset.
- Check the dimensions and data types of the dataset columns.
- Check for missing values and basic descriptive statistics.
-
Data Preprocessing:
- Encode categorical variables into numeric values using
LabelEncoder. - Replace any missing values with column mean values.
- Normalize the feature data using
StandardScaler.
- Encode categorical variables into numeric values using
-
Model Training and Evaluation:
- Train three models: Logistic Regression, Random Forest, and SVM.
- Apply cross-validation (CV) to each model and calculate the mean accuracy and standard deviation.
Given the Convergence Warnings for Logistic Regression, it might be beneficial to increase the number of iterations or try another solver. Your pipeline looks good overall, but here's a refined version of the script with some minor improvements for handling the Logistic Regression issue:
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from sklearn.preprocessing import LabelEncoder, StandardScaler from sklearn.model_selection import cross_val_score from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier from sklearn.svm import SVC # Supposons que 'data' est déjà chargé sous forme de DataFrame # data = pd.read_csv('your_dataset.csv') # Dimensions du dataset print("Dimensions du dataset:", data.shape) # Types de données print(data.dtypes) # Vérifier les valeurs manquantes print(data.isnull().sum()) # Statistiques descriptives print(data.describe()) # Encoder les variables catégorielles categorical_cols = data.select_dtypes(include=['object']).columns label_encoders = {} for col in categorical_cols: le = LabelEncoder() data[col] = le.fit_transform(data[col]) label_encoders[col] = le # Heatmap des corrélations plt.figure(figsize=(14, 12)) sns.heatmap(data.corr(), annot=False, cmap='coolwarm') plt.title('Heatmap des corrélations') plt.show() # Remplacer les valeurs manquantes par la moyenne de chaque colonne data.fillna(data.mean(), inplace=True) # Séparation des caractéristiques et de la cible X = data.drop('label', axis=1) y = data['label'] # Normalisation des caractéristiques scaler = StandardScaler() X_scaled = scaler.fit_transform(X) # Définition des modèles models = { 'Logistic Regression': LogisticRegression(max_iter=200), # Increased max_iter to fix convergence issue 'Random Forest': RandomForestClassifier(), 'SVM': SVC(probability=True) } # Validation croisée pour chaque modèle for name, model in models.items(): scores = cross_val_score(model, X_scaled, y, cv=5) print(f"{name} Accuracy: {scores.mean():.2f} (+/- {scores.std() * 2:.2f})")This script does the same steps as before but addresses the ConvergenceWarning in
LogisticRegressionby increasingmax_iterto 200. Additionally, it maintains the structure you've established for preprocessing and model evaluation.- Cross-Validation Accuracy Results:
- Logistic Regression Accuracy: 0.95 (+/- 0.00)
- Random Forest Accuracy: 1.00 (+/- 0.00)
- SVM Accuracy: 0.99 (+/- 0.00)
These results demonstrate strong performance across all three models, with Random Forest achieving perfect accuracy in cross-validation.
If you encounter the convergence warning again, you might need to further tune the
max_iterparameter or explore alternative solvers like 'sag' or 'liblinear' depending on the size and nature of your data. -