-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprep.py
More file actions
54 lines (31 loc) · 1.35 KB
/
prep.py
File metadata and controls
54 lines (31 loc) · 1.35 KB
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
#Import libraries and functions.
import pandas as pd
from sklearn.model_selection import train_test_split
from acquire import get_telco_data
#function to drop columns in preparation phase.
def drop_cols(df):
df = df.drop(columns = ['customer_id', 'internet_service_type_id', 'contract_type_id', 'payment_type_id', 'churn_month',
'signup_date'])
return df
#function to cast object type to float.
def change_dtype(df):
df.total_charges = df.total_charges.replace(' ', 0)
df.total_charges = df.total_charges.astype(float)
return df
#function to split data into train, val, test
def train_val_test(df, strat, seed = 42):
train, val_test = train_test_split(df, train_size = 0.7,
random_state = seed,
stratify = df[strat])
val, test = train_test_split(val_test, train_size = 0.5,
random_state = seed,
stratify = val_test[strat])
return train, val, test
return df
#this function combines other functions for data preparation
def telco_pipeline():
df = get_telco_data()
df = drop_cols(df)
df = change_dtype(df)
train, val, test = train_val_test(df, 'churn')
return train, val, test