from sklearn.metrics import f1_score,accuracy_score
import numpy as np
from utilities.tools import load_model
import pandas as pd

def predict_MSRP_test_data(n_models,nb_words,nlp_f,test_data_1,test_data_2,test_labels):

    models=[]
    n_h_features=nlp_f.shape[1]
    print('loading the models...')
    for i in range(n_models):
        models.append(load_model(i+1,nb_words,n_h_features))

    preds=[]
    print('predicting the test data...\n')
    i=0
    for m in models:
      i+=1
      preds_prob=m.predict([test_data_1, test_data_2,nlp_f], batch_size=64, verbose=0)
      preds.append(preds_prob[:,1])

    preds=np.asarray(preds)
    final_labels=np.zeros(len(test_data_1),dtype=int)

   #average the predicttion
    for i in range(len(test_data_1)):
        final_labels[i]=round(np.mean(preds[:,i]))
        if i%100==0:
            print(i ,' out of ',len(test_data_1))

    print("test data accuracy: ", accuracy_score(final_labels,test_labels))
    print("test data f_measure: ", f1_score(final_labels, test_labels))

    submission = pd.DataFrame({"Quality": final_labels})
    submission.to_csv("predictions/MSRP.tsv", index=True,index_label='test_id')


def predict_Quora_test_data(n_models,nb_words,nlp_f,test_data_1,test_data_2):

    models=[]
    n_h_features=nlp_f.shape[1]
    print('loading the models...')
    for i in range(n_models):
        models.append(load_model(i+1,nb_words,n_h_features))

    preds=[]
    print('predicting the test data...\n')
    i=0
    for m in models:
      i+=1
      preds_prob=m.predict([test_data_1, test_data_2,nlp_f], batch_size=125, verbose=0)
      preds.append(preds_prob[:,1])

    preds=np.asarray(preds)
    final_labels=np.zeros(len(test_data_1),dtype=float)

   #average the predicttion
    for i in range(len(test_data_1)):
        final_labels[i]=np.mean(preds[:,i])
        if i%10000==0:
            print(i ,' out of ',len(test_data_1))

    print('making the sumbission file')

    submission = pd.DataFrame({"is_duplicate": final_labels})
    submission.to_csv("predictions/Quora.tsv", index=True,index_label='test_id')
[((624, 641), 'numpy.asarray', 'np.asarray', (['preds'], {}), '(preds)\n', (634, 641), True, 'import numpy as np\n'), ((1052, 1091), 'pandas.DataFrame', 'pd.DataFrame', (["{'Quality': final_labels}"], {}), "({'Quality': final_labels})\n", (1064, 1091), True, 'import pandas as pd\n'), ((1657, 1674), 'numpy.asarray', 'np.asarray', (['preds'], {}), '(preds)\n', (1667, 1674), True, 'import numpy as np\n'), ((1974, 2018), 'pandas.DataFrame', 'pd.DataFrame', (["{'is_duplicate': final_labels}"], {}), "({'is_duplicate': final_labels})\n", (1986, 2018), True, 'import pandas as pd\n'), ((920, 961), 'sklearn.metrics.accuracy_score', 'accuracy_score', (['final_labels', 'test_labels'], {}), '(final_labels, test_labels)\n', (934, 961), False, 'from sklearn.metrics import f1_score, accuracy_score\n'), ((997, 1032), 'sklearn.metrics.f1_score', 'f1_score', (['final_labels', 'test_labels'], {}), '(final_labels, test_labels)\n', (1005, 1032), False, 'from sklearn.metrics import f1_score, accuracy_score\n'), ((1822, 1844), 'numpy.mean', 'np.mean', (['preds[:, (i)]'], {}), '(preds[:, (i)])\n', (1829, 1844), True, 'import numpy as np\n'), ((354, 395), 'utilities.tools.load_model', 'load_model', (['(i + 1)', 'nb_words', 'n_h_features'], {}), '(i + 1, nb_words, n_h_features)\n', (364, 395), False, 'from utilities.tools import load_model\n'), ((793, 815), 'numpy.mean', 'np.mean', (['preds[:, (i)]'], {}), '(preds[:, (i)])\n', (800, 815), True, 'import numpy as np\n'), ((1386, 1427), 'utilities.tools.load_model', 'load_model', (['(i + 1)', 'nb_words', 'n_h_features'], {}), '(i + 1, nb_words, n_h_features)\n', (1396, 1427), False, 'from utilities.tools import load_model\n')]