Here are the examples of the python api numpy.asfarray taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
75 Examples
3
Source : utils.py
with MIT License
from acbull
with MIT License
from acbull
def dcg_at_k(r, k):
r = np.asfarray(r)[:k]
if r.size:
return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1)))
return 0.
def ndcg_at_k(r, k):
3
Source : official_eval_ndcg.py
with Apache License 2.0
from aister2020
with Apache License 2.0
from aister2020
def dcg_at_k(r, k):
r = np.asfarray(r)[:k]
if r.size:
return r[0] + np.sum(r[1:] / np.log2(np.arange(3, r.size + 2)))
return 0.
# compute ndcg@k (dcg@k / idcg@k) for a single sample
def get_ndcg(r, ref, k):
3
Source : metrics.py
with Apache License 2.0
from Ascend
with Apache License 2.0
from Ascend
def dcg_at_k(r, k, method=1):
"""Score is discounted cumulative gain (dcg)
Relevance is positive real values. Can use binary
as the previous methods.
Returns:
Discounted cumulative gain
"""
r = np.asfarray(r)[:k]
if r.size:
if method == 0:
return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1)))
elif method == 1:
return np.sum(r / np.log2(np.arange(2, r.size + 2)))
else:
raise ValueError('method must be 0 or 1.')
return 0.
def ndcg_at_k(r, k, method=1):
3
Source : metrics.py
with Apache License 2.0
from Ascend
with Apache License 2.0
from Ascend
def recall_at_k(r, k, all_pos_num):
r = np.asfarray(r)[:k]
return np.sum(r) / all_pos_num
def hit_at_k(r, k):
3
Source : test_discovery.py
with MIT License
from ballet
with MIT License
from ballet
def test_discover_feature_error(sample_data):
features = [
Feature('size', FragileTransformer()),
]
X_df, y_df = sample_data.X, sample_data.y
y = np.asfarray(y_df)
discovery_df = discover(features, X_df, y_df, y)
assert discovery_df.shape[0] == len(features)
assert np.isnan(discovery_df['mean'].at[0])
def test_discover_target_nans(sample_data):
3
Source : test_discovery.py
with MIT License
from ballet
with MIT License
from ballet
def test_discover_target_nans(sample_data):
features = [
Feature('size', NullFiller(0)),
]
X_df, y_df = sample_data.X, sample_data.y
y = np.asfarray(y_df)
# introduce nan to target
y[0] = np.nan
discovery_df = discover(features, X_df, y_df, y)
# stats with target should still be computed
assert not np.isnan(discovery_df['mutual_information']).any()
3
Source : tensorflow_utils.py
with Apache License 2.0
from bentoml
with Apache License 2.0
from bentoml
def __init__(self):
super().__init__()
self.weights = np.asfarray([[1.0], [1.0], [1.0], [1.0], [1.0]])
self.dense = lambda inputs: tf.matmul(inputs, self.weights)
@tf.function(
3
Source : tensorflow_utils.py
with Apache License 2.0
from bentoml
with Apache License 2.0
from bentoml
def __init__(self):
super().__init__()
self.weights = np.asfarray([[1.0], [1.0], [1.0], [1.0], [1.0]])
self.dense = lambda tensor: tf.matmul(tensor, self.weights)
@tf.function(
3
Source : SCSA.py
with GNU General Public License v3.0
from bioinfo-ibms-pumc
with GNU General Public License v3.0
from bioinfo-ibms-pumc
def p_adjust_bh(p):
"""Benjamini-Hochberg p-value correction for multiple hypothesis testing."""
p = asfarray(p)
by_descend = p.argsort()[::-1]
by_orig = by_descend.argsort()
steps = float(len(p)) / arange(len(p), 0, -1)
q = minimum(1, minimum.accumulate(steps * p[by_descend]))
return q[by_orig]
@staticmethod
3
Source : item_recomendation_functions.py
with MIT License
from caserec2018
with MIT License
from caserec2018
def ndcg_at_k(ranking):
"""
Score is normalized discounted cumulative gain (ndcg). Relevance is positive real values. Can use binary
as the previous methods.
:param ranking: ranking to evaluate in dcg format [0, 0, 1], where 1 is correct info
:type ranking: list
:return: Normalized discounted cumulative gain
:rtype: float
"""
ranking = np.asfarray(ranking)
r_ideal = np.asfarray(sorted(ranking, reverse=True))
dcg_ideal = r_ideal[0] + np.sum(r_ideal[1:] / np.log2(np.arange(2, r_ideal.size + 1)))
dcg_ranking = ranking[0] + np.sum(ranking[1:] / np.log2(np.arange(2, ranking.size + 1)))
return dcg_ranking / dcg_ideal
3
Source : metrics.py
with MIT License
from CRIPAC-DIG
with MIT License
from CRIPAC-DIG
def recall_at_k(r, k, all_pos_num):
r = np.asfarray(r)[:k]
if all_pos_num == 0:
return 0
else:
return np.sum(r) / all_pos_num
def hit_at_k(r, k):
3
Source : polyint.py
with Apache License 2.0
from dashanji
with Apache License 2.0
from dashanji
def __init__(self, xi, yi=None, axis=0):
_Interpolator1D.__init__(self, xi, yi, axis)
self.xi = np.asfarray(xi)
self.set_yi(yi)
self.n = len(self.xi)
self.wi = np.zeros(self.n)
self.wi[0] = 1
for j in range(1, self.n):
self.wi[:j] *= (self.xi[j]-self.xi[:j])
self.wi[j] = np.multiply.reduce(self.xi[:j]-self.xi[j])
self.wi **= -1
def set_yi(self, yi, axis=None):
3
Source : nyu.py
with MIT License
from dwofk
with MIT License
from dwofk
def val_transform(self, rgb, depth):
depth_np = depth
transform = transforms.Compose([
transforms.Resize(250.0 / iheight),
transforms.CenterCrop((228, 304)),
transforms.Resize(self.output_size),
])
rgb_np = transform(rgb)
rgb_np = np.asfarray(rgb_np, dtype='float') / 255
depth_np = transform(depth_np)
return rgb_np, depth_np
3
Source : eval.py
with MIT License
from fajieyuan
with MIT License
from fajieyuan
def dcg_at_k(r, k):
r = np.asfarray(r)[:k]
return np.sum(r / np.log2(np.arange(2, r.size + 2)))
def ndcg_at_k(r, k):
3
Source : metrics.py
with MIT License
from gabrielspmoreira
with MIT License
from gabrielspmoreira
def _ndcg_at_k(r, k):
#Based on https://gist.github.com/bwhite/3726239, but with alternative formulation of DCG
#which places stronger emphasis on retrieving relevant documents (used in Kaggle)
def dcg_at_k(r, k):
r = np.asfarray(r)[:k]
if r.size:
return np.sum((np.power(2,r)-1) / np.log2(np.arange(2, r.size + 2)))
return 0.
dcg_max = dcg_at_k(sorted(r, reverse=True), k)
if not dcg_max:
return 0.
return dcg_at_k(r, k) / dcg_max
class HitRate(StreamingMetric):
3
Source : tenkf.py
with MIT License
from gboehl
with MIT License
from gboehl
def init_mv_normal(self, loc=[0, 0], scale=[[1, .5], [.5, 1]]):
loc = np.asfarray(loc)
scale = np.asfarray(scale)
assert len(loc) == len(scale)
self._repr = {"loc": loc.tolist(), "scale": scale.tolist()}
try:
C = np.linalg.cholesky(scale)
Ci = np.linalg.inv(C)
except np.linalg.LinAlgError as err:
C = np.real(sqrtm(scale))
Ci = np.linalg.pinv(C)
chaospy.baseclass.Dist.__init__(self, C=C, Ci=Ci, loc=loc)
# must be patched to allow for a covariance that is only PSD
chaospy.MvNormal.__init__ = init_mv_normal
3
Source : test_quantity_non_ufuncs.py
with BSD 3-Clause "New" or "Revised" License
from holzschu
with BSD 3-Clause "New" or "Revised" License
from holzschu
def test_asfarray(self):
self.check(np.asfarray)
farray = np.asfarray(a=self.q)
assert_array_equal(farray, self.q)
def test_empty_like(self):
3
Source : utils.py
with MIT License
from hwang1996
with MIT License
from hwang1996
def recall_at_k(r, k, all_pos_num):
r = np.asfarray(r)[:k]
return np.sum(r) / all_pos_num
def F1(pre, rec):
3
Source : classic.py
with MIT License
from microprediction
with MIT License
from microprediction
def ackley_on_cube(u: [float]) -> float:
# allow parameter range -32.768 < =x(i) < =32.768, global minimum at x=(0,0,...,0)
rescaled_u = [2 * 32.768 * smoosh(ui) - 32.768 for ui in u]
x = np.asfarray(rescaled_u)
ndim = len(x)
a = 20.;
b = 0.2;
c = 2. * math.pi
return (-a * np.exp(-b * np.sqrt(1. / ndim * np.sum(x ** 2))) - np.exp(
1. / ndim * np.sum(np.cos(c * x))) + a + np.exp(1.)) / 20.0
# Adapted from https://github.com/SISDevelop/SwarmPackagePy/blob/master/SwarmPackagePy/testFunctions.py
def cross_on_cube(u):
3
Source : kitti_dataloader.py
with MIT License
from miraiaroha
with MIT License
from miraiaroha
def val_transform(self, rgb, depth):
transform = Compose([Crop(130, 10, 240, 1200),
Resize(180 / 240),
CenterCrop(self.input_size),
])
rgb_np = transform(rgb)
rgb_np = np.asfarray(rgb_np, dtype='float') / 255
depth_np = np.asfarray(depth, dtype='float32')
depth_np = transform(depth_np)
return rgb_np, depth_np
if __name__ == '__main__':
3
Source : nyu_dataloader.py
with MIT License
from miraiaroha
with MIT License
from miraiaroha
def val_transform(self, rgb, depth):
transform = Compose([Resize(240.0 / iheight),
CenterCrop(self.input_size),
])
rgb_np = transform(rgb)
rgb_np = np.asfarray(rgb_np, dtype='float') / 255
depth_np = transform(depth)
return rgb_np, depth_np
if __name__ == '__main__':
3
Source : inference.py
with MIT License
from ngchc
with MIT License
from ngchc
def compute_psnr(im1, im2):
if im1.shape != im2.shape:
raise Exception('the shapes of two images are not equal')
rmse = np.sqrt(((np.asfarray(im1) - np.asfarray(im2)) ** 2).mean())
psnr = 20 * np.log10(255.0 / rmse)
return psnr
class Net(object):
3
Source : eval.py
with MIT License
from ngchc
with MIT License
from ngchc
def compute_psnr(im1, im2):
if im1.shape != im2.shape:
raise Exception('the shapes of two images are not equal')
rmse = np.sqrt(((np.asfarray(im1) - np.asfarray(im2)) ** 2).mean())
psnr = 20 * np.log10(255.0 / rmse)
return psnr
def main():
3
Source : utils.py
with MIT License
from ngchc
with MIT License
from ngchc
def compute_psnr(im1, im2):
"""
Args:
im1/im2: [0, 255]
Returns:
psnr: Peek Signal-to-Noise Ratio
"""
if im1.shape != im2.shape:
raise Exception('the shapes of two images are not equal')
rmse = np.sqrt(((np.asfarray(im1) - np.asfarray(im2)) ** 2).mean())
psnr = 20 * np.log10(255.0 / rmse)
return psnr
def compute_psnr_from_mse(mse):
3
Source : stats.py
with GNU General Public License v3.0
from oscar-franzen
with GNU General Public License v3.0
from oscar-franzen
def p_adjust_bh(p):
"""Benjamini-Hochberg p-value correction for multiple hypothesis
testing."""
p = np.asfarray(p)
by_descend = p.argsort()[::-1]
by_orig = by_descend.argsort()
steps = float(len(p)) / np.arange(float(len(p)), 0, -1)
q = np.minimum(1, np.minimum.accumulate(steps * p[by_descend]))
return q[by_orig]
3
Source : cfq_transformer.py
with MIT License
from RobertCsordas
with MIT License
from RobertCsordas
def save_valid_details(self):
for name, (losses, oks) in self.helper.state.full_loss_log.items():
losses = np.asfarray(losses)
oks = np.asarray(oks, dtype=np.bool)
torch.save({"losses": losses, "oks": oks}, self.helper.get_storage_path(f"loss_details/{name}.pth"))
def train(self):
3
Source : compare_all.py
with MIT License
from system123
with MIT License
from system123
def string2ndarray(x, dtype=np.float32):
# Remove BS which pandas adds to numpy array string
x = x.replace("\n","").replace("[","").replace("]","").replace(",","")
x = re.sub('\s+', ' ', x).strip().split(" ")
return np.asfarray(x, dtype)
def extract_img_id(x):
3
Source : vector.py
with MIT License
from tneumann
with MIT License
from tneumann
def dehomogenize(a):
""" makes homogeneous vectors inhomogenious by dividing by the last element in the last axis
>>> dehomogenize([1, 2, 4, 2]).tolist()
[0.5, 1.0, 2.0]
>>> dehomogenize([[1, 2], [4, 4]]).tolist()
[[0.5], [1.0]]
"""
a = np.asfarray(a)
return a[...,:-1] / a[...,np.newaxis,-1]
# just some handy aliases
dehom = dehomogenize
3
Source : nyu_dataloader.py
with Apache License 2.0
from tutuxh
with Apache License 2.0
from tutuxh
def val_transform(rgb, depth):
depth_trans = depth
# perform 1st part of data augmentation
transform = transforms.Compose([
# Resize
transforms.Resize(240.0 / iheight),
# CenterCrop
transforms.CenterCrop((oheight, owidth)),
])
depth_trans = transform(depth_trans)
rgb_trans = transform(rgb)
rgb_trans = np.asfarray(rgb_trans, dtype = 'float') / 255
return rgb_trans, depth_trans
class NYUDataset(data.Dataset):
3
Source : core.py
with MIT License
from vfilimonov
with MIT License
from vfilimonov
def _keep_aspect(bbox, w=None, h=None):
if w is None and h is None:
# Should happen only on local Windows machine
w, h = np.asfarray(plt.gcf().get_size_inches())
else:
w, h = float(w), float(h)
bx, by, bw, bh = np.asfarray(bbox)
aspect_fig = w / h
aspect_bbox = bw / bh
if aspect_fig > aspect_bbox: # Figure is wider than bbox
newh = bw / aspect_fig
bbox = [bx, by + bh/2. - newh/2., bw, newh]
else:
neww = bh * aspect_fig
bbox = [bx + bw/2. - neww/2., by, neww, bh]
return bbox
def _intersection_area(a, b):
0
Source : slsqp.py
with GNU General Public License v3.0
from adityaprakash-bobby
with GNU General Public License v3.0
from adityaprakash-bobby
def approx_jacobian(x, func, epsilon, *args):
"""
Approximate the Jacobian matrix of a callable function.
Parameters
----------
x : array_like
The state vector at which to compute the Jacobian matrix.
func : callable f(x,*args)
The vector-valued function.
epsilon : float
The perturbation used to determine the partial derivatives.
args : sequence
Additional arguments passed to func.
Returns
-------
An array of dimensions ``(lenf, lenx)`` where ``lenf`` is the length
of the outputs of `func`, and ``lenx`` is the number of elements in
`x`.
Notes
-----
The approximation is done using forward differences.
"""
x0 = asfarray(x)
f0 = atleast_1d(func(*((x0,)+args)))
jac = zeros([len(x0), len(f0)])
dx = zeros(len(x0))
for i in range(len(x0)):
dx[i] = epsilon
jac[i] = (func(*((x0+dx,)+args)) - f0)/epsilon
dx[i] = 0.0
return jac.transpose()
def fmin_slsqp(func, x0, eqcons=(), f_eqcons=None, ieqcons=(), f_ieqcons=None,
0
Source : _differentialevolution.py
with GNU General Public License v3.0
from adityaprakash-bobby
with GNU General Public License v3.0
from adityaprakash-bobby
def init_population_array(self, init):
"""
Initialises the population with a user specified population.
Parameters
----------
init : np.ndarray
Array specifying subset of the initial population. The array should
have shape (M, len(x)), where len(x) is the number of parameters.
The population is clipped to the lower and upper `bounds`.
"""
# make sure you're using a float array
popn = np.asfarray(init)
if (np.size(popn, 0) < 5 or
popn.shape[1] != self.parameter_count or
len(popn.shape) != 2):
raise ValueError("The population supplied needs to have shape"
" (M, len(x)), where M > 4.")
# scale values and clip to bounds, assigning to population
self.population = np.clip(self._unscale_parameters(popn), 0, 1)
self.num_population_members = np.size(self.population, 0)
self.population_shape = (self.num_population_members,
self.parameter_count)
# reset population energies
self.population_energies = (np.ones(self.num_population_members) *
np.inf)
# reset number of function evaluations counter
self._nfev = 0
@property
0
Source : test_discovery.py
with MIT License
from ballet
with MIT License
from ballet
def test_discover(sample_data, expensive_stats):
features = [
Feature(
'size', NullFiller(0),
source='foo.features.contrib.user_a.feature_1'
),
Feature(
'strength', NullFiller(100),
source='foo.features.contrib.user_b.feature_1'
)
]
X_df, y_df = sample_data.X, sample_data.y
y = np.asfarray(y_df)
df = discover(features, X_df, y_df, y, expensive_stats=expensive_stats)
expected_cols = {
'name', 'description', 'input', 'transformer', 'primitives', 'output',
'author', 'source', 'mutual_information',
'conditional_mutual_information', 'ninputs', 'nvalues', 'ncontinuous',
'ndiscrete', 'mean', 'std', 'variance', 'min', 'median', 'max',
'nunique',
}
actual_cols = df.columns
assert not expected_cols.symmetric_difference(actual_cols)
assert df.shape[0] == len(features)
# test filter
input = 'size'
discovery_df = discover(features, X_df, y_df, y, input=input)
assert discovery_df.shape[0] == len([
feature
for feature in features
if feature.input == input or input in feature.input
])
# test no data available
# have to clear cache, as values on data already known
ballet.discovery._summarize_feature.memory.clear()
discovery_df = discover(features, None, None, None)
assert discovery_df.shape[0] == len(features)
actual_cols = discovery_df.columns
assert not expected_cols.symmetric_difference(actual_cols)
assert np.isnan(discovery_df['mean'].at[0])
def test_discover_feature_error(sample_data):
0
Source : pyqtgraph.py
with GNU General Public License v3.0
from BraiNEdarwin
with GNU General Public License v3.0
from BraiNEdarwin
def _update_image(self, plot_object, config):
z = config['z']
img = plot_object['image']
hist = plot_object['hist']
scales = plot_object['scales']
# make sure z is a *new* numpy float array (pyqtgraph barfs on ints),
# and replace nan with minimum val bcs I can't figure out how to make
# pyqtgraph handle nans - though the source does hint at a way:
# http://www.pyqtgraph.org/documentation/_modules/pyqtgraph/widgets/ColorMapWidget.html
# see class RangeColorMapItem
z = np.asfarray(z).T
with warnings.catch_warnings():
warnings.simplefilter('error')
try:
z_range = (np.nanmin(z), np.nanmax(z))
except:
# we get a warning here when z is entirely NaN
# nothing to plot, so give up.
return
z[np.where(np.isnan(z))] = z_range[0]
hist_range = hist.getLevels()
if hist_range == plot_object['histlevels']:
plot_object['histlevels'] = z_range
hist.setLevels(*z_range)
hist_range = z_range
img.setImage(self._clean_array(z), levels=hist_range)
scales_changed = False
for axletter, axscale in scales.items():
if axscale.revisit:
axdata = config.get(axletter, None)
newscale = self._get_transform(axdata)
if (newscale.translate != axscale.translate or
newscale.scale != axscale.scale):
scales_changed = True
scales[axletter] = newscale
if scales_changed:
img.resetTransform()
img.translate(scales['x'].translate, scales['y'].translate)
img.scale(scales['x'].scale, scales['y'].scale)
def _update_cmap(self, plot_object):
0
Source : _differentialevolution.py
with MIT License
from buds-lab
with MIT License
from buds-lab
def init_population_array(self, init):
"""
Initialises the population with a user specified population.
Parameters
----------
init : np.ndarray
Array specifying subset of the initial population. The array should
have shape (M, len(x)), where len(x) is the number of parameters.
The population is clipped to the lower and upper bounds.
"""
# make sure you're using a float array
popn = np.asfarray(init)
if (np.size(popn, 0) < 5 or
popn.shape[1] != self.parameter_count or
len(popn.shape) != 2):
raise ValueError("The population supplied needs to have shape"
" (M, len(x)), where M > 4.")
# scale values and clip to bounds, assigning to population
self.population = np.clip(self._unscale_parameters(popn), 0, 1)
self.num_population_members = np.size(self.population, 0)
self.population_shape = (self.num_population_members,
self.parameter_count)
# reset population energies
self.population_energies = (np.ones(self.num_population_members) *
np.inf)
# reset number of function evaluations counter
self._nfev = 0
@property
0
Source : _differentialevolution.py
with Apache License 2.0
from dashanji
with Apache License 2.0
from dashanji
def init_population_array(self, init):
"""
Initializes the population with a user specified population.
Parameters
----------
init : np.ndarray
Array specifying subset of the initial population. The array should
have shape (M, len(x)), where len(x) is the number of parameters.
The population is clipped to the lower and upper bounds.
"""
# make sure you're using a float array
popn = np.asfarray(init)
if (np.size(popn, 0) < 5 or
popn.shape[1] != self.parameter_count or
len(popn.shape) != 2):
raise ValueError("The population supplied needs to have shape"
" (M, len(x)), where M > 4.")
# scale values and clip to bounds, assigning to population
self.population = np.clip(self._unscale_parameters(popn), 0, 1)
self.num_population_members = np.size(self.population, 0)
self.population_shape = (self.num_population_members,
self.parameter_count)
# reset population energies
self.population_energies = np.full(self.num_population_members,
np.inf)
# reset number of function evaluations counter
self._nfev = 0
@property
0
Source : differential_evolution.py
with MIT License
from DebangLi
with MIT License
from DebangLi
def init_population_array(self, init):
"""
Initialises the population with a user specified population.
Parameters
----------
init : np.ndarray
Array specifying subset of the initial population. The array should
have shape (M, len(x)), where len(x) is the number of parameters.
The population is clipped to the lower and upper `bounds`.
"""
# make sure you're using a float array
popn = np.asfarray(init)
if (np.size(popn, 0) < 5 or
popn.shape[1] != self.parameter_count or
len(popn.shape) != 2):
raise ValueError("The population supplied needs to have shape"
" (M, len(x)), where M > 4.")
# scale values and clip to bounds, assigning to population
self.population = np.clip(self._unscale_parameters(popn), 0, 1)
self.num_population_members = np.size(self.population, 0)
self.population_shape = (self.num_population_members,
self.parameter_count)
# reset population energies
self.population_energies = (np.ones(self.num_population_members) *
np.inf)
# reset number of function evaluations counter
self._nfev = 0
@property
0
Source : rank.py
with MIT License
from deeplearningbrasil
with MIT License
from deeplearningbrasil
def dcg_at_k(r, k, method=0):
"""Score is discounted cumulative gain (dcg)
Relevance is positive real values. Can use binary
as the previous methods.
Example from
http://www.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf
>>> r = [3, 2, 3, 0, 0, 1, 2, 2, 3, 0]
>>> dcg_at_k(r, 1)
3.0
>>> dcg_at_k(r, 1, method=1)
3.0
>>> dcg_at_k(r, 2)
5.0
>>> dcg_at_k(r, 2, method=1)
4.2618595071429155
>>> dcg_at_k(r, 10)
9.6051177391888114
>>> dcg_at_k(r, 11)
9.6051177391888114
Args:
r: Relevance scores (list or numpy) in rank order
(first element is the first item)
k: Number of results to consider
method: If 0 then weights are [1.0, 1.0, 0.6309, 0.5, 0.4307, ...]
If 1 then weights are [1.0, 0.6309, 0.5, 0.4307, ...]
Returns:
Discounted cumulative gain
"""
r = np.asfarray(r)[:k]
if r.size:
if method == 0:
return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1)))
elif method == 1:
return np.sum(r / np.log2(np.arange(2, r.size + 2)))
else:
raise ValueError("method must be 0 or 1.")
return 0.0
def ndcg_at_k(r, k, method=0):
0
Source : stats.py
with GNU Lesser General Public License v3.0
from deeptime-ml
with GNU Lesser General Public License v3.0
from deeptime-ml
def evaluate_samples(samples: Iterable[Any], quantity: str, delimiter: str = '/', *args, **kwargs) -> List[Any]:
r"""Evaluate a quantity (like a property, function, attribute) of an iterable of objects and return the result.
Parameters
----------
samples : list of object
The samples which contain sought after quantities.
quantity : str, optional, default=None
Name of attribute, which will be evaluated on samples. If None, no quantity is evaluated and the samples
are assumed to already be the quantity that is to be evaluated.
delimiter : str, optional, default='/'
Separator to call members of members.
*args
pass through
**kwargs
pass through
Returns
-------
result : list of any or ndarray
The collected data, if it can be converted to numpy array then numpy array.
"""
if quantity is not None and delimiter in quantity:
qs = quantity.split(delimiter)
quantity = qs[-1]
for q in qs[:-1]:
samples = [call_member(s, q) for s in samples]
if quantity is not None:
samples = [call_member(s, quantity, *args, **kwargs) for s in samples]
try:
samples = np.asfarray(samples)
except:
pass
return samples
class QuantityStatistics:
0
Source : get_overall_ClasswiseIou_ndcg.py
with MIT License
from dips4717
with MIT License
from dips4717
def dcg_at_k(r, k, method=1):
r = np.asfarray(r)[:k]
return np.sum(r / np.log2(np.arange(2, r.size + 2)))
def ndcg_at_k(r, k):
0
Source : rank_metrics.py
with MIT License
from dips4717
with MIT License
from dips4717
def dcg_at_k(r, k, method=0):
"""Score is discounted cumulative gain (dcg)
Relevance is positive real values. Can use binary
as the previous methods.
Example from
http://www.stanford.edu/class/cs276/handouts/EvaluationNew-handout-6-per.pdf
>>> r = [3, 2, 3, 0, 0, 1, 2, 2, 3, 0]
>>> dcg_at_k(r, 1)
3.0
>>> dcg_at_k(r, 1, method=1)
3.0
>>> dcg_at_k(r, 2)
5.0
>>> dcg_at_k(r, 2, method=1)
4.2618595071429155
>>> dcg_at_k(r, 10)
9.6051177391888114
>>> dcg_at_k(r, 11)
9.6051177391888114
Args:
r: Relevance scores (list or numpy) in rank order
(first element is the first item)
k: Number of results to consider
method: If 0 then weights are [1.0, 1.0, 0.6309, 0.5, 0.4307, ...]
If 1 then weights are [1.0, 0.6309, 0.5, 0.4307, ...]
Returns:
Discounted cumulative gain
"""
r = np.asfarray(r)[:k]
if r.size:
if method == 0:
return r[0] + np.sum(r[1:] / np.log2(np.arange(2, r.size + 1)))
elif method == 1:
return np.sum(r / np.log2(np.arange(2, r.size + 2)))
else:
raise ValueError('method must be 0 or 1.')
return 0.
def ndcg_at_k(r, k, method=0):
0
Source : complexity.py
with MIT License
from ealcobaca
with MIT License
from ealcobaca
def ft_n1(
cls,
N: np.ndarray,
y: np.ndarray,
metric: str = "minkowski",
p: t.Union[int, float] = 2,
N_scaled: t.Optional[np.ndarray] = None,
norm_dist_mat: t.Optional[np.ndarray] = None,
) -> float:
"""Compute the fraction of borderline points.
This measure is in [0, 1] range.
Parameters
----------
N : :obj:`np.ndarray`
Numerical fitted data.
y : :obj:`np.ndarray`
Target attribute.
metric : str, optional
Metric used to calculate the distances between the instances.
Check the ``scipy.spatial.distance.cdist`` documentation to
get a list of all available metrics. This argument is used
only if ``norm_dist_mat`` is None.
p : int, optional
Power parameter for the Minkowski metric. When p = 1, this is
equivalent to using Manhattan distance (l1), and Euclidean
distance (l2) for p = 2. For arbitrary p, Minkowski distance
(l_p) is used. Used only if ``norm_dist_mat`` is None.
N_scaled : :obj:`np.ndarray`, optional
Numerical data ``N`` with each feature normalized in [0, 1]
range. Used only if ``norm_dist_mat`` is None. Used to take
advantage of precomputations.
norm_dist_mat : :obj:`np.ndarray`, optional
Square matrix with the pairwise distances between each
instance in ``N_scaled``, i.e., between the normalized
instances. Used to take advantage of precomputations.
Returns
-------
float
Fraction of borderline points.
References
----------
.. [1] Ana C. Lorena, Luís P. F. Garcia, Jens Lehmann, Marcilio C. P.
Souto, and Tin K. Ho. How Complex is your classification problem?
A survey on measuring classification complexity (V2). (2019)
(Cited on page 9-10). Published in ACM Computing Surveys (CSUR),
Volume 52 Issue 5, October 2019, Article No. 107.
"""
if norm_dist_mat is None:
norm_dist_mat, _, _ = cls._calc_norm_dist_mat(
N=N, metric=metric, p=p, N_scaled=N_scaled
)
_norm_dist_mat = np.asfarray(norm_dist_mat)
# Compute the minimum spanning tree using Kruskal's Minimum
# Spanning Tree algorithm.
# Note: in the paper, the authors used Prim's algorithm.
# Our implementation may change it in a future version due to
# time complexity advantages of Prim's algorithm in this context.
mst = scipy.sparse.csgraph.minimum_spanning_tree(
csgraph=np.triu(_norm_dist_mat, k=1), overwrite=True
)
node_id_i, node_id_j = np.nonzero(mst)
# Which edges have nodes with different class
which_have_diff_cls = y[node_id_i] != y[node_id_j]
# Number of vertices connected with different classes
borderline_inst_num = np.unique(
np.concatenate(
[
node_id_i[which_have_diff_cls],
node_id_j[which_have_diff_cls],
]
)
).size
n1 = borderline_inst_num / y.size
return n1
@classmethod
0
Source : complexity.py
with MIT License
from ealcobaca
with MIT License
from ealcobaca
def ft_lsc(
cls,
N: np.ndarray,
y: np.ndarray,
metric: str = "minkowski",
p: t.Union[int, float] = 2,
cls_inds: t.Optional[np.ndarray] = None,
N_scaled: t.Optional[np.ndarray] = None,
norm_dist_mat: t.Optional[np.ndarray] = None,
nearest_enemy_dist: t.Optional[np.ndarray] = None,
) -> float:
"""Local set average cardinality.
The Local-Set (LS) of an example `x_i` in a dataset ``N`` is
defined as the set of points from ``N`` whose distance to `x_i`
is smaller than the distance from `x_i` and its nearest enemy
(the nearest instance from a distinct class of `x_i`.)
The cardinality of the LS of an example indicates its proximity
to the decision boundary and also the narrowness of the gap
between the classes.
This measure is in [0, 1 - 1/n] range, where `n` is the number
of instances in ``N``.
Parameters
----------
N : :obj:`np.ndarray`
Numerical fitted data.
y : :obj:`np.ndarray`
Target attribute.
metric : str, optional
Metric used to calculate the distances between the instances.
Check the ``scipy.spatial.distance.cdist`` documentation to
get a list of all available metrics. This argument is used
only if ``norm_dist_mat`` is None.
p : int, optional
Power parameter for the Minkowski metric. When p = 1, this is
equivalent to using Manhattan distance (l1), and Euclidean
distance (l2) for p = 2. For arbitrary p, Minkowski distance
(l_p) is used. Used only if ``norm_dist_mat`` is None.
cls_inds : :obj:`np.ndarray`, optional
Boolean array which indicates the examples of each class.
The rows corresponds to each distinct class, and the columns
corresponds to the instances. Used only if the argument
``nearest_enemy_dist`` is None.
N_scaled : :obj:`np.ndarray`, optional
Numerical data ``N`` with each feature normalized in [0, 1]
range. Used only if ``norm_dist_mat`` is None. Used to take
advantage of precomputations.
norm_dist_mat : :obj:`np.ndarray`, optional
Square matrix with the pairwise distances between each
instance in ``N_scaled``, i.e., between the normalized
instances. Used to take advantage of precomputations.
nearest_enemy_dist : :obj:`np.ndarray`, optional
Distance of each instance to its nearest enemy (instances
of a distinct class.)
Returns
-------
float
Local set average cardinality.
References
----------
.. [1] Ana C. Lorena, Luís P. F. Garcia, Jens Lehmann, Marcilio C. P.
Souto, and Tin K. Ho. How Complex is your classification problem?
A survey on measuring classification complexity (V2). (2019)
(Cited on page 15). Published in ACM Computing Surveys (CSUR),
Volume 52 Issue 5, October 2019, Article No. 107.
.. [2] Enrique Leyva, Antonio González, and Raúl Pérez. A set of
complexity measures designed for applying meta-learning to instance
selection. IEEE Transactions on Knowledge and Data Engineering,
27(2):354–367, 2014.
"""
if norm_dist_mat is None:
norm_dist_mat, _, _ = cls._calc_norm_dist_mat(
N=N, metric=metric, p=p, N_scaled=N_scaled
)
_norm_dist_mat = np.asfarray(norm_dist_mat)
if nearest_enemy_dist is None:
if cls_inds is None:
cls_inds = _utils.calc_cls_inds(y)
nearest_enemy_dist, _ = cls._calc_nearest_enemies(
norm_dist_mat=_norm_dist_mat,
cls_inds=cls_inds,
)
lsc = 1.0 - np.sum(_norm_dist_mat < nearest_enemy_dist) / (y.size ** 2)
return lsc
@classmethod
0
Source : complexity.py
with MIT License
from ealcobaca
with MIT License
from ealcobaca
def ft_cls_coef(
cls,
N: np.ndarray,
y: np.ndarray,
radius: t.Union[int, float] = 0.15,
metric: str = "minkowski",
p: t.Union[int, float] = 2,
cls_inds: t.Optional[np.ndarray] = None,
N_scaled: t.Optional[np.ndarray] = None,
norm_dist_mat: t.Optional[np.ndarray] = None,
) -> float:
"""Clustering coefficient.
The clustering coefficient of a vertex `v_i` is given by the
ratio of the number of edges between its neighbors (in a Same-class
Radius Neighbor Graph) and the maximum number of edges that could
possibly exist between them.
This measure is in [0, 1] range.
Parameters
----------
N : :obj:`np.ndarray`
Numerical fitted data.
y : :obj:`np.ndarray`
Target attribute.
radius : float or int, optional
Maximum distance between each pair of instances of the same
class to both be considered neighbors of each other. Note that
each feature of ``N`` is first normalized into the [0, 1]
range before the neighbor calculations.
metric : str, optional
Metric used to calculate the distances between the instances.
Check the ``scipy.spatial.distance.cdist`` documentation to
get a list of all available metrics. This argument is used
only if ``norm_dist_mat`` is None.
p : int, optional
Power parameter for the Minkowski metric. When p = 1, this is
equivalent to using Manhattan distance (l1), and Euclidean
distance (l2) for p = 2. For arbitrary p, Minkowski distance
(l_p) is used. Used only if ``norm_dist_mat`` is None.
cls_inds : :obj:`np.ndarray`, optional
Boolean array which indicates the examples of each class.
The rows corresponds to each distinct class, and the columns
corresponds to the instances.
N_scaled : :obj:`np.ndarray`, optional
Numerical data ``N`` with each feature normalized in [0, 1]
range. Used only if ``norm_dist_mat`` is None. Used to take
advantage of precomputations.
norm_dist_mat : :obj:`np.ndarray`, optional
Square matrix with the pairwise distances between each
instance in ``N_scaled``, i.e., between the normalized
instances. Used to take advantage of precomputations.
Returns
-------
float
Clustering coefficient of given data.
References
----------
.. [1] Ana C. Lorena, Luís P. F. Garcia, Jens Lehmann, Marcilio C. P.
Souto, and Tin K. Ho. How Complex is your classification problem?
A survey on measuring classification complexity (V2). (2019)
(Cited on page 9). Published in ACM Computing Surveys (CSUR),
Volume 52 Issue 5, October 2019, Article No. 107.
"""
if cls_inds is None:
cls_inds = _utils.calc_cls_inds(y)
if norm_dist_mat is None:
norm_dist_mat, _, _ = cls._calc_norm_dist_mat(
N=N, metric=metric, p=p, N_scaled=N_scaled
)
_norm_dist_mat = np.asfarray(norm_dist_mat)
# Note: -1 to discount self-loops
neighbor_edges = np.full(y.size, fill_value=-1, dtype=int)
for inds_cur_cls in cls_inds:
dist_mat_subset = _norm_dist_mat[inds_cur_cls, :][:, inds_cur_cls]
neigh_num = np.sum(dist_mat_subset < radius, axis=1)
neighbor_edges[inds_cur_cls] += neigh_num
# Note: also including the node itself to calculate the total
# possible edges between 'k' nodes, as the formula presented
# in the original paper is not supposed to work with only the
# number of the node neighbors, as the paper seems to claim.
total_nodes = np.sum(_norm_dist_mat < radius, axis=1)
cls_coef = 1.0 - 2.0 * float(
np.mean(neighbor_edges / (1e-8 + total_nodes * (total_nodes - 1)))
)
# Note: the R mfe implementation calculates cls_coef as:
# cls_coef = 1 - transitivity(g), like the code below:
# import networkx
# adj_mat = ... # Calculate adjacency matrix
# g = networkx.Graph(adj_mat)
# cls_coef = 1 - networkx.transitivity(g)
# print(cls_coef)
# Whether one implementation should be preferred over another is
# up to discussion.
return cls_coef
@classmethod
0
Source : complexity.py
with MIT License
from ealcobaca
with MIT License
from ealcobaca
def ft_hubs(
cls,
N: np.ndarray,
y: np.ndarray,
radius: t.Union[int, float] = 0.15,
metric: str = "minkowski",
p: t.Union[int, float] = 2,
cls_inds: t.Optional[np.ndarray] = None,
N_scaled: t.Optional[np.ndarray] = None,
norm_dist_mat: t.Optional[np.ndarray] = None,
) -> np.ndarray:
"""Hub score.
The hub score scores each node by the number of connections it
has to other nodes, weighted by the number of connections these
neighbors have.
The values of node hub score are given by the principal eigenvector
of (A.t * A), where A is the adjacency matrix of the graph.
The average value of this measure is in [0, 1] range.
Parameters
----------
N : :obj:`np.ndarray`
Numerical fitted data.
y : :obj:`np.ndarray`
Target attribute.
radius : float or int, optional
Maximum distance between each pair of instances of the same
class to both be considered neighbors of each other. Note that
each feature of ``N`` is first normalized into the [0, 1]
range before the neighbor calculations.
metric : str, optional
Metric used to calculate the distances between the instances.
Check the ``scipy.spatial.distance.cdist`` documentation to
get a list of all available metrics. This argument is used
only if ``norm_dist_mat`` is None.
p : int, optional
Power parameter for the Minkowski metric. When p = 1, this is
equivalent to using Manhattan distance (l1), and Euclidean
distance (l2) for p = 2. For arbitrary p, Minkowski distance
(l_p) is used. Used only if ``norm_dist_mat`` is None.
cls_inds : :obj:`np.ndarray`, optional
Boolean array which indicates the examples of each class.
The rows corresponds to each distinct class, and the columns
corresponds to the instances.
N_scaled : :obj:`np.ndarray`, optional
Numerical data ``N`` with each feature normalized in [0, 1]
range. Used only if ``norm_dist_mat`` is None. Used to take
advantage of precomputations.
norm_dist_mat : :obj:`np.ndarray`, optional
Square matrix with the pairwise distances between each
instance in ``N_scaled``, i.e., between the normalized
instances. Used to take advantage of precomputations.
Returns
-------
:obj:`np.ndarray`
Complement of the hub score of every node.
References
----------
.. [1] Ana C. Lorena, Luís P. F. Garcia, Jens Lehmann, Marcilio C. P.
Souto, and Tin K. Ho. How Complex is your classification problem?
A survey on measuring classification complexity (V2). (2019)
(Cited on page 9). Published in ACM Computing Surveys (CSUR),
Volume 52 Issue 5, October 2019, Article No. 107.
"""
if cls_inds is None:
cls_inds = _utils.calc_cls_inds(y)
if norm_dist_mat is None:
norm_dist_mat, _, _ = cls._calc_norm_dist_mat(
N=N, metric=metric, p=p, N_scaled=N_scaled
)
_norm_dist_mat = np.asfarray(norm_dist_mat)
adj_mat = np.zeros_like(_norm_dist_mat, dtype=_norm_dist_mat.dtype)
for inds_cur_cls in cls_inds:
_inds = np.flatnonzero(
inds_cur_cls
) # type: t.Union[t.Tuple, np.ndarray]
_inds = tuple(np.meshgrid(_inds, _inds, copy=False, sparse=True))
dist_mat_subset = _norm_dist_mat[_inds]
adj_mat[_inds] = dist_mat_subset * (dist_mat_subset < radius)
# Note: 'adj_mat' is symmetric because the underlying graph is
# undirected. Also, A^t * A = A * A^t, with 'A' = adj_mat.
_, eigvecs = np.linalg.eigh(np.dot(adj_mat.T, adj_mat))
# Note: in the reference paper, it is used the average value of
# the principal eigenvector. However, to enable summarization,
# and just like the R mfe package, here we chose to return all
# complement of the hub scores for every node.
hubs = 1.0 - np.abs(eigvecs[:, -1])
return hubs
0
Source : statistical.py
with MIT License
from ealcobaca
with MIT License
from ealcobaca
def ft_max(cls, N: np.ndarray) -> np.ndarray:
"""Compute the maximum value from each attribute.
Parameters
----------
N : :obj:`np.ndarray`
Fitted numerical data.
Returns
-------
:obj:`np.ndarray`
Attribute maximum values.
References
----------
.. [1] Robert Engels and Christiane Theusinger. Using a data metric for
preprocessing advice for data mining applications. In 13th European
Conference on on Artificial Intelligence (ECAI), pages 430 – 434,
1998.
"""
return np.asfarray(N.max(axis=0))
@classmethod
0
Source : statistical.py
with MIT License
from ealcobaca
with MIT License
from ealcobaca
def ft_mean(cls, N: np.ndarray) -> np.ndarray:
"""Compute the mean value of each attribute.
Parameters
----------
N : :obj:`np.ndarray`
Fitted numerical data.
Returns
-------
:obj:`np.ndarray`
Attribute mean values.
References
----------
.. [1] Robert Engels and Christiane Theusinger. Using a data metric for
preprocessing advice for data mining applications. In 13th European
Conference on on Artificial Intelligence (ECAI), pages 430 – 434,
1998.
"""
return np.asfarray(N.mean(axis=0))
@classmethod
0
Source : statistical.py
with MIT License
from ealcobaca
with MIT License
from ealcobaca
def ft_min(cls, N: np.ndarray) -> np.ndarray:
"""Compute the minimum value from each attribute.
Parameters
----------
N : :obj:`np.ndarray`
Fitted numerical data.
Returns
-------
:obj:`np.ndarray`
Attribute minimum values.
References
----------
.. [1] Robert Engels and Christiane Theusinger. Using a data metric for
preprocessing advice for data mining applications. In 13th European
Conference on on Artificial Intelligence (ECAI), pages 430 – 434,
1998.
"""
return np.asfarray(N.min(axis=0))
@classmethod
0
Source : statistical.py
with MIT License
from ealcobaca
with MIT License
from ealcobaca
def ft_sd(cls, N: np.ndarray, ddof: int = 1) -> np.ndarray:
"""Compute the standard deviation of each attribute.
Parameters
----------
N : :obj:`np.ndarray`
Fitted numerical data.
ddof : float, optional
Degrees of freedom for standard deviation.
Returns
-------
:obj:`np.ndarray`
Attribute standard deviations.
References
----------
.. [1] Robert Engels and Christiane Theusinger. Using a data metric for
preprocessing advice for data mining applications. In 13th European
Conference on on Artificial Intelligence (ECAI), pages 430 – 434,
1998.
"""
return np.asfarray(N.std(axis=0, ddof=ddof))
@classmethod
0
Source : statistical.py
with MIT License
from ealcobaca
with MIT License
from ealcobaca
def ft_var(cls, N: np.ndarray, ddof: int = 1) -> np.ndarray:
"""Compute the variance of each attribute.
Parameters
----------
N : :obj:`np.ndarray`
Fitted numerical data.
ddof : float, optional
Degrees of freedom for variance.
Returns
-------
:obj:`np.ndarray`
Attribute variances.
References
----------
.. [1] Ciro Castiello, Giovanna Castellano, and Anna Maria Fanelli.
Meta-data: Characterization of input features for meta-learning.
In 2nd International Conference on Modeling Decisions for
Artificial Intelligence (MDAI), pages 457–468, 2005.
"""
return np.asfarray(N.var(axis=0, ddof=ddof))
@classmethod
See More Examples