<jupyter_start><jupyter_text>Laboratorio  4<NAME> - 161260Abril Palencia - 18198<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import re

%matplotlib inline
df = pd.read_csv('VirusSample.csv')
df
df.file[0]
df.api[0]
df.info()
np.unique(df[['class']])
df['class'].value_counts()
df['class'].value_counts().plot(kind='barh')
df['class'].replace({"Adware": 0, "Agent": 1, "Backdoor": 2, "Crypt": 3, "Downloader": 4, "Dropper": 5,
                    "Keylogger": 6, "Ransomware": 7, "Riskware": 8, "Spyware": 9, "Trojan": 10, "Virus": 11, 
                    "Worms": 12}, inplace=True)
df['class'].value_counts()
df['class'].value_counts().plot(kind='barh')<jupyter_output><empty_output><jupyter_text>Eliminar las que tienen menos de 100 instancias, pueden crear ruido<jupyter_code># df.drop([8, 5, 3, 6], axis=3, inplace = True)
# eliminar = [8, 5, 3, 6]
df = df.drop(df.index[df['class'] == 4])
df = df.drop(df.index[df['class'] == 9])
df = df.drop(df.index[df['class'] == 7])
df = df.drop(df.index[df['class'] == 8])
df = df.drop(df.index[df['class'] == 5])
df = df.drop(df.index[df['class'] == 3])
df = df.drop(df.index[df['class'] == 6])
df.info()
df['class'].value_counts()
df['class'].value_counts().plot(kind='barh')
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
df['class'] = le.fit_transform(df['class'])
df
df.file = le.fit_transform(df.file)
df.api = le.fit_transform(df.api)
df
target = df['class']
df_implement = df[['file', 'api']]
target
df_implement
from sklearn.preprocessing import MinMaxScaler
ss = MinMaxScaler()
ss.fit(df_implement)
df_implement = pd.DataFrame(ss.transform(df_implement))
df_implement
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(df_implement, target, train_size=0.7, 
                                                    test_size=0.3, random_state=5)
x_train.shape, x_test.shape<jupyter_output><empty_output><jupyter_text>Decision Tree Classifier<jupyter_code>from sklearn.tree import DecisionTreeClassifier
decision = DecisionTreeClassifier()
decision = decision.fit(x_train, y_train)
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
target_prediction = decision.predict(x_test)
accuracy_score(y_test, target_prediction)
confusion_matrix(y_test, target_prediction)
print(classification_report(y_test, target_prediction))<jupyter_output>precision    recall  f1-score   support

           0       0.68      0.66      0.67        79
           1       0.28      0.39      0.33        28
           2       0.91      0.87      0.89       120
           3       0.92      0.91      0.92      1838
           4       0.88      0.88      0.88       724
           5       0.61      0.72      0.66       131

    accuracy                           0.88      2920
   macro avg       0.72      0.74      0.72      2920
weighted avg       0.88      0.88      0.88      2920<jupyter_text>Naive Bayes<jupyter_code>x_train_bow, x_test_bow, y_train_bow, y_test_bow = train_test_split(df_implement, target, train_size=0.7, 
                                                                    test_size=0.3, random_state=50)
from sklearn.naive_bayes import MultinomialNB
nb_bow = MultinomialNB()
nb_bow.fit(x_train_bow, y_train_bow)
pred_bow = nb_bow.predict(x_test_bow)
accuracy_score(y_test_bow, pred_bow)
confusion_matrix(y_test_bow, pred_bow)
print(classification_report(y_test_bow, pred_bow))
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
cv = KFold(n_splits=10, random_state=1, shuffle=True)

print(cross_val_score(decision, x_train, y_train, scoring='accuracy', cv=cv, n_jobs=-1))
print(cross_val_score(nb_bow, x_train_bow, y_train_bow, scoring='accuracy', cv=cv, n_jobs=-1))<jupyter_output>[0.87683284 0.87683284 0.87812041 0.87518355 0.90161527 0.87518355
 0.86490455 0.88105727 0.88693098 0.8825257 ]
[0.61730205 0.6686217  0.64610866 0.66226138 0.64904552 0.61674009
 0.59030837 0.65051395 0.63142438 0.6123348 ]
{'fraction_non_alphanumeric': 0.1299115913555992, 'fraction_numerical': 0.08668958742632613, 'mean_word_length': 3.5305895439377086, 'pattern_counts': {'":': 13, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '264542', 'license': 'MIT', 'n_tokens_mistral': 1711, 'n_tokens_neox': 1506, 'n_words': 294, 'path': 'Lab4.ipynb', 'repo_name': 'Crismaria11/Lab04_SecDS', 'size': '4083'}