<file_sep># streamlit_apps_ml
Created some simple streamlit ML apps
<file_sep>import numpy as np
import streamlit as st
import matplotlib.pyplot as plt
from datetime import date

import yfinance as yf
from fbprophet import Prophet
from fbprophet.plot import plot_plotly
from plotly import graph_objs as go

# We using facebook prophet here from prediction


START = "2015-01-01"
TODAY = date.today().strftime("%Y-%m-%d")

stocks = ("AAPL", "GOOG", "MSFT", "GME")
selected_stock = st.selectbox("Select dataset for prediction", stocks)

n_years = st.slider("Years of predeiction", 1, 4)
period = n_years * 365

@st.cache # will cash the downloaded data so it wont download again and again
def load_data(ticker):
    data = yf.download(ticker, START, TODAY) # return data in dataframe format
    data.reset_index(inplace = True) # replace index with date as index

    return data

data_load_state = st.text("Load data...")
data = load_data(selected_stock)
data_load_state.text("Loading data... done!")

st.subheader("Raw data")
st.write(data.tail())

def plot_raw_data():
    fig = go.Figure()
    fig.add_trace(go.Scatter(x = data['Date'], y = data['Open'], name = 'stock_open'))
    fig.add_trace(go.Scatter(x = data['Date'], y = data['Close'], name = 'stock_close'))
    fig.layout.update(title_text = "Time Series Data", xaxis_rangeslider_visible=True)
    st.plotly_chart(fig)

plot_raw_data()

# Forcasting
df_train = data[['Date', 'Close']]
df_train = df_train.rename(columns = {"Date":"ds", "Close":"y"}) # fbprophen requires this format so we rename the df

model = Prophet()
model.fit(df_train)
future = model.make_future_dataframe(periods = period)
forcast = model.predict(future)

st.subheader("Forcasted data")
st.write(forcast.tail())

st.write('Forcast data')
forcast_fig = plot_plotly(model, forcast)
st.plotly_chart(forcast_fig)

st.write('Forcast components')
fig2 = model.plot_components(forcast)
st.write(fig2)






#
<file_sep>import numpy as np
import streamlit as st
from sklearn import datasets
import matplotlib.pyplot as plt

from sklearn.svm import SVC
from sklearn.decomposition import PCA               # principal componant analysis algo
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split


st.title("Streamlit learning")
st.write(""" ## Explore diffirent classifier """)

# selectbox
dataset_name = st.sidebar.selectbox("Select Dataset", ("Iris", "Breast Cancer", "Wine"))
classifier_name = st.sidebar.selectbox("Select Classifier", ("KNN", "SVM", "Random Forest"))

def get_dataset(dataset_name):

    if dataset_name == "Iris":
        data = datasets.load_iris()
    elif dataset_name == "Breast Cancer":
        data = datasets.load_breast_cancer()
    else:
        data = datasets.load_wine()

    X = data.data
    y = data.target

    return X, y

def add_parameter_ui(clf_name):
    params = dict()

    if clf_name == "KNN":
        K = st.sidebar.slider("K", 1, 15)
        params["K"] = K
    elif clf_name == "SVM":
        C = st.sidebar.slider("C", 0.01, 10.0)
        params["C"] = C
    else:
        max_depth = st.sidebar.slider("max_depth", 2, 15)
        n_estimators = st.sidebar.slider("n_estimators", 1, 100)

        params["max_depth"] = max_depth
        params["n_estimators"] = n_estimators

    return params

def  get_classifier(clf_name, params):

    if clf_name == "KNN":
        clf = KNeighborsClassifier(n_neighbors = params['K'])
    elif clf_name == "SVM":
        clf = SVC(C = params['C'])
    else:
        clf = RandomForestClassifier(n_estimators = params['n_estimators'],
                                    max_depth = params['max_depth'],
                                    random_state = 1234
        )

    return clf

X, y = get_dataset(dataset_name)

params = add_parameter_ui(classifier_name)
clf = get_classifier(classifier_name, params)

st.write("Shape of the dataset: ", X.shape)
st.write("Number of classes: ", len(np.unique(y)))

show_dataset = st.checkbox("Show dataset (Type: {})".format(type(X)))
if show_dataset:
    st.write("{} dataset: ".format(dataset_name), X)
    st.write("Labels: ", y)

# Classification

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 1234)

clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)

acc = accuracy_score(y_test, y_pred)
st.write(f"Classifier = {classifier_name}")
st.write(f"Accuracy = {acc}")

# PLOT datasets

pca = PCA(2) # 2 = no. of dimantion (2D), PCA converts features to lowar dimantional space
X_projected = pca.fit_transform(X)

x1 = X_projected[:, 0]
x2 = X_projected[:, 1]

fig = plt.figure()
plt.scatter(x1, x2, c = y, alpha = 0.8, cmap = "viridis")
plt.xlabel("Principal componet 1")
plt.ylabel("Principal componet 2")
plt.colorbar()

st.pyplot(fig)



# TODO:
## add more parameters (sklearn)
## add other classifiers
## add feature scaling
