- Machine Learning for Cybersecurity Cookbook
- Emmanuel Tsukerman
- 170字
- 2021-06-24 12:28:55
How to do it...
The following steps demonstrate how to take a dataset, consisting of features X and labels y, and split these into a training and testing subset:
- Start by importing the train_test_split module and the pandas library, and read your features into X and labels into y:
from sklearn.model_selection import train_test_split
import pandas as pd
df = pd.read_csv("north_korea_missile_test_database.csv")
y = df["Missile Name"]
X = df.drop("Missile Name", axis=1)
- Next, randomly split the dataset and its labels into a training set consisting 80% of the size of the original dataset and a testing set 20% of the size:
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=31
)
- We apply the train_test_split method once more, to obtain a validation set, X_val and y_val:
X_train, X_val, y_train, y_val = train_test_split(
X_train, y_train, test_size=0.25, random_state=31
)
- We end up with a training set that's 60% of the size of the original data, a validation set of 20%, and a testing set of 20%.
The following screenshot shows the output:

推薦閱讀
- 集成架構中型系統(tǒng)
- 智能傳感器技術與應用
- Mastering Spark for Data Science
- 樂高創(chuàng)意機器人教程(中級 下冊 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實踐指導叢書)
- ESP8266 Home Automation Projects
- Splunk Operational Intelligence Cookbook
- 軟件工程及實踐
- 電腦上網(wǎng)輕松入門
- SMS 2003部署與操作深入指南
- 強化學習
- 基于Proteus的單片機應用技術
- 寒江獨釣:Windows內核安全編程
- 電腦上網(wǎng)入門
- AVR單片機工程師是怎樣煉成的
- 精通ROS機器人編程(原書第2版)