My input file is under the form:
gold,ProgramName,RequirementID,MethodID,DataTypeName,DataTypeID,FieldMethodOwnerClass,VariableName,fieldMethodID
Trace,chess,1,1,boolean,0,1,_moveRight,3
Trace,chess,1,1,boolean,0,1,_computerIsWhite,4
NoTrace,chess,2,1,boolean,0,1,_moveRight,3
NoTrace,chess,2,1,boolean,0,1,_computerIsWhite,4
NoTrace,chess,3,1,boolean,0,1,_moveRight,3
NoTrace,chess,3,1,boolean,0,1,_computerIsWhite,4
NoTrace,chess,4,1,boolean,0,1,_moveRight,3
NoTrace,chess,4,1,boolean,0,1,_computerIsWhite,4
NoTrace,chess,5,1,boolean,0,1,_moveRight,3
NoTrace,chess,5,1,boolean,0,1,_computerIsWhite,4
NoTrace,chess,6,1,boolean,0,1,_moveRight,3
NoTrace,chess,6,1,boolean,0,1,_computerIsWhite,4
NoTrace,chess,7,1,boolean,0,1,_moveRight,3
NoTrace,chess,7,1,boolean,0,1,_computerIsWhite,4
NoTrace,chess,8,1,boolean,0,1,_moveRight,3
NoTrace,chess,8,1,boolean,0,1,_computerIsWhite,4
NoTrace,chess,1,4,de.java_chess.javaChess.game.Game,50,1,_game,1
NoTrace,chess,1,4,byte,0,67,KING,353
NoTrace,chess,1,4,byte,0,67,PAWN,348
NoTrace,chess,1,4,de.java_chess.javaChess.game.Game,50,1,_game,1
NoTrace,chess,2,4,de.java_chess.javaChess.game.Game,50,1,_game,1
NoTrace,chess,2,4,byte,0,67,KING,353
NoTrace,chess,2,4,byte,0,67,PAWN,348
NoTrace,chess,2,4,de.java_chess.javaChess.game.Game,50,1,_game,1
NoTrace,chess,3,4,de.java_chess.javaChess.game.Game,50,1,_game,1
NoTrace,chess,3,4,byte,0,67,KING,353
NoTrace,chess,3,4,byte,0,67,PAWN,348
I would like to have my data split into a training and a test set but I would like to have all rows corresponding to 1 combination of <requirementID, MethodID> into the training set or into the test set. For instance, you notice that the 2 first rows both have <1,1> as values for the <requirementID, MethodID> tuple. The situation I want to avoid is the following: I don't want to have the first row in the training set and the second row in the test set. I would like to have both of them either in the training set or in the test set. Thus, all rows correponding to one combination of <requirementID, MethodID> should be either in the training set or the test set. For instance, the two first rows with <1,1> as values for <RequirementID,MethodID> should be in the training set and the third and fourth rows with values <2,1> should be in the test set.
Here is the Python code I am using. As you notice, it's splitting randomly into a test and a training set, which I don't want, I want the plit to be made according to the rules described above:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
import sys
def main():
dataset = pd.read_csv( 'inputFields.txt', sep= ',', index_col=False)
#convert Inner, Root, Leaf into 0, 1, 2
dataset['ProgramName'] = dataset['ProgramName'].astype('category').cat.codes
dataset['DataTypeName'] = dataset['DataTypeName'].astype('category').cat.codes
dataset['VariableName'] = dataset['VariableName'].astype('category').cat.codes
dataset['gold'] = dataset['gold'].astype('category').cat.codes
pd.set_option('display.max_columns', None)
row_count, column_count = dataset.shape
X = dataset.iloc[:, 1:column_count].values
y = dataset.iloc[:, 0].values
print(y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)
################################################################################
classifier = RandomForestClassifier(n_estimators=400, random_state=0)
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
print('confusion matrix
',confusion_matrix(y_test,y_pred))
print('classification report
', classification_report(y_test,y_pred))
print('accuracy score', accuracy_score(y_test, y_pred))
if __name__=="__main__":
main()
question from:
https://stackoverflow.com/questions/65925829/divide-into-test-set-and-training-set-with-all-rows-corresponding-to-one-combina