5-5-KNN-detect-Rootkit.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import numpy as np
  2. from sklearn.neighbors import KNeighborsClassifier
  3. from sklearn.model_selection import train_test_split, cross_val_score
  4. from datasets import Datasets
  5. import matplotlib.pyplot as plt
  6. import pandas as pd
  7. def get_rootkit_and_normal(kdd99_data):
  8. x = []
  9. y = []
  10. for data in kdd99_data:
  11. if (data[41] in ["normal.", "rootkit."]) and (data[2] == "telnet"):
  12. # 提取结果
  13. y.append(1 if data[41] == "rootkit." else 0)
  14. # 提取特征
  15. x.append(list(map(lambda i: float(i), data[9:21])))
  16. return x, y
  17. def main():
  18. kdd99_data = Datasets.load_kdd99()
  19. x, y = get_rootkit_and_normal(kdd99_data)
  20. # x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
  21. knn = KNeighborsClassifier(n_neighbors=3)
  22. # knn.fit(x_train, y_train)
  23. # print(knn.score(x_test, y_test))
  24. scores = cross_val_score(knn, x, y, cv=2, scoring='accuracy')
  25. print(scores.mean()) # 0.9777777777777777
  26. if __name__ == "__main__":
  27. main()