SVM - Detect malware traffic

Viewed 72

Hi There I am new to machine learning

I have collected some data from some pcap: The sample is like below

Byte                 Count    ByteAvg        isMalware
[74, 74, 74, ...]     3570    188.298880           0
[66, 69, 90, ...]     915     157.691803           0
                            .....
[103, 103, 76 ...]    1075    127.526512           1 
[66, 69, 90, ...]     6877    140.671805           1

And I try the sample code of svm

df = pd.read_csv("traffic.csv")
# split data and class
X = df.drop('isMalware', axis=1)
y = df['isMalware']

# Spilt train and test data 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20)
# Training
svclassifier = SVC(kernel='linear')
svclassifier.fit(X_train, y_train)
y_pred = svclassifier.predict(X_test)

print(confusion_matrix(y_test,y_pred))
print(classification_report(y_test,y_pred))

However the error is raised

ValueError: could not convert string to float: '[74, 74, 74, 66, 66, 66, 280

How can I change the CSV format and in order to put it into the SVM classifier?

1 Answers

The column Byte didn't have the good type.

SVM use only numerical (int , foat ) features , you can read this https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html

you can just drop the column Byte so X become:

X = df.drop(columns = ['isMalware', 'Bytes'])

or you can label the column Byte by using LabelEncode from sklearn https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html

from sklearn import preprocessing


le = preprocessing.LabelEncoder()
le.fit(X["Byte"])
X["Byte"] = le.transform(X["Byte"])
Related