<jupyter_start><jupyter_text>Joining Datasets<jupyter_code>merge_1 = pd.merge(df_tlco_churn, df_demograph, on='Customer ID')
merge_2 = pd.merge(merge_1, df_services, on='Customer ID')
merge_3 = pd.merge(merge_2, df_status, on='Customer ID')
df_telco = merge_3
df_telco.head()
columns_to_be_dropped = ['Count_x', 'Count_y', 'Gender_y','Senior Citizen_y', 'Dependents_y','Count_x','Phone Service_y',
       'Multiple Lines_y','Internet Service_y','Online Security_y', 'Online Backup_y', 'Streaming TV_y', 'Streaming Movies_y',
       'Contract_y','Paperless Billing_y', 'Payment Method_y','Total Charges_y','Count_y', 'Quarter_y','Churn Label_y',
       'Churn Value_y', 'Churn Score_y', 'CLTV_y','Churn Reason_y', 'Tenure in Months', 'Monthly Charge', 'Quarter_x',
        'Total Long Distance Charges', 'Total Charges_x', 'Churn Label_x','Customer Status', 'Churn Category', 'Churn Reason_x' ]
df_telco = df_telco.drop(labels = columns_to_be_dropped, axis=1)
df_telco.columns
df_telco = df_telco.rename(columns={'Gender_x':'Gender', 'Senior Citizen_x':'Senior Citizen',
        'Phone Service_x':'Phone Service', 'Multiple Lines_x':'Multiple Lines',
        'Dependents_x':'Dependents',
       'Internet Service_x':'Internet Service', 'Online Security_x':'Online Security', 'Online Backup_x':'Online Backup',
       'Streaming TV_x':'Streaming TV',
       'Streaming Movies_x':'Streaming Movies', 'Contract_x':'Contract', 'Paperless Billing_x':'Paperless Billing',
       'Payment Method_x':'Payment Method','Churn Value_x':'Churn Value', 'Churn Score_x':'Churn Score',
       'CLTV_x':'CLTV'})
df_selected = df_telco[['State', 'City', 'Zip Code',
       'Latitude', 'Longitude', 'Gender', 'Senior Citizen', 'Partner',
        'Phone Service', 'Multiple Lines',
        'Age', 'Under 30', 'Married', 'Number of Dependents', 'Dependents',
        'Referred a Friend', 'Number of Referrals', 'Offer',
       'Internet Service', 'Online Security', 'Online Backup',
       'Device Protection', 'Tech Support', 'Streaming TV',
       'Streaming Movies', 'Contract', 'Paperless Billing',
       'Payment Method','Churn Value', 'Churn Score',
       'Avg Monthly Long Distance Charges', 'Internet Type',
       'Avg Monthly GB Download', 'Device Protection Plan',
       'Premium Tech Support', 'Streaming Music', 'Unlimited Data','Monthly Charges',
       'Total Refunds', 'Total Extra Data Charges', 'Total Revenue','Tenure Months', 'CLTV',
       'Satisfaction Score']]
df_selected.dtypes
df_selected['Zip Code'] = df_selected['Zip Code'].astype('object')
df_selected.dtypes
df_object = df_selected.select_dtypes(include='object')
df_object = df_object.drop(labels=['State', 'City', 'Zip Code'], axis=1)

for i in df_object.columns:
    print('***************************************************************************************')
    print(i)
    etiquetas = df_object[i].unique()
    sectores = list(df_object[i].value_counts())
    plt.pie(sectores, labels=etiquetas, colors=['green', "red", 'blue', 'orange', 'white', 'pink', 'yellow'],autopct='%1.1f%%',startangle=90)
    plt.axis('equal')
    plt.title('Percentage of %s' %i)
    plt.show()
    x = df_object[i].value_counts()
    print('Number of values:')
    print(x)<jupyter_output>***************************************************************************************
Gender<jupyter_text>Nalisha Questions for the Presentation<jupyter_code>df_telco.columns<jupyter_output><empty_output><jupyter_text>The best costumers for Churn Score<jupyter_code>df_telco["Churn Score"].min()
df_telco[df_telco["Churn Score"]==5]<jupyter_output><empty_output><jupyter_text>The best costumers for Total Revenue<jupyter_code>df_telco["Total Revenue"].max()
df_telco[df_telco["Total Revenue"]==11979.34]<jupyter_output><empty_output><jupyter_text>The best costumers for CLTV<jupyter_code>df_telco["CLTV"].max()
df_telco[df_telco["CLTV"]==6500]
df_telco.columns<jupyter_output><empty_output><jupyter_text>Total extra data charges per internet service per contract type<jupyter_code>df_telco.groupby(['Contract','Internet Service'])['Total Extra Data Charges'].sum()
df_telco.groupby(['Internet Service', 'Contract'])['Total Extra Data Charges'].sum().unstack().plot(kind="bar", figsize=(10,10)).legend(loc='center left', bbox_to_anchor=(1, 0.5))<jupyter_output><empty_output><jupyter_text>Total revenue per age per contract type<jupyter_code>df_telco.groupby(['Age','Contract'])['Total Revenue'].sum()
df_telco.groupby(['Age','Contract'])['Total Revenue'].sum().unstack().plot(kind="area", figsize=(10,10)).legend(loc='center left', bbox_to_anchor=(1, 0.5))<jupyter_output><empty_output><jupyter_text>Satisfaction Score per contract type<jupyter_code>df_telco.groupby(['Contract'])['Satisfaction Score'].mean()
df_telco.groupby(['Contract'])['Satisfaction Score'].mean().plot(kind="barh", figsize=(10,10)).legend(loc='center left', bbox_to_anchor=(1, 0.5))<jupyter_output><empty_output><jupyter_text>Model Preparation<jupyter_code>df_selected.dtypes
df_selected.to_csv('telco_cltv.csv',sep=';',header=True, index=True, decimal=',')
df_selected = df_selected.drop(labels=['Zip Code','Latitude','Longitude'], axis=1)
numerical_features = df_selected.select_dtypes(include=['float64', 'int64']).columns
categorical_features = df_selected.select_dtypes(include=['object']).columns
numerical_features
df_model=pd.concat([df_selected[numerical_features], df_selected[categorical_features]], axis=1)
# from sklearn.preprocessing import StandardScaler

# features = df_model[numerical_features]
# scaler = RobustScaler().fit(features.values)

# features = scaler.transform(features.values)

# df_model[numerical_features] = features
# df_model.head()
for i in df_model.columns:
    print('*********************************')
    print(i)
    x = df_model[i].value_counts()
    print(x)<jupyter_output>*********************************
Age
42    156
47    153
40    150
44    148
23    146
     ... 
80     66
78     63
66     63
67     61
72     58
Name: Age, Length: 62, dtype: int64
*********************************
Number of Dependents
0    5416
1     553
2     531
3     517
5      10
4       9
6       3
7       2
8       1
9       1
Name: Number of Dependents, dtype: int64
*********************************
Number of Referrals
0     3821
1     1086
5      264
3      255
7      248
9      238
4      236
2      236
10     223
6      221
8      213
11       2
Name: Number of Referrals, dtype: int64
*********************************
Churn Value
0    5174
1    1869
Name: Churn Value, dtype: int64
*********************************
Churn Score
80    151
71    148
77    145
67    143
76    141
     ... 
88     33
9       3
7       2
8       2
5       1
Name: Churn Score, Length: 85, dtype: int64
*********************************
Avg Monthly Long Distance Charges
0.00     682
18.26      7
42[...]<jupyter_text>PCA<jupyter_code>df_cat = df_model[categorical_features]
df_cat.head(3)
df_cat.columns
df_cat = df_cat.drop(labels=['State','City', 'Payment Method', 'Paperless Billing', 'Internet Service', 'Offer'], axis=1)
df_cat = pd.get_dummies(df_cat, columns = df_cat.columns, drop_first=True)
df_cat.head()
#from sklearn.decomposition import PCA

#pca_cat = PCA (n_components=20)
#p_comp_transformed_cat = pca_cat.fit_transform(df_cat)
#print(p_comp_transformed_cat)
#variance_ratio = pca_cat.explained_variance_ratio_

#print(variance_ratio)
#pca_cat.explained_variance_ratio_.cumsum()
#pca_columns = []

#for i in range(len(variance_ratio)):
    #x = 'pca_'+ str(i+1)
    #pca_columns.append(x)
#principalDf_cat = pd.DataFrame(data = p_comp_transformed_cat, columns = pca_columns)
#df_model = pd.concat([df_model[numerical_features], principalDf_cat], axis = 1)
df_model = pd.concat([df_model[numerical_features], df_cat], axis = 1)
df_model.head()
df_model.columns
prueba = df_model
prueba = prueba.drop(labels=['Satisfaction Score', 'Total Revenue', 'Tenure Months', 'Churn Value', 'Number of Referrals',
                            'Total Extra Data Charges', 'Avg Monthly GB Download','Online Security_No internet service',
                             'Online Backup_No internet service','Device Protection_No internet service',
                             'Streaming TV_No internet service', 'Tech Support_No internet service', 
                             'Streaming Movies_No internet service','Dependents_Yes', 'Monthly Charges',  'Churn Score',
                             'Avg Monthly Long Distance Charges','Under 30_Yes', 'Married_Yes','Senior Citizen_Yes'], axis=1)
prueba.columns
#prueba = pd.concat([jan, prueba[['pca_1', 'pca_2', 'pca_3', 'pca_4', 'pca_5', 'pca_6', 'pca_7',
       #'pca_8', 'pca_9', 'pca_10', 'pca_11', 'pca_12', 'pca_13', 'pca_14',
      # 'pca_15', 'pca_16', 'pca_17', 'pca_18', 'pca_19', 'pca_20']]], axis=1)
sns.heatmap(prueba.corr())
prueba.corr()
prueba.columns
train = prueba[['Age', 'Number of Dependents', 'Total Refunds', 
        'Gender_Male', 'Partner_Yes',
       'Phone Service_Yes', 'Multiple Lines_No phone service',
       'Multiple Lines_Yes','Referred a Friend_Yes', 'Online Security_Yes', 'Online Backup_Yes',
       'Device Protection_Yes', 'Tech Support_Yes', 'Streaming TV_Yes',
       'Streaming Movies_Yes', 'Contract_One year', 'Contract_Two year',
       'Internet Type_DSL', 'Internet Type_Fiber Optic', 'Internet Type_None',
       'Device Protection Plan_Yes', 'Premium Tech Support_Yes',
       'Streaming Music_Yes', 'Unlimited Data_Yes']]<jupyter_output><empty_output><jupyter_text>Runing the Linear Regression Model<jupyter_code>from sklearn.model_selection import train_test_split

X = prueba.drop("CLTV", axis = 1)
variables = X.columns
X = X.values

y = prueba["CLTV"]

X_train, X_test, y_train, y_test = train_test_split(
    X, y, random_state=20, test_size=0.25)
from sklearn.linear_model import LinearRegression
from sklearn.metrics import classification_report, confusion_matrix

lr = LinearRegression()
model=lr.fit(X,y)
# print the intercept
print("Constant: ", model.intercept_)
print("R2: ", model.score(X,y))
predictions = lr.predict(X)
plt.scatter(y,predictions)
sns.distplot((y-predictions),bins=100);
from sklearn import metrics

print('MAE:', metrics.mean_absolute_error(y, predictions))
print('MSE:', metrics.mean_squared_error(y, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y, predictions)))
X = sm.add_constant(X)

result = sm.OLS( y, X).fit()
print (result.summary())<jupyter_output>OLS Regression Results                            
==============================================================================
Dep. Variable:                   CLTV   R-squared:                       0.097
Model:                            OLS   Adj. R-squared:                  0.094
Method:                 Least Squares   F-statistic:                     35.71
Date:                Sun, 13 Feb 2022   Prob (F-statistic):          1.89e-137
Time:                        20:04:18   Log-Likelihood:                -59471.
No. Observations:                7043   AIC:                         1.190e+05
Df Residuals:                    7021   BIC:                         1.191e+05
Df Model:                          21                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|[...]<jupyter_text>Runing Random Forest Regressor<jupyter_code>from sklearn.ensemble import RandomForestRegressor

rf = RandomForestRegressor(n_estimators = 10, random_state = 0)
random_forest =rf.fit(X_train,y_train)
random_forest.score(X_train,y_train)
y_predict = random_forest.predict(X_test)
y_predict
random_forest.feature_importances_
importance = random_forest.feature_importances_

# summarize feature importance
for i,v in enumerate(importance):
    print('Feature: %0d, Score: %.5f' % (i,v))
    
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()
def plot_feature_importance(importance,names,model_type):
    #Create arrays from feature importance and feature names
    feature_importance = np.array(importance)
    feature_names = np.array(names)

    #Create a DataFrame using a Dictionary
    data={'feature_names':feature_names,'feature_importance':feature_importance}
    fi_df = pd.DataFrame(data)

    #Sort the DataFrame in order decreasing feature importance
    fi_df.sort_values(by=['feature_importance'], ascending=False,inplace=True)

    #Define size of bar plot
    plt.figure(figsize=(10,8))
    #Plot Searborn bar chart
    sns.barplot(x=fi_df['feature_importance'], y=fi_df['feature_names'],color='#9976ff')
    #Add chart labels
    plt.title(model_type + ' FEATURE IMPORTANCE')
    plt.xlabel('FEATURE IMPORTANCE')
    plt.ylabel('FEATURE NAMES')
plot_feature_importance(random_forest.feature_importances_,train.columns,'RANDOM FOREST')
random_forest.set_params()
prueba.columns<jupyter_output><empty_output><jupyter_text>Runing Ridge Regression<jupyter_code>from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold
from sklearn.linear_model import Ridge
from numpy import absolute
from numpy import mean
from numpy import std

# define model
rg = Ridge(alpha=1.0)
# define model evaluation method
cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
# evaluate model
scores = cross_val_score(rg, X, y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1)
# force scores to be positive
scores = absolute(scores)
print('Mean MAE: %.3f (%.3f)' % (mean(scores), std(scores)))
rg.fit(X_train, y_train)
rg.score(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Runing Lasso Regression<jupyter_code>from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedKFold
from sklearn.linear_model import Lasso
from numpy import absolute
from numpy import mean
from numpy import std

# define model
lasso = Lasso(alpha=1.0)
# define model evaluation method
cv = RepeatedKFold(n_splits=10, n_repeats=3, random_state=1)
# evaluate model
scores = cross_val_score(lasso, X, y, scoring='neg_mean_absolute_error', cv=cv, n_jobs=-1)
# force scores to be positive
scores = absolute(scores)
print('Mean MAE: %.3f (%.3f)' % (mean(scores), std(scores)))
lasso.fit(X_train, y_train)
lasso.score(X_train, y_train)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16371834894207424, 'fraction_numerical': 0.027124523066250433, 'mean_word_length': 4.644479248238058, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '121321', 'license': 'MIT', 'n_tokens_mistral': 5124, 'n_tokens_neox': 4691, 'n_words': 1038, 'path': 'GroupE_Assignment_MarketingIntelligence.ipynb', 'repo_name': 'JP-Thoma/IEMarketingIntelligence', 'size': '14415'}