<jupyter_start><jupyter_text>Continuous Features<jupyter_code>fig, axes = plt.subplots(1,2, figsize=(12, 7))

sns.distplot(df["tenure"], ax=axes[0])
sns.distplot(df["MonthlyCharges"], ax=axes[1])
df[['tenure', 'MonthlyCharges', 'Churn']].groupby('Churn').mean()
df[['tenure', 'Contract']].groupby('Contract').mean()
df.columns
df.drop(['customerID', 'gender', 'PhoneService', 'Contract', 'TotalCharges'], axis = 1, inplace= True)<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
encode_features = ['SeniorCitizen', 'Partner', 'Dependents', 'MultipleLines', 'InternetService', 'OnlineSecurity', 'OnlineBackup','DeviceProtection','TechSupport', 'StreamingTV','StreamingMovies','PaperlessBilling','PaymentMethod']
dummies = pd.get_dummies(df, columns = encode_features, drop_first= True)
dummies.head(5)
scaler = MinMaxScaler()
t = scaler.fit_transform(df[['tenure']])
m = scaler.fit_transform(df[['MonthlyCharges']])
dummies['tenure'] = t
dummies['MonthlyCharges'] = m
dummies.head(5)
dummies.shape<jupyter_output><empty_output><jupyter_text>Resampling<jupyter_code>sns.countplot('Churn', data=df).set_title('Class distribution before sampling')
dummies_no = dummies[dummies.Churn == 0]
dummies_yes = dummies[dummies.Churn == 1]
print(len(dummies_yes))
print(len(dummies_no))
### Upsampling positive class(1's) 
dummies_yes_upsampled = dummies_yes.sample(n = len(dummies_no), replace= True, random_state= 10)
print(len(dummies_yes_upsampled))
dummies_upsampled = dummies_no.append(dummies_yes_upsampled).reset_index(drop= True)
sns.countplot('Churn', data= dummies_upsampled).set_title('Class distribution after resampling')
### Creating model
from sklearn.model_selection import train_test_split
X = dummies_upsampled.drop(['Churn'], axis=1)  #Independent variables
y = dummies_upsampled['Churn']  #Dependent variable
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size= 0.2, random_state= 10)
## Ridge Classifier
from sklearn.linear_model import RidgeClassifier
from sklearn.metrics import accuracy_score
ridge_clf = RidgeClassifier()
ridge_clf.fit(X_train, y_train)
pred_train = ridge_clf.predict(X_train)
accuracy_score(y_train, pred_train)
pred_test = ridge_clf.predict(X_test)
accuracy_score(y_test, pred_test)
ridge_clf.score(X_test, y_test)
ridge_clf.score(X_train, y_train)
## Random forest
from sklearn.ensemble import RandomForestClassifier
forest_clf = RandomForestClassifier(n_estimators= 100, max_depth= 10)
forest_clf.fit(X_train, y_train)
forest_clf.score(X_train, y_train)
pred_train = forest_clf.predict(X_train)
accuracy_score(y_train, pred_train)
forest_clf.score(X_test, y_test)
## Parameter tuning
from sklearn.model_selection import GridSearchCV
parameters = {'n_estimators' : [150, 200, 250, 300], 'max_depth': [15,20,25]}
forest = RandomForestClassifier()
clf = GridSearchCV(estimator= forest, param_grid= parameters, n_jobs= -1, cv = 5)
clf.fit(X, y)
clf.best_params_
clf.best_score_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13633419689119172, 'fraction_numerical': 0.014572538860103627, 'mean_word_length': 10.032142857142857, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '586473', 'license': 'MIT', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1147, 'n_words': 195, 'path': 'Customer Churn Prediction(ML).ipynb', 'repo_name': 'ShrirakshaAcharya18/Insurance_ml', 'size': '3088'}