def mlp_classifier_new(config_mlp, X, y, file): clf = MLPClassifier(hidden_layer_sizes=(config_mlp), max_iter=1000) skf = StratifiedKFold(n_splits=10) skf.get_n_splits(X, y) score = [] f1Score = [] for train_index, test_index in skf.split(X, y): #print("TRAIN:", train_index, "TEST:", test_index) score.append( clf.fit(X[train_index], y[train_index]).score(X[test_index], y[test_index])) #acuracia y_pred_mlp = clf.predict(X[test_index]) f1Score.append(f1_score(y[test_index], y_pred_mlp, average='binary')) print('acurácia: ', fmean(score), " desvio padrão: ", (stdev(score))) print('f1-score ', fmean(f1Score), "desvio padrão: ", (stdev(f1Score))) file.write("\ncamadas: {}\tneuronios: {}\n".format(len(config_mlp), config_mlp[0])) file.write("{}\t{}\n".format(fmean(score), stdev(score))) file.write("{}\t{}\n\n".format(fmean(f1Score), stdev(f1Score)))
def iterate(params): p = params limits = [None, None] data = {} p['percent_diff'] = None p['count'] = 0 while check_completion(p['percent_diff'], p['precision']) == False: p['count'] += 1 results = calculateData(p) data = results[0] p = results[1] p['percent_diff'] = p['Pnf'] / p['Pnm'] if p['percent_diff'] <= 1: limits[0] = p['IC_x'] if limits[1] == None: p['IC_x'] *= 2 else: p['IC_x'] = s.fmean(limits) else: limits[1] = p['IC_x'] if limits[0] == None: p['IC_x'] /= 2 else: p['IC_x'] = s.fmean(limits) pause(limits, data, **p) return data, p
def getMetadataMedia(metadatas): if metadatas['danceability'] is None or len( metadatas['danceability']) == 0: return metadatas # Fazer sistema de rankeamento, pegar o TIPO da musica # Desvio Padrão metadata = {} metadata['danceability'] = "{:.3f}".format( statistics.fmean(metadatas['danceability'])) metadata['energy'] = "{:.3f}".format(statistics.fmean(metadatas['energy'])) metadata['loudness'] = "{:.3f}".format( statistics.fmean(metadatas['loudness'])) metadata['mode'] = statistics.mode(metadatas['mode']) metadata['speechiness'] = "{:.3f}".format( statistics.fmean(metadatas['speechiness'])) metadata['acousticness'] = "{:.3f}".format( statistics.fmean(metadatas['acousticness'])) metadata['instrumentalness'] = "{:.3f}".format( statistics.fmean(metadatas['instrumentalness'])) metadata['liveness'] = "{:.3f}".format( statistics.fmean(metadatas['liveness'])) metadata['valence'] = "{:.3f}".format( statistics.fmean(metadatas['valence'])) metadata['tempo'] = "{:.3f}".format(statistics.fmean(metadatas['tempo'])) metadata['duration_ms'] = "{:.3f}".format( statistics.fmean(metadatas['duration_ms'])) return metadata
def log_evaluation(self, population: 'Population', **kwargs): count_penalization = 0 partitions = list() fitnesses = list() costs = list() generation = population.generation for individual in population: qtd = individual.qtd_partitions if qtd > 1: count_penalization += 1 partitions.append(qtd) fitnesses.append(individual.fitness) costs.append(individual.cost) currentbest = population.current_best bestoverall = population.documented_best self.log("evaluation", generation, fmean(costs), pvariance(costs), fmean(fitnesses), pvariance(fitnesses), count_penalization, fmean(partitions), pvariance(partitions), currentbest.cost, currentbest.fitness, bestoverall.cost, bestoverall.fitness, bestoverall.last_improvement)
def __str__(self): temp = fmean([fmean(i) for i in self.grades.values() ]) if self.grades.values() else 0 return (f"Имя: {self.name}\n" f"Фамилия: {self.surname}\n" f"Средняя оценка за домашние задания: {temp}\n" f"Курсы в процессе изучения: {self.courses_in_progress}\n" f"Завершенные курсы: {self.finished_courses}")
def __init__(self, DIR): self.DIR = DIR self.edges = purr.readXMLtag(purr.mrf(DIR, r"*.edg.xml"), "edge") self.nodes = purr.readXMLtag(purr.mrf(DIR, r"*.nod.xml"), "node") x = [float(node['x']) for node in self.nodes] y = [float(node['y']) for node in self.nodes] self.centroid = (stats.fmean(x), stats.fmean(y)) self.xMax = max(x) self.xMin = min(x) self.yMax = max(y) self.yMin = min(y) return
def analyze_clusters_intra_distances(evaluation, table_name): clusters_mean_intra_distances = [] clusters_stdev_intra_distances = [] for cluster in evaluation.clusters.items(): clusters_mean_intra_distances.append(cluster[1].intra_dist_mean) clusters_stdev_intra_distances.append(cluster[1].intra_dist_stdev) evaluation.clusters_intra_dist_mean_mean = statistics.fmean( clusters_mean_intra_distances) evaluation.clusters_intra_dist_mean_stdev = statistics.stdev( clusters_mean_intra_distances) evaluation.clusters_intra_dist_stdev_mean = statistics.fmean( clusters_stdev_intra_distances) evaluation.clusters_intra_dist_stdev_stdev = statistics.stdev( clusters_stdev_intra_distances)
def _iavg(*data: _num.Number) -> float: """ Intuitive AVeraGe Returns the best average for the data set Use fraction.Fraction instances for ratios Ignores mode; nobody likes it >>> iavg(3, 4, 5) 4 >>> from fractions import Fraction as F >>> iavg(F(60, 20), F(200, 50), F(5, 1)) Fraction(180, 47) """ if len(data) == 1 and isinstance(data[0], (list, tuple)): data = data[0] target = 1.5 * iqr(*data) q = qdict(*data) for item in data: if item < q['q1'] - target or item > q['q3'] + target: return stat.median(data) elif isinstance(item, fraction.Fraction): return stat.harmonic_mean(data) else: return stat.fmean(data)
def measure_transitive_closure(iterations_num): adj = timeit.repeat( "Utils.get_transitive_closure_adj_matrix(intersection)", setup="from __main__ import Utils, intersection", repeat=iterations_num, number=1) sqr = timeit.repeat( "Utils.get_transitive_closure_squaring(intersection)", setup="from __main__ import Utils, intersection", repeat=iterations_num, number=1) average_adj = round(fmean(adj), 6) average_sqr = round(fmean(sqr), 6) result = str(average_adj) + ' ' + str(average_sqr) return result
def calculate_stats(self, data): dates = [ d.get('date') for d in data ] digital_values = [ d.get('digital_value') for d in data ] timestamps = [ d.get('timestamp') for d in data ] init_date, end_date = min(dates), max(dates) init_timestamp, end_timestamp = min(timestamps), max(timestamps) time_span = end_timestamp - init_timestamp mean_value = fmean(digital_values) n_samples = len(data) sensor = data[0].get('sensor') username = data[0].get('username') ip = data[0].get('ip') measure = data[0].get('measure') return { 'digital_values': digital_values, 'end_date': end_date, 'end_timestamp': end_timestamp, 'init_date': init_date, 'init_timestamp': init_timestamp, 'ip': ip, 'mean_value': round(mean_value, 1), 'measure': measure, 'n_samples': n_samples, 'sensor': sensor, 'time_span': time_span, 'username': username }
def statistics_element(self, element): """ Calcularemos el promedio. Desde values obtendremos la informacion por elemento. Usaremos 3 metodos: default mean, float mean y calculo manual. """ self.response = None self.values = self.values_page(element) if isinstance(self.values.get("data"), dict): self.values_data = self.values.get("data").get("values") self.values_list = [ value for value in self.values_data.values() ] self.lenofvalues = len(self.values_list) self.sumofvalues = sum(self.values_list) self.meanofvalues = fmean(self.values_list) self.fmeanofvalues = mean(self.values_list) self.avgcomputofvalues = float(self.sumofvalues) / self.lenofvalues self.response["data"] = { "avg_stat-dmn": self.meanofvalues, "avg_stat-fmn": self.fmeanofvalues, "avg_comp-ute": self.avgcomputofvalues, "sum": self.sumofvalues, "len": self.lenofvalues } return self.response
def fmean(angka): inp = angka.split(" ") merge = [] for x in inp: merge.append(int(x)) inp = statistics.fmean(merge) print(inp)
def measure_print(iterations_num): pr = timeit.repeat("Utils.print_label_matrices(intersection)", setup="from __main__ import Utils, intersection", repeat=iterations_num, number=1) average = round(fmean(pr), 6) result = str(average) return result
def calc(first, second, third, fourth): """Returns the average meaning from the list and max argument.""" my_list = [first, second, third, fourth] global MAX_NUMBER if max(my_list) > MAX_NUMBER: MAX_NUMBER = max(my_list) average = statistics.fmean(my_list) return average, MAX_NUMBER
def compute_averages(dataMatrix): average = [] for classNum in range(len(dataMatrix)): local_average = [] for i in (np.transpose(dataMatrix[classNum]))[:-1]: mean = stat.fmean(i) local_average.append(mean) average.append(local_average) return average
def seal_file(self): date = self.current_date i = 1 total_list = self.values[self.headline[1]] count = len(total_list) count_monotonic = 0 while i < count: if total_list[i] >= total_list[i - 1]: count_monotonic += 1 i += 1 result = { "test": { "name": "ddl" }, "monotonicity": count_monotonic / len(total_list), "size": { "count": count, "collectionCount": self.collections_per_database, "indexesPercollection": self.indexes_per_collection, "numberOfShards": self.no_shards, "replicationFactor": self.replication_factor }, "configuration": { "version": self.version, "branch": self.branch, "mode": self.mode, "edition": self.edition }, "isoDate": date.isoformat(), "date": date.timestamp(), "ms": 1000 * date.timestamp() } i = 1 while i < len(self.headline): name = self.headline[i] test_values = self.values[name] result['test'][name] = { "average": statistics.fmean(test_values), "median": statistics.median(test_values), "min": min(test_values), "max": max(test_values), "deviation": { "pst": statistics.pstdev(test_values), "pvariance": statistics.pvariance(test_values), "stdev": statistics.stdev(test_values), "variance": statistics.variance(test_values) }, "values": test_values, "numberRuns": count } i += 1 print(json.dumps(result))
def stats(utt_len): moy = stat.fmean(utt_len) median = stat.median(utt_len) var = stat.variance(utt_len) quart1 = np.percentile(utt_len, 25) quart3 = np.percentile(utt_len, 75) printStats(moy, median, var, quart1, quart3) return moy, median, var, quart1, quart3
def measure_intersection(iterations_num): intersection = timeit.repeat( "Utils.get_intersection(graph, automaton)", setup="from __main__ import Utils, graph, automaton", repeat=iterations_num, number=1) average = round(fmean(intersection), 6) result = str(average) return result
def main(): try: data = json.loads(request.data) validator.validate(data) except ValidationError: return jsonify(error=True, msg="JSON data invalid") except: return jsonify(error=True, msg="Invalid POST data") query_types = list(data['query_type'].split('|')) returns = {} filtered_data = df for _filter in data.get('filters', []): if _filter['operand1'] == 'discount': if not isinstance(_filter['operand2'], Number): return jsonify(error=True, msg="Discount percentage is not a number") operator = _filter['operator'] percent = _filter['operand2'] filtered_data = filtered_data[filtered_data.price.apply( filter_by_discount(operator, percent))] elif _filter['operand1'] == 'brand.name': operator = _filter['operator'] if operator != '==': return jsonify( error=True, msg="To filter by brand name, use operator '=='") brand_name = _filter['operand2'] filtered_data = filtered_data[filtered_data.brand.apply( filter_by_brand(brand_name))] if "discounted_products_list" in query_types: returns.update({ 'discounted_products_list': [x['$oid'] for x in filtered_data._id] }) if "avg_discount" in query_types: discounts = filtered_data.price.apply(discount_percentage) avg_discount = fmean(discounts) if len(discounts) > 0 else 0.0 returns.update({'avg_discount': avg_discount}) if "expensive_list" in query_types: expensive_list = filtered_data[filtered_data.apply( filter_by_expensive_product, axis=1)] returns.update( {'expensive_list': [x['$oid'] for x in expensive_list._id]}) return jsonify(returns)
def get_average( self, value, params=None): # returns the average value of a data header set params = params or dict() if value == 'smoker': raise Exception("Boolean value is not valid entry") elif value == 'sex' or value == 'region': raise Exception("String value is not valid entry") return round(statistics.fmean(self.get_data(value, params)), 2)
def get_celery_success(core_id, time_elap, fps_batch, wins, seeker_plot, hiding_plot ): return { 'core_id': core_id, 'time_elapsed': time_elap, 'fps_peak': round(max(fps_batch)), 'fps_lower': round(min(fps_batch)), 'fps_mean': round(statistics.fmean(fps_batch)), 'fps_median': round(statistics.median(fps_batch)), 'fps_quantiles': [round(quantile) for quantile in statistics.quantiles(fps_batch)], 'wins': wins, 'seeker_plot': seeker_plot, 'hiding_plot': hiding_plot, }
def measure_intra_distances(clusters, table_name): for cluster_id in clusters: cluster = clusters[cluster_id] result = DB.exec(f"select Distance from {table_name} " f"where Cluster = {cluster_id}") count = 0 intra_distances = [] for row in result: count += 1 intra_distances.append(row["Distance"]) cluster.intra_dist_mean = statistics.fmean(intra_distances) cluster.intra_dist_stdev = statistics.stdev( intra_distances) if count >= 2 else 0.0
def run_benchmarks(query, repeat=5): results = defaultdict(list) with get_new_connection() as connection: for query in queries: for _ in range(repeat): start = time.perf_counter() run_query_on_connection(connection, query) results[query].append(time.perf_counter() - start) return { key: round(statistics.fmean(val), PRECISION) for key, val in results.items() }
def add_trade(self, time, price: float, volume: float, sell=False): time = time_as_nano(time) if time > self.end_time: return False trade_cost = price * volume self.trade_cnt += 1 self.prices.append(price) self.amounts.append(volume) self.amounts.sort() self.totals.append(trade_cost) self.totals.sort() if self.open == 0 or time < self.last_time: self.open = price if self.close == 0 or time > self.last_time: self.close = price if self.high == 0 or price > self.high: self.high = price if self.low == 0 or price < self.low: self.low = price self.volume += volume if sell: self.sell_totals.append(trade_cost) self.sell_avg = statistics.fmean(self.sell_totals) else: self.buy_totals.append(trade_cost) self.buy_avg = statistics.fmean(self.buy_totals) # calc totals self.trade_med = statistics.median(self.totals) self.trade_avg = statistics.fmean(self.totals) self.price_avg = statistics.fmean(self.prices) self.last_time = time
def summary(self): summary = {} for section, durations in self.sections_duration.items(): p80, p95 = self.percentiles(durations) summary[section] = { 'count': len(durations), 'min': round(min(durations) * 1000), 'max': round(max(durations) * 1000), 'avg': round(statistics.fmean(durations) * 1000), 'median': round(statistics.median(durations) * 1000), 'p80': round(p80 * 1000), 'p95': round(p95 * 1000), } return summary
def summary(self): summary = {} for section, durations in self.sections_duration.items(): p80, p95 = self.percentiles(durations) summary[section] = { "count": len(durations), "min": round(min(durations) * 1000), "max": round(max(durations) * 1000), "avg": round(statistics.fmean(durations) * 1000), "median": round(statistics.median(durations) * 1000), "p80": round(p80 * 1000), "p95": round(p95 * 1000), } return summary
def nayve_bayes_new(X, y, file): clf = MultinomialNB() skf = StratifiedKFold(n_splits=10) skf.get_n_splits(X, y) score = [] f1Score = [] for train_index, test_index in skf.split(X, y): #print("TRAIN:", train_index, "TEST:", test_index) score.append( clf.fit(X[train_index], y[train_index]).score(X[test_index], y[test_index])) #acuracia y_pred_mlp = clf.predict(X[test_index]) f1Score.append(f1_score(y[test_index], y_pred_mlp, average='binary')) print('acurácia: ', fmean(score), " desvio padrão: ", stdev(score)) print('f1-score ', fmean(f1Score), "desvio padrão: ", stdev(f1Score)) file.write("{}\t{}\n".format(fmean(score), stdev(score))) file.write("{}\t{}\n\n".format(fmean(f1Score), stdev(f1Score)))
def process_screen_rolling_backtest_result(json: dict, start_dt, end_dt, precision): if precision is None: precision = 2 length = len(json['rows']) data = [start_dt, end_dt, length] data.extend(val for val in json['average'][4:8]) data.append(min(float(item[8]) for item in json['rows'])) data.append(max(float(item[9]) for item in json['rows'])) data.append(json['average'][10]) data.append( round(statistics.fmean(float(item[5]) for item in json['rows'][0:13] ), precision) if length >= 13 else None) data.append( round(statistics.fmean(float(item[5]) for item in json['rows'][0:65] ), precision) if length >= 65 else None) data.append( round((statistics.geometric_mean( float(item[5]) / 100 + 1 for item in json['rows']) - 1) * 100, precision)) data.append( round((statistics.geometric_mean( float(item[6]) / 100 + 1 for item in json['rows']) - 1) * 100, precision)) data.append( round((statistics.geometric_mean( float(item[5]) / 100 + 1 for item in json['rows'][0:13]) - 1) * 100, precision) if length >= 13 else None) data.append( round((statistics.geometric_mean( float(item[5]) / 100 + 1 for item in json['rows'][0:65]) - 1) * 100, precision) if length >= 65 else None) data.append( round(statistics.fmean(float(item[4]) for item in json['rows'][0:65] ), precision) if length >= 65 else None) return data
def main(): array = list(np.random.randint(low=1, high=50, size=10)) big_array = list(np.random.randint(low=1, high=100000, size=5000)) rprint("[green]Unsorted array:", array) bubblesort(array) rprint("[green]Sorted array:", array) times = np.array([]) for i in range(10): start_time = time.time() bubblesort(big_array) stop_time = time.time() times = np.append(times, [stop_time - start_time]) rprint( "[yellow]Average Time taken over 10 sorting operations to sort an \ array of size 5000:", fmean(times), "[yellow]seconds.")
def main(): random.seed(42) array = [random.randint(a=1, b=50) for _ in range(10)] big_array = [random.randint(a=1, b=100000) for _ in range(5000)] rprint("[green]Unsorted array:", array) radixsort(array) rprint("[green]Sorted array:", array) times = [] for i in range(10): start_time = time.time() radixsort(big_array) stop_time = time.time() times.append(stop_time - start_time) rprint( "[yellow]Average Time taken over 10 sorting operations to \ sort an array of size 5000:", fmean(times), "[yellow]seconds.")