# ---
# 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 numpy as np
import pandas as pd

data = pd.read_excel('data_v7.xlsx')

data.head()

len(data)

len(data[data.isnull().any(axis=1)])

data.drop(['Unnamed: 0','locations_month','label','date'],axis=1,inplace=True)

data.drop(['pressureInches','visibility'],axis=1,inplace=True)

data.head()

loc_lst = list(data['locations'].unique())

i = 0
for haha in loc_lst:
    
    data.loc[data.locations == haha, 'locations'] = i
    i+=1

data.head()

data.tail()



data.corr()

from sklearn.decomposition import PCA
from sklearn.preprocessing import MinMaxScaler

import matplotlib.pyplot as plt

scaler = MinMaxScaler(feature_range=[0, 1])
data_rescaled = scaler.fit_transform(data)

pca = PCA().fit(data_rescaled)
plt.figure(figsize=(20,10))
plt.plot(np.cumsum(pca.explained_variance_ratio_))
plt.xlabel('Number of Components')
plt.ylabel('Variance (%)') #for each component
plt.title('Dengue Dataset No. of Components vs Variance')
plt.show()

np.cumsum(pca.explained_variance_ratio_)

# 8 features

pca = PCA(n_components=8)
data_pca = pca.fit_transform(data_rescaled)

data_pca.shape

new_df = pd.DataFrame(data_pca)

new_df.corr()

labels = pd.read_excel('data_v7.xlsx',usecols=['label'])

labels_pca = np.array(labels)

labels_pca.shape

data_pca.shape

X = data_pca.copy()

y = labels_pca.copy()

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
# -

from sklearn.ensemble import RandomForestClassifier

rfc = RandomForestClassifier()

rfc.fit(X_train,y_train)

rfc_pred = rfc.predict(X_test)

from sklearn.metrics import classification_report

print(classification_report(y_test,rfc_pred))

rfc.feature_importances_

from sklearn.svm import SVC

svc = SVC()

svc.fit(X_train,y_train)

svc_pred = svc.predict(X_test)

print(classification_report(y_test,svc_pred))

from sklearn.linear_model import LogisticRegression

lr = LogisticRegression()

lr.fit(X_train,y_train)

lr_pred = lr.predict(X_test)

print(classification_report(lr_pred,y_test))







from sklearn.model_selection import GridSearchCV

# +
penalty = ['l1', 'l2']
C = [0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000]
class_weight = [{1:0.5, 0:0.5}, {1:0.4, 0:0.6}, {1:0.6, 0:0.4}, {1:0.7, 0:0.3}]
solver = ['liblinear', 'saga']

param_grid = dict(penalty=penalty,
                  C=C,
                  class_weight=class_weight,
                  solver=solver)

grid = GridSearchCV(estimator=lr,
                    param_grid=param_grid,
                    scoring='roc_auc',
                    verbose=1,
                    n_jobs=-1)
grid_result = grid.fit(X_train, y_train)

print('Best Score: ', grid_result.best_score_)
print('Best Params: ', grid_result.best_params_)
# -

from sklearn.model_selection import RandomizedSearchCV

from sklearn.pipeline import Pipeline



pipe = Pipeline([("classifier", RandomForestClassifier())])
# Create dictionary with candidate learning algorithms and their hyperparameters
search_space = [
                {"classifier": [LogisticRegression()],
                 "classifier__penalty": ['l2','l1'],
                 "classifier__C": np.logspace(0, 4, 10)
                 },
                {"classifier": [LogisticRegression()],
                 "classifier__penalty": ['l2'],
                 "classifier__C": np.logspace(0, 4, 10),
                 "classifier__solver":['newton-cg','saga','sag','liblinear'] ##This solvers don't allow L1 penalty
                 },
                {"classifier": [RandomForestClassifier()],
                 "classifier__n_estimators": [10, 100, 1000],
                 "classifier__max_depth":[5,8,15,25,30,None],
                 "classifier__min_samples_leaf":[1,2,5,10,15,100],
                 "classifier__max_leaf_nodes": [2, 5,10]}]
# create a gridsearch of the pipeline, the fit the best model
gridsearch = GridSearchCV(pipe, search_space, cv=5, verbose=0,n_jobs=-1) # Fit grid search
best_model = gridsearch.fit(X_train, y_train)

best_model.best_params_

temp_lr = LogisticRegression(C=2.7825594022071245, class_weight=None, dual=False,
                    fit_intercept=True, intercept_scaling=1, l1_ratio=None,
                    max_iter=100, multi_class='warn', n_jobs=None, penalty='l1',
                    random_state=None, solver='warn', tol=0.0001, verbose=0,
                    warm_start=False)

temp_lr.fit(X_train,y_train)

print(classification_report(temp_lr.predict(X_test),y_test))


def svc_param_selection(X, y, nfolds):
    Cs = [0.001, 0.01, 0.1, 1, 10]
    gammas = [0.001, 0.01, 0.1, 1]
    param_grid = {'C': Cs, 'gamma' : gammas}
    grid_search = GridSearchCV(SVC(kernel='rbf'), param_grid, cv=nfolds)
    grid_search.fit(X, y)
    grid_search.best_params_
    return grid_search.best_params_


svc_params = svc_param_selection(X_train,y_train,3)

temp_svc = SVC(gamma=10).fit(X_train,y_train).predict(X_test)

print(classification_report(temp_svc,y_test))

param_grid = { 
    'n_estimators': [200, 500],
    'max_features': ['auto', 'sqrt', 'log2'],
    'max_depth' : [4,5,6,7,8],
    'criterion' :['gini', 'entropy']
}

CV_rfc = GridSearchCV(estimator=rfc, param_grid=param_grid, cv= 5)
CV_rfc.fit(X_train, y_train)

CV_rfc.best_params_

temp_rfc=RandomForestClassifier(random_state=101, max_features='log2', n_estimators= 500, max_depth=4, criterion='entropy')

temp_rfc.fit(X_train,y_train)

print(classification_report((temp_rfc.predict(X_test)),y_test))

# +
grid={"C":np.logspace(-3,3,7), "penalty":["l1","l2"]}
logreg=LogisticRegression()
logreg_cv=GridSearchCV(logreg,grid,cv=10)
logreg_cv.fit(X_train,y_train)

print("tuned hpyerparameters :(best parameters) ",logreg_cv.best_params_)
print("accuracy :",logreg_cv.best_score_)

# +
logreg2=LogisticRegression(C=10,penalty="l1")
logreg2.fit(X_train,y_train)

print(classification_report(logreg2.predict(X_test),y_test))
# -



StarcoderdataJupyterScriptsDedupFiltered