added codes

This commit is contained in:
Morten Hjorth-Jensen
2024-09-29 21:58:56 +02:00
parent efdc7bd11e
commit fae683c3f1
8 changed files with 405 additions and 187 deletions
+26
View File
@@ -0,0 +1,26 @@
import numpy as np
class LassoRegression:
def __init__(self, learning_rate=0.01, num_iterations=1000, lambda_reg=1.0):
self.learning_rate = learning_rate
self.num_iterations = num_iterations
self.lambda_reg = lambda_reg
self.weights = None
def fit(self, X, y):
num_samples, num_features = X.shape
self.weights = np.zeros(num_features)
for _ in range(self.num_iterations):
linear_model = np.dot(X, self.weights)
gradient = (1 / num_samples) * np.dot(X.T, (linear_model - y)) + self.lambda_reg * np.sign(self.weights)
# Update weights
self.weights -= self.learning_rate * gradient
def predict(self, X):
return np.dot(X, self.weights)
# Example usage
if __name__ == "__main__":
# Sample data
X = np.array([[1, 2], [2, 3], [3, 4], [4, 5]])
y = np.array([1, 2, 3, 4])
model = LassoRegression(learning_rate=0.01, num_iterations=1000, lambda_reg=0.1)
model.fit(X, y)
predictions = model.predict(X)
print("Predictions:", predictions)
+32
View File
@@ -0,0 +1,32 @@
import numpy as np
class LogisticRegression:
def __init__(self, learning_rate=0.01, num_iterations=1000):
self.learning_rate = learning_rate
self.num_iterations = num_iterations
self.weights = None
def sigmoid(self, z):
return 1 / (1 + np.exp(-z))
def fit(self, X, y):
num_samples, num_features = X.shape
self.weights = np.zeros(num_features)
for _ in range(self.num_iterations):
linear_model = np.dot(X, self.weights)
y_predicted = self.sigmoid(linear_model)
# Gradient calculation
gradient = np.dot(X.T, (y_predicted - y)) / num_samples
# Update weights
self.weights -= self.learning_rate * gradient
def predict(self, X):
linear_model = np.dot(X, self.weights)
y_predicted = self.sigmoid(linear_model)
return [1 if i >= 0.5 else 0 for i in y_predicted]
# Example usage
if __name__ == "__main__":
# Sample data
X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]])
y = np.array([0, 0, 0, 1]) # AND gate
model = LogisticRegression(learning_rate=0.1, num_iterations=1000)
model.fit(X, y)
predictions = model.predict(X)
print("Predictions:", predictions)
+37
View File
@@ -0,0 +1,37 @@
import numpy as np
class Perceptron:
def __init__(self, learning_rate=0.01, n_iters=1000):
self.learning_rate = learning_rate
self.n_iters = n_iters
self.weights = None
self.bias = None
def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
for _ in range(self.n_iters):
for idx, x_i in enumerate(X):
linear_output = np.dot(x_i, self.weights) + self.bias
y_predicted = self.activation_function(linear_output)
# Update weights and bias
update = self.learning_rate * (y[idx] - y_predicted)
self.weights += update * x_i
self.bias += update
def activation_function(self, x):
return 1 if x >= 0 else 0
def predict(self, X):
linear_output = np.dot(X, self.weights) + self.bias
y_predicted = [self.activation_function(i) for i in linear_output]
return np.array(y_predicted)
# Example usage
if __name__ == "__main__":
# Sample data (AND logic gate)
X = np.array([[0, 0],
[0, 1],
[1, 0],
[1, 1]])
y = np.array([0, 0, 0, 1]) # AND outputs
perceptron = Perceptron(learning_rate=0.1, n_iters=10)
perceptron.fit(X, y)
predictions = perceptron.predict(X)
print("Final predictions:", predictions)
+107
View File
@@ -0,0 +1,107 @@
# Importing various packages
from random import random, seed
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import sys
# the number of datapoints
n = 100
x = 2*np.random.rand(n,1)
y = 4+3*x+np.random.randn(n,1)
X = np.c_[np.ones((n,1)), x]
# Hessian matrix
H = (2.0/n)* X.T @ X
# Get the eigenvalues
EigValues, EigVectors = np.linalg.eig(H)
print(f"Eigenvalues of Hessian Matrix:{EigValues}")
beta_linreg = np.linalg.pinv(X.T @ X) @ X.T @ y
print(beta_linreg)
beta = np.random.randn(2,1)
eta = 1.0/np.max(EigValues)
Niterations = 1000
for iter in range(Niterations):
gradient = (2.0/n)*X.T @ (X @ beta-y)
beta -= eta*gradient
print(beta)
xnew = np.array([[0],[2]])
xbnew = np.c_[np.ones((2,1)), xnew]
ypredict = xbnew.dot(beta)
ypredict2 = xbnew.dot(beta_linreg)
plt.plot(xnew, ypredict, "r-")
plt.plot(xnew, ypredict2, "b-")
plt.plot(x, y ,'ro')
plt.axis([0,2.0,0, 15.0])
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.title(r'Gradient descent example')
plt.show()
X = np.c_[np.ones((n,1)), x]
XT_X = X.T @ X
#Ridge parameter lambda
lmbda = 0.001
Id = n*lmbda* np.eye(XT_X.shape[0])
# Hessian matrix
H = (2.0/n)* XT_X+2*lmbda* np.eye(XT_X.shape[0])
# Get the eigenvalues
EigValues, EigVectors = np.linalg.eig(H)
print(f"Eigenvalues of Hessian Matrix:{EigValues}")
beta_linreg = np.linalg.pinv(XT_X+Id) @ X.T @ y
print(beta_linreg)
# Start plain gradient descent
beta = np.random.randn(2,1)
eta = 1.0/np.max(EigValues)
Niterations = 100
for iter in range(Niterations):
gradients = 2.0/n*X.T @ (X @ (beta)-y)+2*lmbda*beta
beta -= eta*gradients
print(beta)
ypredict = X @ beta
ypredict2 = X @ beta_linreg
plt.plot(x, ypredict, "r-")
plt.plot(x, ypredict2, "b-")
plt.plot(x, y ,'ro')
plt.axis([0,2.0,0, 15.0])
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.title(r'Gradient descent example for Ridge')
plt.show()
# And now with Lasso
# Start plain gradient descent
beta_lasso = np.random.randn(2,1)
eta = 0.01
Niterations = 100
for iter in range(Niterations):
gradients = 2.0/n*X.T @ (X @ (beta)-y)+2*lmbda*np.sign(beta)
beta_lasso -= eta*gradients
print('Gradient descent with Lasso:', beta_lasso)
ypredict = X @ beta_lasso
plt.plot(x, ypredict, "r-")
plt.plot(x, y ,'ro')
plt.axis([0,2.0,0, 15.0])
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
plt.title(r'Gradient descent example for Lasso')
plt.show()
+19
View File
@@ -0,0 +1,19 @@
import Quanthon as qt
#Initializing a Single Qubit
#Initialize a single qubit by creating an instance of the Qubits class.
qubit = qt.Qubits(1)
# Apply a Hadamard gate on the first qubit
qubit.H(0)
# Apply a Pauli-X gate on the first qubit
qubit.X(0)
# Apply a Pauli-Y gate on the first qubit
qubit.Y(0)
# Apply a Pauli-Z gate on the first qubit
qubit.Z(0)
result = qubit.measure(n_shots=10)