<jupyter_start><jupyter_text>Determine the raltionship between region and churn, tenure, monthly rate, frequency of recharge, monthly revenue, income in 90 days/3, earning frequency, data volume purchased, top pack(top 50 analysis), top pack frequency, churn.<jupyter_code>train_data.describe()
test_data.describe()
train_data.info()
test_data.info()
train_data.nunique()
test_data['CHURN'] = 'test'
train_data.set_index('user_id', inplace = True)
test_data.set_index('user_id', inplace = True)
train_data[['REGION','TENURE', 'REVENUE', 'CHURN']].groupby(['REGION']).count().plot(kind = 'bar')
train_data[['REGION','TENURE', 'REVENUE']].groupby(['REGION']).mean().plot(kind = 'bar')
train_data.groupby(['CHURN', 'ON_NET']).count()['TENURE']
#sns.lmplot('REVENUE', 'ON_NET', hue='CHURN', data = train_data)
#sns.lmplot('REVENUE', 'MONTANT', hue='CHURN', data = train_data)
#data = pd.concat([train_data, test_data]).reset_index(drop= True)
#data.tail()<jupyter_output><empty_output><jupyter_text>missing_col = data.columns[data.isna().any()]missing_col<jupyter_code>train_data.set_index(['TENURE', 'REGULARITY', 'FREQUENCE_RECH', 'FREQUENCE', 'TOP_PACK'], inplace=True)
train_data.fillna(method= 'bfill', inplace = True)
train_data.reset_index(inplace=True)
train_data.head(2)
train_data.set_index(['REGION', 'DATA_VOLUME'], inplace=True)
train_data.fillna(method= 'bfill', inplace = True)
train_data.reset_index(inplace=True)
train_data.head(2)
train_data.dropna(inplace=True)
train_data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 399955 entries, 0 to 399954
Data columns (total 18 columns):
 #   Column          Non-Null Count   Dtype  
---  ------          --------------   -----  
 0   REGION          399955 non-null  object 
 1   DATA_VOLUME     399955 non-null  float64
 2   TENURE          399955 non-null  object 
 3   REGULARITY      399955 non-null  int64  
 4   FREQUENCE_RECH  399955 non-null  float64
 5   FREQUENCE       399955 non-null  float64
 6   TOP_PACK        399955 non-null  object 
 7   MONTANT         399955 non-null  float64
 8   REVENUE         399955 non-null  float64
 9   ARPU_SEGMENT    399955 non-null  float64
 10  ON_NET          399955 non-null  float64
 11  ORANGE          399955 non-null  float64
 12  TIGO            399955 non-null  float64
 13  ZONE1           399955 non-null  float64
 14  ZONE2           399955 non-null  float64
 15  MRG             399955 non-null  object 
 16  FREQ_TOP_PACK   399955 non-null  float64
 17  CHURN     [...]<jupyter_text>for col in missing_col.drop(['REGION', 'TOP_PACK']):    data[col].fillna(data[col].median(), inplace= True)    for col in ['TOP_PACK', 'REGION']:    data[col].fillna(col + '_NAN', inplace = True)<jupyter_code>#train_data['MONTANT'] = train_data['user_id'].map(G1)
#train_data.groupby(['user_id','TENURE', 'REGULARITY', 'MRG', 'CHURN']).mean()['MONTANT'].fillna(method= 'ffill')
from scipy.stats import skew, norm
numeric = train_data.select_dtypes(include = 'number').columns
skew_features = train_data[numeric].apply(lambda x: skew(x)).sort_values(ascending = False)

high_skew = skew_features[skew_features > 0.5]
skew_index = high_skew.index

print("There are {} numerical features with Skew > 0.5:".format(high_skew.shape[0]))
skew_features.head(18)
train_data.head(2)<jupyter_output><empty_output><jupyter_text>for col in ['REVENUE', 'MONTANT', 'ORANGE']:    fig, ax = plt.subplots(1, 2)        sns.distplot(train_data[col], ax = ax[0])    sns.distplot(train_data[col], ax = ax[1])        plt.tight_layout()<jupyter_code>#data.drop(['MRG', 'ZONE1', 'ZONE2', 'TOP_PACK', 'FREQUENCE', 'ARPU_SEGMENT', 'TIGO', 'TENURE', 'FREQ_TOP_PACK'], 1, inplace = True)
from sklearn import preprocessing
enc = preprocessing.LabelEncoder()
cat_cols = ['TENURE', 'REGION', 'MRG', 'TOP_PACK' ]

for col in cat_cols:
    train_data[col] = train_data[col].astype('str')
    test_data[col]  = test_data[col].astype('str')
    
    train_data[col] = enc.fit_transform(train_data[col])
    test_data[col]  = enc.fit_transform(test_data[col])
from sklearn import linear_model
from sklearn.model_selection import train_test_split
from sklearn.metrics import log_loss
from catboost import CatBoostClassifier, Pool
from xgboost import XGBClassifier, XGBRFClassifier
from sklearn.svm import SVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
X = train_data.drop(['CHURN', 'ZONE1', 'ZONE2',  'DATA_VOLUME', 'TIGO', 'ON_NET',  'TIGO', 'ORANGE', 'FREQ_TOP_PACK', 'MRG',
                   'MONTANT', 'ARPU_SEGMENT', 'FREQUENCE_RECH', 'REVENUE', 'REGION'], 1)
y = train_data.CHURN
#cat_cols = ['TENURE', 'REGION', 'MRG', 'TOP_PACK' ]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
attributes_train = X_train
label_train = y_train

attributes_test_a = X_test
label_test_a = y_test<jupyter_output><empty_output><jupyter_text>attributes_test = test_data.drop(['CHURN', 'ZONE1', 'ZONE2',  'DATA_VOLUME', 'TIGO', 'ON_NET',  'TIGO', 'ORANGE',                            'FREQ_TOP_PACK', 'MRG','MONTANT', 'ARPU_SEGMENT', 'FREQUENCE_RECH', 'REVENUE', 'TENURE'], 1)label_test = test_data<jupyter_code>train_a = Pool(data=attributes_train, label=label_train)
test_a = Pool(data=attributes_test_a, label=label_test_a)
test_set = test_data
cat = CatBoostClassifier()
cat.fit(train_a)
print('CatBoost log loss: {}'.format(log_loss(y_test, cat.predict_proba(X_test)[:, 1])))
#lr = LogisticRegression()
#rf = RandomForestClassifier()
#gbm2 = GradientBoostingClassifier(loss='exponential', learning_rate=0.1, n_estimators=100, max_depth=3)
#gbm = GradientBoostingClassifier()
#dt = DecisionTreeClassifier()
#xgb = XGBRFClassifier()
#xgbc = XGBClassifier()
svc = SVC(probability=True)
#lr.fit(X_train, y_train)
#rf.fit(X_train, y_train)
#gbm2.fit(X_train, y_train)
#gbm.fit(X_train, y_train)
#dt.fit(X_train, y_train)
#xgb.fit(X_train, y_train)
#xgbc.fit(X_train, y_train)
svc.fit(X_train, y_train)
print('Logistic Regression Log loss: {}'.format(log_loss(y_test, lr.predict_proba(X_test)[:, 1])))
print('Random Forest Log loss: {}'.format(log_loss(y_test, rf.predict_proba(X_test)[:, 1])))
#print('Gradient Boosting Log loss: {}'.format(log_loss(y_test, gbm2.predict_proba(X_test)[:, 1])))
print('Gradient Boosting Log loss: {}'.format(log_loss(y_test, gbm.predict_proba(X_test)[:, 1])))
print('Decison Tree {}'.format(log_loss(y_test, dt.predict_proba(X_test)[:, 1])))
print('XBGRFC {}'.format(log_loss(y_test, xgb.predict_proba(X_test)[:, 1])))
print('XBGC {}'.format(log_loss(y_test, xgbc.predict_proba(X_test)[:, 1])))
print('SVC {}'.format(log_loss(y_test, svc.predict_proba(X_test)[:, 1])))<jupyter_output><empty_output><jupyter_text>FEATURE IMPORTANCE PLOT<jupyter_code>feature_importance = pd.DataFrame(cat.feature_importances_ * 100, columns = ['importance'])
feature_importance['feature'] = X.columns

plt.figure(figsize = (12, 6));
sns.barplot(x = 'importance', y = 'feature', data = feature_importance.sort_values(by = ['importance'], 
                                                                                   ascending = False).head(20))
plt.title('GBM FEATURE IMPORTANCE FOR EXPRESSO')
test_x = test_data.drop(['CHURN', 'ZONE1', 'ZONE2',  'DATA_VOLUME', 'TIGO', 'ON_NET',  'TIGO', 'ORANGE', 
                          'FREQ_TOP_PACK', 'MRG','MONTANT', 'ARPU_SEGMENT', 'FREQUENCE_RECH', 'REVENUE', 'REGION'], 1)
predictions = cat.predict_proba(test_x)[:, 1]
submit['CHURN'] = predictions
submit.to_csv('submissionn2.csv', index = False)
submit.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15437818086911131, 'fraction_numerical': 0.030927835051546393, 'mean_word_length': 5.256326530612245, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '169943', 'license': 'Apache-2.0', 'n_tokens_mistral': 3057, 'n_tokens_neox': 2880, 'n_words': 535, 'path': 'DSN Hackathon - Expresso-Catboost.ipynb', 'repo_name': 'vikthour/Expresso-Churn-Challenge', 'size': '7663'}