Kaggle competition: Bag of Words Meets Bags of Popcorn
<SOS> metric: AUC Kaggle data source: bag-of-words-meets-bags-of-popcorn<train_model>
sample = 'This movie sucks'
fixed_size = 1024
print(sample.split())
 features = [(hash(f)%fixed_size, 1)for f in sample.split() ]
print(features )
RF.fit(new_train,target )<predict_on_test>
!unzip /kaggle/input/word2vec-nlp-tutorial/labeledTrainData.tsv.zip
!unzip /kaggle/input/word2vec-nlp-tutorial/unlabeledTrainData.tsv.zip
!unzip /kaggle/input/word2vec-nlp-tutorial/testData.tsv.zip
log_pred=RF.predict(new_test)
 predictions=np.exp(log_pred)-1<prepare_output>
PATH = '/kaggle/working/'
sub = pd.DataFrame()
sub['datetime'] = test['datetime']
sub['count'] = predictions
sub.head()<save_to_csv>
import re
import random
from math import exp, log
from datetime import datetime
from operator import itemgetter
sub.to_csv('submission.csv', index=False )<save_to_csv>
def clean(s):
return " ".join(re.findall(r'\w+', s, flags=re.UNICODE)).lower()
sub.to_csv('submission.csv', index=False )<train_model>
def get_data_tsv(loc_dataset, opts):
for e, line in enumerate(open(loc_dataset, 'rb')) :
if e > 0:
r = line.decode('utf-8' ).strip().split('\t')
 id = r[0]
if opts['clean']:
try:
r[2] = clean(r[2])
 except:
r[1] = clean(r[1])
 if len(r)== 3:
features = [(hash(f)%opts['D'], 1)for f in r[2].split() ]
label = int(r[1])
 else:
features = [(hash(f)%opts['D'], 1)for f in r[1].split() ]
label = 1
if opts['2grams']:
for i in range(len(features)-1):
features.append(
(hash(str(features[i][0])+str(features[i+1][0])) %opts['D'], 1))
yield label, id, features
lgb.fit(new_train,target )<predict_on_test>
def dot_product(features, weights):
dotp = 0
for f in features:
dotp += weights[f[0]]*f[1]
return dotp
log_pred_lgb=lgb.predict(new_test)
 predictions_lgb=np.exp(log_pred_lgb)-1<prepare_output>
def train_tron(loc_dataset, opts):
start = datetime.now()
print('
Pass\t\tErrors\t\tAverage\t\tNr.Samples\tSince Start')
 if opts['random_init']:
random.seed(3003)
 weight = [random.random() ] * opts['D']
else:
weights = [0.] * opts['D']
for pass_nr in range(opts['n_passes']):
error_counter = 0
for e,(label, id, features)in enumerate(\
get_data_tsv(loc_dataset, opts)) :
dp = dot_product(features, weights)> 0.5
error = label - dp
if error != 0:
error_counter += 1
for index, value in features:
weights[index] += opts['learning_rate'] * error * log(1.+value)
 print('%s\t\t%s\t\t%s\t\t%s\t\t%s' %(\
pass_nr+1,
error_counter,
round(1 - error_counter / float(e+1), 5),
e+1, datetime.now() -start))
if error_counter == 0 or error_counter < opts['errors_satisfied']:
print('%s erros found during training, halting' % error_counter)
 break
return weights
sub = pd.DataFrame()
sub['datetime'] = test['datetime']
sub['count'] = predictions_lgb
sub.head()<define_variables>
opts = {}
opts["D"] = 2 ** 25
opts["learning_rate"] = 0.1
opts["n_passes"] = 80
opts["errors_satisfied"] = 0
opts["random_init"] = False
opts["clean"] = True
opts["2grams"] = True
%time
weights = train_tron(PATH+"labeledTrainData.tsv",opts )
ensemble =(0.6*predictions + 0.4*predictions_lgb )<prepare_output>
%time
preds = test_tron(PATH+"testData.tsv",weights,opts )
sub = pd.DataFrame()
sub['datetime'] = test['datetime']
sub['count'] = ensemble
sub.head()<save_to_csv>
with open("submit_perceptron.csv","wb")as outfile:
outfile.write('"id","sentiment"
'.encode('utf-8'))
for p in sorted(preds):
outfile.write("{},{}
".format(p[0],p[3] ).encode('utf-8'))