我的输入文件格式如下:
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
我希望将我的数据分成训练集和测试集,但我希望将与
这是我正在使用的 Python 代码。正如您所注意到的,它随机分成了一个测试集和一个训练集,这是我不想要的,我希望按照上述规则进行分割:
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\n',confusion_matrix(y_test,y_pred))
print('classification report\n', classification_report(y_test,y_pred))
print('accuracy score', accuracy_score(y_test, y_pred))
if __name__=="__main__":
main()