Skip to content

Instantly share code, notes, and snippets.

@analyticsindiamagazine
Created November 26, 2019 09:07
Show Gist options
  • Select an option

  • Save analyticsindiamagazine/a99784a5ce058c484f38f0a656df372f to your computer and use it in GitHub Desktop.

Select an option

Save analyticsindiamagazine/a99784a5ce058c484f38f0a656df372f to your computer and use it in GitHub Desktop.
#Splitting training set into training and validation sets
from sklearn.model_selection import train_test_split
train, val = train_test_split(train_sample, test_size = 0.1, random_state = 123)
#Seperating the independent and dependent variables
cols = [list of column names in order] #last column corresponds to y or target variable
#Training set
X_train = train[cols[:-1]]
Y_train = train[cols[-1]]
#Validation set
X_Val = val[cols[:-1]]
Y_Val = val[cols[-1]]
#Test set
X_test = test_sample
#importing xgboost
from xgboost import XGBClassifier
#Initializing the classifier with default arguments
xgb = XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0,
learning_rate=0.1, max_delta_step=0, max_depth=3,
min_child_weight=1, missing=None, n_estimators=100, n_jobs=1,
nthread=None, objective='multi:softprob', random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
silent=None, subsample=1, verbosity=1)
#Training the classifier
xgb.fit(X_train,Y_train)
#Evaluating the score on validation set
xgb.score(X_Val,Y_Val)
#Predicting for test set
Predictions = xgb.predict(X_test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment