<jupyter_start><jupyter_text>ECI 273 Project<jupyter_code>import pandas as pd
import numpy as np
import keras
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
from sklearn.pipeline import make_pipeline
from sklearn.ensemble import IsolationForest
from sklearn.neighbors import LocalOutlierFactor
from sklearn.covariance import EllipticEnvelope
from sklearn.svm import OneClassSVM
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
import seaborn as sns
from sklearn import datasets
import tensorflow as tf
from tensorflow import keras
from keras.layers import *
from keras.models import *
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from sklearn import linear_model
from sklearn.metrics import r2_score

class LinearRegression:
    #constructor to initialize the coefficient array
    def __init__(self):
        self.b = []
        
    def fit(self,x,y,order):
        N = len(x)
        if order == 0:
            X = np.c_[np.ones(N)]
        elif order == 1:
            X = np.c_[np.ones(N),x]
        elif order == 2:
            X = np.c_[np.ones(N),x,np.square(x)]
        else:
            X = np.c_[np.ones(N),x,np.square(x),np.power(x,3)]
#         print(X)
        self.b = X.T@y@np.linalg.inv(X.T@X)
        


    def predict(self,x):
#         print(self.b)
        prediction = []
        for i in range(len(x)):
            pred = self.b[0]
            for j in range(1,len(self.b)):
                pred+=self.b[j]*np.power(x[i],j)
            prediction.append(pred)
        return prediction
    
    def mse(self,y,prediction):
        s = 0
        for i in range(len(y)):
            s+=(y[i]-prediction[i])**2
        score = s/len(y)
        return score
df = pd.read_csv('D:/UC DAVIS/ECI 273/project/data/clean_data.csv', delimiter=',')
df<jupyter_output><empty_output><jupyter_text>Multivariate Linear Regression<jupyter_code>df = df.dropna()
df = df.reset_index(drop=True)
# df = df.rename(columns={"evap_cfs": "evaporation", "inflow_cfs": "inflow", "elevation_feet": "elevation","storage_ft": "storage","outflow_cfs": "outflow" })
df['outflow'].isnull().values.any()

from sklearn import linear_model

# x = df.iloc[:,:4].values
# x = df.drop(['EVAP CFS','inflow_cfs','elevation_feet','ppt_inches','storage_taf'],1)
# y = df["outflow_catego"].values


reg = linear_model.LinearRegression()
reg.fit(df[['evaporation','outflow','elevation','storage']],df.outflow)
#separate the other attributes from the predicting attribute
x = df[['evaporation','inflow','elevation','storage']]
#separte the predicting attribute into Y for model training 
y = df['outflow']
# importing train_test_split from sklearn
from sklearn.model_selection import train_test_split
# splitting the data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 42)
# importing module
from sklearn.linear_model import LinearRegression
# creating an object of LinearRegression class
LR = LinearRegression()
# fitting the training data
LR.fit(x_train,y_train)
y_prediction =  LR.predict(x_test)
r2 = r2_score(y_test, y_prediction)

print(r2)
## Plotting
len(time_new)
# first we get some dates

time_init = np.arange(0,len(y_train),1)
time_new = np.arange(len(time_init),len(df['outflow']),1)
fig = plt.figure()
fig.set_size_inches(15.5, 8.5)

ax1 = fig.add_subplot(111)

ax1.scatter(time_init, y_train, marker='o', label='Training Data')
ax1.scatter(time_new, y_prediction, marker='o',alpha = 0.5, label='Predicted Data')
ax1.scatter(time_new, y_test, marker='o',alpha = 0.5, label='Test Data')
# ax1.scatter(x[40:],y[40:], s=10, c='r', marker="o", label='second')
plt.legend(loc='lower right')
plt.xlabel('Days since 11/07/2016')
plt.ylabel('Outflow')
plt.show()

def multi_var(data_x,data_y):
    cols = ['evaporation','outflow','elevation','storage']
    data_y = df['inflow']
    for col in cols:
        data_x = df[['evaporation','inflow','elevation','storage']]
        data_x = x.drop(columns=[col])

        #separte the predicting attribute into Y for model training 
        x_train, x_test, y_train, y_test = train_test_split(data_x, data_y, test_size = 0.2, random_state = 42)
        LR.fit(x_train,y_train)
        y_prediction =  LR.predict(x_test)
        r2 = r2_score(y_test, y_prediction)

        print('The r2 score without %s is %f,'% (col, r2))
multi_var(x,y)
def four_var(data_x,data_y):
    feats = ['evaporation','inflow','elevation','storage','outflow']
    for j in feats:
        # this is the factor u are testing for the score
        cols = ['evaporation','inflow','elevation','storage']
        data_y = df['outflow']
        for col in cols:
            data_x = df[['evaporation','inflow','elevation','storage']]
            data_x = data_x.drop(columns=[col])

            #separte the predicting attribute into Y for model training 
            x_train, x_test, y_train, y_test = train_test_split(data_x, data_y, test_size = 0.2, random_state = 42)
            LR.fit(x_train,y_train)
            y_prediction =  LR.predict(x_test)
            r2 = r2_score(y_test, y_prediction)

            print('The r2 score without %s is %f,'% (col, r2))<jupyter_output><empty_output><jupyter_text>Try and improve the R2 score<jupyter_code>#separate the other attributes from the predicting attribute
x = df[['evaporation','outflow','elevation','storage']]
#separte the predicting attribute into Y for model training 
y = df['inflow']

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 42)
from sklearn.model_selection import cross_val_score
# function to get cross validation scores
def get_cv_scores(model):
    scores = cross_val_score(model,
                             x_train,
                             y_train,
                             cv=5,
                             scoring='r2')
    
    print('CV Mean: ', np.mean(scores))
    print('STD: ', np.std(scores))
    print('\n')
from sklearn.linear_model import LinearRegression
# Train model
lr = LinearRegression().fit(x_train, y_train)
# get cross val scores
get_cv_scores(lr)
from sklearn.linear_model import Ridge
# Train model with default alpha=1
ridge = Ridge(alpha=1).fit(x_train, y_train)
# get cross val scores
get_cv_scores(ridge)

from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV# find optimal alpha with grid search
alpha = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
param_grid = dict(alpha=alpha)
grid = GridSearchCV(estimator=ridge, param_grid=param_grid, scoring='r2', 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.linear_model import Lasso
# Train model with default alpha=1
lasso = Lasso(alpha=1).fit(x_train, y_train)
# get cross val scores
get_cv_scores(lasso)
# find optimal alpha with grid search
alpha = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
param_grid = dict(alpha=alpha)
grid = GridSearchCV(estimator=lasso, param_grid=param_grid, scoring='r2', 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_)
# match column names to coefficients
for coef, col in enumerate(x_train.columns):
    print(f'{col}:  {lasso.coef_[coef]}')
from sklearn.linear_model import ElasticNet
# Train model with default alpha=1 and l1_ratio=0.5
elastic_net = ElasticNet(alpha=1, l1_ratio=0.5).fit(x_train, y_train)
# get cross val scores
get_cv_scores(elastic_net)
# find optimal alpha with grid search
alpha = [0.001, 0.01, 0.1, 1, 10, 100, 1000]
l1_ratio = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
param_grid = dict(alpha=alpha, l1_ratio=l1_ratio)
grid = GridSearchCV(estimator=elastic_net, param_grid=param_grid, scoring='r2', 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_)<jupyter_output>Fitting 5 folds for each of 77 candidates, totalling 385 fits<jupyter_text>Log Reg<jupyter_code>df
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2, random_state = 42)
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_score, recall_score
from sklearn.model_selection import train_test_split


y = df['outflow_catego']
x = df.drop(['outflow_catego'],1)
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3)
classifier = LogisticRegression()

def make_model(feature):
    classifier.fit(np.array(x_train[feature]).reshape(-1,1),y_train)
    mpg_pred = classifier.predict(np.array(x_test[feature]).reshape(-1,1))
    print(feature,"Precision:",precision_score(y_test,mpg_pred,average='macro',zero_division=0))
    print(feature,"Recall:",recall_score(y_test,mpg_pred,average='macro',zero_division=0))<jupyter_output><empty_output><jupyter_text>Testing out a ton of other models<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import math
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score, recall_score
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegressionCV
from sklearn.model_selection import cross_val_score
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
import random
from sklearn import tree


from sklearn import linear_model
logr = linear_model.LogisticRegression(n_jobs =-1,solver = 'lbfgs', max_iter=500)
df
import random

random.seed(10)
svm = SVC(gamma='auto')
svm.fit(x_train, y_train)

y_pred = svm.predict(x_test)
y_true = y_test.values.tolist()

rec_svm = recall_score(y_true, y_pred, average='macro')


prec_svm = precision_score(y_true, y_pred, average='macro', zero_division = 0)
print ("The recall score of this model is: ", rec_svm)
print ("The precision score of this model is: ", prec_svm)
print ("The cross val score of this model is: ", cross_val_score(svm, X, y, cv = 10, scoring = 'accuracy').mean())

logr = LogisticRegression(solver='liblinear',multi_class='ovr')
logr.fit(X_train, y_train)

y_pred = logr.predict(X_test)
y_true = y_test.values.tolist()

rec_svm = recall_score(y_true, y_pred, average='macro')

prec_svm = precision_score(y_true, y_pred, average='macro', zero_division = 0)

print ("The recall score of this model is: ", rec_svm)
print ("The precision score of this model is: ", prec_svm)
print ("The cross val score of this model is: ", cross_val_score(logr, X, y, cv = 10, scoring = 'accuracy').mean())
#model for mpg
make_model('inflow_cfs')
make_model('evap_cfs')
make_model('elevation_feet')
make_model('ppt_inches')
make_model('storage_taf')
make_model('outflow_cfs')<jupyter_output>inflow_cfs Precision: 0.4345311854605535
inflow_cfs Recall: 0.35428329092451233
evap_cfs Precision: 0.3228589032655576
evap_cfs Recall: 0.3333333333333333
elevation_feet Precision: 0.3228589032655576
elevation_feet Recall: 0.3333333333333333
ppt_inches Precision: 0.3228589032655576
ppt_inches Recall: 0.3333333333333333
storage_taf Precision: 0.3228589032655576
storage_taf Recall: 0.3333333333333333
outflow_cfs Precision: 0.6274509803921569
outflow_cfs Recall: 0.6666666666666666
{'fraction_non_alphanumeric': 0.11996505024027959, 'fraction_numerical': 0.03652249890782001, 'mean_word_length': 4.7954430379746835, 'pattern_counts': {'":': 5, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '422749', 'license': 'MIT', 'n_tokens_mistral': 4363, 'n_tokens_neox': 4008, 'n_words': 945, 'path': 'Multivar and Log Reg.ipynb', 'repo_name': 'mmaliu97/ECI-273-Project', 'size': '11445'}