mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-06-05 14:55:15 +02:00
replace np.int by int
This commit is contained in:
parent
a6d78d79aa
commit
65af6ee35e
15 changed files with 3889 additions and 2375 deletions
|
|
@ -2,7 +2,8 @@
|
|||
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
||||
import numpy as np
|
||||
|
||||
def conf_matrix(p,labels,names=['1','0'],threshold=.5,show=True):
|
||||
|
||||
def conf_matrix(p, labels, names=["1", "0"], threshold=0.5, show=True):
|
||||
"""
|
||||
Returns error rate and true/false positives in a binary classification problem
|
||||
- Actual classes are displayed by column.
|
||||
|
|
@ -16,18 +17,18 @@ def conf_matrix(p,labels,names=['1','0'],threshold=.5,show=True):
|
|||
:type show: False|True
|
||||
"""
|
||||
assert p.size == labels.size, "Arrays p and labels have different dimensions."
|
||||
decision = np.ones((labels.size,1))
|
||||
decision[p<threshold] = 0
|
||||
decision = np.ones((labels.size, 1))
|
||||
decision[p < threshold] = 0
|
||||
diff = decision - labels
|
||||
false_0 = diff[diff == -1].size
|
||||
false_1 = diff[diff == 1].size
|
||||
true_1 = np.sum(decision[diff ==0])
|
||||
true_1 = np.sum(decision[diff == 0])
|
||||
true_0 = labels.size - true_1 - false_0 - false_1
|
||||
error = (false_1 + false_0)/np.float(labels.size)
|
||||
error = (false_1 + false_0) / float(labels.size)
|
||||
if show:
|
||||
print(100. - error * 100,'% instances correctly classified')
|
||||
print('%-10s| %-10s| %-10s| ' % ('',names[0],names[1]))
|
||||
print('----------|------------|------------|')
|
||||
print('%-10s| %-10s| %-10s| ' % (names[0],true_1,false_0))
|
||||
print('%-10s| %-10s| %-10s| ' % (names[1],false_1,true_0))
|
||||
return error,true_1, false_1, true_0, false_0
|
||||
print(100.0 - error * 100, "% instances correctly classified")
|
||||
print("%-10s| %-10s| %-10s| " % ("", names[0], names[1]))
|
||||
print("----------|------------|------------|")
|
||||
print("%-10s| %-10s| %-10s| " % (names[0], true_1, false_0))
|
||||
print("%-10s| %-10s| %-10s| " % (names[1], false_1, true_0))
|
||||
return error, true_1, false_1, true_0, false_0
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import numpy as np
|
|||
import warnings
|
||||
import GPy
|
||||
|
||||
|
||||
def index_to_slices(index):
|
||||
"""
|
||||
take a numpy array of integers (index) and return a nested list of slices such that the slices describe the start, stop points for each integer in the index.
|
||||
|
|
@ -16,28 +17,35 @@ def index_to_slices(index):
|
|||
returns
|
||||
>>> [[slice(0,2,None),slice(4,5,None)],[slice(2,4,None),slice(8,10,None)],[slice(5,8,None)]]
|
||||
"""
|
||||
if len(index)==0:
|
||||
return[]
|
||||
if len(index) == 0:
|
||||
return []
|
||||
|
||||
#contruct the return structure
|
||||
ind = np.asarray(index,dtype=np.int)
|
||||
ret = [[] for i in range(ind.max()+1)]
|
||||
# contruct the return structure
|
||||
ind = np.asarray(index, dtype=int)
|
||||
ret = [[] for i in range(ind.max() + 1)]
|
||||
|
||||
#find the switchpoints
|
||||
ind_ = np.hstack((ind,ind[0]+ind[-1]+1))
|
||||
switchpoints = np.nonzero(ind_ - np.roll(ind_,+1))[0]
|
||||
# find the switchpoints
|
||||
ind_ = np.hstack((ind, ind[0] + ind[-1] + 1))
|
||||
switchpoints = np.nonzero(ind_ - np.roll(ind_, +1))[0]
|
||||
|
||||
[ret[ind_i].append(slice(*indexes_i)) for ind_i,indexes_i in zip(ind[switchpoints[:-1]],zip(switchpoints,switchpoints[1:]))]
|
||||
[
|
||||
ret[ind_i].append(slice(*indexes_i))
|
||||
for ind_i, indexes_i in zip(
|
||||
ind[switchpoints[:-1]], zip(switchpoints, switchpoints[1:])
|
||||
)
|
||||
]
|
||||
return ret
|
||||
|
||||
|
||||
def get_slices(input_list):
|
||||
num_outputs = len(input_list)
|
||||
_s = [0] + [ _x.shape[0] for _x in input_list ]
|
||||
_s = [0] + [_x.shape[0] for _x in input_list]
|
||||
_s = np.cumsum(_s)
|
||||
slices = [slice(a,b) for a,b in zip(_s[:-1],_s[1:])]
|
||||
slices = [slice(a, b) for a, b in zip(_s[:-1], _s[1:])]
|
||||
return slices
|
||||
|
||||
def build_XY(input_list,output_list=None,index=None):
|
||||
|
||||
def build_XY(input_list, output_list=None, index=None):
|
||||
num_outputs = len(input_list)
|
||||
if output_list is not None:
|
||||
assert num_outputs == len(output_list)
|
||||
|
|
@ -47,27 +55,35 @@ def build_XY(input_list,output_list=None,index=None):
|
|||
|
||||
if index is not None:
|
||||
assert len(index) == num_outputs
|
||||
I = np.hstack( [np.repeat(j,_x.shape[0]) for _x,j in zip(input_list,index)] )
|
||||
I = np.hstack([np.repeat(j, _x.shape[0]) for _x, j in zip(input_list, index)])
|
||||
else:
|
||||
I = np.hstack( [np.repeat(j,_x.shape[0]) for _x,j in zip(input_list,range(num_outputs))] )
|
||||
I = np.hstack(
|
||||
[np.repeat(j, _x.shape[0]) for _x, j in zip(input_list, range(num_outputs))]
|
||||
)
|
||||
|
||||
X = np.vstack(input_list)
|
||||
X = np.hstack([X,I[:,None]])
|
||||
X = np.hstack([X, I[:, None]])
|
||||
|
||||
return X,Y,I[:,None]#slices
|
||||
return X, Y, I[:, None] # slices
|
||||
|
||||
def build_likelihood(Y_list,noise_index,likelihoods_list=None):
|
||||
|
||||
def build_likelihood(Y_list, noise_index, likelihoods_list=None):
|
||||
Ny = len(Y_list)
|
||||
if likelihoods_list is None:
|
||||
likelihoods_list = [GPy.likelihoods.Gaussian(name="Gaussian_noise_%s" %j) for y,j in zip(Y_list,range(Ny))]
|
||||
likelihoods_list = [
|
||||
GPy.likelihoods.Gaussian(name="Gaussian_noise_%s" % j)
|
||||
for y, j in zip(Y_list, range(Ny))
|
||||
]
|
||||
else:
|
||||
assert len(likelihoods_list) == Ny
|
||||
#likelihood = GPy.likelihoods.mixed_noise.MixedNoise(likelihoods_list=likelihoods_list, noise_index=noise_index)
|
||||
likelihood = GPy.likelihoods.mixed_noise.MixedNoise(likelihoods_list=likelihoods_list)
|
||||
# likelihood = GPy.likelihoods.mixed_noise.MixedNoise(likelihoods_list=likelihoods_list, noise_index=noise_index)
|
||||
likelihood = GPy.likelihoods.mixed_noise.MixedNoise(
|
||||
likelihoods_list=likelihoods_list
|
||||
)
|
||||
return likelihood
|
||||
|
||||
|
||||
def ICM(input_dim, num_outputs, kernel, W_rank=1,W=None,kappa=None,name='ICM'):
|
||||
def ICM(input_dim, num_outputs, kernel, W_rank=1, W=None, kappa=None, name="ICM"):
|
||||
"""
|
||||
Builds a kernel for an Intrinsic Coregionalization Model
|
||||
|
||||
|
|
@ -80,13 +96,26 @@ def ICM(input_dim, num_outputs, kernel, W_rank=1,W=None,kappa=None,name='ICM'):
|
|||
"""
|
||||
if kernel.input_dim != input_dim:
|
||||
kernel.input_dim = input_dim
|
||||
warnings.warn("kernel's input dimension overwritten to fit input_dim parameter.")
|
||||
warnings.warn(
|
||||
"kernel's input dimension overwritten to fit input_dim parameter."
|
||||
)
|
||||
|
||||
K = kernel.prod(GPy.kern.Coregionalize(1, num_outputs, active_dims=[input_dim], rank=W_rank,W=W,kappa=kappa,name='B'),name=name)
|
||||
K = kernel.prod(
|
||||
GPy.kern.Coregionalize(
|
||||
1,
|
||||
num_outputs,
|
||||
active_dims=[input_dim],
|
||||
rank=W_rank,
|
||||
W=W,
|
||||
kappa=kappa,
|
||||
name="B",
|
||||
),
|
||||
name=name,
|
||||
)
|
||||
return K
|
||||
|
||||
|
||||
def LCM(input_dim, num_outputs, kernels_list, W_rank=1,name='ICM'):
|
||||
def LCM(input_dim, num_outputs, kernels_list, W_rank=1, name="ICM"):
|
||||
"""
|
||||
Builds a kernel for an Linear Coregionalization Model
|
||||
|
||||
|
|
@ -98,15 +127,15 @@ def LCM(input_dim, num_outputs, kernels_list, W_rank=1,name='ICM'):
|
|||
:type W_rank: integer
|
||||
"""
|
||||
Nk = len(kernels_list)
|
||||
K = ICM(input_dim,num_outputs,kernels_list[0],W_rank,name='%s%s' %(name,0))
|
||||
K = ICM(input_dim, num_outputs, kernels_list[0], W_rank, name="%s%s" % (name, 0))
|
||||
j = 1
|
||||
for kernel in kernels_list[1:]:
|
||||
K += ICM(input_dim,num_outputs,kernel,W_rank,name='%s%s' %(name,j))
|
||||
K += ICM(input_dim, num_outputs, kernel, W_rank, name="%s%s" % (name, j))
|
||||
j += 1
|
||||
return K
|
||||
|
||||
|
||||
def Private(input_dim, num_outputs, kernel, output, kappa=None,name='X'):
|
||||
def Private(input_dim, num_outputs, kernel, output, kappa=None, name="X"):
|
||||
"""
|
||||
Builds a kernel for an Intrinsic Coregionalization Model
|
||||
|
||||
|
|
@ -117,7 +146,7 @@ def Private(input_dim, num_outputs, kernel, output, kappa=None,name='X'):
|
|||
:param W_rank: number tuples of the corregionalization parameters 'W'
|
||||
:type W_rank: integer
|
||||
"""
|
||||
K = ICM(input_dim,num_outputs,kernel,W_rank=1,kappa=kappa,name=name)
|
||||
K = ICM(input_dim, num_outputs, kernel, W_rank=1, kappa=kappa, name=name)
|
||||
K.B.W.fix(0)
|
||||
_range = range(num_outputs)
|
||||
_range.pop(output)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue