Train Test Split is technically not an example of cross-validation. It is basically a technique to randomly split data into training and test sets since training and testing a model on the same data will lead to overfitting.
This recipe includes the following topics:
- Load data/file from github
- Split columns into the usual feature columns(X) and target column(Y)
- Set test size to 33%
- Set seed to reproduce the same random data each time
- Split data using train_test_split() helper method
- Instantiate a classification model (LogisticRegression)
- Call fit() to train model using X_train and Y_train
- Evaluate model on unseen data X_test and Y_test
# import modules
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
# read data file from github
# dataframe: pimaDf
gitFileURL = 'https://raw.githubusercontent.com/andrewgurung/data-repository/master/pima-indians-diabetes.data.csv'
cols = ['preg', 'plas', 'pres', 'skin', 'test', 'mass', 'pedi', 'age', 'class']
pimaDf = pd.read_csv(gitFileURL, names = cols)
# convert into numpy array for scikit-learn
pimaArr = pimaDf.values
# Let's split columns into the usual feature columns(X) and target column(Y)
# Y represents the target 'class' column whose value is either '0' or '1'
X = pimaArr[:, 0:8]
Y = pimaArr[:, 8]
# set test size to 33%
test_size = 0.33
# set seed to reproduce the same random data each time
seed = 7
# split data using train_test_split() helper method
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)
# instantiate a classification model
model = LogisticRegression()
# call fit() to train model using X_train and Y_train
model.fit(X_train, Y_train)
# evaluate model on unseen data X_test and Y_test
result = model.score(X_test, Y_test)
# display accuracy
print("Accuracy of LogisticRegression with train-test-split(0.33): %.3f" % (result*100.0))
Accuracy of LogisticRegression with train-test-split(0.33): 75.591