分为测试集和训练集,所有行对应训练集或测试集中的一个属性值组合

时间:2021-01-27 19:13:31

标签: python split training-data test-data

我的输入文件格式如下:

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

我希望将我的数据分成训练集和测试集,但我希望将与 的 1 个组合相对应的所有行都放入训练集或测试集。例如,您注意到前 2 行都有 <1,1> 作为 元组的值。我想避免的情况如下:我不想在训练集中有第一行,在测试集中有第二行。我希望将它们都放在训练集中或测试集中。因此,对应于 的一个组合的所有行都应该在训练集中或测试集中。例如,将 <1,1> 作为 值的前两行应该在训练集中,而值为 <2,1> 的第三和第四行应该在测试集中。

这是我正在使用的 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()

0 个答案:

没有答案