Kaggle competition: Python for Data science ITEA
<SOS> metric: smape Kaggle data source: python-for-data-science-itea<data_type_conversions>
warnings.simplefilter(action='ignore', category=FutureWarning )
data=pd.concat([train_x,test_x],axis=0)
 data.DateTime=pd.to_datetime(data.DateTime)
 data.head()<categorify>
train = pd.read_csv('.. /input/insta_train.csv')
 test = pd.read_csv('.. /input/insta_test.csv' )
h = FeatureHasher(n_features=100,input_type="string")
 data["Name"]=data["Name"].fillna("None")
<count_values>
v = TfidfVectorizer(max_features=None, max_df=0.1, stop_words='english', min_df=0.0003)
 x = v.fit(train['caption'].fillna('' ).values)
 a = v.transform(train['caption'].fillna('' ).values)
 b = v.transform(test['caption'].fillna('' ).values )
for i in data["Name"].value_counts().index[0:400]:
print(i )<feature_engineering>
model = PCA(n_components=100, random_state=0)
 model.fit(a.toarray())
 transformed = model.transform(a.toarray())
 transformed_test = model.transform(b.toarray() )
fre_name=data["Name"].value_counts().index[0:400]
def etl(data):
data["year"]=data.DateTime.dt.year
data["month"]=data.DateTime.dt.month
data["day"]=data.DateTime.dt.day
data["dayofweek"]=data.DateTime.dt.dayofweek
data["hour"]=data.DateTime.dt.hour
data["AgeuponOutcome"]= data.AgeuponOutcome.fillna("-1")
 data["SexuponOutcome"]=data.SexuponOutcome.fillna("Unknown")
 data["color"]=data.Color.apply(lambda e: e.split("/")[0].split(" ")[0])
 data["color1"]=data.Color.apply(lambda e: re.split("/",e)[1].split(" ")[0]
if len(re.split("/",e)) >1 else "None")
 data["breed"]=data.Breed.apply(lambda e : e.split("/")[0])
 data["breed1"]=data.Breed.apply(lambda e: re.split("/",e)[1]
if len(re.split("/",e)) >1 else "None")
 data["is_mix"]=data.Breed.apply(lambda e : '1' if "Mix" in e else '0')
 cols=["year","month","day","dayofweek","hour","AgeuponOutcome","SexuponOutcome","color","color1",
"breed","breed1","is_mix"]
d=pd.get_dummies(data.AnimalType,prefix="AnimalType")
 for col in cols:
d=pd.concat([d,pd.get_dummies(data[col],prefix=col)],axis=1)
 for name in fre_name:
d[name]=data.Name.apply(lambda e :1 if e ==name else 0)
 d.drop(["Name"])
 return d 
<prepare_x_and_y>
train = pd.concat([train, pd.DataFrame(transformed)], axis=1)
 test = pd.concat([test, pd.DataFrame(transformed_test)], axis=1 )
d=etl(data)
<categorify>
kmeans = KMeans(
 n_clusters=30,
random_state=0 
)
 kmeans_train = kmeans.fit(a )
enc = LabelEncoder()
enc.fit(train.OutcomeType)
 train_y=enc.transform(train.OutcomeType)
 train_y<choose_model_class>
train['cluster'] = kmeans_train.labels_
test['cluster'] = kmeans.predict(b )
clf = linear_model.LogisticRegression(C=0.1,max_iter=2000,random_state=234)
 clf = RandomForestClassifier(n_estimators=1000,max_depth=50, min_samples_split=1, random_state=234)
 clf.fit(train1, train_y)
 y_pred = clf.predict(train1)
 print("Number of mislabeled points out of a total %d points : %d" %(train1.shape[0],(train_y != y_pred ).sum()))<prepare_x_and_y>
y = train['likes']
T_train_xgb = xgb.DMatrix(train1, label=train_y)
 watchlist = [(T_train_xgb, 'train'),(T_train_xgb, 'eval')]
params = {"objective": "multi:softprob",
"booster" : "gbtree",
"num_class" : 5,
"eta": 0.3,
"subsample": 0.9,
"colsample_bytree": 0.7,
"seed": 1301,
"max_depth":80,
"eval_metric":"mlogloss"
}
gbm = xgb.train(dtrain=T_train_xgb,params=params, evals=watchlist,num_boost_round = 35,verbose_eval=True)
<prepare_output>
def ing(df):
df['user_len'] = df['user'].str.len()
df['username_len'] = df['username'].str.len()
df['img_len'] = df['img'].str.len()
df['description_len'] = df['description'].str.len()
df['caption_upper'] = df['caption'].str.findall(r'[A-Z]' ).str.len()
df['po_co'] = df['posts']/(df['comments']+1)
 df['po_co_pow'] = df['posts']*(df['comments'])
 df['fol'] = df['followers']/(df['followings']+1)
 df['fol_pow'] = df['followers']*(df['followings'])
 df['act'] = df['comments']/(df['followers']+1)
 df['pos'] = df['posts']/(df['followers']+1)
 df['pof'] = df['posts']/(df['followings']+1)
 df['user_count'] = df.groupby('user')['posts'].transform('count')
 df['language_encoded'] = df.groupby('user')['language_encoded'].transform('median')
 df['hashtag_max'] = df.groupby('user')['hashtag'].transform('max')
 df['hashtag_min'] = df.groupby('user')['hashtag'].transform('min')
 df['hashtag_mean'] = df.groupby('user')['hashtag'].transform('mean')
 df['hashtag_std'] = df.groupby('user')['hashtag'].transform('std')
 df['hashtag_mean_diff'] = df['hashtag'] /(df['hashtag_mean']+1)
 df['comments_max'] = df.groupby('user')['comments'].transform('max')
 df['comments_min'] = df.groupby('user')['comments'].transform('min')
 df['comments_mean'] = df.groupby('user')['comments'].transform('mean')
 df['comments_std'] = df.groupby('user')['comments'].transform('std')
 df['comments_mean_diff'] = df['comments'] /(df['comments_mean']+1)
 df['bot'] = df['user'].str.count('_|
df['bot_caption'] = pd.to_numeric(df['caption'].str.count('_|
df['numbers_user'] = df['user'].str.contains('0|1|2|3|4|5|6|7|8|9' ).astype(int)
 df['numbers_caption'] = pd.to_numeric(df['caption'].str.contains('0|1|2|3|4|5|6|7|8|9'), errors='coerce')
 df['s1080.1080'] = pd.to_numeric(df['img'].str.contains('1080\.1080'), errors='coerce')
 df['e35'] = pd.to_numeric(df['img'].str.contains('e35'), errors='coerce')
 return df
id=test.ID
pred = gbm.predict(xgb.DMatrix(test1))
id.shape,pred.shape
submission=pd.DataFrame({"ID":id})
 submission['Adoption']=pred[:,0]
submission['Died']=pred[:,1]
submission['Euthanasia']=pred[:,2]
submission['Return_to_owner']=pred[:,3]
submission['Transfer']=pred[:,4] 
<prepare_output>
train = ing(train ).select_dtypes(exclude=['object'] ).drop(['likes'], axis=1 ).fillna(0)
 test = ing(test ).select_dtypes(exclude=['object'] ).fillna(0 )
id=test.ID
pred = clf.predict_proba(test1)
 id.shape,pred.shape
submission=pd.DataFrame({"ID":id})
 submission['Adoption']=pred[:,0]
submission['Died']=pred[:,1]
submission['Euthanasia']=pred[:,2]
submission['Return_to_owner']=pred[:,3]
submission['Transfer']=pred[:,4] 
<save_to_csv>
lgbm_model = lgb.LGBMRegressor(
 application='mae',
metric='mape',
learning_rate=0.005,
n_estimators = 5500,
num_leaves = 10,
colsample_bytree = 0.59,
subsample = 0.5,
max_bin = 100,
reg_alpha = 1,
bagging_freq = 5,
reg_lamba=10,
min_data = 1)
 param_grid = {
}
def smape(A, F):
A = 10**(A)-1
F = 10**(F)-1
return 100/len(A)* np.sum(2 * np.abs(F - A)/(np.abs(A)+ np.abs(F)))
 scoring = {'smape': make_scorer(smape, greater_is_better=False)}
submission.to_csv("submission.csv",index=False )<save_to_csv>
xgb_model = xgb.XGBRegressor(max_depth=7,
learning_rate=0.01,
n_estimators=870,
booster='gbtree',
n_jobs=-1,
min_child_weight=5,
subsample=0.8,
colsample_bytree=0.8,
base_score=0.5,
random_state=42,
reg_alpha=0.2,
reg_lambda=3
)
submission.to_csv("submission.csv",index=False)
<define_variables>
VotReg = VotingRegressor(estimators=[('lgbm', lgbm_model),('xgb', xgb_model)])
 VotReg.fit(train, np.log10(y+1))
embed_size = 300
max_features = 95000
maxlen = 70<import_modules>
print(10**(VotReg.predict(test)) -1)
 prediction = pd.read_csv('.. /input/sample_submission.csv')
 prediction['likes'] =(10**(VotReg.predict(test)))-1
prediction.to_csv('exp_submission.csv', index=False )