<jupyter_start><jupyter_text>BASELINE MODEL<jupyter_code>baseline = evalData.copy()
baseline.dtypes
baseline['GameType'] = pd.np.where(baseline.Event.str.contains("Blitz"), "Blitz",
            pd.np.where(baseline.Event.str.contains("Bullet"), "Bullet",
                        pd.np.where(baseline.Event.str.contains("Rapid"), "Rapid",
                                    pd.np.where(baseline.Event.str.contains("Classical"), "Classical",
                                                "Correspondence"))))
baselineCols = ['Result', 'WhiteElo', 'BlackElo', 'WhiteRatingDiff', 'BlackRatingDiff', 'ECO', 
                'TimeControl', 'Termination', 'GameType']
from sklearn.preprocessing import LabelEncoder
baseline['ECO'] = LabelEncoder().fit_transform(baseline['ECO'])
baseline['TimeControl'] = LabelEncoder().fit_transform(baseline['TimeControl'])
baseline['Termination'] = LabelEncoder().fit_transform(baseline['Termination'])
baseline['GameType'] = LabelEncoder().fit_transform(baseline['GameType'])
baseline['Result'] = pd.np.where(baseline.Result.str.contains("1-0"), "1",
            pd.np.where(baseline.Result.str.contains("0-1"), "0",
                        "2"))
baseline['Result'].value_counts()
baseline['Result'] = baseline['Result'].astype(int)
baselineTrain = baseline[baselineCols]
baselineTrain.dtypes
baselineTrain.reset_index(inplace=True, drop=True)
baselineTrain.columns
blTrainingSet, blTestSet = train_test_split(baselineTrain, test_size=0.2)

XTrain = blTrainingSet.drop('GameType',axis =1)
YTrain = blTrainingSet['GameType']
XTest = blTestSet.drop('GameType', axis =1)
YTest = blTestSet['GameType']
XTrain = XTrain.drop('TimeControl', axis =1)
XTest = XTest.drop('TimeControl', axis =1)
XTest<jupyter_output><empty_output><jupyter_text>Game Type Prediction    Baseline Model<jupyter_code>neigh = KNeighborsClassifier(n_neighbors=5)
neigh.fit(XTrain, YTrain) 
predicted = neigh.predict(XTest)
acc = accuracy_score(YTest, predicted)
print(acc)
baselineTrain['isUpset']= 0
for i in range(0,len(baselineTrain)):
    if baselineTrain['Result'][i]== 0:
        if baselineTrain['BlackElo'][i]<baselineTrain['WhiteElo'][i]:
            baselineTrain['isUpset'][i]=1
    if baselineTrain['Result'][i]== 1:
        if baselineTrain['BlackElo'][i]>baselineTrain['WhiteElo'][i]:
            baselineTrain['isUpset'][i]=1
baselineTrain['isUpset'].value_counts()
fTrainingSet, fTestSet = train_test_split(baselineTrain, test_size=0.2)
XTrain = fTrainingSet.drop('isUpset',axis =1)
YTrain = fTrainingSet['isUpset']
XTest = fTestSet.drop('isUpset', axis =1)
YTest = fTestSet['isUpset']
neigh = KNeighborsClassifier(n_neighbors=2)
neigh.fit(XTrain, YTrain) 
predicted = neigh.predict(XTest)
acc = accuracy_score(YTest, predicted)
print(acc)<jupyter_output>0.845610278372591<jupyter_text>White Rating Prediction<jupyter_code>XTrain = blTrainingSet.drop('WhiteElo',axis =1)
XTrain = XTrain.drop('BlackElo',axis =1)
YTrain = blTrainingSet['WhiteElo']
XTest = blTestSet.drop('WhiteElo', axis =1)
XTest = XTest.drop('BlackElo', axis =1)
YTest = blTestSet['WhiteElo']
# Linear Regression Model Creation
lin_reg = LinearRegression()
lin_reg.fit(XTrain, YTrain)
#predict on test data split
y_test_predictions_lin_reg = lin_reg.predict(XTest)
# print (len(y_test_predictions_lin_reg))
lin_mse = mean_squared_error(YTest, (y_test_predictions_lin_reg))
lin_rmse = np.sqrt(lin_mse)
print(lin_rmse)<jupyter_output>322.5617835567131<jupyter_text>Data Cleaning<jupyter_code>cleanedEvalData = evalData.copy()
cleanedEvalData['GameType'] = pd.np.where(cleanedEvalData.Event.str.contains("Blitz"), "Blitz",
            pd.np.where(cleanedEvalData.Event.str.contains("Bullet"), "Bullet",
                        pd.np.where(cleanedEvalData.Event.str.contains("Rapid"), "Rapid",
                                    pd.np.where(cleanedEvalData.Event.str.contains("Classical"), "Classical",
                                                "Correspondence"))))


cleanedEvalData['ECO'] = LabelEncoder().fit_transform(cleanedEvalData['ECO'])
cleanedEvalData['TimeControl'] = LabelEncoder().fit_transform(cleanedEvalData['TimeControl'])
cleanedEvalData['Termination'] = LabelEncoder().fit_transform(cleanedEvalData['Termination'])
cleanedEvalData['GameType'] = LabelEncoder().fit_transform(cleanedEvalData['GameType'])

cleanedEvalData['Result'] = pd.np.where(cleanedEvalData.Result.str.contains("1-0"), "1",
            pd.np.where(cleanedEvalData.Result.str.contains("0-1"), "0",
                        "2"))

cleanedEvalData['Result'] = cleanedEvalData['Result'].astype(int)<jupyter_output><empty_output><jupyter_text>___________________________________INTERMEDIATE REPORT MODELS___________________________________<jupyter_code>irData = cleanedEvalData[['Result', 'WhiteElo', 'BlackElo', 'WhiteRatingDiff', 'BlackRatingDiff',
       'ECO', 'TimeControl', 'Termination', 'Moves', 'GameType']]
irData.reset_index(inplace=True, drop=True)
irData['MovesCount'] = ''
#Moves Count
for i in range(0, irData.shape[0]):
    gamestring = io.StringIO(irData['Moves'][i])
    game = cp.read_game(gamestring)
    count=0
    if game is not None:
        for move in game.mainline_moves():
            count=count+1
    irData['MovesCount'][i]=count
irData["WhiteCaptured"] = ""
irData["BlackCaptured"] = ""
irData.head()
for i in range(0, irData.shape[0]):
    board = chess.Board()
    if i%5000 == 0:
        print(i)
    gamestring = io.StringIO(irData['Moves'][i])
    game = cp.read_game(gamestring)
    if game is not None:
        for move in game.mainline_moves():
            board.push(move)
    boardMap = board.piece_map()
    blackCount = 0
    whiteCount =0
    for cell in boardMap:
        if str(boardMap[cell]).isupper():
            whiteCount= whiteCount+1
        elif str(boardMap[cell]).islower():
            blackCount = blackCount + 1
    irData['WhiteCaptured'][i] = 16 - whiteCount
    irData['BlackCaptured'][i] = 16 - blackCount
irData['EloDifference'] = irData['WhiteElo'].astype(int) - irData['BlackElo'].astype(int)
irData["RemTimeAftFirstMoveWhite"] = ""
irData["RemTimeAftLastMoveWhite"] = ""
irData["RemTimeAftFirstMoveBlack"] = ""
irData["RemTimeAftLastMoveBlack"] = ""
irData['WhiteTotalEval'] = ''
irData['BlackTotalEval'] = ''
for i in range(0, len(irData['Moves'])):
    if i%5000 == 0:
        print(i)
    try:
        if irData['Termination'][i]!='Abandoned' and irData['Moves'][i] is not None:
            if 'eval' in irData['Moves'][i]:
                splitres = irData['Moves'][i].split(' } ')
                whiteEvalSum = 0
                blackEvalSum = 0

                for j in range(0, len(splitres)):
                    if j%2 == 0:
                        if '%eval ' in splitres[j]:
                            #if splitres[j].split(']')[0].split('%eval ')[1]=='#1':
                            #    whiteEvalSum = whiteEvalSum + 1
                            if '#' not in splitres[j].split(']')[0].split('%eval ')[1]:
                                whiteEvalSum = whiteEvalSum + float(splitres[j].split(']')[0].split('%eval ')[1])
                    else:
                        if '%eval ' in splitres[j]:
                            #if '%eval ' in splitres[j] and splitres[j].split(']')[0].split('%eval ')[1]=='#1':
                            #    blackEvalSum = blackEvalSum + 1
                            if '#' not in splitres[j].split(']')[0].split('%eval ')[1]:
                                blackEvalSum = blackEvalSum + float(splitres[j].split(']')[0].split('%eval ')[1])
                irData['WhiteTotalEval'][i] = whiteEvalSum
                irData['BlackTotalEval'][i] = blackEvalSum

                if 'clk' in irData['Moves'][i]:
                    irData["RemTimeAftFirstMoveWhite"][i] = splitres[0].split(']')[1].split('%clk ')[1]
                    irData["RemTimeAftFirstMoveBlack"][i] = splitres[1].split(']')[1].split('%clk ')[1]
                    length = len(splitres)
                    if length%2 == 0:
                        if '%eval ' in splitres[length-2]:
                            irData["RemTimeAftLastMoveWhite"][i] = splitres[length-2].split(']')[1].split('%clk ')[1]
                        elif '%eval ' not in splitres[length-2]:
                            irData["RemTimeAftLastMoveWhite"][i] = splitres[length-2].split(']')[0].split('%clk ')[1]
                        if '%eval ' in splitres[length-1]:
                            irData["RemTimeAftLastMoveBlack"][i] = splitres[length-1].split(']')[1].split('%clk ')[1]
                        elif '%eval ' not in splitres[length-1]:
                            irData["RemTimeAftLastMoveBlack"][i] = splitres[length-1].split(']')[0].split('%clk ')[1]
                    else:
                        if '%eval ' in splitres[length-2]:
                            irData["RemTimeAftLastMoveBlack"][i] = splitres[length-2].split(']')[1].split('%clk ')[1]
                        elif '%eval ' not in splitres[length-2]:
                            irData["RemTimeAftLastMoveBlack"][i] = splitres[length-2].split(']')[0].split('%clk ')[1]
                        if '%eval ' in splitres[length-1]:
                            irData["RemTimeAftLastMoveWhite"][i] = splitres[length-1].split(']')[1].split('%clk ')[1]
                        elif '%eval ' not in splitres[length-1]:
                            irData["RemTimeAftLastMoveWhite"][i] = splitres[length-1].split(']')[0].split('%clk ')[1]


            elif 'clk' in irData['Moves'][i]:
                splitres = irData['Moves'][i].split(' } ')
                irData["RemTimeAftFirstMoveWhite"][i] = splitres[0].split("]")[0].split('%clk ')[1]
                irData["RemTimeAftFirstMoveBlack"][i] = splitres[1].split("]")[0].split('%clk ')[1]
                length = len(splitres)
                if length%2 == 0:
                    irData["RemTimeAftLastMoveWhite"][i] = splitres[length-2].split("]")[0].split('%clk ')[1]
                    irData["RemTimeAftLastMoveBlack"][i] = splitres[length-1].split("]")[0].split('%clk ')[1]
                else:
                    irData["RemTimeAftLastMoveBlack"][i] = splitres[length-2].split("]")[0].split('%clk ')[1]
                    irData["RemTimeAftLastMoveWhite"][i] = splitres[length-1].split("]")[0].split('%clk ')[1]
    except:
        print(i)

irData.isnull().sum()
irData.drop(irData[irData['RemTimeAftFirstMoveWhite']==''].index, inplace = True)
irData.drop(irData[irData['RemTimeAftLastMoveWhite']==''].index, inplace = True)
irData.drop(irData[irData['RemTimeAftFirstMoveBlack']==''].index, inplace = True)
irData.drop(irData[irData['RemTimeAftLastMoveBlack']==''].index, inplace = True)
irData.reset_index(inplace=True, drop=True)
irData.shape
irData['a']=''
irData['b']=''
irData['c']=''
irData['d']=''
for i in range(0,irData.shape[0]):
    irData['a'][i] = pd.Timedelta(str(irData['RemTimeAftFirstMoveWhite'][i])).seconds
    irData['b'][i] = pd.Timedelta(str(irData['RemTimeAftLastMoveWhite'][i])).seconds
    irData['c'][i] = pd.Timedelta(str(irData['RemTimeAftFirstMoveBlack'][i])).seconds
    irData['d'][i] = pd.Timedelta(str(irData['RemTimeAftLastMoveBlack'][i])).seconds

irData['RemTimeAftFirstMoveWhite'] = irData['a']
irData['RemTimeAftLastMoveWhite'] = irData['b']
irData['RemTimeAftFirstMoveBlack'] = irData['c']
irData['RemTimeAftLastMoveBlack'] = irData['d']

irData = irData.drop('a',axis =1)
irData = irData.drop('b',axis =1)
irData = irData.drop('c',axis =1)
irData = irData.drop('d',axis =1)
irData['MovesCount']= irData['MovesCount'].astype(int)
irData['WhiteCaptured']= irData['WhiteCaptured'].astype(int)
irData['BlackCaptured']= irData['BlackCaptured'].astype(int)
irData['WhiteTotalEval']= irData['WhiteTotalEval'].astype(float)
irData['BlackTotalEval']= irData['BlackTotalEval'].astype(float)
irData['RemTimeAftFirstMoveWhite']= irData['RemTimeAftFirstMoveWhite'].astype(int)
irData['RemTimeAftLastMoveWhite']= irData['RemTimeAftLastMoveWhite'].astype(int)
irData['RemTimeAftFirstMoveBlack']= irData['RemTimeAftFirstMoveBlack'].astype(int)
irData['RemTimeAftLastMoveBlack']= irData['RemTimeAftLastMoveBlack'].astype(int)
irData.columns<jupyter_output><empty_output><jupyter_text>GameType Prediction<jupyter_code>normalizedData = irData[irData['GameType']==2]
normalizedData = normalizedData.append(irData[irData['GameType']==0].iloc[0:1309,:], ignore_index = True)
normalizedData = normalizedData.append(irData[irData['GameType']==1].iloc[0:1309,:], ignore_index = True)
normalizedData = normalizedData.append(irData[irData['GameType']==4].iloc[0:1309,:], ignore_index = True)
gametypeData = normalizedData[['Result', 'WhiteElo', 'BlackElo', 'WhiteRatingDiff', 'BlackRatingDiff',
       'ECO', 'Termination','GameType', 'MovesCount',
       'WhiteCaptured', 'BlackCaptured']]
irTrainingSet, irTestSet = train_test_split(gametypeData, test_size=0.2)

XTrain = irTrainingSet.drop('GameType',axis =1)
YTrain = irTrainingSet['GameType']
XTest = irTestSet.drop('GameType', axis =1)
YTest = irTestSet['GameType']
neigh = KNeighborsClassifier(n_neighbors=5)
neigh.fit(XTrain, YTrain) 
predicted = neigh.predict(XTest)
acc = accuracy_score(YTest, predicted)
print(acc)<jupyter_output>0.38685015290519875<jupyter_text>White Rating Prediction<jupyter_code>normalizedData
gametypeData = normalizedData[['Result', 'WhiteElo', 'BlackElo', 'WhiteRatingDiff', 'WhiteRatingDiff',
       'ECO', 'TimeControl', 'Termination', 'GameType', 'MovesCount',
       'WhiteCaptured', 'WhiteCaptured', 'EloDifference',
       'RemTimeAftFirstMoveWhite', 'RemTimeAftLastMoveWhite',
       'RemTimeAftFirstMoveWhite', 'RemTimeAftLastMoveWhite', 'WhiteTotalEval',
       'WhiteTotalEval']]
irTrainingSet, irTestSet = train_test_split(gametypeData, test_size=0.2)
XTrain = irTrainingSet.drop('WhiteElo',axis =1)
XTrain = XTrain.drop('BlackElo',axis =1)
YTrain = irTrainingSet['WhiteElo']
XTest = irTestSet.drop('WhiteElo', axis =1)
XTest = XTest.drop('BlackElo', axis =1)
YTest = irTestSet['WhiteElo']
# Linear Regression Model Creation
lin_reg = LinearRegression()
lin_reg.fit(XTrain, YTrain)
#predict on test data split
y_test_predictions_lin_reg = lin_reg.predict(XTest)
# print (len(y_test_predictions_lin_reg))
lin_mse = mean_squared_error(YTest, (y_test_predictions_lin_reg))
lin_rmse = np.sqrt(lin_mse)
print(lin_rmse)
import lightgbm as lgb
d_train = lgb.Dataset(XTrain, label=YTrain)
params = {}
# params['learning_rate'] = 0.003
# params['boosting_type'] = 'gbdt'
# params['objective'] = 'binary'
# params['metric'] = 'binary_logloss'
# params['sub_feature'] = 0.5
# params['num_leaves'] = 10
# params['min_data'] = 50
# params['max_depth'] = 10
clf = lgb.train({},d_train)

y_test_predictions_lin_reg = clf.predict(XTest)
lin_mse = mean_squared_error(YTest, (y_test_predictions_lin_reg))
lin_rmse = np.sqrt(lin_mse)
print(lin_rmse)<jupyter_output>227.2274193314572<jupyter_text>FINAL MODEL<jupyter_code>irData
irData['GoodMoveWhite'] = 0
irData['MistakeWhite'] = 0
irData['BrilliantMoveWhite'] = 0
irData['BlunderWhite'] = 0
irData['InterestingMoveWhite'] = 0
irData['DubiousMoveWhite'] = 0
irData['GoodMoveBlack'] = 0
irData['MistakeBlack'] = 0
irData['BrilliantMoveBlack'] = 0
irData['BlunderBlack'] = 0
irData['InterestingMoveBlack'] = 0
irData['DubiousMoveBlack'] = 0

irData['RookMovesWhite'] = 0
irData['KnightMovesWhite'] = 0
irData['BishopMovesWhite'] = 0
irData['KingMovesWhite'] = 0
irData['QueenMovesWhite'] = 0
irData['PawnMovesWhite'] = 0
irData['KingCastlingWhite'] = False
irData['QueenCastlingWhite'] = False

irData['RookMovesBlack'] = 0
irData['KnightMovesBlack'] = 0
irData['BishopMovesBlack'] = 0
irData['KingMovesBlack'] = 0
irData['QueenMovesBlack'] = 0
irData['PawnMovesBlack'] = 0
irData['KingCastlingBlack'] = False
irData['QueenCastlingBlack'] = False

irData['ChecksByWhite'] = 0
irData['ChecksByBlack'] = 0
irData['CheckmateDone'] = False
irData['PawnPromotionWhite'] = 0
irData['PawnPromotionBlack'] = 0

for i in range(0,irData.shape[0]):
    if i%1000 == 0:
        print(i)
    if irData['Termination'][i]!='Abandoned' and irData['Moves'][i] is not None:
        if "$" in irData['Moves'][i]:
            splitres = irData['Moves'][i].split(' } ')
            
            for j in range(0, len(splitres)):
                move = splitres[j].split(' ')
                if j%2 == 0:
                    if move[1].startswith('R'):
                        irData['RookMovesWhite'][i] = irData['RookMovesWhite'][i] +1
                    elif move[1].startswith('N'):
                        irData['KnightMovesWhite'][i] = irData['KnightMovesWhite'][i] +1
                    elif move[1].startswith('B'):
                        irData['BishopMovesWhite'][i] = irData['BishopMovesWhite'][i] +1
                    elif move[1].startswith('K'):
                        irData['KingMovesWhite'][i] = irData['KingMovesWhite'][i] +1
                    elif move[1].startswith('Q'):
                        irData['QueenMovesWhite'][i] = irData['QueenMovesWhite'][i] +1
                    elif move[1]== 'O-O':
                        irData['KingCastlingWhite'][i] = True
                    elif move[1]== 'O-O-O':
                        irData['QueenCastlingWhite'][i] = True
                    else:
                        irData['PawnMovesWhite'][i] = irData['PawnMovesWhite'][i] +1
                        
                    if move[1].endswith('+'):
                        irData['ChecksByWhite'][i] = irData['ChecksByWhite'][i] +1
                        
                    if '=' in move[1]:
                        irData['PawnPromotionWhite'][i] = irData['PawnPromotionWhite'][i] +1
                        
                    if '$' in splitres[j]:
                        if move[2] == '$1':
                            irData['GoodMoveWhite'][i] = irData['GoodMoveWhite'][i] +1
                        elif move[2] == '$2':
                            irData['MistakeWhite'][i] = irData['MistakeWhite'][i] +1
                        elif move[2] == '$3':
                            irData['BrilliantMoveWhite'][i] = irData['BrilliantMoveWhite'][i]+1
                        elif move[2] == '$4':
                            irData['BlunderWhite'][i] = irData['BlunderWhite'][i] + 1
                        elif move[2] == '$5':
                            irData['InterestingMoveWhite'][i] = irData['InterestingMoveWhite'][i] +1
                        elif move[2] == '$6':
                            irData['DubiousMoveWhite'][i] = irData['DubiousMoveWhite'][i] +1
                            
                else:
                    if move[1].startswith('R'):
                        irData['RookMovesBlack'][i] = irData['RookMovesBlack'][i] +1
                    elif move[1].startswith('N'):
                        irData['KnightMovesBlack'][i] = irData['KnightMovesBlack'][i] +1
                    elif move[1].startswith('B'):
                        irData['BishopMovesBlack'][i] = irData['BishopMovesBlack'][i] +1
                    elif move[1].startswith('K'):
                        irData['KingMovesBlack'][i] = irData['KingMovesBlack'][i] +1
                    elif move[1].startswith('Q'):
                        irData['QueenMovesBlack'][i] = irData['QueenMovesBlack'][i] +1
                    elif move[1]== 'O-O':
                        irData['KingCastlingBlack'][i] = True
                    elif move[1]== 'O-O-O':
                        irData['QueenCastlingBlack'][i] = True
                    else:
                        irData['PawnMovesBlack'][i] = irData['PawnMovesBlack'][i] +1
                    
                    if move[1].endswith('+'):
                        irData['ChecksByBlack'][i] = irData['ChecksByBlack'][i] +1
                        
                    if '=' in move[1]:
                        irData['PawnPromotionBlack'][i] = irData['PawnPromotionBlack'][i] +1
                        
                    if '$' in splitres[j]:
                        if move[2] == '$1':
                            irData['GoodMoveBlack'][i][i] = irData['GoodMoveBlack'][i] +1
                        elif move[2] == '$2':
                            irData['MistakeBlack'][i] = irData['MistakeBlack'][i] +1
                        elif move[2] == '$3':
                            irData['BrilliantMoveBlack'][i] = irData['BrilliantMoveBlack'][i] +1
                        elif move[2] == '$4':
                            irData['BlunderBlack'][i] = irData['BlunderBlack'][i] + 1
                        elif move[2] == '$5':
                            irData['InterestingMoveBlack'][i] = irData['InterestingMoveBlack'][i] +1
                        elif move[2] == '$6':
                            irData['DubiousMoveBlack'][i] = irData['DubiousMoveBlack'][i] +1
            
            if splitres[len(splitres)-1].split(' ')[1].endswith('#'):
                irData['CheckmateDone'][i] = True
            

irData<jupyter_output><empty_output><jupyter_text>GameType Prediction<jupyter_code>normalizedData = irData[irData['GameType']==2]
normalizedData = normalizedData.append(irData[irData['GameType']==0].iloc[0:1309,:], ignore_index = True)
normalizedData = normalizedData.append(irData[irData['GameType']==1].iloc[0:1309,:], ignore_index = True)
normalizedData = normalizedData.append(irData[irData['GameType']==4].iloc[0:1309,:], ignore_index = True)
clusterData = irData[[ 'TimeControl',  'GameType', 'MovesCount',
       'WhiteCaptured', 'BlackCaptured','WhiteTotalEval','BlackTotalEval',
        'GoodMoveWhite', 'MistakeWhite', 'BrilliantMoveWhite',
       'BlunderWhite', 'InterestingMoveWhite', 'DubiousMoveWhite',
       'GoodMoveBlack', 'MistakeBlack', 'BrilliantMoveBlack', 'BlunderBlack',
       'InterestingMoveBlack', 'DubiousMoveBlack', 'RookMovesWhite',
       'KnightMovesWhite', 'BishopMovesWhite', 'KingMovesWhite',
       'QueenMovesWhite', 'PawnMovesWhite', 'KingCastlingWhite',
       'QueenCastlingWhite', 'RookMovesBlack', 'KnightMovesBlack',
       'BishopMovesBlack', 'KingMovesBlack', 'QueenMovesBlack',
       'PawnMovesBlack', 'KingCastlingBlack', 'QueenCastlingBlack',
       'ChecksByWhite', 'ChecksByBlack', 'CheckmateDone', 'PawnPromotionWhite',
       'PawnPromotionBlack']]
fTrainingSet, fTestSet = train_test_split(clusterData, test_size=0.2)
clusterData.shape
clusterData['GameType'].value_counts()<jupyter_output><empty_output><jupyter_text>GameType PredictionkNN classifier<jupyter_code>XTrain = fTrainingSet.drop('GameType',axis =1)
YTrain = fTrainingSet['GameType']
XTest = fTestSet.drop('GameType', axis =1)
YTest = fTestSet['GameType']
XTrain = XTrain.drop('TimeControl', axis =1)
XTest = XTest.drop('TimeControl', axis =1)
neigh = KNeighborsClassifier(n_neighbors=5)
neigh.fit(XTrain, YTrain) 
predicted = neigh.predict(XTest)
acc = accuracy_score(YTest, predicted)
print(acc)
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report, confusion_matrix
from sklearn.metrics import precision_recall_fscore_support as score

precision, recall, fscore, _ = score(YTest, predicted)
print('precision: {}'.format(precision))
print('recall: {}'.format(recall))
print('fscore: {}'.format(fscore))
param = {'max_depth':2, 'eta':1, 'objective':'binary:logistic' }
num_round = 2
bst = xgb.train(param, dtrain, num_round)
# make prediction
preds = bst.predict(dtest)
def xgb_model(train_data, train_label, test_data, test_label):
    clf = xgb.XGBClassifier(max_depth=7,
                           min_child_weight=1,
                           learning_rate=0.1,
                           n_estimators=500,
                           silent=True,
                           objective='binary:logistic',
                           gamma=0,
                           max_delta_step=0,
                           subsample=1,
                           colsample_bytree=1,
                           colsample_bylevel=1,
                           reg_alpha=0,
                           reg_lambda=0,
                           scale_pos_weight=1,
                           seed=1,
                           missing=None)
    clf.fit(train_data, train_label, eval_metric='auc', verbose=True,
            eval_set=[(test_data, test_label)], early_stopping_rounds=100)
    y_pre = clf.predict(test_data)
    y_pro = clf.predict_proba(test_data)[:, 1]
    print ("AUC Score : " ,metrics.roc_auc_score(test_label, y_pro))
    print("Accuracy : "  ,metrics.accuracy_score(test_label, y_pre))
    return clf
from sklearn.cluster import KMeans
from xgboost.sklearn import XGBClassifier
import xgboost as xgb
xclass = XGBClassifier()
xclass.fit(XTrain, YTrain)
predicted = xclass.predict(XTest)
acc = accuracy_score(YTest, predicted)
print(acc)
precision, recall, fscore, _ = score(YTest, predicted)
print('precision: {}'.format(precision))
print('recall: {}'.format(recall))
print('fscore: {}'.format(fscore))
from sklearn.tree import DecisionTreeClassifier

# Make a decision tree and train
tree = DecisionTreeClassifier()
tree.fit(XTrain, YTrain)
predicted = tree.predict(XTest)

acc = accuracy_score(YTest, predicted)
print(acc)
clusterData
clusterX = clusterData.drop('GameType', axis =1)
model = KMeans(n_clusters = 4)
model.fit(clusterX)
y_kmeans = model.predict(clusterX)
plt.scatter(clusterData['WhiteElo'], y_kmeans
            , c=y_kmeans, s=50, cmap='viridis')

clusterData['GameType'].value_counts()
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
clusterData['GameType'].value_counts()
#clustering with distance_threshold as 2
part_df = clusterData
part_df_scaled = pd.DataFrame(scaler.fit_transform(part_df), columns=part_df.columns)
ag = AgglomerativeClustering(None, affinity='euclidean', distance_threshold=20)
ag.fit(part_df_scaled)
ag.labels_.max()
pca = PCA(2)
part_df_scaled_compress = pca.fit_transform(part_df_scaled)
pca_df = pd.DataFrame(part_df_scaled_compress, columns=['pca0', 'pca1'])
pca_df['labels'] = ag.labels_
pca_df['GameType'] = finalData.GameType
pca_df.head(2)
plt.figure(figsize=(12,12))
sns.scatterplot(
    x= 'pca0',
    y= 'pca1',
    data = pca_df,
    hue = 'labels',
    palette=sns.color_palette("hls", pca_df.labels.unique().shape[0]),
    legend="full",
    alpha=0.9
)
plt.title('Clustering (after pca)', fontsize=20)
plt.xlabel('PCA one', fontsize=15)
plt.ylabel('PCA two', fontsize=15)<jupyter_output><empty_output><jupyter_text>White Rating Prediction<jupyter_code>predData = irData.copy()
predData = predData.drop('Moves',axis =1)
fTrainingSet, fTestSet = train_test_split(predData, test_size=0.2)
XTrain = fTrainingSet.drop('WhiteElo',axis =1)
XTrain = XTrain.drop('BlackElo',axis =1)
YTrain = fTrainingSet['WhiteElo']
XTest = fTestSet.drop('WhiteElo', axis =1)
XTest = XTest.drop('BlackElo', axis =1)
YTest = fTestSet['WhiteElo']
# Linear Regression Model Creation
lin_reg = LinearRegression()
lin_reg.fit(XTrain, YTrain)
#predict on test data split
y_test_predictions_lin_reg = lin_reg.predict(XTest)
# print (len(y_test_predictions_lin_reg))
lin_mse = mean_squared_error(YTest, (y_test_predictions_lin_reg))
lin_rmse = np.sqrt(lin_mse)
print(lin_rmse)
from sklearn import linear_model
clf = linear_model.Lasso(alpha=0.1)
clf.fit(XTrain, YTrain)
y_test_predictions_lin_reg = clf.predict(XTest)

lin_mse = mean_squared_error(YTest, (y_test_predictions_lin_reg))
lin_rmse = np.sqrt(lin_mse)
print(lin_rmse)
YTrain = fTrainingSet['BlackElo']
YTest = fTestSet['BlackElo']
# Linear Regression Model Creation
lin_reg = LinearRegression()
lin_reg.fit(XTrain, YTrain)
#predict on test data split
y_test_predictions_lin_reg = lin_reg.predict(XTest)
# print (len(y_test_predictions_lin_reg))
lin_mse = mean_squared_error(YTest, (y_test_predictions_lin_reg))
lin_rmse = np.sqrt(lin_mse)
print(lin_rmse)
data['Black'].value_counts()
data[(data['Black']=='Meshter') | (data['White']=='Meshter')]['Event'].value_counts()
bData = data.copy()
bData["RemTimeAftFirstMoveWhite"] = ""
bData["RemTimeAftLastMoveWhite"] = ""
bData["RemTimeAftFirstMoveBlack"] = ""
bData["RemTimeAftLastMoveBlack"] = ""
bData['WhiteTotalEval'] = ''
bData['BlackTotalEval'] = ''
for i in range(0, len(bData['Moves'])):
    if i%5000 == 0:
        print(i)
    try:
        if bData['Termination'][i]!='Abandoned' and bData['Moves'][i] is not None:
            if 'eval' in bData['Moves'][i]:
                splitres = bData['Moves'][i].split(' } ')
                whiteEvalSum = 0
                blackEvalSum = 0

                for j in range(0, len(splitres)):
                    if j%2 == 0:
                        if '%eval ' in splitres[j]:
                            #if splitres[j].split(']')[0].split('%eval ')[1]=='#1':
                            #    whiteEvalSum = whiteEvalSum + 1
                            if '#' not in splitres[j].split(']')[0].split('%eval ')[1]:
                                whiteEvalSum = whiteEvalSum + float(splitres[j].split(']')[0].split('%eval ')[1])
                    else:
                        if '%eval ' in splitres[j]:
                            #if '%eval ' in splitres[j] and splitres[j].split(']')[0].split('%eval ')[1]=='#1':
                            #    blackEvalSum = blackEvalSum + 1
                            if '#' not in splitres[j].split(']')[0].split('%eval ')[1]:
                                blackEvalSum = blackEvalSum + float(splitres[j].split(']')[0].split('%eval ')[1])
                bData['WhiteTotalEval'][i] = whiteEvalSum
                bData['BlackTotalEval'][i] = blackEvalSum

                if 'clk' in bData['Moves'][i]:
                    bData["RemTimeAftFirstMoveWhite"][i] = splitres[0].split(']')[1].split('%clk ')[1]
                    bData["RemTimeAftFirstMoveBlack"][i] = splitres[1].split(']')[1].split('%clk ')[1]
                    length = len(splitres)
                    if length%2 == 0:
                        if '%eval ' in splitres[length-2]:
                            bData["RemTimeAftLastMoveWhite"][i] = splitres[length-2].split(']')[1].split('%clk ')[1]
                        elif '%eval ' not in splitres[length-2]:
                            bData["RemTimeAftLastMoveWhite"][i] = splitres[length-2].split(']')[0].split('%clk ')[1]
                        if '%eval ' in splitres[length-1]:
                            bData["RemTimeAftLastMoveBlack"][i] = splitres[length-1].split(']')[1].split('%clk ')[1]
                        elif '%eval ' not in splitres[length-1]:
                            bData["RemTimeAftLastMoveBlack"][i] = splitres[length-1].split(']')[0].split('%clk ')[1]
                    else:
                        if '%eval ' in splitres[length-2]:
                            bData["RemTimeAftLastMoveBlack"][i] = splitres[length-2].split(']')[1].split('%clk ')[1]
                        elif '%eval ' not in splitres[length-2]:
                            bData["RemTimeAftLastMoveBlack"][i] = splitres[length-2].split(']')[0].split('%clk ')[1]
                        if '%eval ' in splitres[length-1]:
                            bData["RemTimeAftLastMoveWhite"][i] = splitres[length-1].split(']')[1].split('%clk ')[1]
                        elif '%eval ' not in splitres[length-1]:
                            bData["RemTimeAftLastMoveWhite"][i] = splitres[length-1].split(']')[0].split('%clk ')[1]


            elif 'clk' in bData['Moves'][i]:
                splitres = bData['Moves'][i].split(' } ')
                bData["RemTimeAftFirstMoveWhite"][i] = splitres[0].split("]")[0].split('%clk ')[1]
                bData["RemTimeAftFirstMoveBlack"][i] = splitres[1].split("]")[0].split('%clk ')[1]
                length = len(splitres)
                if length%2 == 0:
                    bData["RemTimeAftLastMoveWhite"][i] = splitres[length-2].split("]")[0].split('%clk ')[1]
                    bData["RemTimeAftLastMoveBlack"][i] = splitres[length-1].split("]")[0].split('%clk ')[1]
                else:
                    bData["RemTimeAftLastMoveBlack"][i] = splitres[length-2].split("]")[0].split('%clk ')[1]
                    bData["RemTimeAftLastMoveWhite"][i] = splitres[length-1].split("]")[0].split('%clk ')[1]
    except:
        print(i)

bData['a']=''
bData['b']=''
bData['c']=''
bData['d']=''
for i in range(0,bData.shape[0]):
    if i%5000==0:
        print(i)
    bData['a'][i] = pd.Timedelta(str(bData['RemTimeAftFirstMoveWhite'][i])).seconds
    bData['b'][i] = pd.Timedelta(str(bData['RemTimeAftLastMoveWhite'][i])).seconds
    bData['c'][i] = pd.Timedelta(str(bData['RemTimeAftFirstMoveBlack'][i])).seconds
    bData['d'][i] = pd.Timedelta(str(bData['RemTimeAftLastMoveBlack'][i])).seconds

bData['RemTimeAftFirstMoveWhite'] = bData['a']
bData['RemTimeAftLastMoveWhite'] = bData['b']
bData['RemTimeAftFirstMoveBlack'] = bData['c']
bData['RemTimeAftLastMoveBlack'] = bData['d']

bData = bData.drop('a',axis =1)
bData = bData.drop('b',axis =1)
bData = bData.drop('c',axis =1)
bData = bData.drop('d',axis =1)
bData
bData.drop(bData[bData['RemTimeAftFirstMoveWhite']==''].index, inplace = True)
bData.drop(bData[bData['RemTimeAftLastMoveWhite']==''].index, inplace = True)
bData.drop(bData[bData['RemTimeAftFirstMoveBlack']==''].index, inplace = True)
bData.drop(bData[bData['RemTimeAftLastMoveBlack']==''].index, inplace = True)
bData['a'] = pd.to_datetime(bData['RemTimeAftFirstMoveWhite'],format= '%H:%M:%S').dt.time
bData['b'] = pd.to_datetime(bData['RemTimeAftLastMoveWhite'],format= '%H:%M:%S').dt.time
bData['c'] = pd.to_datetime(bData['RemTimeAftFirstMoveBlack'],format= '%H:%M:%S').dt.time
bData['d'] = pd.to_datetime(bData['RemTimeAftLastMoveBlack'],format= '%H:%M:%S').dt.time
bData.dtypes
pd.to_timedelta(bData['a'], unit='s')
pd.Timedelta(str(bData['RemTimeAftFirstMoveWhite'])).seconds
pd.Timedelta(bData['RemTimeAftFirstMoveWhite']).seconds<jupyter_output><empty_output><jupyter_text>Upsets<jupyter_code>irData.shape
irData['isUpset']=0
for i in range(0,len(irData)):
    if irData['Result'][i]== 0:
        if irData['BlackElo'][i]<irData['WhiteElo'][i]:
            irData['isUpset'][i]=1
    if irData['Result'][i]== 1:
        if irData['BlackElo'][i]>irData['WhiteElo'][i]:
            irData['isUpset'][i]=1
upsetData = irData.copy()
irData.dtypes
upsetData['KingCastlingWhite'] = LabelEncoder().fit_transform(upsetData['KingCastlingWhite'])
upsetData['QueenCastlingWhite'] = LabelEncoder().fit_transform(upsetData['QueenCastlingWhite'])
upsetData['KingCastlingBlack'] = LabelEncoder().fit_transform(upsetData['KingCastlingBlack'])
upsetData['QueenCastlingBlack'] = LabelEncoder().fit_transform(upsetData['QueenCastlingBlack'])
upsetData['CheckmateDone'] = LabelEncoder().fit_transform(upsetData['CheckmateDone'])
upsetData = upsetData.drop('Moves',axis =1)
fTrainingSet, fTestSet = train_test_split(upsetData, test_size=0.2)
XTrain = fTrainingSet.drop('isUpset',axis =1)
YTrain = fTrainingSet['isUpset']
XTest = fTestSet.drop('isUpset', axis =1)
YTest = fTestSet['isUpset']
neigh = KNeighborsClassifier(n_neighbors=2)
neigh.fit(XTrain, YTrain) 
predicted = neigh.predict(XTest)
acc = accuracy_score(YTest, predicted)
print(acc)
from sklearn.linear_model import LogisticRegression

clf = LogisticRegression(random_state=0).fit(XTrain, YTrain)
predicted = clf.predict(XTest)


acc = accuracy_score(YTest, predicted)
print(acc)
lin_mse = mean_squared_error(YTest, (predicted))
lin_rmse = np.sqrt(lin_mse)
print(lin_rmse)<jupyter_output>0.6454001498167309<jupyter_text>END<jupyter_code>#export_csv = evalData.to_csv (r'C:\Users\ujwal\data\stonybrook\fall2019\dataScienceFundamentals\project\dataset\evalData.csv', index = None, header=True)
#goodmoves = dataNotNull[dataNotNull['Moves'].str.contains("\$") & dataNotNull['Moves'].str.contains("eval")]
#a = goodmoves['Moves'][86]
#"$" in a
#goodmoves.shape
evalData['GoodMoveWhite'] = 0
evalData['MistakeWhite'] = 0
evalData['BrilliantMoveWhite'] = 0
evalData['BlunderWhite'] = 0
evalData['InterestingMoveWhite'] = 0
evalData['DubiousMoveWhite'] = 0
evalData['GoodMoveBlack'] = 0
evalData['MistakeBlack'] = 0
evalData['BrilliantMoveBlack'] = 0
evalData['BlunderBlack'] = 0
evalData['InterestingMoveBlack'] = 0
evalData['DubiousMoveBlack'] = 0
pieces = ['R','K','B','K','Q']
evalData['RookMovesWhite'] = 0
evalData['KnightMovesWhite'] = 0
evalData['BishopMovesWhite'] = 0
evalData['KingMovesWhite'] = 0
evalData['QueenMovesWhite'] = 0
evalData['PawnMovesWhite'] = 0
evalData['KingCastlingWhite'] = False
evalData['QueenCastlingWhite'] = False

evalData['RookMovesBlack'] = 0
evalData['KnightMovesBlack'] = 0
evalData['BishopMovesBlack'] = 0
evalData['KingMovesBlack'] = 0
evalData['QueenMovesBlack'] = 0
evalData['PawnMovesBlack'] = 0
evalData['KingCastlingBlack'] = False
evalData['QueenCastlingBlack'] = False
evalData['ChecksByWhite'] = 0
evalData['ChecksByBlack'] = 0
evalData['CheckmateDone'] = False
evalData['PawnPromotionWhite'] = 0
evalData['PawnPromotionBlack'] = 0

#evalData.reset_index(inplace=True, drop=True)
for i in range(0,evalData.shape[0]):
    if i%1000 == 0:
        print(i)
    if evalData['Termination'][i]!='Abandoned' and evalData['Moves'][i] is not None:
        if "$" in evalData['Moves'][i]:
            splitres = evalData['Moves'][i].split(' } ')
            
            for j in range(0, len(splitres)):
                move = splitres[j].split(' ')
                if j%2 == 0:
                    if move[1].startswith('R'):
                        evalData['RookMovesWhite'][i] = evalData['RookMovesWhite'][i] +1
                    elif move[1].startswith('N'):
                        evalData['KnightMovesWhite'][i] = evalData['KnightMovesWhite'][i] +1
                    elif move[1].startswith('B'):
                        evalData['BishopMovesWhite'][i] = evalData['BishopMovesWhite'][i] +1
                    elif move[1].startswith('K'):
                        evalData['KingMovesWhite'][i] = evalData['KingMovesWhite'][i] +1
                    elif move[1].startswith('Q'):
                        evalData['QueenMovesWhite'][i] = evalData['QueenMovesWhite'][i] +1
                    elif move[1]== 'O-O':
                        evalData['KingCastlingWhite'][i] = True
                    elif move[1]== 'O-O-O':
                        evalData['QueenCastlingWhite'][i] = True
                    else:
                        evalData['PawnMovesWhite'][i] = evalData['PawnMovesWhite'][i] +1
                        
                    if move[1].endswith('+'):
                        evalData['ChecksByWhite'][i] = evalData['ChecksByWhite'][i] +1
                        
                    if '=' in move[1]:
                        evalData['PawnPromotionWhite'][i] = evalData['PawnPromotionWhite'][i] +1
                        
                    if '$' in splitres[j]:
                        if move[2] == '$1':
                            evalData['GoodMoveWhite'][i] = evalData['GoodMoveWhite'][i] +1
                        elif move[2] == '$2':
                            evalData['MistakeWhite'][i] = evalData['MistakeWhite'][i] +1
                        elif move[2] == '$3':
                            evalData['BrilliantMoveWhite'][i] = evalData['BrilliantMoveWhite'][i]+1
                        elif move[2] == '$4':
                            evalData['BlunderWhite'][i] = evalData['BlunderWhite'][i] + 1
                        elif move[2] == '$5':
                            evalData['InterestingMoveWhite'][i] = evalData['InterestingMoveWhite'][i] +1
                        elif move[2] == '$6':
                            evalData['DubiousMoveWhite'][i] = evalData['DubiousMoveWhite'][i] +1
                            
                else:
                    if move[1].startswith('R'):
                        evalData['RookMovesBlack'][i] = evalData['RookMovesBlack'][i] +1
                    elif move[1].startswith('N'):
                        evalData['KnightMovesBlack'][i] = evalData['KnightMovesBlack'][i] +1
                    elif move[1].startswith('B'):
                        evalData['BishopMovesBlack'][i] = evalData['BishopMovesBlack'][i] +1
                    elif move[1].startswith('K'):
                        evalData['KingMovesBlack'][i] = evalData['KingMovesBlack'][i] +1
                    elif move[1].startswith('Q'):
                        evalData['QueenMovesBlack'][i] = evalData['QueenMovesBlack'][i] +1
                    elif move[1]== 'O-O':
                        evalData['KingCastlingBlack'][i] = True
                    elif move[1]== 'O-O-O':
                        evalData['QueenCastlingBlack'][i] = True
                    else:
                        evalData['PawnMovesBlack'][i] = evalData['PawnMovesBlack'][i] +1
                    
                    if move[1].endswith('+'):
                        evalData['ChecksByBlack'][i] = evalData['ChecksByBlack'][i] +1
                        
                    if '=' in move[1]:
                        evalData['PawnPromotionBlack'][i] = evalData['PawnPromotionBlack'][i] +1
                        
                    if '$' in splitres[j]:
                        if move[2] == '$1':
                            evalData['GoodMoveBlack'][i][i] = evalData['GoodMoveBlack'][i] +1
                        elif move[2] == '$2':
                            evalData['MistakeBlack'][i] = evalData['MistakeBlack'][i] +1
                        elif move[2] == '$3':
                            evalData['BrilliantMoveBlack'][i] = evalData['BrilliantMoveBlack'][i] +1
                        elif move[2] == '$4':
                            evalData['BlunderBlack'][i] = evalData['BlunderBlack'][i] + 1
                        elif move[2] == '$5':
                            evalData['InterestingMoveBlack'][i] = evalData['InterestingMoveBlack'][i] +1
                        elif move[2] == '$6':
                            evalData['DubiousMoveBlack'][i] = evalData['DubiousMoveBlack'][i] +1
            
            if splitres[len(splitres)-1].split(' ')[1].endswith('#'):
                evalData['CheckmateDone'][i] = True
            
evalData
#evalData.reset_index(inplace=True, drop=True)
print(evalData['Moves'][16])
evalData.loc[16,:]
evalData.columns
x = dataNotNull[dataNotNull['Moves'].str.contains("\=")] 
x['Moves'][16]
m = '1. e4 { [%eval 0.24] [%clk 0:05:00] } 1... e5 { [%eval 0.37] [%clk 0:05:00] } 2. Nc3 { [%eval 0.13] [%clk 0:04:59] } 2... Nc6 { [%eval 0.25] [%clk 0:04:59] } 3. Nf3 { [%eval 0.16] [%clk 0:04:55] } 3... Bb4 { [%eval 0.47] [%clk 0:04:47] } 4. d4 { [%eval -0.02] [%clk 0:04:53] } 4... f6 $6 { [%eval 0.72] [%clk 0:04:28] } 5. Bd2 { [%eval 0.87] [%clk 0:04:49] } 5... exd4 { [%eval 0.93] [%clk 0:04:25] } 6. Nb5 { [%eval 0.51] [%clk 0:04:45] } 6... Bxd2+ $6 { [%eval 1.19] [%clk 0:04:14] } 7. Qxd2 { [%eval 0.9] [%clk 0:04:43] } 7... a6 { [%eval 1.09] [%clk 0:04:07] } 8. Nbxd4 { [%eval 1.05] [%clk 0:04:40] } 8... Qe7 $6 { [%eval 1.98] [%clk 0:03:56] } 9. Bd3 $2 { [%eval 0.95] [%clk 0:04:18] } 9... d5 $2 { [%eval 2.74] [%clk 0:03:23] } 10. Nxc6 $6 { [%eval 2.07] [%clk 0:04:02] } 10... bxc6 { [%eval 1.87] [%clk 0:03:21] } 11. Qe2 $6 { [%eval 1.29] [%clk 0:03:35] } 11... dxe4 $2 { [%eval 2.37] [%clk 0:03:14] } 12. Bxe4 { [%eval 2.26] [%clk 0:03:32] } 12... Bd7 $4 { [%eval 5.37] [%clk 0:02:58] } 13. O-O-O { [%eval 4.72] [%clk 0:03:21] } 13... Nh6 $4 { [%eval 11.48] [%clk 0:02:46] } 14. Rxd7 { [%eval 10.95] [%clk 0:03:13] } 14... Qc5 $6 { [%eval #8] [%clk 0:01:54] } 15. Bxc6+ { [%eval #7] [%clk 0:03:00] } 15... Kf8 { [%eval #6] [%clk 0:01:51] } 16. Bxa8 $6 { [%eval 33.8] [%clk 0:02:50] } 16... Nf7 $6 { [%eval #4] [%clk 0:01:39] } 17. Rhd1 $6 { [%eval 27.95] [%clk 0:02:39] } 17... g6 $6 { [%eval #5] [%clk 0:01:35] } 18. Bd5 { [%eval #5] [%clk 0:02:32] } 18... Nd6 { [%eval #1] [%clk 0:01:18] } 19. Qe7# { [%clk 0:02:28] }'
d = evalData.copy()
d['GameType'] = pd.np.where(d.Event.str.contains("Blitz"), "Blitz",
            pd.np.where(d.Event.str.contains("Bullet"), "Bullet",
                        pd.np.where(d.Event.str.contains("Rapid"), "Rapid",
                                    pd.np.where(d.Event.str.contains("Classical"), "Classical",
                                                "Correspondence"))))
d
r = (d['WhiteElo'].max() - d['WhiteElo'].min())/3
def addRating(row):
    if row['WhiteElo']<=d['WhiteElo'].min()+r :
        return 'low'
    elif row['WhiteElo']<=d['WhiteElo'].max()-r :
        return 'medium'
    return 'high'
d['Rating'] = d.apply(addRating, axis = 1)
d
d.columns
d['indexCol'] = d.index
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
d['Termination'] = LabelEncoder().fit_transform(d['Termination'])
d['KingCastlingWhite'] = LabelEncoder().fit_transform(d['KingCastlingWhite'])
d['QueenCastlingWhite'] = LabelEncoder().fit_transform(d['QueenCastlingWhite'])
d['KingCastlingBlack'] = LabelEncoder().fit_transform(d['KingCastlingBlack'])
d['QueenCastlingBlack'] = LabelEncoder().fit_transform(d['QueenCastlingBlack'])
d['PawnPromotionWhite'] = LabelEncoder().fit_transform(d['PawnPromotionWhite'])
d['PawnPromotionBlack'] = LabelEncoder().fit_transform(d['PawnPromotionBlack'])
d['GameType'] = LabelEncoder().fit_transform(d['GameType'])
d['Rating'] = LabelEncoder().fit_transform(d['Rating'])
myCols = ['WhiteElo', 'BlackElo',  'Termination', 'MistakeWhite',  'BlunderWhite',
        'DubiousMoveWhite',
       'MistakeBlack', 'BlunderBlack',
       'DubiousMoveBlack', 'RookMovesWhite',
       'KnightMovesWhite', 'BishopMovesWhite', 'KingMovesWhite',
       'QueenMovesWhite', 'PawnMovesWhite', 'RookMovesBlack', 'KnightMovesBlack',
       'BishopMovesBlack', 'KingMovesBlack', 'QueenMovesBlack',
       'PawnMovesBlack', 'ChecksByWhite', 'ChecksByBlack', 'GameType', 'Rating']
r = d[myCols].corr(method='pearson',min_periods=1) 
plt.figure(figsize=(15, 10) )
ax = sns.heatmap(r, vmin=-1, vmax=1, xticklabels=r.columns, 
                      yticklabels=r.columns, cmap='RdBu_r', annot=False, linewidth=0.5)
ax.set_xticklabels(ax.get_xticklabels(), rotation=40, ha="right")
evalData['DubiousMoveWhite'].value_counts()
d['GameType'].value_counts()
d[d['GameType']=='Blitz'].iloc[0:783,:]
evalData
d1 = d[d['GameType']=='Classical']
d1 = d1.append(d[d['GameType']=='Blitz'].iloc[0:783,:], ignore_index = True)
d1 = d1.append(d[d['GameType']=='Rapid'].iloc[0:783,:], ignore_index = True)
d1 = d1.append(d[d['GameType']=='Bullet'].iloc[0:783,:], ignore_index = True)
d1
s = d1['BlunderWhite'].groupby(d1['GameType']).value_counts()
s.values
a= pd.DataFrame({"GameType":['Blitz','Blitz','Blitz','Blitz','Blitz','Bullet','Bullet','Bullet','Bullet','Bullet',
                            'Classical','Classical','Classical','Classical','Classical', 'Rapid','Rapid','Rapid',
                            'Rapid','Rapid',], 
                         "BlunderWhite":[1,0,2,3,4,1,2,0,3,4,1,0,2,3,4,1,0,2,3,4],
                "Count":[204,186,178,93,58,197,174,165,102,68,235,186,154,89,42,240,206,153,95,38]})
ax = sns.lineplot(x="BlunderWhite", y="Count", hue="GameType",data=a)
plt.figure(figsize=(15,20))
sns.lmplot( x="indexCol", y="MistakeWhite", data=d.iloc[:1000,:], fit_reg=False, hue='GameType', legend=False)
 
# Move the legend to an empty part of the plot
plt.legend(loc='lower right')
d2 = evalData.copy()
d2
r = (d2['WhiteElo'].max() - d2['WhiteElo'].min())/3
d2['Rating'] = d2.apply(addRating, axis = 1)
d2['Rating'].value_counts()
d3 = d2[d2['Rating']=='high']
d3 = d3.append(d2[d2['Rating']=='low'].iloc[0:844,:], ignore_index = True)
d3 = d3.append(d2[d2['Rating']=='medium'].iloc[0:844,:], ignore_index = True)
s2 = d3['BlunderWhite'].groupby(d3['Rating']).value_counts()
s2
s2.values[0:6]
b= pd.DataFrame({"Rating":['high','high','high','high','high','high', 'low','low','low','low','low','low','medium',
                            'medium','medium','medium','medium','medium',], 
                         "BlunderWhite":[0,1,2,3,4,5,1,2,0,3,4,5,1,0,2,3,4,5],
                "Count":[324, 232, 143,  76,  35,  19, 218, 209, 138, 109,  54,  52, 239, 218, 168, 104,  50,  35]})
bx = sns.lineplot(x="BlunderWhite", y="Count", hue="Rating",data=b)
plt.figure(figsize=(15,20))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17232142857142857, 'fraction_numerical': 0.02726328903654485, 'mean_word_length': 3.0904535417020553, 'pattern_counts': {'":': 6, '<': 52, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '662262', 'license': 'MIT', 'n_tokens_mistral': 17338, 'n_tokens_neox': 16448, 'n_words': 2495, 'path': 'finalNotebook.ipynb', 'repo_name': 'ujwalvemula/data_science_chess_analysis', 'size': '48160'}