-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntro_DS_Project_1.py
286 lines (134 loc) · 3.93 KB
/
Intro_DS_Project_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#Data Processing Libraries
import numpy as np
import pandas as pd
#Data Visualization libraries
import matplotlib.pyplot as plt
import seaborn as sns
import sweetviz as sv
#to display inline
get_ipython().run_line_magic('matplotlib', 'inline')
#to split dataset into train and test data set
from sklearn.model_selection import train_test_split
# In[2]:
#read file from location and loading it for processing
df= pd.read_csv('C:/Users/LENOVO/Downloads/telco-customer-churn.csv')
# In[3]:
#loading top 5 and bottom 5 rows for analysis
df #top 5 rows and bottom 5 rows
# In[4]:
#Basic understanding of data set
df.shape #output = (rows,columns)
# In[5]:
df.info()
# In[6]:
#Customer id is unique and has no significance on target label hence dropping customer id
df.drop('customerID',axis='columns',inplace=True)
# In[7]:
df.info()
# In[8]:
#To change datatype of TotalCharges from object to numeric one
df['TotalCharges'] = pd.to_numeric(df['TotalCharges'],errors='coerce')
# In[9]:
df.info()
# In[10]:
#To check is there are any null values
print(df.isnull().any())
# In[11]:
print('# No of null values in TotalCharges:-',df["TotalCharges"].isnull().sum())
# In[12]:
df[pd.to_numeric(df.TotalCharges,errors='coerce').isnull()]
# In[13]:
#Dropping NAN values record as their tenure is also 0
df= df.dropna()
# In[14]:
df.info()
# In[15]:
df.shape
# In[16]:
df.dtypes
# # In given dataset now we have 16 categorical columns whaeras 4 numerical columns
# In[17]:
#to get values of categorical columns
def print_col_values(df):
for column in df:
if df[column].dtypes=='object':
print(f'{column}: {df[column].unique()}')
# In[18]:
print_col_values(df)
# In[19]:
df.replace('No internet service','No',inplace=True)
df.replace('No phone service','No',inplace=True)
# In[20]:
print_col_values(df)
# In[21]:
binary_Columns=['SeniorCitizen','Partner','PaperlessBilling','PhoneService','MultipleLines','OnlineSecurity','OnlineSecurity',
'OnlineBackup','Dependents','DeviceProtection','TechSupport','StreamingTV','StreamingMovies','StreamingMovies','Churn']
for col in binary_Columns:
df[col].replace({'Yes':1,'No':0},inplace=True)
# In[22]:
df['gender'].replace({'Female':1, 'Male':0})
# In[23]:
df.gender.unique()
# In[24]:
for col in df:
print(f'{col}:{df[col].unique()}')
# In[25]:
df.dtypes
# In[26]:
InternetService = {
'DSL' : 11,
'Fiber optic' : 22,
'No' : 33
}
Contract={
'Month-to-month' : 44,
'One year' :55,
'Two year':66
}
PaymentMethod={
'Electronic check':111,
'Mailed check':222,
'Bank transfer (automatic)':333,
'Credit card (automatic)':444
}
Internet_service = df['InternetService'].map(InternetService)
Contract_data = df['Contract'].map(Contract)
Payment_Method= df['PaymentMethod'].map(PaymentMethod)
# In[27]:
new_data = df.copy()
new_data['InternetService'] = Internet_service
new_data['Contract'] = Contract_data
new_data['PaymentMethod'] = Payment_Method
print(new_data)
# In[28]:
new_data.dtypes
# In[29]:
new_data['gender'].replace({'Female':1, 'Male':0},inplace=True)
# In[30]:
new_data.gender.unique()
# In[31]:
#converted all data to numeric datatype
new_data.dtypes
# In[32]:
new_data.describe().T
# In[33]:
plt.figure(figsize = (20,40))
sns.boxplot(x="value",y="variable",data=pd.melt(new_data))
plt.show()
# In[44]:
#Getting the list of all columns
columns_list=list(binary_Columns)
#converting columns to display into matrix format
columns_array = np.array(columns_list)
columns_array = np.reshape(columns_array,(5,3))
# In[45]:
rows = 5; columns = 3
f,axes=plt.subplots(rows,columns,figsize=(30,30))
print("Analysis of each feature with Bar chart")
for row in range(rows):
for column in range(columns):
sns.countplot(df[columns_array[row][column]],palette="Set1",ax=axes[row,column])
# In[ ]: