import pandas as pd
import numpy as np
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.metrics import confusion_matrix
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_validate
from sklearn.model_selection import train_test_split
malData = pd.read_csv("malware.csv",sep="|")

legit = malData[0:41323].drop(["legitimate"],axis=1)

mal = malData[42323::].drop(["legitimate"],axis=1) # we will be treating legitimate column 
                                                   # as label thus removing it from the data

print(legit.shape[0],legit.shape[1])
print(mal.shape[0],mal.shape[1])
print(malData.columns)              #check the columns availabe

print(malData.head(5))              #print the first 5 data from the csv

data_in = malData.drop(["Name","md5","legitimate"],axis=1).values #these are not necessary for classification

labels = malData['legitimate'].values

extratrees = ExtraTreesClassifier().fit(data_in,labels)
select = SelectFromModel(extratrees,prefit=True)
data_in_new= select.transform(data_in)

print(data_in.shape,data_in_new.shape)

features = data_in_new.shape[1]
importances = extratrees.feature_importances_
indices = np.argsort(importances)[::-1]

for f in range(features):
    print("%d"%(f+1),malData.columns[2+indices[f]],importances[indices[f]])


legit_train,legit_test,mal_train,mal_test =train_test_split(data_in_new,labels,test_size=0.2)

classif =RandomForestClassifier(n_estimators=100)

classif.fit(legit_train,mal_train)

print("The score of the Algo", classif.score(legit_test,mal_test)*100)

result = classif.predict(legit_test)

conf_mat = confusion_matrix(mal_test,result)

print("False positive ", conf_mat[0][1]/sum(conf_mat[0])*100)

print("Flase negatives", conf_mat[1][0]/sum(conf_mat[1])*100)


