<jupyter_start><jupyter_text>Fill nan<jupyter_code># fill null values with the mean values of that feature
df["tenure"].fillna(df["tenure"].mean(), inplace=True)
# fill null values with the mode values of that feature is repeated more often than any other 
df["SeniorCitizen"].fillna(df["SeniorCitizen"].mode()[0], inplace=True)

df.isna().sum()
corr=df.corr()
plt.figure(figsize=(14,6))
sns.heatmap(corr,annot=True)<jupyter_output><empty_output><jupyter_text>Standardization<jupyter_code>from sklearn.preprocessing import StandardScaler

sc=StandardScaler()

df['tenure']=sc.fit_transform(df['tenure'].values.reshape(-1,1))
df['MonthlyCharges']=sc.fit_transform(df['MonthlyCharges'].values.reshape(-1,1))
df['TotalCharges']=sc.fit_transform(df['TotalCharges'].values.reshape(-1,1))<jupyter_output><empty_output><jupyter_text>Label encoding<jupyter_code>df['Partner'].dtype
if df['Partner'].dtype == 'O':
    print('yes')
else:
    print('no')
from sklearn.preprocessing import LabelEncoder

le = {}
le_name_mapping = {}

for i in df.columns:
    if df[i].dtype == 'O':
        le[i] = LabelEncoder()
        df[i] = le[i].fit_transform(df[i])
        le_name_mapping[i] = dict(zip(le[i].classes_, le[i].transform(le[i].classes_)))  
        print(i,":-",le_name_mapping[i])
df.head()
corr=df.corr()
plt.figure(figsize=(35,24))
sns.heatmap(corr,annot=True)
corr

X = df[['gender', 'SeniorCitizen', 'tenure', 'OnlineSecurity', 'Contract', 'PaperlessBilling', 'PaymentMethod', 'MonthlyCharges']]
y = df['Churn']
y.value_counts()<jupyter_output><empty_output><jupyter_text>* The data is biased to 0  under sampling<jupyter_code>from sklearn.linear_model import LogisticRegression
from imblearn.under_sampling import InstanceHardnessThreshold
iht = InstanceHardnessThreshold(random_state=0,
                                 estimator=LogisticRegression(
                                     solver='lbfgs', multi_class='auto'))
X_resampled, y_resampled = iht.fit_resample(X, y)
#print(sorted(Counter(y_resampled).items()))<jupyter_output><empty_output><jupyter_text>Splitting the dataset into the Training set and Test set<jupyter_code># Splitting the dataset into the Training set and Test set

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_resampled, y_resampled, test_size=0.33, random_state=42)
print(y_train.shape)
#y_train.value_counts()<jupyter_output>(2504,)<jupyter_text>* the ratio of label 0 = 73.696 % from y train<jupyter_code>print(y_test.shape)
#y_test.value_counts()<jupyter_output>(1234,)<jupyter_text>* the ratio of label 0 = 72.989 % from y test  Logistic Regression<jupyter_code># Importing the libraries
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Fitting Logistic Regression to the Training set
log_reg = LogisticRegression()
log_reg.fit(X_train,y_train)
# Predicting the Test set results
y_pred = log_reg.predict(X_test)

# accuracy
print('Accuracy for train= ',round(log_reg.score(X_train,y_train),4) *100, '%')

print('Accuracy for test = ',round(accuracy_score(y_test,y_pred),4) *100, '%')
# Making the Confusion Matrix will contain the correct and incorrect prediction on the dataset.
from sklearn.metrics import confusion_matrix

cm_log_reg = confusion_matrix(y_test, y_pred)
print(cm_log_reg)
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))<jupyter_output>Accuracy for train=  98.16 %
Accuracy for test =  98.06 %
[[637   0]
 [ 24 573]]
              precision    recall  f1-score   support

           0       0.96      1.00      0.98       637
           1       1.00      0.96      0.98       597

   micro avg       0.98      0.98      0.98      1234
   macro avg       0.98      0.98      0.98      1234
weighted avg       0.98      0.98      0.98      1234<jupyter_text>KNN<jupyter_code>from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train,y_train)
knn_pred=knn.predict(X_test)
# Accuracy
print('Accuracy for train = ',round(knn.score(X_train,y_train),4) *100, '%\n')
print('Accuracy for test = ',round(accuracy_score(y_test,knn_pred),4) *100, '%\n')

# Making the Confusion Matrix will contain the correct and incorrect prediction on the dataset.
from sklearn.metrics import confusion_matrix

cm_knn = confusion_matrix(y_test, knn_pred)
print('Confusion Matrix :- \n',cm_knn)

from sklearn.metrics import classification_report
print(classification_report(y_test, knn_pred))
import sklearn.metrics as metrics
score=[]
for k in range(1,100):
    knn=KNeighborsClassifier(n_neighbors=k,weights='uniform')
    knn.fit(X_train,y_train)
    predKNN=knn.predict(X_test)
    accuracy=metrics.accuracy_score(predKNN,y_test)
    score.append(accuracy*100)
    print ('k = ',k,'-> accuracy : ',accuracy)
print(score.index(max(score))+1,' : ',round(max(score),2),'%')
m=score.index(max(score))+1
knn = KNeighborsClassifier(n_neighbors=m)
knn.fit(X_train,y_train)
knn_pred=knn.predict(X_test)
# Accuracy
print('Accuracy for train = ',round(knn.score(X_train,y_train),4) *100, '%\n')

print('Accuracy for test = ',round(accuracy_score(y_test,knn_pred),4) *100, '%\n')


# Making the Confusion Matrix will contain the correct and incorrect prediction on the dataset.
from sklearn.metrics import confusion_matrix

cm_knn = confusion_matrix(y_test, knn_pred)
print('Confusion Matrix :- \n',cm_knn)
from sklearn.metrics import classification_report
print(classification_report(y_test, knn_pred))
train_accuracy=np.empty(len(range(1,100)))
test_accuracy=np.empty(len(range(1,100)))
for i, k in enumerate(range(1,100)):
    # Setup a k-NN Classifier with k neighbors: knn
    knn = KNeighborsClassifier(k)
    # Fit the classifier to the training data
    knn.fit(X_train, y_train)
    #Compute accuracy on the training set
    train_accuracy[i] = knn.score(X_train,y_train)
     #Compute accuracy on the testing set
    test_accuracy[i] = knn.score(X_test, y_test)
# Generate plot
plt.title('k-NN: Varying Number of Neighbors')
plt.plot(range(1,100), test_accuracy, label = 'Testing Accuracy')
plt.plot(range(1,100), train_accuracy, label = 'Training Accuracy')
plt.legend()
plt.xlabel('Number of Neighbors')
plt.ylabel('Accuracy')
plt.show()<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>from sklearn.svm import SVC
svm_rbf=SVC(kernel='rbf').fit(X_train,y_train)
svm_rbf_pred=svm_rbf.predict(X_test)
# Accuracy
print('Accuracy for train = ',round(svm_rbf.score(X_train, y_train),2) *100, '%\n')

print('Accuracy for test = ',round(accuracy_score(y_test,svm_rbf_pred),4) *100, '% \n')
# Making the Confusion Matrix will contain the correct and incorrect prediction on the dataset.
cm_svm_rbf = confusion_matrix(y_test, svm_rbf_pred)
print('Confusion Matrix :- \n',cm_svm_rbf)

from sklearn.metrics import classification_report
print(classification_report(y_test, svm_rbf_pred))
svm_linear=SVC(kernel='linear').fit(X_train,y_train)
svm_pred=svm_linear.predict(X_test)
# Accuracy
print('Accuracy for train = ',round(svm_linear.score(X_train, y_train),3) *100, '%\n')

print('Accuracy for test = ',round(accuracy_score(y_test,svm_pred),4) *100, '% \n')
# Making the Confusion Matrix will contain the correct and incorrect prediction on the dataset.
cm_svm_lin = confusion_matrix(y_test, svm_pred)
print('Confusion Matrix :- \n',cm_svm_lin)

from sklearn.metrics import classification_report
print(classification_report(y_test, svm_pred))
svm_poly=SVC(kernel='poly').fit(X_train,y_train)
svm_polr_pred=svm_poly.predict(X_test)
# Accuracy
print('Accuracy for train = ',round(svm_poly.score(X_train, y_train),3) *100, '%\n')

print('Accuracy for test = ',round(accuracy_score(y_test,svm_polr_pred),4) *100, '% \n')
# Making the Confusion Matrix will contain the correct and incorrect prediction on the dataset.
cm_svm_polr = confusion_matrix(y_test, svm_polr_pred)
print('Confusion Matrix :- \n',cm_svm_polr)

from sklearn.metrics import classification_report
print(classification_report(y_test, svm_polr_pred))<jupyter_output>/home/marie/anaconda3/lib/python3.6/site-packages/sklearn/svm/base.py:196: FutureWarning: The default value of gamma will change from 'auto' to 'scale' in version 0.22 to account better for unscaled features. Set gamma explicitly to 'auto' or 'scale' to avoid this warning.
  "avoid this warning.", FutureWarning)<jupyter_text>Naive bayes<jupyter_code>from sklearn.naive_bayes import GaussianNB
nb=GaussianNB().fit(X_train,y_train)
nb_pred=nb.predict(X_test)
# Accuracy
print('Accuracy for train = ',round(nb.score(X_train, y_train),3) *100, '%\n')

print('Accuracy for test = ',round(accuracy_score(y_test,nb_pred),4) *100, '%\n')
# Making the Confusion Matrix will contain the correct and incorrect prediction on the dataset.
cm_nb = confusion_matrix(y_test, nb_pred)
print('Confusion Matrix :- \n',cm_nb)

from sklearn.metrics import classification_report
print(classification_report(y_test, nb_pred))

# Fitting Decision Tree Classification to the Training set
from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
dt.fit(X_train, y_train)

# Predicting the Test set results
y_pred_dt = dt.predict(X_test)
# Accuracy
print('Accuracy for train = ',round(dt.score(X_train, y_train),3) *100, '%\n')
print('Accuracy for test = ',round(accuracy_score(y_test,y_pred_dt),3) *100, '%\n')

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm_dt = confusion_matrix(y_test, y_pred_dt)
print('Confusion Matrix :- \n',cm_dt)
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred_dt))<jupyter_output>Accuracy for train =  100.0 %

Accuracy for test =  96.7 %

Confusion Matrix :- 
 [[620  17]
 [ 24 573]]
              precision    recall  f1-score   support

           0       0.96      0.97      0.97       637
           1       0.97      0.96      0.97       597

   micro avg       0.97      0.97      0.97      1234
   macro avg       0.97      0.97      0.97      1234
weighted avg       0.97      0.97      0.97      1234<jupyter_text>RandomForest<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score,confusion_matrix
rfc=RandomForestClassifier(n_estimators=10,random_state=45,criterion='gini').fit(X_train,y_train)
# Predicting the Test set results
rfc_pred=rfc.predict(X_test)

# Accuracy
print('Accuracy for train = ',round(rfc.score(X_train, y_train),3) *100, '%\n')
print('Accuracy for test = ',round(accuracy_score(y_test,rfc_pred),3) *100, '%\n')

# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm_dt = confusion_matrix(y_test, rfc_pred)
print('Confusion Matrix :- \n',cm_dt)
from sklearn.metrics import classification_report
print(classification_report(y_test, rfc_pred))
# pickle.dump(rfc,open("rfc.pkl","wb"))
# my_rfc=pickle.load(open("rfc.pkl","rb"))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup