text_plain_output_1.png
from sklearn.compose import make_column_transformer

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error

from sklearn.pipeline import make_pipeline

from sklearn.pipeline import make_pipeline

from sklearn.preprocessing import LabelEncoder

from sklearn.preprocessing import OneHotEncoder,LabelEncoder, OrdinalEncoder

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

data = pd.read_csv('/kaggle/input/telco-customer-churn/WA_Fn-UseC_-Telco-Customer-Churn.csv')

data.columns

data.isnull()

data.shape

data.isnull().sum()

data.fillna(data.mean())

data.select_dtypes(include='int')

num_data = data.select_dtypes(include=['int', 'float'])

data.drop(columns=['Partner'])

data.drop(columns=['gender', 'tenure', 'TotalCharges'])

from sklearn.preprocessing import LabelEncoder
l = LabelEncoder()
data['MonthlyCharges'] = l.fit_transform(data['MonthlyCharges'])

y = data.Churn

le = LabelEncoder()
y = le.fit_transform(y)
y

x = data.drop(columns=['customerID', 'Churn', 'TotalCharges'])

x.columns

nom_cols = ['gender', 'InternetService', 'Contract', 'PaymentMethod']
ord_cols = ['Partner', 'Dependents', 'PhoneService', 'MultipleLines', 'OnlineSecurity', 'OnlineBackup', 'DeviceProtection', 'TechSupport', 'StreamingTV', 'StreamingMovies', 'PaperlessBilling']

trans_cols = make_column_transformer((OneHotEncoder(), nom_cols), (OrdinalEncoder(), ord_cols), remainder='passthrough')

trans_cols.fit_transform(x)

from sklearn.linear_model import LinearRegression
le = LinearRegression()
from sklearn.pipeline import make_pipeline
pipe = make_pipeline(trans_cols, le)
pipe.fit(x_train, y_train)

pipe.fit(x_train, y_train)
pred = pipe.predict(x_test)
from sklearn.metrics import mean_squared_error
mean_squared_error(pred, y_test)

from sklearn.neighbors import KNeighborsClassifiers
model = KNeighborsClassifier(n_neighbors=5)
from sklearn.pipeline import make_pipeline
pipe = make_pipeline(trans_cols, model)
pipe.fit(x_train, y_train)