def _update(self): self.K = self.kernel(self.x) if self.K.shape[0] == 0: self.L = np.zeros((0, 0)) else: sn2 = np.exp(2*self.kernel.lik) self.K = self.K + sn2*np.eye(len(self)) self.L = scipy.linalg.cholesky(self.K, lower=True)
def _normalize_weights(self): ''' Normalize the local training weight data ''' if self.weights is None: # initial setup sv, sn = self.valid.shape[0], self.invalid.shape[0] self.weights = np.concatenate((sn * np.ones(sv), sv * np.ones(sn))) else: exponential = M.exp(-self.alldesired * self.classifier) self.weights = self.weights * exponential self.weights = self.weights / self.weights.sum()
def _normalize_weights(self): ''' Normalize the local training weight data ''' if self.weights is None: # initial setup sv,sn = self.valid.shape[0], self.invalid.shape[0] self.weights = np.concatenate((sn*np.ones(sv), sv*np.ones(sn))) else: exponential = M.exp(-self.alldesired * self.classifier) self.weights = self.weights * exponential self.weights = self.weights / self.weights.sum()
def change_solution(sol, g, temp): sol2 = exchange(sol) delta = compare_solution(sol2, sol, g) if delta < 0: return sol2 else: a = rd.random() if a < m.exp(-delta / temp): return sol2 #probabilité non-nulle d'adopter la solution même si celle-ci est moins bonne else: return sol
def kernelTrans(X, A, kTup): m, n = shape(X) K = mat(zeros((m, 1))) if kTup[0] == 'lin': K = X * A.T elif kTup[0] == 'rbf': for j in range(m): deltaRow = X[j, :] - A K[j] = deltaRow * deltaRow.T K = exp(K / (-1 * kTup[1]**2)) else: raise NameError('that Kernel is not recognized') return K
def __call__(self, x, z=[], diag=False): if x.shape[0] == 0: return np.zeros((0, 0)) if diag: return self.sf2*np.ones((x.shape[0], 1)) sx = x*self.covd if z == []: K = np.asmatrix(ssd.squareform(ssd.pdist(sx, 'sqeuclidean'))) else: sz = z*self.covd K = np.asmatrix(ssd.cdist(sx, sz, 'sqeuclidean')) K = self.sf2*np.exp(-K/2) return K
def minfo(self, x): x = np.asmatrix(x) assert x.shape[1] == self.d n = x.shape[0] # Handle empty test set if n == 0: return 0 Kbb = self.kernel(x) # Handle empty training set if len(self) == 0: S = Kbb else: Kba = self.kernel(x, self.x) S = Kbb - Kba*scipy.linalg.cho_solve((self.L, True), Kba.T) sn2 = np.exp(2*self.kernel.lik) (sng, mi) = numpy.linalg.slogdet(np.eye(n) + S/sn2) return 0.5*mi
np.seterr(divide='ignore', invalid='ignore') n = 200 a = nm.linspace(0, nm.pi, n / 2) x_u = np.c_[nm.cos(a) + 0.5, nm.cos(a) - 0.5].reshape(n, 1) u = -10 * x_u + nm.randn(n, 1) x_v = np.c_[nm.sin(a), -nm.sin(a)].reshape(n, 1) v = 10 * x_v + nm.randn(n, 1) x = np.c_[u, v] y = np.zeros((n, 1)) y[0] = 1 y[n - 1] = -1 x2 = np.sum(np.power(x, 2), 1) hh = 2 * 1**2 k = nm.exp(-(nm.repmat(x2, 1, n) + nm.repmat(x2.T, n, 1) - 2 * x * x.T) / hh) w = k t_tmp1 = k**2 + 1 * np.eye(n) + 10 * k * (nm.diag(sum(w)) - w) * k t = np.linalg.inv(t_tmp1) * (k * y) m = 100 X = nm.linspace(-20, 20, m).T X2 = np.power(X, 2) U = nm.exp( -(nm.repmat(np.power(u, 2), 1, m) + nm.repmat(X2.T, n, 1) - 2 * u * X.T) / hh) V = nm.exp( -(nm.repmat(np.power(v, 2), 1, m) + nm.repmat(X2.T, n, 1) - 2 * v * X.T) / hh) plt.figure()
# Generate absorbance data and save testing file for A->B import random from numpy import matrix, matlib import csv from frange import frange t_0 = matrix(frange(-1e-6, 0, 5e-11)).transpose() t_1 = matrix(frange(0, 1.9999500e-6, 5e-11)).transpose() #t_1 = matrix(frange(0, 4000, 25)).transpose() # the following constants work well k = [2.2e7, 3.17e6] # rate constant #k = [0.003, 0.0015] a_0 = 1e-3 # initial concentration of A c = matlib.empty([t_1.size, 3]) c[:,0] = a_0 * matlib.exp(-k[0] * t_1) # concentration of A c[:,1] = a_0 * (k[0] / (k[1] - k[0])) * (matlib.exp(-k[0] * t_1) - matlib.exp(-k[1] * t_1)) # concentration of B c[:,2] = a_0 - c[:,0] - c[:,1] # concentration of C a = matlib.empty([3, 1]) a[0,0] = 1e3 # molar absorption of species A a[1,0] = 4e2 # molar absorption of species B a[2,0] = 7e2 # molar absorption of species C y_1 = matlib.dot(c, a) y_1 = y_1.transpose().tolist()[0] y_1 = map(lambda y: y + (0.04 * random.random() - 0.02), y_1) t_0 = t_0.transpose().tolist()[0] t_1 = t_1.transpose().tolist()[0]
def sample(self, x): m = self.inf(x, meanonly=True) return m + np.exp(self.kernel.lik) * np.randn(m.shape)
#!/usr/bin/python # Generate absorbance data and save testing file for A->B import random from numpy import matrix, matlib import csv from frange import frange t_0 = matrix(frange(-1e-6, 0, 5e-11)).transpose() t_1 = matrix(frange(0, 1.9999500e-6, 5e-11)).transpose() k = 2.2e7 # rate constant a_0 = 1e-3 # initial concentration of A c = a_0 * matlib.exp(-k * t_1) # molar absorption of species A a = matlib.empty([1, 1]) a[0, 0] = 1e3 y_1 = matlib.dot(c, a) y_1 = y_1.transpose().tolist()[0] y_1 = map(lambda y: y + (0.04 * random.random() - 0.02), y_1) t_0 = t_0.transpose().tolist()[0] t_1 = t_1.transpose().tolist()[0] fullLightVoltage = -0.0951192897786 y_1 = map(lambda y: fullLightVoltage * (10**-y), y_1) y_0 = [] for i in range(0, len(t_0)): y_0.append(fullLightVoltage + 0.01 * random.random() - 0.005)
#!/usr/bin/python # Generate absorbance data and save testing file for A->B import random from numpy import matrix, matlib import csv from frange import frange t_0 = matrix(frange(-1e-6, 0, 5e-11)).transpose() t_1 = matrix(frange(0, 1.9999500e-6, 5e-11)).transpose() k = [2.2e7, 3.124e7] # rate constant a_0 = 1e-3 # initial concentration of A a_1 = 2e-3 # initial concentration of C c = matlib.empty([t_1.size, 2]) c[:,0] = a_0 * matlib.exp(-k[0] * t_1) c[:,1] = a_1 * matlib.exp(-k[1] * t_1) # molar absorption of species A a = matlib.empty([2, 1]) a[0,0] = 1e3 a[1,0] = 1e3 y_1 = matlib.dot(c, a) y_1 = y_1.transpose().tolist()[0] y_1 = map(lambda y: y + (0.04 * random.random() - 0.02), y_1) t_0 = t_0.transpose().tolist()[0] t_1 = t_1.transpose().tolist()[0] fullLightVoltage = -0.0951192897786 y_1 = map(lambda y:fullLightVoltage*(10**-y), y_1)
def V_sphere(diameter): return (4 / 3 * ml.pi * (diameter / 2)**3) #%% #--------------------------------------------- # Making an identical plot as in the reference paper -> DID NOT USE IN THE END #--------------------------------------------- pH = np.linspace(3, 12, 10) plt.plot(pH, r_OLI2( pH, 298.15)) #compared to hangx and spiers paper its way too high... plt.show() plt.plot( pH, ml.log(ml.exp(r_OLI2(pH, 298.15)) / 10000)) #making a plot like in Oelkers, I do / 10000 to convert # m^2 to cm^2 plt.plot(pH, (ml.log(2) / r_OLI2( pH, 298.15))) #compared to hangx and spiers paper its way too high... #rendering plt.xlabel("pH") plt.ylabel("ln 2 / log r") title = "Dissolution in function of pH – DID NOT USE" plt.grid() plt.title(title) plt.show()
def new_k(k2, New_T2): K_T2 = constants.convert_temperature(New_T2, 'C', 'K') exp1 = Ea / (K_T2 * constants.R) exp2 = Ea / (Ref_T1 * constants.R) k1 = k2 / (ml.exp(exp1) / ml.exp(exp2)) return k1
def softmax(self, y): maxy = np.amax(y) probas = np.exp(y-maxy) probas = probas / probas.sum(axis=0) return np.array(probas)
def test_SE_diag(self): K = self.k(self.x, diag=True) sf2 = np.exp(2*self.hyp['cov'][-1]) Ktrue = sf2*np.ones((3, 1)).T self.assertTrue(almostEqual(Ktrue, K))
def __init__(self, hyp): self.mean = hyp['mean'] self.cov = np.exp(hyp['cov'][:-1]) self.covd = np.asmatrix(np.diagflat(1/self.cov)) self.sf2 = np.exp(2*hyp['cov'][-1]) self.lik = hyp['lik']
#!/usr/bin/python # Generate absorbance data and save testing file for A->B import random from numpy import matrix, matlib import csv from frange import frange t_0 = matrix(frange(-1e-6, 0, 5e-11)).transpose() t_1 = matrix(frange(0, 1.9999500e-6, 5e-11)).transpose() k = 2.2e7 # rate constant a_0 = 1e-3 # initial concentration of A c = a_0 * matlib.exp(-k * t_1) # molar absorption of species A a = matlib.empty([1, 1]) a[0,0] = 1e3 y_1 = matlib.dot(c, a) y_1 = y_1.transpose().tolist()[0] y_1 = map(lambda y: y + (0.04 * random.random() - 0.02), y_1) t_0 = t_0.transpose().tolist()[0] t_1 = t_1.transpose().tolist()[0] fullLightVoltage = -0.0951192897786 y_1 = map(lambda y:fullLightVoltage*(10**-y), y_1) y_0 = [] for i in range(0, len(t_0)): y_0.append(fullLightVoltage + 0.01 * random.random() - 0.005)
# Generate absorbance data and save testing file for A->B import random from numpy import matrix, matlib import csv from frange import frange t_0 = matrix(frange(-1e-6, 0, 5e-11)).transpose() t_1 = matrix(frange(0, 1.9999500e-6, 5e-11)).transpose() #t_1 = matrix(frange(0, 4000, 25)).transpose() # the following constants work well k = [2.2e7, 3.17e6] # rate constant #k = [0.003, 0.0015] a_0 = 1e-3 # initial concentration of A c = matlib.empty([t_1.size, 3]) c[:, 0] = a_0 * matlib.exp(-k[0] * t_1) # concentration of A c[:, 1] = a_0 * (k[0] / (k[1] - k[0])) * ( matlib.exp(-k[0] * t_1) - matlib.exp(-k[1] * t_1)) # concentration of B c[:, 2] = a_0 - c[:, 0] - c[:, 1] # concentration of C a = matlib.empty([3, 1]) a[0, 0] = 1e3 # molar absorption of species A a[1, 0] = 4e2 # molar absorption of species B a[2, 0] = 7e2 # molar absorption of species C y_1 = matlib.dot(c, a) y_1 = y_1.transpose().tolist()[0] y_1 = map(lambda y: y + (0.04 * random.random() - 0.02), y_1) t_0 = t_0.transpose().tolist()[0] t_1 = t_1.transpose().tolist()[0]
def test_SE_diag(self): K = self.k(self.x, diag=True) sf2 = np.exp(2 * self.hyp['cov'][-1]) Ktrue = sf2 * np.ones((3, 1)).T self.assertTrue(almostEqual(Ktrue, K))
import math import numpy as np import numpy.matlib as nm import matplotlib.pyplot as plt n = 100 u = nm.randn(n, 1) / 4 + 2 x = nm.randn(n, 1) / 2 + 1 w = 2 * nm.exp(-8 * np.power((x - 2), 2) + 2 * np.power((x - 1), 2)) y = nm.sin(nm.pi * x) / (nm.pi * x) + 0.1 * nm.randn(n, 1) x2 = np.ones(len(x)).reshape(len(x), 1) # 生成一个维度为(n,1),元素全是1的数组 x = np.c_[x, x2] # x(:,2)=1;加第二行为1 t1 = np.multiply(nm.repmat(w, 1, 2), x) # np.multiply为矩阵对应元素相乘 t = np.linalg.inv(x.T * t1) * (x.T * (np.multiply(w, y))) # np.linalg.inv(.)求逆矩阵 X = nm.linspace(-1, 3, 100) # 生成数据节点 Y = nm.sin(nm.pi * X) / (nm.pi * X) # 根据节点计算Y u = np.c_[u, x2] v = u * t print(u.shape) plt.figure() plt.plot(x[:, 0], y, 'bo', label='xi,yi') # 绘制原始数据点 plt.plot(X, Y, 'r-', label='f(x)') # 绘制重要度加权的最小二乘曲线 plt.plot(u[:, 0], v, 'kx', label='xi1,yi1') # 绘制最小二乘曲线 plt.legend() # 显示绘制的legend plt.show() # 显示绘制的画布