best_aro = sys.maxint

for i in range(50):

    train_ids = ids
    random.shuffle(train_ids)
    all_ids = train_ids[141:]
    train_ids = train_ids[0:140]

    # calcultae valence and arousal find_a_v_mens
    val_mean, aro_mean = find_a_v_mens_va(train_ids, valence, arousal)
    train_feat = find_in_dict(feat, train_ids)
    test_feat = find_in_dict(feat, all_ids)

    # use regression
    X_v, X_a = regression(train_feat, val_mean, aro_mean)

    # calculating features for whole dataset

    #print all_feat.shape

    # use regresion function to calculate v and a
    all_val = np.sum(np.array(test_feat) * X_v, axis=1)
    all_aro = np.sum(test_feat * X_a, axis=1)

    #print all_val.shape
    #print all_aro.shape

    print "ATTEMPT" + str(i)
    avg = average_distance_va(all_val, all_aro, valence, arousal, all_ids)
    nearest = nearest_dist_average_va(all_val, all_aro, valence, arousal,
    Xtrain = X[1:trainlen]
    Yvtrain = Yv[1:trainlen]
    Yatrain = Ya[1:trainlen]

    Xtest = X[trainlen + 1:]
    Yvtest = Yv[trainlen + 1:]
    Yatest = Ya[trainlen + 1:]
    idstest = all_ids[trainlen + 1:]

    print "check 5"
    print X.shape
    print len(Yv)
    print len(Ya)

    rv, ra = regression(Xtrain, Yvtrain, Yatrain)
    Yvpred = np.sum(Xtest * rv, axis=1)
    Yapred = np.sum(Xtest * ra, axis=1)

    # print len(Yvpred)
    # print len(Yvtest)
    # print Yvpred.shape
    # print Yvtest.shape
    # avg = averagedist(Yvpred, Yapred, Yvtest, Yatest)
    avg = average_distance_va(Yvpred, Yapred, valence, arousal, idstest)

    if avg < best_avg:
        best_avg = avg

print "BEST"
print 'Average distance: ' + str(best_avg)
for i in range(50):

    train_ids = ids
    random.shuffle(train_ids)
    all_ids = train_ids[141:]
    train_ids = train_ids[0:140]


	# calcultae valence and arousal find_a_v_mens
    val_mean, aro_mean = find_a_v_mens_va(train_ids, valence, arousal)
    train_feat = find_in_dict(feat, train_ids)
    test_feat = find_in_dict(feat, all_ids)

	# use regression
    X_v, X_a = regression(train_feat, val_mean, aro_mean)

	# calculating features for whole dataset

	#print all_feat.shape

	# use regresion function to calculate v and a
    all_val = np.sum(np.array(test_feat) * X_v, axis=1)
    all_aro = np.sum(test_feat * X_a, axis=1)

	#print all_val.shape
	#print all_aro.shape

    print "ATTEMPT" + str(i)
    avg = average_distance_va(all_val, all_aro, valence, arousal, all_ids)
    nearest = nearest_dist_average_va(all_val, all_aro, valence, arousal, all_ids)
    Xtrain = X[1:trainlen]
    Yvtrain = Yv[1:trainlen]
    Yatrain = Ya[1:trainlen]

    Xtest = X[trainlen + 1 :]
    Yvtest = Yv[trainlen + 1 :]
    Yatest = Ya[trainlen + 1 :]
    idstest = all_ids[trainlen + 1 :]

    print "check 5"
    print X.shape
    print len(Yv)
    print len(Ya)

    rv, ra = regression(Xtrain, Yvtrain, Yatrain)
    Yvpred = np.sum(Xtest * rv, axis=1)
    Yapred = np.sum(Xtest * ra, axis=1)

    # print len(Yvpred)
    # print len(Yvtest)
    # print Yvpred.shape
    # print Yvtest.shape
    # avg = averagedist(Yvpred, Yapred, Yvtest, Yatest)
    avg = average_distance_va(Yvpred, Yapred, valence, arousal, idstest)

    if avg < best_avg:
        best_avg = avg

print "BEST"
print "Average distance: " + str(best_avg)