This demo program shows how to use Tensorflow to predict if a customer will continue/discontinue its subscription.
The output:
The Code:
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 | import pandas as pd from sklearn.model_selection import train_test_split from tensorflow.keras.models import Sequential, load_model from tensorflow.keras.layers import Dense, Flatten from sklearn.metrics import accuracy_score from sklearn.metrics import ConfusionMatrixDisplay, classification_report, confusion_matrix from matplotlib import style style.use('classic') df = pd.read_csv('cont_subs.csv') X = pd.get_dummies(df.drop(['cont_subs', 'Customer ID'], axis=1)) y = df['cont_subs'].apply(lambda x: 1 if x=='Yes' else 0) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.15) X.columns y_train.head() model = Sequential() model.add(Dense(units=64, activation='relu', input_dim=len(X_train.columns))) model.add(Dense(units=128, activation='relu')) model.add(Dense(units=1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='sgd', metrics='accuracy') model.fit(X_train, y_train, epochs=512, batch_size=32) y_hat = model.predict(X_test) y_hat = [0 if val < 0.5 else 1 for val in y_hat] accuracy_score(y_test, y_hat) cm = confusion_matrix(y_test, y_hat) disp = ConfusionMatrixDisplay(confusion_matrix = cm) disp.plot() |
No comments:
Post a Comment