0
$\begingroup$

I want to process my dataset and eliminate data by using RFC .I have given my code below

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from pandas import DataFrame
from sklearn.multioutput import MultiOutputClassifier
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.metrics import accuracy_score
from sklearn import datasets
from sklearn.metrics import classification_report
from sklearn.impute import SimpleImputer

from google.colab import drive
drive.mount('/content/drive', force_remount=True) 
df=pd.read_csv('/content/drive/My Drive/IoT_Intrusion_Dataset_2020.csv');


df['Src_IP']=df['Src_IP'].astype(str).str.replace(".","").astype(int)
df['Dst_IP']=df['Dst_IP'].astype(str).str.replace(".","").astype(int)
df=df.drop(['Flow_ID','Timestamp'], axis = 1)
df.describe()

le=LabelEncoder()
df.Label=le.fit_transform(df.Label)
df.Cat=le.fit_transform(df.Cat)
df.Sub_Cat=le.fit_transform(df.Sub_Cat)
df.Label=df.Label.astype('category')
df.Cat=df.Cat.astype('category')
df.Sub_Cat=df.Sub_Cat.astype('category')
df.describe(include='all')

df=df.replace([np.inf, -np.inf], np.nan).dropna(axis=1)
df=df.apply(lambda row: row.fillna(row.mean()), axis=1)


 x=df.drop(['Label','Cat','Sub_Cat'],axis=1)
 y=df[['Label','Cat','Sub_Cat']]
 x.head()

I Have used parsona correlation algo for removing data whose correlated value is greater than 0.7

 x_train,x_test,y_train,y_test =train_test_split(x,y,test_size=0.3,random_state=0)
 x_train.shape,x_test.shape

 x_train.corr()

 plt.figure(figsize=(60,40))
 cor=x_train.corr()
 sns.heatmap(cor, annot=True, cmap=plt.cm.CMRmap_r)
 plt.show()
# y = y.reshape(-1,1)
# y = y.reshape(-1,1)
def correlation(dataset, threshold):
    col_corr = set()  # Set of all the names of correlated columns
    corr_matrix = dataset.corr()
    for i in range(len(corr_matrix.columns)):
       for j in range(i):
          if abs(corr_matrix.iloc[i, j]) > threshold: # we are interested in absolute coeff value
              colname = corr_matrix.columns[i]  # getting the name of column
              col_corr.add(colname)
return col_corr
corr_features = correlation(x_train, 0.7)
x_train=x_train.drop(corr_features,axis=1)
x_test=x_test.drop(corr_features,axis=1)

After that i have tried to use Randon forest classifier where i am getting multivalue error

sel = SelectFromModel(RandomForestClassifier(n_estimators = 100))
sel.fit(x_train, y_train)
sel.get_support()
features=x_train.columns[sel.get_support()]
np.mean(sel.estimator_.feature_importances_)
x_train_rfc=sel.transform(x_train)
x_test_rfc=sel.transform(x_test)
def run_randomforest(x_train,x_test,y_train,y_test):
   clf=RandomForestClassifier(n_estimators=100,random_state=0,n_jobs=-1)
   clf.fit(x_train,y_train)
   y_pred=clf.predict(x_test)
   print("Accuracy :",accuracy_score(y_test,y_pred))

Here is the error

  run_randomforest(x_train_rfc,x_test_rfc,y_train,y_test)

Here i got ValueError: multiclass-multioutput is not supported

  ValueError                                Traceback (most recent call last)
 <ipython-input-31-5c7cd11594be> in <module>()
 ----> 1 run_randomforest(x_train_rfc,x_test_rfc,y_train,y_test)

 2 frames
 /usr/local/lib/python3.7/dist-packages/sklearn/metrics/_classification.py in 
 _check_targets(y_true, y_pred)
 95     # No metrics support "multiclass-multioutput" format
 96     if (y_type not in ["binary", "multiclass", "multilabel-indicator"]):
 97         raise ValueError("{0} is not supported".format(y_type))
 98 
 99     if y_type in ["binary", "multiclass"]:

 ValueError: multiclass-multioutput is not supported

Then i applied RFC(recursive feature elimination where i got ValueError: bad input shape (438048, 3)

 from sklearn.feature_selection import RFE
 sel=RFE(RandomForestClassifier
      (n_estimators=100,random_state=0,n_jobs=-1),n_features_to_select=15)
 sel.fit(x_train, y_train)

here is the error,

 ValueError                                Traceback (most recent call last)
 <ipython-input-33-62ed3e043139> in <module>()
  1 from sklearn.feature_selection import RFE
  2 
 sel=RFE(RandomForestClassifier(n_estimators=100,random_state=0,n_jobs=-1),
     n_features_to_select=15)
  3 sel.fit(x_train, y_train)

  3 frames
  /usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in column_or_1d(y, warn)
 795         return np.ravel(y)
 796 
 797     raise ValueError("bad input shape {0}".format(shape))
  798 
  799 

  ValueError: bad input shape (438048, 3)
    
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.