Kaggle competition: Classifying Movie Reviews
ssub["item_cnt_month"]=(xgboost_lagged_features["item_cnt_month"]+random_forest_lagged_features["item_cnt_month"])/2<save_to_csv>
training_data, training_labels = load_svmlight_file('.. /input/movie-ratings/movie-ratings/data-splits/data.train')
 testing_data, testing_labels = load_svmlight_file('.. /input/movie-ratings/movie-ratings/data-splits/data.test' )
ssub.to_csv("weighted_averaging.csv",index=False )<load_from_csv>
training_labels[training_labels == 0] = -1
testing_labels[testing_labels == 0] = -1
PATH = '.. /input/competitive-data-science-predict-future-sales/'
train_data = pd.read_csv(PATH+'sales_train.csv')
 test_data = pd.read_csv(PATH+'test.csv' ).set_index('ID')
 item_data = pd.read_csv(PATH+'items.csv')
 item_category_data = pd.read_csv(PATH+'item_categories.csv')
 shop_data = pd.read_csv(PATH+'shops.csv' )<filter>
class AveragePerceptron:
def __init__(self, learning_rate=0.01, margin=0, epochs=20, avg_decay=False, decay=False):
self.w = None
self.b = None
self.a = None
self.ba = None
self.learning_rate = learning_rate
self.margin = margin
self.epochs = epochs
self.decay = decay
self.avg_decay = avg_decay
def fit(self, X, y, updates=0, **kwargs):
if kwargs:
self.learning_rate = kwargs['learning_rate']
self.margin = kwargs['margin']
self.epochs = kwargs['epochs']
self.decay = kwargs['decay']
self.avg_decay = kwargs['avg_decay']
assert X.shape[0] == y.shape[0]
if self.w is None:
self.w =(( 0.1 -(-0.1)) *(np.random.random_sample(X.shape[1])) - 0.1 ).T
if self.b is None:
self.b =(0.1 -(-0.1)) *(np.random.random_sample())- 0.1
c, t = 0, 0
self.a = np.zeros(( 1,self.w.shape[0]))
self.ba = 0
for _ in tqdm(range(self.epochs)) :
p = np.random.permutation(X.shape[0])
 current_X, current_y = X[p], y[p]
for x, label in zip(current_X, current_y):
if(label *(np.sum(x * self.w.T)+ self.b)) < self.margin:
self.w += self.learning_rate * label * x /(1 + t)
 self.b += self.learning_rate * label /(1 + t)
 if self.avg_decay:
self.a = self.a + label * x /(_+1)
 self.ba = self.ba + label /(_+1)
 else:
self.a = self.a + label * x
self.ba = self.ba + label
c = 0
c = c + 1
if self.decay:
t = t + 1
return self.score(X,y)
 def predict(self, X):
X = csr_matrix(( X.data, X.indices, X.indptr), shape=(X.shape[0], self.w.shape[1]))
return np.array([np.sign(np.sum(x * self.a.T)+ self.ba)for x in X])
 def get_params(self, deep = False):
return {'learning_rate':self.learning_rate, 'margin': self.margin, 'epochs': self.epochs,
'avg_decay': self.avg_decay, 'decay': self.decay}
def set_params(self, **parameters):
for parameter, value in parameters.items() :
setattr(self, parameter, value)
 return self
def score(self, X, y):
assert X.shape[0] == y.shape[0]
correct = sum(1 for actual, prediction in zip(y, self.predict(X)) if np.sign(actual)== np.sign(prediction))
return correct/X.shape[0]
train_data[train_data.item_price < 0]<feature_engineering>
perceptron = AveragePerceptron()
print('training accuracy: {}'.format(perceptron.fit(training_data, training_labels)) )
train_data = train_data[train_data.item_cnt_day < 1001]
train_data = train_data[train_data.item_price < 100000]
median = train_data[(train_data.shop_id==32)&(train_data.item_id==2973)&(train_data.date_block_num==4)& 
(train_data.item_price>0)].item_price.median()
train_data.loc[train_data.item_price < 0, 'item_price'] = median<feature_engineering>
perceptron.score(testing_data, testing_labels )
train_data.loc[train_data.shop_id == 0, 'shop_id'] = 57
test_data.loc[test_data.shop_id == 0, 'shop_id'] = 57
train_data.loc[train_data.shop_id == 1, 'shop_id'] = 58
test_data.loc[test_data.shop_id == 1, 'shop_id'] = 58
train_data.loc[train_data.shop_id == 10, 'shop_id'] = 11
test_data.loc[test_data.shop_id == 10, 'shop_id'] = 11<categorify>
clf = GridSearchCV(AveragePerceptron() , param_grid={'learning_rate':[1, 0.1, 0.01], 'margin':[ 0, 0.1], 'decay': [False, True], 'epochs':[50], 'avg_decay':[False, True]}, cv=10, scoring='accuracy', n_jobs=-1)
 clf.fit(testing_data, testing_labels )
shop_data.loc[shop_data.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
shop_data['city'] = shop_data['shop_name'].str.split(' ' ).map(lambda x: x[0])
 shop_data.loc[shop_data.city == '!Якутск', 'city'] = 'Якутск'
shop_data['city_code'] = LabelEncoder().fit_transform(shop_data['city'])
 shop_data = shop_data[['shop_id','city_code']]
item_category_data['split'] = item_category_data['item_category_name'].str.split('-')
 item_category_data['type'] = item_category_data['split'].map(lambda x: x[0].strip())
 item_category_data['type_code'] = LabelEncoder().fit_transform(item_category_data['type'])
 item_category_data['subtype'] = item_category_data['split'].map(lambda x: x[1].strip() if len(x)> 1 else x[0].strip())
 item_category_data['subtype_code'] = LabelEncoder().fit_transform(item_category_data['subtype'])
 item_category_data = item_category_data[['item_category_id','type_code', 'subtype_code']]
item_data.drop(['item_name'], axis=1, inplace=True )<define_variables>
print(clf.best_params_)
 print(clf.best_score_ )
%%time
matrix = []
cols = ['date_block_num','shop_id','item_id']
for i in range(34):
sales = train_data[train_data.date_block_num==i]
matrix.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype='int16'))
matrix = pd.DataFrame(np.vstack(matrix), columns=cols)
 matrix['date_block_num'] = matrix['date_block_num'].astype(np.int8)
 matrix['shop_id'] = matrix['shop_id'].astype(np.int8)
 matrix['item_id'] = matrix['item_id'].astype(np.int16)
 matrix.sort_values(cols,inplace=True)
 matrix.info()<feature_engineering>
cross_val_score(AveragePerceptron() , training_data, y=training_labels, scoring='accuracy',
fit_params=clf.best_params_, cv=5, n_jobs=-1 )
train_data['revenue'] = train_data['item_price'] * train_data['item_cnt_day']<merge>
clf.best_estimator_.score(testing_data, testing_labels )
%%time
group = train_data.groupby(['date_block_num','shop_id','item_id'] ).agg({'item_cnt_day': ['sum']})
 group.columns = ['item_cnt_month']
group.reset_index(inplace=True)
 matrix = pd.merge(matrix, group, on=cols, how='left')
 matrix['item_cnt_month'] =(matrix['item_cnt_month'] 
.fillna(0)
.clip(0,20)
.astype(np.float16))<categorify>
perceptron = clf.best_estimator_
eval_data, _ = load_svmlight_file('.. /input/movie-ratings/movie-ratings/data-splits/data.eval.anon')
 submission_pred = perceptron.predict(eval_data)
 submission_pred[submission_pred == -1.0] = 0
%%time
numerical_features = ['item_price','revenue']
transform_data = train_data
transform_data[numerical_features].apply(lambda x: skew(x.dropna())).sort_values(ascending=False )<sort_values>
with open('submission.csv', 'w')as submission:
with open('.. /input/movie-ratings/movie-ratings/data-splits/data.eval.anon.id', 'r')as example_ids:
submission.write('example_id,label
')
 for example_id, label in zip(example_ids, submission_pred):
submission.write('{},{}
'.format(example_id.strip() , int(label)) )