How to use step_init method in autotest

Best Python code snippet using autotest_python

st-DBSCAN.py

Source:st-DBSCAN.py Github

copy

Full Screen

1import numpy as np2from scipy.spatial.distance import pdist, squareform3from sklearn.cluster import DBSCAN4from sklearn.utils import check_array5from ML import calculate_rand_score, stock_labels6from utils import get_dataset_steps_positions_velocities_headings7def get_mean_score(name_file):8 try:9 with open(name_file):10 score = np.mean(np.array(np.loadtxt(name_file), dtype=float))11 return score12 except IOError:13 print("Could not open file {0}".format(name_file))14 exit()15class ST_DBSCAN:16 """17 ST_DBSCAN class for clustering18 ref19 - ST-DBSCAN: An algorithm for clustering spatial–temporal data20 Derya Birant, Alp Kut21 ----------22 :param eps1: float, the density threshold for spatial neighborhood23 :param eps2: float, The temporal threshold for temporal neighborhood24 :param min_samples: The number of samples required for an object to be a core point.25 :param metric_1: string, metric for spatial neighborhood26 :param metric_2: string, metric for temporal neighborhood27 string default='euclidean', can also be a custom function28 The used distance metric - more options are29 ‘braycurtis’, ‘canberra’, ‘chebyshev’, ‘cityblock’, ‘correlation’,30 ‘cosine’, ‘dice’, ‘euclidean’, ‘hamming’, ‘jaccard’, ‘jensenshannon’,31 ‘kulsinski’, ‘mahalanobis’, ‘matching’, ‘rogerstanimoto’, ‘sqeuclidean’,32 ‘russellrao’, ‘seuclidean’, ‘sokalmichener’, ‘sokalsneath’, ‘yule’.33 :param indices_1: list of column indices where spatial attributes are situated in the data34 :param indices_2: list of column indices where non-spatial attributes are situated in the data35 """36 def __init__(self,37 eps1,38 eps2,39 min_samples,40 indices_1,41 indices_2,42 metric_1='euclidean',43 metric_2='euclidean',44 ):45 self.eps1 = eps146 self.eps2 = eps247 self.indices_1 = indices_148 self.indices_2 = indices_249 self.min_samples = min_samples50 self.metric_1 = metric_151 self.metric_2 = metric_252 self.labels = None53 assert self.eps1 > 0, 'eps1 must be positive'54 assert self.eps2 > 0, 'eps2 must be positive'55 assert type(self.min_samples) == int, 'min_samples must be a positive integer'56 assert self.min_samples > 0, 'min_samples must be a positive integer'57 def fit(self, X):58 # check if input is correct59 X = check_array(X)60 if not self.eps1 > 0.0 or not self.eps2 > 0.0 or not self.min_samples > 0.0:61 raise ValueError('eps1, eps2, minPts must be positive')62 # Compute squared distance matrix for63 non_spatial_square_dist_matrix = pdist(X[:, self.indices_1], metric=self.metric_1)64 spatial_square_dist_matrix = pdist(X[:, self.indices_2], metric=self.metric_2)65 # filter the euc_dist matrix using the time_dist66 dist = np.where(non_spatial_square_dist_matrix <= self.eps1, spatial_square_dist_matrix, 10 * self.eps2)67 db = DBSCAN(eps=self.eps2,68 min_samples=self.min_samples,69 metric='precomputed')70 db.fit(squareform(dist))71 self.labels = db.labels_72 def stock_labels_to_directory(self, directory, nb_obs, step_init, step_end):73 true_steps = np.arange(step_init, step_end)74 steps = np.arange(0, step_end - step_init)75 ind_init = steps[0]76 # for each step77 for (step, step_true) in zip(steps, true_steps):78 # get all the observations of step i79 ind_to_get = np.arange(ind_init, ind_init + nb_obs)80 label_step = self.labels[ind_to_get]81 stock_labels(label_step, step_true, repository=directory,82 filename="ST_DBSCAN_eps1=" + str(self.eps1) + "eps2="83 + str(self.eps2) + "Nsample="84 + str(self.min_samples) + "label")85 ind_init += nb_obs86 def generate_results(self, directory, step_init, step_end):87 steps = list(np.arange(step_init, step_end)) # steps to take into account in the calculation88 filename_true = "ground_truth_label" # file name for ground-truth (see for example file_name89 # argument in stock_file function in build_ground_truth function in module ML.py)90 filename_pred = "ST_DBSCAN_eps1=" + str(self.eps1) + "eps2=" + \91 str(self.eps2) + "Nsample=" + str(self.min_samples) + "label"92 score_mean = calculate_rand_score(steps, directory, filename_true, filename_pred)93 return score_mean94def split_data(data, n_indiv, time_step):95 list_data = list()96 for i in np.arange(0, data.shape[0], n_indiv * time_step):97 list_data.append(data[i:i + time_step * n_indiv, :])98 return list_data99if __name__ == "__main__":100 n_indiv = 120101 n_time_step = 3102 directory = "simulation_data/"103 step_init = 300104 step_end = 500105 split = False # True if we split the data in sequences of n_time_step, False else106 # build the dataset107 data = get_dataset_steps_positions_velocities_headings(step_init, step_end, n_indiv, directory)108 # split the dateset to have series of n_time_step times-steps with positions for each boids109 list_data = split_data(data, n_indiv, n_time_step)110 # parameters111 eps1 = [3]112 eps2 = [85]113 min_samples = [5]114 # eps1 = 2 -> eps2=80, min_sample=5 best found115 # eps1 = 1 -> eps2=80, min_sample=5 best found116 # eps1 = 3 -> eps2=80, min_sample=5 best found117 results = list()118 param_list = list()119 # test each parameters120 if split:121 for eps_1 in eps1:122 for eps_2 in eps2:123 for min_sample in min_samples:124 # we test on 10 sets of n_time_frame samples125 mean_res = list()126 for i in range(40):127 st_dbscan = ST_DBSCAN(eps_1, eps_2, min_sample, [0], [1, 2])128 st_dbscan.fit(list_data[i])129 st_dbscan.stock_labels_to_directory(directory=directory,130 nb_obs=n_indiv,131 step_init=step_init + i * n_time_step,132 step_end=step_init + (i + 1) * n_time_step)133 ari_score = st_dbscan.generate_results(directory=directory,134 step_init=step_init + i * n_time_step,135 step_end=step_init + (i + 1) * n_time_step)136 mean_res.append(ari_score)137 print("eps1: ", eps_1)138 print("eps2: ", eps_2)139 print("min_sample", min_sample)140 results.append(np.mean(mean_res))141 param_list.append([eps_1, eps_2, min_sample])142 else:143 for eps_1 in eps1:144 for eps_2 in eps2:145 for min_sample in min_samples:146 st_dbscan = ST_DBSCAN(eps_1, eps_2, min_sample, [0], [1, 2])147 st_dbscan.fit(data)148 st_dbscan.stock_labels_to_directory(directory=directory,149 nb_obs=n_indiv,150 step_init=step_init,151 step_end=step_end)152 print("eps1: ", eps_1)153 print("eps2: ", eps_2)154 print("min_sample", min_sample)155 ari_score = st_dbscan.generate_results(directory=directory,156 step_init=step_init,157 step_end=step_end)158 results.append(ari_score)159 param_list.append([eps_1, eps_2, min_sample])160 print("best score from all {0} trial(s): {1}".format(len(results),161 results[np.argmax(results)]))162 print("best parameters from all {0} "...

Full Screen

Full Screen

opti.py

Source:opti.py Github

copy

Full Screen

1#!/usr/bin/env python2# -*- coding: utf-8 -*-3"""4This module is an simple optimization module that helps find the point of 5maximum value for a given function.6"""7from sage.all import *8def optimize(func, args_init, step_init, step_min, iter_max, verbose=False):9 r"""10 Optimization function finding the maximum reaching coordinates for ``func``11 with a random walk. 12 13 :param function func: The function to be optimized, each of its arguments must 14 be numerical and will be tweaked to find ``func``'s maximum. 15 :param tuple args_init: Initial coordinates for the random walk. 16 :param real step_init: The step size will vary with time in this function, so17 this is the initial value for the step size. 18 :param real step_min: The limit size for the step.19 :param int iter_max: Upper iterations bound for each loop to avoid infinite 20 loops.21 :param bool verbose: If ``verbose`` then extra run information will be 22 displayed in terminal.23 :returns: vector, complex -- The coordinates of the optimum found for 24 ``func`` and the value of ``func`` at this point.25 """26 solution = vector(CC, args_init).normalized()27 solution_temp = solution28 value = func(solution)29 value_temp = value30 step_current = step_init31 if verbose:32 print("Initial solution : " + func.__name__ + 33 str(solution) + " = " + str(value))34 iter_nb = 035 while step_current > step_min:36 count = 137 while value_temp <= value and count < iter_max:38 direction = vector(39 [uniform(-1,1) for _ in range(len(args_init))]40 ).normalized()41 solution_temp = solution + direction*step_current42 value_temp = func(solution_temp)43 count += 144 if value_temp > value:45 solution = solution_temp46 value = value_temp47 else:48 step_current = step_current/249 50 if verbose:51 print("Final solution : " + func.__name__ + 52 str(solution) + " = " + str(value))53 return (solution, value)54def optimize_normalized(func, normalizer_func, args_init, step_init, step_min, iter_max, verbose=False):55 r"""56 Optimization function finding the maximum reaching coordinates for ``func``57 with a random walk. 58 59 :param function func: The function to be optimized, each of its arguments must 60 be numerical and will be tweaked to find ``func``'s maximum. 61 :param tuple args_init: Initial coordinates for the random walk. 62 :param real step_init: The step size will vary with time in this function, so63 this is the initial value for the step size. 64 :param real step_min: The limit size for the step.65 :param int iter_max: Upper iterations bound for each loop to avoid infinite 66 loops.67 :param bool verbose: If ``verbose`` then extra run information will be 68 displayed in terminal. 69 :returns: vector, complex -- The coordinates of the optimum found for 70 ``func`` and the value of ``func`` at this point.71 """72 solution = vector(CC, normalizer_func(args_init))73 solution_temp = solution74 value = func(solution)75 value_temp = value76 step_current = step_init77 if verbose:78 print("Initial solution : " + func.__name__ + 79 str(solution) + " = " + str(value))80 iter_nb = 081 while step_current > step_min:82 count = 183 while value_temp <= value and count < iter_max:84 direction = vector(85 [uniform(-1,1) for _ in range(len(args_init))]86 ).normalized()87 solution_temp = vector(normalizer_func(solution + direction*step_current))88 value_temp = func(solution_temp)89 count += 190 if value_temp > value:91 solution = solution_temp92 value = value_temp93 else:94 step_current = step_current/295 if verbose:96 print("optimization, current step: " + str(step_current))97 98 if verbose:99 print("Final solution : " + func.__name__ + 100 str(solution) + " = " + str(value))101 return (solution, value)102def optimize_2spheres(func, args_init, step_init, step_min, iter_max, radius=1, verbose = False):103 r"""104 Optimization function finding the maximum reaching coordinates for ``func``105 with a random walk on a two sphere of dimension half the size of 106 ``args_init``. (Work in progress !)107 For now, this function is in project and is not used, it can be ignored.108 109 :param function func: The function to be optimized, each of its arguments must be 110 numerical and will be tweaked to find ``func``'s maximum. 111 :param tuple args_init: Initial coordinates for the random walk.112 :param real step_init: The step size will vary with time in this function, so113 this is the initial value for the step size. 114 :param real step_min: The limit size for the step.115 :param int iter_max: Upper iterations bound for each loop to avoid infinite 116 loops.117 :param real radius: Sphere radius.118 :param bool verbose: If ``verbose`` then extra run information will be displayed in 119 terminal.120 :returns: vector, complex -- The coordianates of the optimum found for 121 ``func`` and the value of ``func`` at this point.122 """123 def unwrap(vector_tuple):124 """Takes in vectors and returns a list of their coefficients125 """126 arguments_unwraped = []127 for vector_instance in vector_tuple:128 for coefficient in vector_instance:129 arguments_unwraped.append(coefficient)130 return arguments_unwraped131 def point_on_cone(cone_center, cone_spherical_radius, sphere_radius): # TODO132 dimension = len(cone_center)133 rotation_cone_center_to_Z = rotation_to_Z(cone_center) # matrix134 point_random_angles = [uniform(-pi,pi) for _ in dimension - 2]135 cone_angle = cone_spherical_radius/sphere_radius136 point = vector_from_pherical(sphere_radius, cone_angle, *point_random_angles)137 point = rotation_cone_center_to_Z.inverse() * point138 return point139 dimension = len(args_init)/2140 solution = vector(CC, args_init[:dimension]).normalized(), \141 vector(CC, args_init[dimension:]).normalized()142 value = func(unwrap(solution))143 value_temp = value144 step_current = step_init145 if verbose:146 print("Initial solution : " + func.__name__ + 147 str(solution) + " = " + str(value))148 iter_nb = 0149 while step_current > step_min:150 solution_temp = solution # initialization151 count = 1152 while CC(value_temp) <= CC(value) and count < iter_max:153 solution_temp = point_on_cone(solution, step_current, 1)154 value_temp = func(unwrap(solution_temp))155 count += 1156 if CC(value_temp) > CC(value):157 solution = solution_temp158 value = value_temp159 else:160 step_current = step_current/2161 162 if verbose:163 print("Final solution : " + func.__name__ + 164 str(solution) + " = " + str(value))...

Full Screen

Full Screen

python_sgd.py

Source:python_sgd.py Github

copy

Full Screen

1import time2import numpy as np3from benchopt.base import BaseSolver4class Solver(BaseSolver):5 name = 'Python-SGD' # stochastic gradient descent6 # any parameter defined here is accessible as a class attribute7 parameters = {'step_init': [1.]}8 def set_objective(self, X, y, lmbd):9 self.X, self.y, self.lmbd = X, y, lmbd10 def init(self):11 # print("---------------------------------- Initializing SGD weights")12 time.sleep(.2)13 n_samples, n_features = self.X.shape14 self.w = np.zeros(n_features)15 def run(self, n_iter):16 n_samples, n_features = self.X.shape17 # w = np.zeros(n_features)18 w = self.w19 # self.step_init = 1e3 # TODO: set as parameter ?20 # idx_samples = np.random.choice(n_samples, n_iter)21 # steps = self.step_init / np.sqrt(1 + np.arange(1, n_iter + 1)) # which decreasing rule for the step size ?22 t_new = 123 for i in range(n_iter):24 # When n_iter is known in advance:25 # idx = idx_samples[i]26 # step = steps[i]27 # When n_iter is NOT known in advance:28 idx = np.random.choice(n_samples)29 step = self.step_init / np.sqrt(1 + i)30 # SGD step31 w -= step * self.grad_i_logreg_l2(w, self.X, self.y, self.lmbd, idx)32 self.w = w33 def grad_i_logreg_l2(self, w, X, y, lmbd, i):34 return self.grad_i_logreg(w, X, y, i) + lmbd * w35 def grad_i_logreg(self, w, X, y, i):36 return - X[i] * y[i] / (1. + np.exp(y[i] * (X[i] @ w)))37 def get_result(self):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful