Ejemplo n.º 1
0
    def _aggregate(self):
        if not self._modified:
            return
        
        functions = defaultdict(lambda: defaultdict(list))
        for profile in filter(self._validate_profile, self._profiles.values()):
            for function, values in profile["functions"].items():
                functions[function]["num_calls"].append(values["num_calls"])
                functions[function]["total_time"].append(values["total_time"])
                functions[function]["cum_time"].append(values["cum_time"])

        for function, lists in functions.items():
            self._results[function] = Aggregator.Result(
                num_calls_median = int(median(lists["num_calls"])),
                total_time_mean = mean(lists["total_time"]),
                total_time_stdev = stdev(lists["total_time"]),
                cum_time_mean = mean(lists["cum_time"]),
                cum_time_stdev = stdev(lists["cum_time"]))
        
        run_times = lambda: (p["run_time"] for p in self._profiles.values())
        self._summary = Aggregator.Summary(
            run_time_mean = mean(run_times()),
            run_time_stdev = stdev(run_times()))

        self._modified = False
Ejemplo n.º 2
0
	def process_result(self, t_frame, r_frame):
		print(t_frame, r_frame) 
		
		try:
			stat_const = float(2.776)

			res2 = [] # frame transmission 
			res3 = [] # throughput 
			for i in range(int(self.T[0])):
				# frame transmission
				res2.append(t_frame[i]/r_frame[i])
				res3.append(self.F * r_frame[i] / self.R)

			# print(res2, res3)

			avg_res2 = statistics.mean(res2)
			sd2 = statistics.stdev(res2)
			dif2 = sd2/math.sqrt(int(self.T[0]))*stat_const
			upper_bound2 = avg_res2 + dif2 
			lower_bound2 = avg_res2 - dif2 

			avg_res3 = statistics.mean(res3)
			sd3 = statistics.stdev(res3)
			dif3 = sd3/math.sqrt(int(self.T[0]))*stat_const
			upper_bound3 = avg_res3 + dif3
			lower_bound3 = avg_res3 - dif3 

		except ZeroDivisionError: 
			return float("inf"), float("inf"), float("inf"), 0, 0, 0

		return avg_res2, lower_bound2, upper_bound2, avg_res3, lower_bound3, upper_bound3 
Ejemplo n.º 3
0
def display_histogram_scipy(args, result):
    import boltons.statsutils
    import matplotlib.pyplot as plt
    import pylab
    import scipy.stats as stats

    samples = result.get_samples()
    samples.sort()

    samples_stats = boltons.statsutils.Stats(samples)

    median = statistics.median(samples)
    # median +- MAD
    fit = stats.norm.pdf(samples, median, samples_stats.median_abs_dev)
    pylab.plot(samples, fit, '-o', label='median-mad')

    # median +- std dev
    fit2 = stats.norm.pdf(samples, median, statistics.stdev(samples, median))
    pylab.plot(samples, fit2, '-v', label='median-stdev')

    # mean + std dev
    fit3 = stats.norm.pdf(samples, statistics.mean(samples), statistics.stdev(samples))
    pylab.plot(samples, fit3, '-+', label='mean-stdev')

    legend = plt.legend(loc='upper right', shadow=True, fontsize='x-large')
    pylab.hist(samples, bins=25, normed=True)
    pylab.show()
Ejemplo n.º 4
0
def test():
    n = [random.random() for _ in range(20)]
    bag = Bag()
    for i in n:
        bag.add(i)
    print(bag)
    for i in bag:
        print(i)

    total = 0
    for item in bag:
        total += item
    mean = total / bag.size()

    total = 0
    for item in bag:
        total += (item - mean)**2
    std = math.sqrt(total / (bag.size() - 1))

    print('calculated mean is {}'.format(mean))
    print('calculated stdev is {}'.format(std))

    print('stdlib mean is {}'.format(statistics.mean(n)))
    print('stdlib stdev is {}'.format(statistics.stdev(n)))

    assert(round(mean, 10) == round(statistics.mean(n), 10))
    assert(round(std, 10) == round(statistics.stdev(n), 10))
Ejemplo n.º 5
0
    def calc_evalmetrics(self):
        """Calculate evaluation metrics: 
           Tour Recall, Tour Precision, Tour F1-score, RMSE of POI visit duration
           Tour Popularity, Tour Interest, Popularity and Interest Rank
        """
        assert(len(self.recommendSeqs) > 0)

        # calculate intersection size of recommended POI set and real POI set
        intersize = dict()
        for k, v in self.recommendSeqs.items():
            intersize[k] = len(set(v) & set(self.sequences[k]))

        # calculate tour recall
        recalls = []
        for k, v in intersize.items():
            recalls.append(v / len(self.sequences[k]))

        # calculate tour precision
        precisions = []
        for k, v in intersize.items():
            precisions.append(v / len(self.recommendSeqs[k]))

        # calculate F1-score
        f1scores = []
        assert(len(recalls) == len(precisions))
        for i in range(len(recalls)):
            f1scores.append(2 * precisions[i] * recalls[i] / (precisions[i] + recalls[i]))

        print('Recall:   ', stat.mean(recalls),    stat.stdev(recalls))
        print('Precision:', stat.mean(precisions), stat.stdev(precisions))
        print('F1-score: ', stat.mean(f1scores),   stat.stdev(f1scores))
Ejemplo n.º 6
0
    def bootstrap_test(self, nsamples=100, noise=0.2):
        """Returns mean and std. dev. of successful recognitions."""
        boot = {}
        for vec, pat in zip(self.pattern_vectors, self.patterns):
            boot[pat] = {"closest": [], "iterations": [], "full_matches": [], "accuracy": []}
            for sample in range(nsamples):
                recalled, noisy, iterations = self.recall_noisy(vec, noise=noise)
                self.show_pattern(noisy, "{}_{}_noisy_{}".format(
                    noise, pat, sample))
                self.show_pattern(recalled,      "{}_{}_recalled_{}".format(
                    noise, pat, sample))

                # equal to any patterns?
                matches = {}
                full_match = None
                for vec2, pat2 in zip(self.pattern_vectors, self.patterns):
                    matches[pat2] = list( \
                            vec2[0] == recalled[0]).count(True)
                    if matches[pat2] == vec2.size:
                        full_match = pat2

                boot[pat]["iterations"].append(iterations)
                boot[pat]["full_matches"].append(full_match)
                boot[pat]["closest"].append(pat == max(matches, key=matches.get))
                boot[pat]["accuracy"].append(matches[pat] / vec.size)
            boot[pat]["iterations"] = (mean(boot[pat]["iterations"]), stdev(boot[pat]["iterations"]))
            boot[pat]["accuracy"] = (mean(boot[pat]["accuracy"]), stdev(boot[pat]["accuracy"]))

            count_matches = lambda l: len(list(filter(lambda f: not f is None, l)))

            boot[pat]["full_matches"] = count_matches(boot[pat]["full_matches"])
            boot[pat]["closest"] = count_matches(boot[pat]["closest"])
        return boot
def main(total_rolls=20000):
    rolls_list = rolls(total_rolls, 1, 6)
    sliced_sum20 = sliced_sums(20, rolls_list)
    sums20 = sums(sliced_sum20, -20)
    roll_count20 = lens(sliced_sum20)
    sliced_sum10k = sliced_sums(10000, rolls_list)
    sums10k = sums(sliced_sum10k, -10000)
    roll_count10k = lens(sliced_sum10k)
    paired_sums = [(20, sums20), (10000, sums10k)]
    paired_rolls = [(20, roll_count20), (10000, roll_count10k)]

    answers("Mean of the sum - {0} when M is {0}:",
            paired_sums, lambda s: statistics.mean(s))
    answers("Mean of the number of rolls when M is {0}:",
            paired_rolls, lambda s: statistics.mean(s))
    answers("Standard deviation of the sum - {0} when M is {0}:",
            paired_sums, lambda s: statistics.stdev(s))
    answers("Standard deviation of the number of rolls when M is {0}:",
            paired_rolls, lambda s: statistics.stdev(s))
    answers("\nView of the rolls summing to {0}\n" +
            format("Count", ">7") + " " + format("Sum", ">7") + " Rolls\n",
            [(20, sliced_sum20), (10000, sliced_sum10k)],
            lambda ss: ''.join(
                format(len(s[1]), ">7") + " " + format(s[0], ">7") + " " +
                format(s[1]) + "\n" for s in ss)
            , sep=''
            )
Ejemplo n.º 8
0
def main():
    dailymotion = acquire_dailymotion()
    print "Dailymotion"
    print "total videos: " + str(len(dailymotion[0]))
    print "mean views: " + str(statistics.mean(dailymotion[0]))
    print "median views: " + str(statistics.median(dailymotion[0]))
    print "STD views: " + str(statistics.stdev(dailymotion[0]))
    print "Average Date: " + str(convert_to_datetime(statistics.mean(dailymotion[1])))
    print "Median Date: " + str(convert_to_datetime(statistics.median(dailymotion[1])))
    print "Average Lengths: " + str(statistics.mean(dailymotion[2]))
    print "Median Lengths: " + str(statistics.median(dailymotion[2]))
    print "STD Lengths: " + str(statistics.stdev(dailymotion[2]))
    print "Top 20 most used word in title: "
    word_count_dailymotion("title")
    print "Top 20 most used word in description:"
    word_count_dailymotion("description")
    youtube = acquire_youtube()
    print "YouTube"
    print "total videos: " + str(len(youtube[0]))
    print "mean views: " + str(statistics.mean(youtube[0]))
    print "median views: " + str(statistics.median(youtube[0]))
    print "STD views: " + str(statistics.stdev(youtube[0]))
    print "Average Date: " + str(convert_to_datetime(statistics.mean(youtube[1])))
    print "Median Date: " + str(convert_to_datetime(statistics.median(youtube[1])))
    print "Video Definition: " , str(statistics.mode(youtube[2])) , " - " , str(youtube[2].count(statistics.mode(youtube[2]))) ,"/" , str(len(youtube[2]))
    print "Average Lengths: " + str(statistics.mean(youtube[3]))
    print "Median Lengths: " + str(statistics.median(youtube[3]))
    print "STD Lengths: " + str(statistics.stdev(youtube[3]))    
    print "Top 20 most used word in title: "
    word_count_yt("title")
    print "Top 20 most used words in description: "
    word_count_yt("description")
    client.close()
Ejemplo n.º 9
0
def mean_dev(training_set):
    '''
    Calculates and returns the mean and standard deviation to the classes yes and no of a given training set
    '''
    class_yes = []
    class_no = []
    mean_yes = {}
    mean_no = {}
    dev_yes = {}
    dev_no = {}
    for key in training_set[0]:
        for i in range(len(training_set)):
            if training_set[i]['DiabetesClass'] == 'yes':
                class_yes.append(training_set[i][key])
            else:
                class_no.append(training_set[i][key])
        if not key == 'DiabetesClass':
            mean_yes[key] = statistics.mean(class_yes)
            mean_no[key] = statistics.mean(class_no)
            dev_yes[key] = statistics.stdev(class_yes)
            dev_no[key] = statistics.stdev(class_no)
        else:
            prob_yes = float(len(class_yes) / len(training_set))
            prob_no = float(len(class_no) / len(training_set))
        class_yes = []
        class_no = []
    return mean_yes, mean_no, dev_yes, dev_no, prob_yes, prob_no
Ejemplo n.º 10
0
    def good_stdev(self, current_offer):
        if self.counter < 5:
            return False

        # array of utilities the opponent would get for their offer
        past_utils = [self.utility_for(x) for x in self.opponent_offers]
        old_stdev = statistics.stdev(past_utils)
        old_mean = statistics.mean(past_utils)

        if past_utils[-1] < self.penalty:
            return False

        new_utils = []
        # filter outliers (2 standard deviations above or below)
        for u in past_utils:
            if old_mean - 2*old_stdev < u < old_mean + 2*old_stdev:
               new_utils.append(u)

        if len(new_utils) < 2:
            return False

        # if the utility we get for the offer is greater than the mean + 1 std dev, then return True
        offer_utility = self.utility_for(current_offer)
        new_stdev = statistics.stdev(new_utils)
        new_mean = statistics.mean(new_utils)
        return offer_utility > new_mean + new_stdev
Ejemplo n.º 11
0
def run_simulation(init_duration, init_stake, samples, player):
    """ Run simulation, print the result to stdout

    """
    wheel = create_wheel()
    table = Table(wheel)
    game = RouletteGame(wheel, table)
    simulator = Simulator(game, player,
                          init_duration=init_duration, samples=samples,
                          init_stake=init_stake)
    simulator.gather()
    durations = simulator.durations
    maxima = simulator.maxima
    print(player)
    print()
    print("Durations")
    print("  min :", min(durations))
    print("  max :", max(durations))
    print("  mean: %.2f" % statistics.mean(durations))
    print("  dev : %.2f" % statistics.stdev(durations))
    print("Maxima")
    print("  min :", min(maxima))
    print("  max :", max(maxima))
    print("  mean: %.2f" % statistics.mean(maxima))
    print("  dev : %.2f" % statistics.stdev(maxima))
Ejemplo n.º 12
0
def replacePearsonPvalueWithZscore():
    all_sample_data={}
    for tissue in tissue_comparison_scores:
        for (r,p,sample) in tissue_comparison_scores[tissue]:
            all_sample_data[sample] = [] ### populate this dictionary and create sub-dictionaries
        break

    for tissue in tissue_comparison_scores:
        for (r,p,sample) in tissue_comparison_scores[tissue]:
            all_sample_data[sample].append(r)

    sample_stats={}
    all_dataset_rho_values=[]
    ### Get average and standard deviation for all sample rho's
    for sample in all_sample_data:
        all_dataset_rho_values+=all_sample_data[sample]
        avg=statistics.avg(all_sample_data[sample])
        stdev=statistics.stdev(all_sample_data[sample])
        sample_stats[sample]=avg,stdev
    
    global_rho_avg = statistics.avg(all_dataset_rho_values)
    global_rho_stdev = statistics.stdev(all_dataset_rho_values)
    
    ### Replace the p-value for each rho
    for tissue in tissue_comparison_scores:
        scores = []
        for (r,p,sample) in tissue_comparison_scores[tissue]:
            #u,s=sample_stats[sample]
            #z = (r-u)/s
            z = (r-global_rho_avg)/global_rho_stdev ### Instead of doing this for the sample background, do it relative to all analyzed samples
            scores.append([r,z,sample])
        tissue_comparison_scores[tissue] = scores
Ejemplo n.º 13
0
def pCalc (movMat, movNumber, n, reviewers):
      
    xVals = [int(x) for i,x in enumerate(movMat[movNumber][1].split(';')) if i in reviewers]
    yVals = [int(x) for i,x in enumerate(movMat[n][1].split(';')) if i in reviewers]
    
    xi = sum(xVals) #get first movie values
    average1 = xi/len(reviewers)
    stdDev1 = statistics.stdev(xVals)
  
    yi = sum(yVals) #get second movie values
    average2 = yi/len(yVals)
    stdDev2 = statistics.stdev(yVals)

    r = 0 		#get r value
    newSum = [((x - average1) / stdDev1) * ((y-average2)/stdDev2) for x,y in zip(xVals, yVals)]
    r = (1/(len(reviewers)-1))*sum(newSum)    
    
    review = []		#append all values to the list
    review.append(r)
    review.append(average1)
    review.append(average2)
    review.append(stdDev1)
    review.append(stdDev2)
    review.append(n)
    review.append(len(reviewers))
         
    return review 
Ejemplo n.º 14
0
def extract_results(input, out):
    header = ['fft_type', 'n_threads', 'n_points', 'n_iterations',
              'Throughput', 'Stdev']
    records = []
    for dirs, subdirs, files in os.walk(input):
        for file in files:
            if ('.stderr' not in file):
                continue
            l = []
            threads = string_between(file, 'n_thr', '.')
            points = string_between(file, 'n_p', 'n_i')
            iters = string_between(file, 'n_i', 'n_thr')
            exe = string_between(file, '', '-bench')
            #implementation = dirs.split('/')[-2]
            for line in open(os.path.join(dirs, file), 'r'):
                if 'Throughput' in line:
                    temp = string_between(line, ':', 'M')
                    l.append(float(temp.strip()))
            if l:
                records.append([exe, threads, points, iters,
                                stat.mean(l), stat.stdev(l)])
                print file
                percent = 100.0 * stat.stdev(l) / stat.mean(l)
                if percent > 10:
                    print "The previous file has %.2f %% error" % percent
            else:
                print "I found an empty file called %s" % file
    records.sort(key=lambda a: (a[0], int(a[1]), int(a[2]), int(a[3])))
    t = PrettyTable(header)
    t.align = 'l'
    t.border = False
    for r in records:
        t.add_row(r)
    with open(out, 'w') as f:
        f.writelines(str(t) + '\n')
Ejemplo n.º 15
0
def main(y):
    print("Give me",y,"numbers")
    r = int(input())
    numbers =[]
    while len(numbers) < y:
        numbers.append(r)
        if len(numbers) < y:
            r = int(input("Give me another number"))
    print (numbers)
    #sum of the 10 numbers
    def function(x):
        name = x
        var = 0
        for i in numbers:
            var += i
        result = var/len(numbers)
        if name == 1:
            return var
        else:
            return result
    #Paul´s blog idea -->https://pololarinette.wordpress.com/2015/10/15/wsq10-lists/
    #Standar deviation
    import statistics
    statistics.stdev(numbers)

    print("The sum of the",y,"numbers is",function(1))
    print("The average of the",y,"numbers is",function(2))
    print("The stardart deviation is",statistics.stdev(numbers))
def bench_throughput_latency(samples=3, max_rsd=0.1,
                             *args, **kwargs):
    while True:
        throughputs = []
        latencies = []
        iteration = 1
        errors = 0
        while iteration <= samples:
            sys.stdout.write(' [*] Iteration %d/%d\r' % (iteration,samples))
            sys.stdout.flush()
            try:
                bench = Benchmark(*args, **kwargs)
                bench.run()
                throughputs.append(bench.throughput)
                latencies.append(bench.latency)
                iteration += 1
            except Exception as e:
                errors += 1
                if errors >= samples:
                    raise
                print(' [!] Iteration %d failed: %s\n%s' % \
                    (iteration, str(type(e)), str(e)))
        mean_throughput = statistics.mean(throughputs)
        mean_latency = statistics.mean(latencies)
        stdev_throughput = statistics.stdev(throughputs)
        stdev_latency = statistics.stdev(latencies)
        rsd_throughput = stdev_throughput/mean_throughput
        rsd_latency = stdev_latency/mean_latency
        if rsd_throughput > max_rsd or rsd_latency > max_rsd:
            sys.stderr.write(' [!] Discarding experiment: '+\
                'throughput RSD %.2f %%, latency RSD %.2f %%\n' %\
                (rsd_throughput*100, rsd_latency*100))
            continue
        return (mean_throughput, mean_latency,
            rsd_throughput, rsd_latency)
Ejemplo n.º 17
0
def addDataToPlt(fig, ax, dates, diff, c = 'c', label="raw", isMultiple=True):
    assert len(dates) == len(diff), "Plot and data are of different lenght"
    label1 = "average of 3"
    label2 = "average of 7"

    med3 = [i for i in diff]
    med7 = [i for i in diff]
    for i in range(3, len(diff) - 4):
        if i > 2 and i < len(diff) - 4:
            med7[i] = stats.median(diff[i-3:i+3])
        if i > 0 and i < len(diff) - 2:
            med3[i] = stats.median(diff[i-1:i+2])
        
    marker = "o"
    if len(diff) > 200:
        marker = "."
    if not isMultiple:
        if len(diff) > 1 and stats.stdev(diff) > 0.1:
            logger.error("Why do you have a high stdev?" + str(stats.stdev(diff)))
            marker = "x"

    ax.plot_date(dates, diff, c, xdate=True, marker = marker, linestyle="", label=label)
    if isMultiple:
        ax.plot_date(dates, med3, 'b', xdate=True, marker = ".", linestyle="", label=label1)
        ax.plot_date(dates, med7, 'r', xdate=True, marker = ".", linestyle="", label=label2)
    ax.xaxis.set_major_locator(matplotlib.dates.HourLocator())
    ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%H:00"))
    ax.xaxis.set_minor_locator(matplotlib.dates.MinuteLocator())
    ax.autoscale_view()
    fig.autofmt_xdate()
    ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
Ejemplo n.º 18
0
    def cmp(tweet1_counts1, tweet2_counts2):
        (tweet1, counts1) = tweet1_counts1
        (tweet2, counts2) = tweet2_counts2

        "1. Из твитов с разным количеством элементов списков более информативен тот, в котором элементов больше"
        if sum(counts1) > sum(counts2):
            return -1
        elif sum(counts2) > sum(counts1):
            return 1

        """
        2. Из твитов с одинаковой суммой количеств элементов список выбираем тот, где количества элементов более
        сбалансированы (например, [1, 1] лучше, чем [2, 0])
        """
        std1 = statistics.stdev(counts1)
        std2 = statistics.stdev(counts2)
        if std1 < std2:
            return -1
        elif std2 < std1:
            return 1

        "И, наконец, наиболее информативен твит большей длины"
        if len(tweet1) > len(tweet2):
            return -1
        elif len(tweet2) > len(tweet1):
            return 1

        return 0
def model_analysis(x, x_matrix, y, line, y_hat, b):
    n = len(x) # number of samples
    s_x = stats.stdev(x) # standard deviation of x values
    s_y = stats.stdev(y) # standard deviation of y values
    s2_x = stats.variance(x) # variance of x values
    s2_y = stats.variance(y) # variance of y values
    s_xy = b * s2_x # covariance of VM
    
    mad_temp = 0
    SSE = 0
    for i in range(len(y)):
        temp = abs(y[i] - y_hat[i])
        mad_temp += temp
        SSE += temp**2 # sum of squares for error
    MAD = mad_temp / n    
    s_err = math.sqrt(SSE / (n - 2)) # standard error of estimate
    s_b = s_err / math.sqrt((n - 1) * s2_x)
    
    r = s_xy / (s_x * s_y) # sample coefficient of correlation
    R_2 = line.score(x_matrix, y) # coefficient of determination 
    R_2calc = s_xy**2 / (s2_x * s2_y)
    t = b / s_b # t-value for slope assuming true slope = 0
    
    f1.write('\nSkew = ' + str(b) + '\n')
    f1.write('Coefficient of correlation (r) = ' + str(r) + '\n')
    #f1.write('Coefficient of determination (R^2) via scikit = ' + str(R_2) + '\n')
    f1.write('Coefficient of determination (R^2) calculate = ' + str(R_2calc) + '\n')
    f1.write('Test statistic for clock skew (t) = ' + str(t) + '\n')
    f1.write('Mean Absolute Deviation (MAD) = ' + str(MAD) + '\n')
    f1.write('Sum of Squares for Forecast Error (SSE) = ' + str(SSE) + '\n')
    
    return
    def get_result(self, raw_fname):
        node_flow_num_vs_performance = {}

        in_file = open(raw_fname, 'r')
        lines = in_file.readlines()
        in_file.close()
        for line in lines:
            #get each value
            line = line[1:-2]
            #print(line)
            items = line.split(', ')
            if len(items) != 9:
                continue
            num_nodes = int(items[0])
            num_tasks = int(items[1])
            num_candidate_pair = int(items[2])
            max_node_flows = int(items[3])
            num_pairs = int(items[4])
            latency = int(items[5])
            flow_avg_latency = int(items[7])
            max_flow_latency = int(items[8])

            if max_node_flows not in node_flow_num_vs_performance:
                node_flow_num_vs_performance[max_node_flows] = []
            node_flow_num_vs_performance[max_node_flows].append(
                (num_nodes, num_tasks, num_candidate_pair, num_pairs, latency, flow_avg_latency, max_flow_latency)
            )
                
        #statistics results
        for max_node_flows, tuples in sorted(node_flow_num_vs_performance.items(), key=lambda item: item[0]):
            list_num_tasks = []
            list_num_candidate_pair = []
            list_num_pairs = []
            list_latency = []
            list_flow_avg_latency = []
            all_rounds_max_flow_latency = 0
            for one_tuple in tuples:
                list_num_tasks.append(one_tuple[1])
                list_num_candidate_pair.append(one_tuple[2])
                list_num_pairs.append(one_tuple[3])
                list_latency.append(one_tuple[4])
                list_flow_avg_latency.append(one_tuple[5])
                all_rounds_max_flow_latency = max(all_rounds_max_flow_latency, int(one_tuple[6]))
            avg_num_tasks = statistics.mean(list_num_tasks)
            avg_num_candidate_pair = statistics.mean(list_num_candidate_pair)
            avg_num_pairs = statistics.mean(list_num_pairs)
            avg_latency = statistics.mean(list_latency)
            avg_flow_avg_latency = statistics.mean(list_flow_avg_latency)
            stdv_num_tasks = 0
            stdv_num_candidate_pair = 0
            stdv_num_pairs = 0
            stdv_latency = 0
            stdv_flow_avg_latency = 0
            if len(tuples) > 1:
                stdv_num_tasks = statistics.stdev(list_num_tasks)
                stdv_num_candidate_pair = statistics.stdev(list_num_candidate_pair)
                stdv_num_pairs = statistics.stdev(list_num_pairs)
                stdv_latency = statistics.stdev(list_latency)
                stdv_flow_avg_latency = statistics.stdev(list_flow_avg_latency)
            print(max_node_flows, avg_num_tasks, stdv_num_tasks, avg_num_candidate_pair, stdv_num_candidate_pair, avg_num_pairs, stdv_num_pairs, avg_latency, stdv_latency, avg_flow_avg_latency, stdv_flow_avg_latency, all_rounds_max_flow_latency) 
def csv_dict_reader(file_obj):
    """
    Read a CSV file using csv.DictReader
    """
    reader = csv.DictReader(file_obj, delimiter=',')
    num_likes = []
    num_comments = []
    num_shares = []
    for line in reader:
        p = int(line["num_likes"])
        q = int(line["first_page_comment"])
        r = int(line["comments_beyond_pageone"])
        num_likes.append(p)
        num_comments.append(q)
        num_shares.append(r)
    mean_num_likes = statistics.mean(num_likes)
    stdev_num_likes = statistics.stdev(num_likes)
    mean_num_comments = statistics.mean(num_comments)
    stdev_num_comments = statistics.stdev(num_comments)
    mean_num_shares = statistics.mean(num_shares)
    stdev_num_shares = statistics.stdev(num_shares)
    covariance_likes = stdev_num_likes / mean_num_likes
    covariance_comments = stdev_num_comments / mean_num_comments
    covariance_shares = stdev_num_shares / mean_num_shares
    w = csv.writer(open("svm_dataset.csv","a"),delimiter=',',quoting=csv.QUOTE_ALL)
    
    w.writerow([mean_num_likes,stdev_num_likes,covariance_likes,mean_num_comments,stdev_num_comments,covariance_comments,mean_num_shares,stdev_num_shares,covariance_shares])
    def calculate_switches_one_setting_result(self, switches_rounds_result, avg_switches_result_one_setting):
        #switches_rounds_result
        #pair: key-switch_id, value-{{sec, switch_one_round_result_c},{sec, switch_one_round_result_c}}
        switches_avg_real_targetflow_num = []
        for switch_id, rounds_result_map in switches_rounds_result.items():
            avg_switch_one_setting = avg_switch_result_one_setting_c()
            avg_switches_result_one_setting[switch_id] = avg_switch_one_setting
            #avg_switch_one_setting.condition_map_size = self.switches_condition_map_size[switch_id]

            fn_list = []
            fp_list = []
            accuracy_list = []
            all_volume_list = []
            all_flow_num_list = []
            real_target_flow_num_list = []
            fn_num_list = []
            fn_num_not_targetflow_list = []
            fn_num_not_captured_list = []
            condition_pkt_num_list = []
            for sec, one_round_result in rounds_result_map.items():
                #print("fn-fp-accuracy:{0}-{1}-{2}" .format(one_round_result.fn, one_round_result.fp, one_round_result.accuracy))
                fn_list.append(one_round_result.fn)
                fp_list.append(one_round_result.fp)
                accuracy_list.append(one_round_result.accuracy)
                all_volume_list.append(one_round_result.all_volume_one_interval)
                all_flow_num_list.append(one_round_result.all_flow_num_one_interval)
                real_target_flow_num_list.append(one_round_result.real_target_flow_num)
                fn_num_list.append(one_round_result.fn_num)
                fn_num_not_targetflow_list.append(one_round_result.fn_num_not_targetflow)
                fn_num_not_captured_list.append(one_round_result.fn_num_not_captured)
                condition_pkt_num_list.append(one_round_result.condition_pkt_num)

            avg_switch_one_setting.avg_fn = statistics.mean(fn_list)
            avg_switch_one_setting.avg_fp = statistics.mean(fp_list)
            avg_switch_one_setting.avg_accuracy = statistics.mean(accuracy_list)
            avg_switch_one_setting.avg_real_target_flow_num = statistics.mean(real_target_flow_num_list)
            switches_avg_real_targetflow_num.append(round(avg_switch_one_setting.avg_real_target_flow_num, 1))
            avg_switch_one_setting.avg_fn_num = statistics.mean(fn_num_list)
            avg_switch_one_setting.avg_fn_num_not_targetflow = statistics.mean(fn_num_not_targetflow_list)
            avg_switch_one_setting.avg_fn_num_not_captured = statistics.mean(fn_num_not_captured_list)
            avg_switch_one_setting.avg_condition_pkt_num = statistics.mean(condition_pkt_num_list)
            if len(rounds_result_map) > 1:
                avg_switch_one_setting.stdv_fn =statistics.stdev(fn_list)
                avg_switch_one_setting.stdv_fp =statistics.stdev(fp_list)
                avg_switch_one_setting.stdv_accuracy =statistics.stdev(accuracy_list)
                avg_switch_one_setting.stdv_condition_pkt_num = statistics.stdev(condition_pkt_num_list)
            print("switch_id:{switch_id}, #interval:{num_interval}, traffic:{all_volume}Mbyte, #flow:{all_flow_num}, #target_flow:{target_flow_num}, #buckets:{avg_sample_map_size}, avg_fn:{fn}(stdv:{fn_stdv}), avg_accuracy:{accuracy}(stdv:{acc_stdv})" \
                .format( \
                    switch_id=switch_id, \
                    num_interval = len(rounds_result_map), \
                    all_volume = int(sum(all_volume_list)/1000000), \
                    all_flow_num = sum(all_flow_num_list), \
                    target_flow_num = round(avg_switch_one_setting.avg_real_target_flow_num, 1), \
                    avg_sample_map_size = avg_switch_one_setting.avg_sample_map_size, \
                    fn=round(avg_switch_one_setting.avg_fn, 4), \
                    fn_stdv = round(avg_switch_one_setting.stdv_fn, 4), \
                    accuracy=round(avg_switch_one_setting.avg_accuracy, 4), \
                    acc_stdv = round(avg_switch_one_setting.stdv_accuracy, 4) \
                ))
def get_precision_recall_fscore_overall(results, k):
    precision, recall, fscore = [], [], []
    for res in results:
        precision.append(res[0])
        recall.append(res[1])
        fscore.append(res[2])
    return stat.mean(precision), stat.stdev(precision),stat.mean(recall), \
           stat.stdev(recall), stat.mean(fscore), stat.stdev(fscore)
Ejemplo n.º 24
0
def userStDev(user1, user2):
    ratingDictionary = buildRatingDictionary(int(user1), int(user2))
    userOneRatings = []
    userTwoRatings = []
    for dictionary in ratingDictionary:
        userOneRatings.append(ratingDictionary[dictionary][0])
        userTwoRatings.append(ratingDictionary[dictionary][1])
    return [statistics.stdev(userOneRatings), statistics.stdev(userTwoRatings)]
Ejemplo n.º 25
0
    def get_test_results(self, guess_tags, correct_tags):
        tag_score_dict = {}
        
        correct_tag_type ={}
        wrong_tag_type = {}
    
        conf_right = []
        conf_wrong = []
    
        total_tags = 0
        total_wrong_tags = 0
    
        total_sentences = len(guess_tags)
        total_wrong_sent = 0
    
        for sent_num, correct_sentence in enumerate(correct_tags):

            perfect_sentence = True
            for word_idx, correct_token in enumerate(correct_sentence.get_tokens()):
                guess_token = guess_tags[sent_num].get_token_at(word_idx)
                assert correct_token.orig == guess_token.orig
                
                for i, (feature, guess) in enumerate(guess_token.get_testable_attr_list()):
                    tag_score_dict[feature] = tag_score_dict.get(feature, 0) + (guess==correct_token.get_testable_attr_list()[i][1])
                
                tag_guess = guess_token.pos_tag
                guess_confidence = guess_token.conf
                total_tags +=1
            
                if(correct_token.pos_tag != tag_guess):
                    total_wrong_tags +=1
                    conf_wrong.append(guess_confidence)
                    perfect_sentence = False
                    error_tuple = (correct_token.pos_tag, tag_guess)
                    wrong_tag_type[error_tuple] = wrong_tag_type.get(error_tuple, 0) + 1
                else:
                    correct_tag_type[tag_guess] = correct_tag_type.get(tag_guess, 0) + 1
                    conf_right.append(guess_confidence)
                
            if not perfect_sentence:
                total_wrong_sent+= 1
                
        if(len(conf_right) >0 and len(conf_wrong)>0): 
            print "average confidence of right tag= " + str(s.mean(conf_right))
            print "average confidence of wrong tag= " + str(s.mean(conf_wrong))
            print "stdev confidence of right tag= " + str(s.stdev(conf_right))
            print "stdev confidence of wrong tag= " + str(s.stdev(conf_wrong))
   
        tag_word_acc = (100.00*(total_tags-total_wrong_tags))/total_tags
        tag_sentence_acc = (100.00*(total_sentences-total_wrong_sent))/total_sentences

        print "tag token accuracy: " + str(tag_word_acc) + "%"
        print "tag sentence accuracy: " + str(tag_sentence_acc) + "%"
        print "have not written tests for parse yet"
        for feature, correct_count in tag_score_dict.iteritems():
            print feature, "accuracy:", (100.0*correct_count)/total_tags
Ejemplo n.º 26
0
def coplot_node_density_and_total_wire_length(trials):
	fig = plt.figure()
	plot = plt.subplot()
	fig.suptitle('Node Density & Total Wire Length vs. Wire Count')
	plt.xlabel('Wire Count')
	plt.ylabel('Node Density (node/unit^2)')

	xs = [r.wire_count for r in trials[0]]
	trial_count = len(trials)
	data_points = len(trials[0])

	#if not all()

	large_set = [ [t[n].average_shortest_path_length / 60 for t in trials] for n in range(data_points)]
	largest = [s.mean(x) for x in large_set]

	discon_set = [ [t[n].total_wire_length for t in trials] for n in range(data_points)]
	disconnected = [s.mean(y) for y in discon_set]

	# There's a bug in errorbar() when called without fmt=None that
	# causes connecting lines to be drawn between data points.  This creates
	# a significant amount of visual noise.  The page below says it was
	# supposed to be fixed in 1.4 and higher, but this doesn't seem to be
	# the cases or I haven't configured the module import correctly.
	# An alternate solution is to draw the data points and the errorbars 
	# separately as is done below.  The error bars seems to be slightly off
	# center, but it's better than the alternative.
	# See:  http://stackoverflow.com/questions/18498742/how-do-you-make-an-errorbar-plot-in-matplotlib-using-linestyle-none-in-rcparams	
	
	h1, = plot.plot(xs, largest, "ok", label="Density")
	

	# Calculate the standard error for each x's seqence of 
	# y values.  There must be at least two data points or exceptions
	# are thrown.
	# See:  https://en.wikipedia.org/wiki/Standard_error	
	if trial_count >= 2:
		data_points_root = sqrt(trial_count)
		stdev = [s.stdev(value) for value in large_set]
		stderr = [x / data_points_root for x in stdev]
		plt.errorbar(xs, largest, yerr=stderr, color="black", fmt=None)
	
	twin = plot.twinx()
	twin.yaxis.label.set_text('Total Wire Length (units)')
	h2, = twin.plot(xs, disconnected, "*r", label="Total")
	plt.legend(handles=[h1,h2])

	if trial_count >= 2:
		data_points_root = sqrt(trial_count)
		stdev = [s.stdev(value) for value in discon_set]
		stderr = [x / data_points_root for x in stdev]
		plt.errorbar(xs, disconnected, yerr=stderr, color="black", fmt=None)

	#plt.plot(xs, ys, 'o')
	plt.savefig('density_vs_total.png')
	plt.show()		
Ejemplo n.º 27
0
 def analiza(self):
     """
     Media y desviación de todos los datos
     """
     self.mediaTotal['C'] = sum(self.propC)/float(len(self.propC))
     self.desvTotal['C'] = statistics.stdev(self.propC)
     self.mediaTotal['H'] = sum(self.propH)/float(len(self.propH))
     self.desvTotal['H'] = statistics.stdev(self.propH)
     self.mediaTotal['R'] = sum(self.propR)/float(len(self.propR))
     self.desvTotal['R'] = statistics.stdev(self.propR)
Ejemplo n.º 28
0
def compute_sharpe(returns, risk_free):
    """ Compute annualized Sharpe Ratio based on daily returns (252 trading days / year)"""

    adj_returns = []
    for i in range(len(returns)):
        adj_returns += [returns[i] - (risk_free / 252)]
    if statistics.stdev(adj_returns) == 0:
        return 0
    sharpe = math.sqrt(252) * statistics.mean(adj_returns) / statistics.stdev(adj_returns)
    return sharpe
Ejemplo n.º 29
0
def save_load_benchmark():
    from relstorage.cache import LocalClientBucket
    from io import BytesIO

    bucket = LocalClientBucket(10*1024*1024)

    i = 1
    j = 0
    while bucket.size < bucket.limit - i:
        val = (str(j) * i).encode('ascii')
        if len(val) > bucket.limit or bucket.size + len(val) > bucket.limit:
            break
        bucket[str(i)] = val
        if i < 1096:
            i += 50
            j += 1
        else:
            j += 1
            i += 1
        print("Len", len(bucket), "size",bucket.size, "i", i)

    print("Len", len(bucket), "size", bucket.size)
    number = 50
    import timeit
    import statistics
    import cProfile
    import pstats

    def write():
        io = BytesIO()
        bucket.write_to_file(io)

    bio = BytesIO()
    bucket.write_to_file(bio)

    def load():
        bio.seek(0)
        b2 = LocalClientBucket(bucket.limit)
        b2.load_from_file(bio)

    write_timer = timeit.Timer(write)
    write_times = write_timer.repeat(number=number)

    print("write average", statistics.mean(write_times), "stddev", statistics.stdev(write_times))

    #pr = cProfile.Profile()
    #pr.enable()

    read_timer = timeit.Timer(load)
    read_times = read_timer.repeat(number=number)
    #pr.disable()
    #ps = pstats.Stats(pr).sort_stats('cumulative')
    #ps.print_stats()

    print("read average", statistics.mean(read_times), "stddev", statistics.stdev(read_times))
def __init__():

	for param1 in params:
		for param2 in params:

			print
			print 'Step Size 1: ' + str(param1)
			print 'Step Size 2: ' + str(param2)

		 	lvlOptimism   = RMAX
			numSeeds      = 2000
			numIterations = 1000

		    #Variables that will store the results for all methods
			res_MAX         = []
			res_SARSA       = []

			'''Already pull all the arms and store them, this way one can
			   easily reproduce the results and come back to see other possibilities'''
			b = []
			for s in xrange(1, numSeeds+1):
				b.append(Bandits(s, numIterations))

		    #Run the experiment x times, where x is the number of seeds
			for s in xrange(1, numSeeds + 1):
				seed = s
				r.seed(seed)

				res_MAX.append([])
				res_SARSA.append([])

				maxReturn = 0

				'''First I just calculate the max return one could've get.'''
				for i in xrange(numIterations):
					maxReturn += max(b[s-1].pullArm(0), b[s-1].pullArm(1))
				
				res_MAX[s-1].append(maxReturn)
				b[s-1].resetEnv()

				'''Agent following the SARSA(0) with two value functions.'''
				r.seed(seed)
				res_SARSA[s-1].append(SARSA_SPLIT(b[s-1], numIterations, param1, param2, lvlOptimism))
				b[s-1].resetEnv()

			'''Now we can take the average return of each method:'''
			res_MAX_avg = []
			res_SARSA_avg = []

			for i in xrange(numSeeds):
				res_MAX_avg.append(stat.mean(res_MAX[i]))
				res_SARSA_avg.append(stat.mean(res_SARSA[i]))

			print 'Max return :', stat.mean(res_MAX_avg), ',', stat.stdev(res_MAX_avg)
			print 'SARSA(0)   :', stat.mean(res_SARSA_avg), ',', stat.stdev(res_SARSA_avg)
Ejemplo n.º 31
0
        #print(GIMap)
        curMap = fill(curMap)
        #print(curMap)

        curSSD = findSSD(curMap, GIMap)
        SSD.append(curSSD)
        SSDList.append([curSSD, image[1]])
        if (count == runs):
            print("----------------FULL------------------")
            fullList.append([curSSD, image[1]])
        print(curSSD)

        length.append(findLength(visited))

mean = statistics.mean(means)
sd = statistics.stdev(means)

meanSSD = statistics.mean(SSD)
sdSSD = statistics.stdev(SSD)

SSDList.sort()
for item in SSDList:
    print(item)

print("-------------------------FULL LIST--------------------------------")
for item in fullList:
    print(item)

print("-------------------------LENGTH------------------------------------")

for item in length:
Ejemplo n.º 32
0
import plotly.figure_factory as ff
import plotly.graph_objects as go
import statistics
import csv
import random
import pandas as pd
df = pd.read_csv("studentMarks.csv")
data = df["Math_score"].to_list()
fig = ff.create_distplot([data], ["Math Scores"], show_hist=False)
#fig.show()
datamean = statistics.mean(data)
datamedian = statistics.median(data)
datamode = statistics.mode(data)
datastdDev = statistics.stdev(data)
print("Mean, Median, Mode of the data is {} {} {} respectively".format(
    datamean, datamedian, datamode))
print("Standard Deviation of the data is {}".format(datastdDev))


def randomSetOfMean(counter):
    dataSet = []
    for i in range(0, counter):
        randomIndex = random.randint(0, len(data) - 1)
        value = data[randomIndex]
        dataSet.append(value)
    mean = statistics.mean(dataSet)
    return mean


meanlist = []
for i in range(0, 1000):
with open('Auto.csv', newline='') as csvfile:
    Auto = csv.reader(csvfile, quotechar='|')
    for row in Auto:
        if count > 0:
            mpg.append(float(row[0]))
            cylinders.append(int(row[1]))
            displacement.append(float(row[2]))
            if row[3] != '?':
                horsepower.append(int(row[3]))  #some ?
            weight.append(int(row[4]))
            acceleration.append((float(row[5])))
            year.append(int(row[6]))
        count += 1

print("Range of mpg: ", min(mpg), '~', max(mpg), '/ mean: ',
      sum(mpg) / count, '/ standard deviation: ', statistics.stdev(mpg))
print("Range of cylinders: ", min(cylinders), '~', max(cylinders), '/ mean: ',
      sum(cylinders) / count, '/ standard deviation: ',
      statistics.stdev(cylinders))
print("Range of displacement: ", min(displacement), '~', max(displacement),
      '/ mean: ',
      sum(displacement) / count, '/ standard deviation: ',
      statistics.stdev(displacement))
print("Range of horsepower: ", min(horsepower), '~', max(horsepower),
      '/ mean: ',
      sum(horsepower) / len(horsepower), '/ standard deviation: ',
      statistics.stdev(horsepower))
print("Range of weight: ", min(weight), '~', max(weight), '/ mean: ',
      sum(weight) / count, '/ standard deviation: ', statistics.stdev(weight))
print("Range of acceleration: ", min(acceleration), '~', max(acceleration),
      '/ mean: ',
Ejemplo n.º 34
0
# expovariate - used to simulate arrival times
# distribution centers around reciprical of argument
# so expovariate(20) centers around .05 (1/20)
expovariate(20)
### 0.11416353263962384
expovariate(20)
### 0.0523532783809665

# triangular mean is centered sharply around midpoint
# (smaller stdev)
from statistics import mean, stdev
data = [triangular(1000, 1100) for i in range(1000)]
mean(data)
### 1048.7593107346681
stdev(data)
### 20.13510622686606

# uniform mean is still centered around midpoint
# but larger stdev
data = [uniform(1000, 1100) for i in range(1000)]
mean(data)
### 1051.0406593043674
stdev(data)
### 28.545863280007982

# gauss gives us very close to mean and stdev
# provided in the arguments
data = [gauss(100, 15) for i in range(1000)]
mean(data)
### 100.05158436289322
Ejemplo n.º 35
0
    total += freq_abs[x]
    print(x, " ", total)

print("FREQ ACUMULADA")
total = 0
for x in range(0, len(freq_abs)):
    total += freq_abs[x]
    print(x, " ", (total / 36) * 100)

print('SEGUNDA QUESTAO')
print()
print('Altura: ', altura)
print('MEDIA DE ALTURA = ', mean(altura))
print("MEDIANA DE ALTURA = ", median(altura))
print("MODA DE ALTURA = ", mode(altura))
print('DESVIO PADRAO DE ALTURA = ', stdev(altura))
print("COEFICIENTE DE VARIACAO DE ALTURA = ", stdev(altura) / mean(altura))
print()
print('Peso: ', peso)
print('MEDIA DE PESO = ', mean(peso))
print("MEDIANA DE PESO = ", median(peso))
print("MODA DE PESO = ", mode(peso))
print('DESVIO PADRAO DE PESO = ', stdev(peso))
print("COEFICIENTE DE VARIACAO DE PESO = ", stdev(peso) / mean(peso))
print()
imc = list(imc.values())
print('Imc: ', imc)
print('MEDIA DE IMC = ', mean(imc))
print("MEDIANA DE IMC = ", median(imc))
print("MODA DE IMC = ", mode(imc))
print('DESVIO PADRAO DE IMC = ', stdev(imc))
Ejemplo n.º 36
0
def process_stats(arr, precision=2, empty=''):
    '''Returns the statistics for the given array'''
    arr_mean = round(mean(arr), precision) if len(arr) > 0 else empty
    arr_median = round(median(arr), precision) if len(arr) > 0 else empty
    arr_stdev = round(stdev(arr), precision) if len(arr) > 1 else empty
    return arr_mean, arr_median, arr_stdev
Ejemplo n.º 37
0
import numpy
from scipy import stats

rent_list = [
    425, 430, 430, 435, 435, 435, 435, 435, 440, 440, 440, 440, 440, 445, 445,
    445, 445, 445, 450, 450, 450, 450, 450, 450, 450, 460, 460, 460, 465, 465,
    465, 470, 470, 472, 475, 475, 475, 480, 480, 480, 480, 485, 490, 490, 490,
    500, 500, 500, 500, 510, 510, 515, 525, 525, 525, 535, 549, 550, 570, 570,
    575, 575, 580, 590, 600, 600, 600, 600, 615, 615
]
rent_list_np = numpy.array(rent_list)
median = statistics.median(rent_list)
mean = statistics.mean(rent_list)
mode = statistics.mode(rent_list)
variance = statistics.variance(rent_list)
coefficient_of_variance = (statistics.stdev(rent_list) / mean) * 100
twenty_fifth_percentile = numpy.percentile(rent_list_np, 25)
fifty_percentile = numpy.percentile(rent_list_np, 50)
seventy_fifth_percentile = numpy.percentile(rent_list_np, 75)

print("The mean is : ", mean)
print("The median is : ", median)
print("The mode is : ", mode)

res = stats.relfreq(rent_list, numbins=9)
start = res.lowerlimit
gap = res.binsize
cumilative_frequency = res.frequency[0]
print("    Range       Frequency    CF")
for i in range(len(res.frequency)):
    print(
Ejemplo n.º 38
0
"plot.py"

from statistics import mean, median, stdev
from matplotlib import pyplot as plt

file = open("packages_time.txt", "r")
pack_times = []
for val in file.read().split():
    pack_times.append(int(val))
file.close()

mean_value = mean(pack_times)
min_value = min(pack_times)
max_value = max(pack_times)
median_value = median(pack_times)
std_dev = stdev(pack_times)

print("Mean value : %f \nMin value : %f\nMax value : %f\nMedian value : %f" %
      (mean_value, min_value, max_value, median_value))
print("Standard deviation : %f" % std_dev)
plt.scatter(list(range(0, len(pack_times))), pack_times)
plt.xlabel('No of package')
plt.ylabel('Time received package in us')
plt.text(
    1,
    min_value + 120,
    "Duration: 7200 sec\nInterval: 0.1 sec\nBMean value: %f us\nMin value: %f us\nMax value: %f us\nMedian value: %f us"
    % (mean_value, min_value, max_value, median_value),
    bbox=dict(facecolor='green', alpha=0.1))
plt.show()
Ejemplo n.º 39
0
print ("Wektor: ",numbers)

minimum = min(numbers)
maximum = max(numbers)
print ("Min: ", minimum)
print ("Max: ", maximum)

newNumbers = sorted(numbers)

print ("Posortowany wektor: ",newNumbers)

srednia = np.average(newNumbers)
print ("Średnia: ",srednia)

odchylenieStandardowe = statistics.stdev(numbers)
print("Odchylenie standardowe: ",odchylenieStandardowe)

#wektorZnormalizowany
wektorZnormalizowany = []
for x in range(0, 29):
    wZ = (newNumbers[x]-minimum)/(minimum-maximum)
    wektorZnormalizowany.append(wZ)
#Wektor standaryzowany
for x in range(0, 29):
    wS = (newNumbers[x]-srednia)/odchylenieStandardowe
    wektorZnormalizowany.append(wS)

print("Wektor znormalizowany: ", wZ)

print("Wektor standaryzowany: ", wS)
    def process(self, number_of_parts, width, height, crf_value, idr_interval, model):
        """Do the necessary crf encodings and assessments
        :param number_of_parts: Number of part/segment for the analysis
        :type number_of_parts: int
        :param width: Width of the CRF encode
        :type width: int
        :param height: Height of the CRF encode
        :type height: int
        :param crf_value: Constant Rate Factor: this is a constant quality factor, see ffmpeg.org for more documentation on this parameter
        :type crf_value: int
        :param idr_interval: IDR interval in seconds
        :type idr_interval: int
        :param model: linear (True) or for each (False)
        :type model: bool
        """

        # Start by probing the input video file
        input_probe = Probe(self.input_file_path)
        input_probe.execute()

        crf_bitrate_list = []
        part_duration = input_probe.duration/number_of_parts
        idr_interval_frames =  idr_interval*input_probe.framerate #rcl: An IDR frame is a special type of I-frame in H.264. An IDR frame specifies that no frame after the IDR frame can reference any frame before it. This makes seeking the H.264 file easier and more responsive in the player.
        #As I have an IDR_FRAME every 2 seconds, I can find out the number of frame between two IDR using framerate !

        # Start Analysis
        for i in range(0,number_of_parts):
            part_start_time = i*part_duration #select extracts to encode

            # Do a CRF encode for the input file
            crf_encode = CrfEncode(self.input_file_path, width, height, crf_value, idr_interval_frames, part_start_time, part_duration)
            crf_encode.execute()

            # Get the Bitrate from the CRF encoded file
            crf_probe = Probe(crf_encode.output_file_path)
            crf_probe.execute()

            # Remove temporary CRF encoded file
            os.remove(crf_encode.output_file_path)

            # Set the crf bitrate
            crf_bitrate_list.append(crf_probe.bitrate)

        # Calculate the average bitrate for all CRF encodings
        self.average_bitrate = statistics.mean(crf_bitrate_list)
        self.peak_bitrate = max(crf_bitrate_list)

        if number_of_parts > 1:
            # Calculate the the standard deviation of crf bitrate values
            self.standard_deviation = statistics.stdev(crf_bitrate_list)

            weight = 1
            weighted_bitrate_sum = 0
            weighted_bitrate_len = 0

            # Giving weight for each bitrate based on the standard deviation
            for bitrate in crf_bitrate_list:
                if bitrate > (self.average_bitrate + self.standard_deviation):
                    weight = 4
                elif bitrate > (self.average_bitrate + self.standard_deviation/2):
                    weight = 2
                elif bitrate < (self.average_bitrate - self.standard_deviation/2):
                    weight = 0.5
                elif bitrate < (self.average_bitrate - self.standard_deviation):
                    weight = 0
                else:
                    weight = 1

                weighted_bitrate_sum += weight*bitrate
                weighted_bitrate_len += weight

            # Set the optimal bitrate from the weighted bitrate of all crf encoded parts
            self.optimal_bitrate = weighted_bitrate_sum/weighted_bitrate_len

        else:
            # Set the optimal bitrate from the only one crf result
            self.optimal_bitrate = self.average_bitrate

        if not model:
            print('        ',width,'x',height,'                ',self.optimal_bitrate*1e-3,'kbps                       encode_for_each','/ nbr part:',number_of_parts,'      ')

        if model:
            # We calculate optimal bitrate of the the remaining profiles using bitrate factor
            self.set_bitrate(number_of_parts)

        # Adding results to json
        result = {}
        result['processing_date'] = str(datetime.datetime.now())
        result['parameters'] = {}
        result['parameters']['method'] = "CRF"
        result['parameters']['width'] = width
        result['parameters']['height'] = height
        result['parameters']['crf_value'] = crf_value
        result['parameters']['idr_interval'] = idr_interval
        result['parameters']['number_of_parts'] = number_of_parts
        result['parameters']['part_duration'] = part_duration
        result['bitrate'] = {}
        result['bitrate']['optimal'] = self.optimal_bitrate
        result['bitrate']['average'] = self.average_bitrate
        result['bitrate']['peak'] = self.average_bitrate
        result['bitrate']['standard_deviation'] = self.standard_deviation
        result['optimized_encoding_ladder'] = {}
        if model == "True":
            result['optimized_encoding_ladder']['model'] = "linear"
        if model == "False":
            result['optimized_encoding_ladder']['model'] = "encode_for_each"

        self.json['analyses'].append(result)
Ejemplo n.º 41
0
import random
import plotly.express as px
import plotly.figure_factory as ff
import statistics
import plotly.graph_objects as go
count = []
diceresult = []
for i in range(0, 100):
    dice1 = random.randint(1, 6)
    dice2 = random.randint(1, 6)
    diceresult.append(dice1 + dice2)
    count.append(i)
#fig=px.bar(x=diceresult,y=count)
mean = sum(diceresult) / len(diceresult)
stdevation = statistics.stdev(diceresult)
median = statistics.median(diceresult)
mode = statistics.mode(diceresult)
print(mean)
print(stdevation)
print(mode)
print(median)
fig = ff.create_distplot([diceresult], ['result'], show_hist=False)
#fig.show()
fsds, fsde = mean - stdevation, mean + stdevation
ssds, ssde = mean - (2 * stdevation), mean + (2 * stdevation)
tsds, tsde = mean - (3 * stdevation), mean + (3 * stdevation)

listofdatawithin1standarddevation = [
    result for result in diceresult if result > fsds and result < fsde
]
listofdatawithin2ndstandarddevation = [
    current_linear_plus_merge_io = []
    current_linear_plus_merge_t = []
    for i in x_range:
        current_binary_io.append(binary_dict[str(t)][str(i)]["I/Os"])
        current_binary_t.append(binary_dict[str(t)][str(i)]["time"])
        current_indexed_io.append(indexed_dict[str(t)][str(i)]["I/Os"])
        current_indexed_t.append(indexed_dict[str(t)][str(i)]["time"])
        current_linear_io.append(linear_dict[str(t)][str(i)]["I/Os"])
        current_linear_t.append(linear_dict[str(t)][str(i)]["time"])
        current_linear_plus_binary_io.append(linear_plus_binary_dict[str(t)][str(i)]["I/Os"])
        current_linear_plus_binary_t.append(linear_plus_binary_dict[str(t)][str(i)]["time"])
        current_linear_plus_merge_io.append(linear_plus_merge_dict[str(t)][str(i)]["I/Os"])
        current_linear_plus_merge_t.append(linear_plus_merge_dict[str(t)][str(i)]["time"])

    y_binary_io.append(statistics.mean(current_binary_io)/div)
    y_binary_io_std.append(statistics.stdev(current_binary_io))
    y_binary_t.append(statistics.mean(current_binary_t))
    y_binary_t_std.append(statistics.stdev(current_binary_t))

    y_indexed_io.append(statistics.mean(current_indexed_io)/div)
    y_indexed_io_std.append(statistics.stdev(current_indexed_io))
    y_indexed_t.append(statistics.mean(current_indexed_t))
    y_indexed_t_std.append(statistics.mean(current_indexed_t))

    y_linear_io.append(statistics.mean(current_linear_io)/div)
    y_linear_io_std.append(statistics.stdev(current_linear_io))
    y_linear_t.append(statistics.mean(current_linear_t))
    y_linear_t_std.append(statistics.stdev(current_linear_t))

    y_linear_plus_binary_io.append(statistics.mean(current_linear_plus_binary_io))
    y_linear_plus_binary_io_std.append(statistics.stdev(current_linear_plus_binary_io))
Ejemplo n.º 43
0
def problem4_2(ran_list):
    """ Compute the mean and standard deviation of a list of floats """
    print(statistics.mean(ran_list))
    print(statistics.stdev(ran_list))
# === 母集団の標準偏差: ===
# statistics.pstdev()

pstdev = statistics.pstdev(l)
print(pstdev)

"""母集団の標準偏差は母分散の平方根である"""
print(math.sqrt(pvariance))

# === 不偏分散・標本分散: ===
# statistics.variance()

variance = statistics.variance(l)
print(variance)

"""組み込み関数 sum() と len() を使って算出することもできる"""
my_variance = sum((x - sum(l) / len(l))**2 for x in l) / (len(l) - 1)
print(my_variance)

# === 標本標準偏差: ===
# statistics.stdev()

stdev = statistics.stdev(l)
print(stdev)

"""
標本標準偏差と呼ばれたり不偏標準偏差と呼ばれたりするが、
statistics.stdev() で取得できるのは上述の不偏分散の平方根
"""
print(math.sqrt(variance))
Ejemplo n.º 45
0
def main(commandline_arguments):
    comm_args = create_and_parse_argument_options(commandline_arguments)
    aln_name = str(comm_args.alignment_file).split("/")[-1].replace(
        ".fasta", "").replace(".fas", "")
    if comm_args.indelible_tree_file:
        indeli_trees = read_indeli_trees_file(comm_args.indelible_tree_file)
        for name, tree in indeli_trees.items():
            alignIO_out = read_align(comm_args.alignment_file +
                                     'INDELI_TRUE_' + name + '.fas')
            deepest_anc = find_deepest_ancestors(tree)
            gapped_sliced_alns = slice_by_anc(alignIO_out, deepest_anc)
            print(name, gapped_sliced_alns)
            with open(comm_args.alignment_file + 'tg_' + name + '.fas',
                      "w") as tagged_aln_file:
                for aln_group_name, alignment in gapped_sliced_alns.items():
                    for entry in alignment:
                        tagged_aln_file.write(
                            str(">" + str(aln_group_name) + "_" +
                                str(entry.id) + "\n" + entry.seq + "\n"))
        sys.exit()
    alignIO_out = read_align(comm_args.alignment_file)
    if comm_args.nucleotide_alignments:
        for sequence in alignIO_out:
            sequence.seq = sequence.seq.back_transcribe()
        tree = tree_construct(alignIO_out, nucl=True)
    else:
        tree = tree_construct(alignIO_out)

    if comm_args.split_by_tree:
        deepestanc_to_child = find_deepest_ancestors(tree)
        sliced_dict = slice_by_anc(alignIO_out, deepestanc_to_child)
        if comm_args.save_split_alignments:
            output_split_alignments(sliced_dict,
                                    comm_args.save_split_alignments,
                                    comm_args.alignment_file)
            sys.exit()
    else:
        sliced_dict = slice_by_name(alignIO_out)

    if comm_args.dhat:
        # #D hat here figure out why it always ranges between 0.4-0.7
        seq_names = list()
        group_names = list()
        for group in sliced_dict:
            if group[-1] == 'b':
                continue
            group_names.append(group)
            seq_names.append([x.id for x in sliced_dict[group]])
        dhat = calc_d_hat(tree, seq_names[0], seq_names[1])
        named_intra1, intra1 = pairwise_dist(tree, seq_names[0])
        named_intra2, intra2 = pairwise_dist(tree, seq_names[1])
        print(aln_name, "\t", ' '.join(group_names), dhat)
        print(named_intra1, intra1)
        print(named_intra2, intra2)
        sys.exit()

    wei_vr_dict, pairwise_dict, pairwise_lists = {}, {}, {}
    for alngroup_name in sliced_dict:
        tree = tree_construct(sliced_dict[alngroup_name])
        seq_names = tree.get_terminals()
        pairwise_dict[alngroup_name], pairwise_lists[
            alngroup_name] = pairwise_dist(tree, seq_names)
        wei_vr_dict[alngroup_name] = generate_weight_vectors(tree)

    print(aln_name, end="\t")
    for group in pairwise_lists:
        print(group,
              mean(pairwise_lists[group]),
              stdev(pairwise_lists[group]),
              end="\t")
        #print(group, [x for x in pairwise_lists[group]])
    print()
Ejemplo n.º 46
0
def generateRooflinePoints(kernelMetrics):
    """
    Generates roofline points from a set of kernel metrics
    The flops type is automatically selected to be the one
    with the highest throughput
    """

    rooflines = dict()
    memRooflines = dict()

    # one point for each kernel
    # runs are averaged
    for kernel in kernelMetrics:
        logging.debug("Starting roofline generation for kernel {}".format(kernel))
        # figure out which flops is highest
        flops = dict()
        for flopsMetric in rooflineMetricsFlops:
            if flopsMetric in kernelMetrics[kernel]:
                flops[flopsMetric] = statistics.mean(kernelMetrics[kernel][flopsMetric]) * flopsMultipliers[flopsMetric]

        if len(flops) == 0:
            logging.debug("flops for {} empty skipping".format(kernel))
            continue

        flopsMetric = max(flops, key=flops.get)	

        # figure out which throughput is highest
        throughput = dict()
        for memMetric in rooflineMetricsMem:
            if memMetric in kernelMetrics[kernel]:
                throughput[memMetric] = statistics.mean(kernelMetrics[kernel][flopsMetric])

        if len(throughput) == 0:
            logging.debug("Throughput for {} empty skipping".format(kernel))
            continue

        memMetric = max(throughput, key=throughput.get)

        durationList    = kernelMetrics[kernel]["Duration"]
        flopsList       = kernelMetrics[kernel][flopsMetric]
        memList         = kernelMetrics[kernel][memMetric]
        # really should use numpy for this but some systems don't have it installed
        flopsPerSecList = [flops / duration if duration > 0 else 0 for flops, duration in zip(flopsList, durationList)]
        throughputList  = [mem   / duration if duration > 0 else 0 for mem,   duration in zip(flopsList, memList)]

        #[flops / duration for flops, duration in zip

        # calculate intensity for each memory type
        # and add it to the list
        for memMetric in rooflineMetricsMem:
            logging.debug("Working on memory metric {}".format(memMetric))
            if memMetric in kernelMetrics[kernel]:
                #intensity = flops / statistics.mean(kernelMetrics[kernel][memMetric])
                intensityList = [flops / data if data > 0 else 0 for flops, data in  zip (flopsList, kernelMetrics[kernel][memMetric])]
                invIntensityList = [data / flops if flops > 0 else 0 for flops, data in zip(flopsList, kernelMetrics[kernel][memMetric])]
                #intensityList = flopsList / np.array(kernelMetrics[kernel][memMetric])
                flopsInfo = abbrMetricNames[memMetric] + " " + abbrMetricNames[flopsMetric] + "/" + kernel
                #print("kernel info {}".format(flopsInfo))
                intensityStdDev = 0
                flopsPerSecStdDev = 0
                if len(intensityList) > 1:
                    intensityStdDev = statistics.stdev(intensityList)
                    invIntensityStdDev = statistics.stdev(invIntensityList)
                    flopsPerSecStdDev = statistics.stdev(flopsPerSecList)
                    throughputStdDev = statistics.stdev(kernelMetrics[kernel][memMetric])

                rooflines[flopsInfo] = [statistics.mean(intensityList),  statistics.mean(flopsPerSecList),
                                         intensityStdDev, flopsPerSecStdDev]
                memRooflines[flopsInfo] = [statistics.mean(invIntensityList), statistics.mean(kernelMetrics[kernel][memMetric]),
                                           invIntensityStdDev, throughputStdDev]

    return rooflines, memRooflines
from statistics import mean, median, mode, stdev, variance

pointers = [4, 5, 10, 2, 3, 5, 4, 3, 2, 5, 1, 0]
print('Mean: {0}'.format(mean(pointers)))
print('Median: {0}'.format(median(pointers)))
print('Mode: {0}'.format(mode(pointers)))

print('Std: {0}'.format(stdev(pointers)))
print('Variance: {0}'.format(variance(pointers)))

Ejemplo n.º 48
0
def simul_with_err2_vecchia(N1=100, N2=100, p=0.33, t=10000, rip=10):
    from one_simulation import R_one_simulation, N_one_simulation
    import my_print as my
    import math
    #import time
    import statistics

    #start = time.time()
    S1 = int(math.sqrt(N1))
    S2 = int(math.sqrt(N2))
    print("S1 = {}".format(S1))
    print("S2 = {}".format(S2), '\n')
    dir_name = repr(N1) + '-' + repr(N2) + '-b'

    eps = 1
    R_S_list = ([], [])
    R_m_Pn1 = [[] for x in range(0, N1)]
    R_m_Pn2 = [[] for x in range(0, N2)]
    R_mean_Pn1 = []
    R_mean_Pn2 = []

    N_S_list = ([], [])
    N_m_Pn1 = [[] for x in range(0, N1)]
    N_m_Pn2 = [[] for x in range(0, N2)]
    N_mean_Pn1 = []
    N_mean_Pn2 = []
    for i in range(0, rip):
        (R_S_mean1, R_S_mean2, R_Pn1, R_Pn2,
         simulation_t) = R_one_simulation(N1,
                                          N2,
                                          S1,
                                          S2,
                                          eps,
                                          p,
                                          t,
                                          dir_name,
                                          i + 1,
                                          flag=True)
        (N_S_mean1, N_S_mean2, N_Pn1, N_Pn2,
         simulation_t) = N_one_simulation(N1,
                                          N2,
                                          S1,
                                          S2,
                                          eps,
                                          p,
                                          t,
                                          dir_name,
                                          i + 1,
                                          flag=True)

        for k in range(len(N_Pn1)):
            N_m_Pn1[k].append(N_Pn1[k])
        for k in range(len(N_Pn2)):
            N_m_Pn2[k].append(N_Pn2[k])

        N_S_list[0].append(N_S_mean1)
        N_S_list[1].append(N_S_mean2)

        for k in range(len(R_Pn1)):
            R_m_Pn1[k].append(R_Pn1[k])
        for k in range(len(R_Pn2)):
            R_m_Pn2[k].append(R_Pn2[k])

        R_S_list[0].append(R_S_mean1)
        R_S_list[1].append(R_S_mean2)

    N_info_hist = {
        'xlab': 'Numero individui n',
        'ylab_1': 'Densità di prob P1',
        'tit1': 'PDF specie I gruppo nested con eps = ' + repr(eps),
        'ylab_2': 'Densità di prob P2',
        'tit2': 'PDF specie II gruppo nested con eps = ' + repr(eps)
    }
    R_info_hist = {
        'xlab': 'Numero individui n',
        'ylab_1': 'Densità di prob P1',
        'tit1': 'PDF specie I gruppo random con eps = ' + repr(eps),
        'ylab_2': 'Densità di prob P2',
        'tit2': 'PDF specie II gruppo random con eps = ' + repr(eps)
    }

    for k in range(N1):
        N_mean_Pn1.append(statistics.mean(N_m_Pn1[k]))

    for k in range(N2):
        N_mean_Pn2.append(statistics.mean(N_m_Pn2[k]))

    my.plot_hist(N_mean_Pn1, N_mean_Pn2, 'N', dir_name, N_info_hist)

    N_S_mean_1 = statistics.mean(N_S_list[0])
    N_S_dev_1 = statistics.stdev(N_S_list[0])
    if N_S_dev_1 == 0:
        N_S_dev_1 = 0.001

    N_S_mean_2 = statistics.mean(N_S_list[1])
    N_S_dev_2 = statistics.stdev(N_S_list[1])
    if N_S_dev_2 == 0:
        N_S_dev_2 = 0.001

    for k in range(N1):
        R_mean_Pn1.append(statistics.mean(R_m_Pn1[k]))

    for k in range(N2):
        R_mean_Pn2.append(statistics.mean(R_m_Pn2[k]))

    my.plot_hist(R_mean_Pn1, R_mean_Pn2, 'R', dir_name, R_info_hist)

    R_S_mean_1 = statistics.mean(R_S_list[0])
    R_S_dev_1 = statistics.stdev(R_S_list[0])
    if R_S_dev_1 == 0:
        R_S_dev_1 = 0.001

    R_S_mean_2 = statistics.mean(R_S_list[1])
    R_S_dev_2 = statistics.stdev(R_S_list[1])
    if R_S_dev_2 == 0:
        R_S_dev_2 = 0.001

    N_eps_S = (eps, N_S_mean_1, N_S_dev_1, N_S_mean_2, N_S_dev_2)
    R_eps_S = (eps, R_S_mean_1, R_S_dev_1, R_S_mean_2, R_S_dev_2)

    return (N_eps_S, R_eps_S)
def polynomial(request):
    print("polynomial was called")
    if request.method == 'POST':
        form = FileForm(request.POST, request.FILES)
        if form.is_valid():
            sampleCsvFile = form.cleaned_data['sampleCsvFile']
            dataframe = pd.read_csv("media/" +
                                    str(form.cleaned_data['sampleCsvFile']))

            dataset = dataframe.values
            labels = dataframe.columns.values.tolist()
            data_json = dataframe.to_json()

            dataList = []
            factorList = []
            label_num = []
            labels_str = []

            for i in range(0, dataset.shape[1]):
                if (not isinstance(dataset[0][i], str)):
                    label_num.append(labels[i])
                    sitenum = 0
                    max, min = 0, 0

                    sum = 0
                    datavec = []
                    for j in range(0, dataset.shape[0]):

                        datavec.append(dataset[j][i])
                        if (sitenum == 0):
                            max, mean, min = dataset[j][i], dataset[j][
                                i], dataset[j][i]

                        else:
                            if (dataset[j][i] > max):
                                max = dataset[j][i]
                            if (dataset[j][i] < min):
                                min = dataset[j][i]
                        sum = sum + dataset[j][i]
                        sitenum = sitenum + 1

                    dataList.append({
                        "parameter": labels[i],
                        "datavec": datavec,
                        "median": statistics.median(datavec),
                        "mean": statistics.mean(datavec),
                        "stdev": statistics.stdev(datavec),
                        "max": max,
                        "min": min,
                        "points": len(datavec),
                        "P99": np.percentile(datavec, 99),
                        "P95": np.percentile(datavec, 95),
                        "P75": np.percentile(datavec, 75),
                        "P25": np.percentile(datavec, 25),
                        "P05": np.percentile(datavec, 5),
                        "P01": np.percentile(datavec, 1),
                    })
                else:
                    labels_str.append(labels[i])
                    datavec = []
                    for j in range(0, dataset.shape[0]):
                        datavec.append(dataset[j][i])
                    factorList.append({
                        "parameter": labels[i],
                        "datavec": datavec
                    })
            return render(
                request, 'data_model/polynomial.html', {
                    'form': form,
                    'dataList': dataList,
                    'factorList': factorList,
                    'data_json': data_json,
                    'dataset': dataset,
                    'labels_all': labels,
                    'labels': label_num,
                    'labels_str': labels_str,
                    'range': range(dataset.shape[1])
                })

        else:
            print("form is not valid!")
            return render(request, 'data_model/polynomial.html',
                          {'form': form})
    else:
        form = FileForm()
    return render(request, 'data_model/polynomial.html', {'form': form})
Ejemplo n.º 50
0
scalingCoef = 0.15660133 / 7.0
spanTest = []
for i in range(200):
    spanTest.append(span_data[i] - (1.06 / 7) * span_data[i])
    finalCorrelation.append(correlationCoefTest[i][99] -
                            scalingCoef * span_data[i])

#Scaling coefficient
coef100 = 74.71029758
coef0 = 26.79919337

#Correlation process from correlation coeficients
for i in range(200):
    correlationTest.append(
        np.correlate(pressure_data[i], pressure_data[0]) * (10**(-9)) /
        statistics.stdev(pressure_data[0]) *
        statistics.stdev(pressure_data[i]) / 1.35200046)

#Plot properties
plt.style.use(["ggplot"])
plt.figure(figsize=(16, 9))
plt.ylim(bottom=0, top=1)
plt.xlim(left=0, right=8)
plt.xlabel('Z/D', fontsize=18)
plt.ylabel('R', fontsize=18)
plt.tick_params(labelsize=18)

#Experimental data plot
plt.scatter(span_data_blue,
            correlation_blue,
            s=100,
Ejemplo n.º 51
0
import statistics

i = [1, 2, 5, 6, 12, 19, 21, 27, 45, 75, 150]
AVG = sum(i) / len(i)
dev = statistics.stdev(i)
print(AVG)
print(dev)
l = []
for I in i:
    l.append((I - AVG) / dev + 10)
norm = [float(i) / sum(l) * 100 for i in l]
print(l)
Ejemplo n.º 52
0
 def getstdev(arg):
     if len(arg) == 0:
         return 0
     else:
         return round(statistics.stdev(arg),2)
                gamma=gamma)

svmLib.clear_folds()

print 'fitting parameters...'

parameters = svmLib.fit_polynomial_parameters()

print 'parameters fitted', parameters

print 'testing model...'

errors = svmLib.test_polynomial_model(parameters["c"], parameters["g"],
                                      parameters["d"])

print 'model tested', errors

mean = statistics.mean(errors)
standard_deviation = statistics.stdev(errors)

print 'mean', mean
print 'standard deviation', standard_deviation

f = open('svm_polynomial_kernel_results.txt', 'w')
f.write('C: ' + str(parameters['c']) + '\n')
f.write('gamma: ' + str(parameters['g']) + '\n')
f.write('degree: ' + str(parameters['d']) + '\n')
f.write('Mean: ' + str(mean) + '\n')
f.write('Standard deviation: ' + str(standard_deviation) + '\n')
f.close()
    case: Series = case.T
    control = control.T
    dev_sum = case.std() + control.std()
    query_signature = (case.mean() - control.mean()) / dev_sum
    query_signature.loc[isinf(query_signature)] = nan
    return query_signature.T


@jit
def signal_to_noise(case, control):
    """Calculates SNR as ratio of means difference and deviation sum.

    Case and control has to be tuples or other hashable iterable.

    Assumes that there are:
        - at least two samples in both case and control
        - the samples have non-zero variation
    """
    dev_sum = (stdev(case) + stdev(control))
    return ((mean(case) - mean(control)) / dev_sum) if dev_sum else nan


@jit()
def fold_change(case, control):
    return mean(case) / mean(control)


@jit()
def signed_fold_change(case, control):
    return abs(mean(case) / mean(control)) * sign(mean(case))
Ejemplo n.º 55
0
def game():

    random.seed()
    timeout = 3 # Set timeout for each action  
    total_moves = 0
    total_score = 0
    time.sleep(1)
    os.system('mpg321 Audio/startgame.mp3')
    time.sleep(1)
    
    while total_moves < 10: # 10 moves in one game

        action_num = random.randint(1,3) # generate move randomly
        if action_num == 1:
            action = "Shake"
            os.system('mpg321 Audio/shake.mp3')

        elif action_num == 2:
            action = "Raise"
            os.system('mpg321 Audio/raise.mp3')

        else:
            action = "Button"
            os.system('mpg321 Audio/button.mp3')
        print("Do action: "+action)
        
        user_action = 0
        start_action_time = time.time()
        elapsed_action_time = time.time() - start_action_time
        action_done = False

        while (elapsed_action_time <= timeout) and (action_done is False):

            # check if Shake or Raise moves are detected (done with accelerometer)
            if (action_done is False):
                x_out = []
                y_out = []
                z_out = []

                for j in range(0,29): # take standard deviation over the past 30 readings in order to tell weather Shake or Raise actions were detected - raw readings were too inaccurate
                    x, y, z = accelerometer_fn.read_xyz()
                    x_out.append(x)
                    y_out.append(y)
                    z_out.append(z)

                x_stdev = stdev(x_out)
                y_stdev = stdev(y_out)
                z_stdev = stdev(z_out)

                if x_stdev > 10000 or y_stdev > 10000 or z_stdev > 10000:
                    action_done = True
                    user_action = "Shake"

                if (x_stdev > 3000 and x_stdev < 7000) or (y_stdev > 3000 and y_stdev < 7000) or (z_stdev > 3000 and z_stdev < 7000):
                    action_done = True
                    user_action = "Raise"

                # clear arrays to ensure next reading is correct
                x_out.clear()
                y_out.clear()
                z_out.clear()
                elapsed_action_time = time.time() - start_action_time          

            # check if Button move is detected
            if (action_done is False):
                input_state = GPIO.input(10)

                if input_state == True:
                    action_done = True
                    user_action = "Button"

            elapsed_action_time = time.time() - start_action_time
            
        # When the button is pressed, the accelerometer detects the board moving, detecting Shake or Raise insted. So check if the botton is pressed after 0.1s of the deteced move
        loopstart_time = time.time()
        loop_time = time.time() - loopstart_time
        if action_done is True and (user_action == "Shake" or user_action == "Raise") and loop_time < 0.1:
            input_state = GPIO.input(10)
            loop_time = time.time() - loopstart_time

            if input_state == True:
                action_done = True
                user_action = "Button"
   
        # Send game data to the database. 
        if action_done is False:
            print("No action") # no move detected within timeout 
            mqtt_senddata.sendmove(action, timeout, constants_game.player, False)

        elif user_action == action:
            print("Right action "+str(action)) # right move is detected
            total_score += 100 # 100 points for right move
            if elapsed_action_time < 1:
                total_score += 50 # plus 50 points for right move done within one second 
            print("score = "+str(total_score))
            mqtt_senddata.sendmove(action, elapsed_action_time, constants_game.player, True)

        else:
            total_score -= 20 # -20 penalty for performing wrong action
            print("Wrong action. Had to do "+str(action)+ ", done "+str(user_action)+" instead\n") # wrong move is detected
            mqtt_senddata.sendmove(action, timeout, constants_game.player, False)

        total_moves += 1
        user_action = 0
        time.sleep(2) #at the end of the move, wait before the next one

    if total_score >= 1000:
        mqtt_senddata.sendgame(True, total_score, constants_game.player)
    else:
        mqtt_senddata.sendgame(False, total_score, constants_game.player)
Ejemplo n.º 56
0
from pylab import axes
import matplotlib.pyplot as plt
import statistics

f_origin = open("./full_dataorigin", "r")
f_addscore = open("./full_addscore", "r")
f_scorenet = open("./full_scorenet", "r")

origin = []
for line in f_origin:
    origin.append(float(line))
m = statistics.mean(origin)
sd = statistics.stdev(origin, xbar=m)
print(m)
print(sd)
addscore = []
for line in f_addscore:
    addscore.append(float(line))
m = statistics.mean(addscore)
sd = statistics.stdev(addscore, xbar=m)
print(m)
print(sd)
scorenet = []
for line in f_scorenet:
    scorenet.append(float(line))
m = statistics.mean(scorenet)
sd = statistics.stdev(scorenet, xbar=m)
print(m)
print(sd)

plt.boxplot([origin, addscore, scorenet])
Ejemplo n.º 57
0
                                                 dataset,
                                                 clasificadorSL_NB,
                                                 seed=seed)
aciertos_particion = [(1 - elem) for elem in errores_particion]

#if clasificadorSL_NB.Multinomial_flag == True:
#    print("MultinomialNB")
#else:
print("GaussianNB")
#print("Errores particion: " + str(errores_particion))
#print("Media: " + str(statistics.mean(errores_particion)))
#print("Desv tipica: " + str(statistics.stdev(errores_particion)))

print("Aciertos particion: " + str(aciertos_particion))
print("Media: " + str(statistics.mean(aciertos_particion)))
print("Desv tipica: " + str(statistics.stdev(aciertos_particion)))
print("matriz confusion:\n" + str(clasificadorSL_NB.matriz_confusion))
print()
'''
# Prueba clasificador KNN
clasificadorSL_KNN = ClasificadorSL.ClasificadorKNN_SL("distance", 3)

errores_particion = clasificadorSL_KNN.validacion(val_cruzada, dataset, clasificadorSL_KNN, seed=seed)
aciertos_particion = [(1 - elem) for elem in errores_particion]

print("KNN")
#print("Errores particion: " + str(errores_particion))
#print("Media: " + str(statistics.mean(errores_particion)))
#print("Desv tipica: " + str(statistics.stdev(errores_particion)))

print("Aciertos particion: " + str(aciertos_particion))
Ejemplo n.º 58
0
parser.add_argument('--file', help='Input file')
parser.add_argument('--out', help='Outfile base')
parser.add_argument('--L', help='Fingerprint length')
parser.add_argument('--norm', help='Normalize')
args = parser.parse_args()

a = AnnoyIndex(int(args.L))
i = 0
names = []

with gzip.open(args.file, 'rt') as f:
    for line in f:
        id, statements, *v = line.split("\t")
        id = re.sub('.json.gz', '', id)
        id = re.sub('\.', '|', id)
        names.append(id)
        v = [float(j) for j in v]
        if args.norm:
            avg = statistics.mean(v)
            std = statistics.stdev(v)
            v = [(j - avg) / std for j in v]
        a.add_item(i, v)
        i = i + 1

a.build(-1)
a.save(args.out + '.tree')

with open(args.out + '.names', 'w') as f:
    for item in names:
        f.write("%s\n" % item)
Ejemplo n.º 59
0
def _mean_stdev(values):
    mean = statistics.mean(values)
    stdev = statistics.stdev(values, xbar=mean)
    return (mean, stdev)
Ejemplo n.º 60
0
    for _ in range(args.num_trials):
        m = Manager()
        return_dict = m.dict()
        manager = Process(target=dealer_executor,
                          kwargs={
                              "f_all": f_all(),
                              "args_all": args_all(),
                              "kwargs_all": kwargs_all(),
                              "num_tasks": args.num_tasks,
                              "port": args.client_port,
                              "interchange": args.interchange,
                              "warmup": args.warmup,
                              "return_dict": return_dict
                          })
        manager.start()
        manager.join()

        serialization_times.append(return_dict["avg_serialization_time"])
        execution_times.append(return_dict["avg_execution_time"])

    # Print stats
    label = "[DEALER-INTERCHANGE-REP]" if args.interchange else "[DEALER-REP]"
    s = stdev(serialization_times) if len(serialization_times) > 1 else 0
    print("{} Avg Serialization Time\n"
          "Mean = {:=10.4f} us, Stdev = {:=10.4f} us".format(
              label, mean(serialization_times), s))
    s = stdev(execution_times) if len(execution_times) > 1 else 0
    print("{} Avg Execution Time\n"
          "Mean = {:=10.4f} us, Stdev = {:=10.4f} us".format(
              label, mean(execution_times), s))