This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Sun Jun 24 00:26:00 2018 | |
@author: b0003 | |
""" | |
import numpy as np | |
np.random.seed(1337) | |
from NaiveBayes_log import NB | |
import matplotlib.pyplot as plt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Univariate Naive Bayes | |
@author: Abdullah Al Nuaimi | |
""" | |
import numpy as np | |
from scipy.misc import logsumexp | |
class NB(): | |
def __init__(self,x,r): | |
# define the input data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.linear_model import LinearRegression | |
import matplotlib.pyplot as plt | |
from sklearn.model_selection import train_test_split | |
from sklearn.model_selection import KFold | |
from sklearn.pipeline import Pipeline | |
def gaussian_noise(mu,sigma,n): | |
return np.random.normal(mu,sigma,n) | |
from sklearn.preprocessing import PolynomialFeatures | |
degree=8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.linear_model import LinearRegression | |
import matplotlib.pyplot as plt | |
from sklearn.model_selection import train_test_split | |
from Poly import Poly | |
from sklearn.model_selection import KFold | |
#%% Import Data | |
x_raw,y_raw=np.loadtxt('data.csv',delimiter=',') | |
# k = 8 | |
degree=8 # k=8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Polynomial Class | |
@author: Abdullah Alnuaimi | |
""" | |
class Poly(): | |
def __init__(self,a,k): | |
self.a=a | |
self.k=k | |
self.A =lambda x,a=a,k=k:[[a*n**k for a,k in zip(a,k)] for n in x] | |
self.V=None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.linear_model import LinearRegression | |
import matplotlib.pyplot as plt | |
from sklearn.model_selection import train_test_split | |
def fit_poly(a,k): | |
'''returns a function of the dot product (A=V.a) ''' | |
A=lambda x,a=a,k=k:[[a*n**k for a,k in zip(a,k)] for n in x] | |
return A | |
def evaluate_poly(x,A): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.linear_model import LinearRegression | |
import matplotlib.pyplot as plt | |
from sklearn.model_selection import train_test_split | |
# loading data | |
x_raw,y_raw=np.loadtxt('data.csv',delimiter=',') | |
# reshape the data | |
x=x_raw.reshape(-1,1) | |
y=y_raw.reshape(-1,1) | |
# split the model into test/train sets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Polynomials | |
@author: Abdullah Alnuaimi | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def fit_poly(a,k): | |
'''returns a function of the dot product (A=V.a) ''' | |
A=lambda x,a=a,k=k:[[a*n**k for a,k in zip(a,k)] for n in x] | |
return A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Polynomials | |
@author: Abdullah Alnuaimi | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def fit_poly(a,k): | |
'''returns a function of the dot product (A=V.a) ''' | |
A=lambda x,a=a,k=k:[[a*n**k for a,k in zip(a,k)] for n in x] | |
return A |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.linear_model import LinearRegression | |
from sklearn.model_selection import train_test_split | |
model=LinearRegression() | |
y=y.reshape(-1,1) # reshaping the data | |
x=x.reshape(-1,1) | |
#spliting into test/train with a test size of .3 or 30 samples. | |
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,shuffle=True) | |
model.fit(x_train,y_train) # fitting the model | |
print(model.score(x_test,y_test)) # get the r2 score | |
p=model.predict(x) # generate a prediction from the model using the original input |
NewerOlder