# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
from pathlib import Path
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler,OneHotEncoder

# %matplotlib inline
from IPython.display import Image, SVG
import matplotlib.pyplot as plt
np.random.seed(0)
# Filepaths, numpy, and Tensorflow
import os
import tensorflow as tf
# Sklearn scaling
from sklearn.preprocessing import MinMaxScaler
# Keras
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Dense
from tensorflow.keras.datasets import mnist

np.random.seed(1)
SAMPLE_SIZE=1000

# +
df = pd.DataFrame({
                    
"Gender" : np.random.choice(["Male", "Female", "Other"], 
                                                SAMPLE_SIZE, p=[.35,.45,.2]),
"Age": np.random.choice(["18-39", "40-59", "60+"],
                                                SAMPLE_SIZE, p=[.4,.31,.29]),
"Income": np.random.choice(["20,000-49,000", "50,000-69,999", "70,000-89,999", "90,000-109,999", "110,000-129,999", "130,000+"],
                                                SAMPLE_SIZE, p=[.19,.17,.14,.15,.15,.2]),
"Holding Period": np.random.choice(["One Day", "Swing Trader", "Position Trader", "Scalp Trader", "Intraday Trader"], 
                                                   SAMPLE_SIZE, p=[.3,.15,.23,.17,.15]),
"Debt Amount": np.random.choice(["$10,000-49,999", "$50,000-99,999", "100,000-149,999", "$150,000+"],
                                                SAMPLE_SIZE, p=[.15,.23,.27,.35]),
"First Time Investing": np.random.choice(["Yes", "No", "Not sure"],
                                                SAMPLE_SIZE, p=[.45,.35,.2]),
"Credit Score": np.random.choice(["300-629", "630-689", "690-719", "720-850"],
                                                SAMPLE_SIZE, p=[.175,.255,.3,.27]),
"Education Level": np.random.choice(["High School", "Bachelors", "Masters", "PhD"],
                                                SAMPLE_SIZE, p=[.2,.35,.25,.2]),
"Investment Amount": np.random.choice(["$1,000-79,999", "$80,000-149,999", "150,000+"],
                                                SAMPLE_SIZE, p=[.395,.399,.206]),
"Crypto Output": np.random.choice(["0","1","2"], 
                                                SAMPLE_SIZE, p=[0.4,0.18,.42]),
                  })
# -

df.to_csv("crypto_customers.csv")

# +
crypto_df = pd.read_csv(
    Path("./crypto_customers.csv")
)

display(crypto_df.head())
display(crypto_df.tail())
# -



crypto_df = crypto_df.drop(columns=["Unnamed: 0"])

crypto_df = crypto_df.dropna()

crypto_df.dtypes

# +
categorical_variable = list(crypto_df.dtypes[crypto_df.dtypes == "object"].index)

categorical_variable
# -

enc = OneHotEncoder(sparse=False)

crypto_df[categorical_variable]

encoded_data = enc.fit_transform(crypto_df[categorical_variable])

# +
encoded_df = pd.DataFrame(
    encoded_data,
    columns = enc.get_feature_names(categorical_variable)
)

display(encoded_df.head())

# +
encoded_df = pd.concat([
    crypto_df.drop(columns=categorical_variable),
    encoded_df,
], axis=1)

encoded_df.head()

# +
y = encoded_df["Crypto Output"]

y[:5]

# +
X = encoded_df.drop(columns=["Crypto Output"])

display(X.head())
display(X.tail())
# -

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# Next, we normalize our training data to be between 0 and 1
scaler = MinMaxScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

X_train.shape, y_train.shape

y_train[:20]

# We need to convert our target labels (expected values) to categorical data
num_classes = 10
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)
# Original label of `5` is one-hot encoded as `0000010000`
y_train[0]

y_train.shape

model=Sequential()

X_train.shape[1]

# We can also choose our activation function. `relu` is a common
model.add(Dense(input_dim=X_train.shape[1], units=30, activation='relu'))

# Add a second hidden layer
model.add(Dense(units=20, activation='relu'))

# Add our final output layer where the number of nodes
# corresponds to the number of y labels
model.add(Dense(units=num_classes, activation='softmax'))

# We can summarize our model
model.summary()

model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

y_train.shape

X_train.shape

fit_model = model.fit(X_train, y_train, epochs=100)

# Evaluate the model using the training data
model_loss, model_accuracy = model.evaluate(X_test, y_test, verbose=2)
print(f"Loss: {model_loss}, Accuracy: {model_accuracy}")

# +
# Create a DataFrame using the model history and an index parameter
model_plot = pd.DataFrame(fit_model.history, index=range(1, len(fit_model.history["loss"]) + 1))

# Vizualize the model plot where the y-axis displays the loss metric
model_plot.plot(y="loss")

# Vizualize the model plot where the y-axis displays the accuracy metric
model_plot.plot(y="accuracy")
# -



StarcoderdataJupyterScriptsDedupFiltered