def _setPortfolioInfo(self):
     port_annual_var: float = round(np.dot(self._weights.T, np.dot(self._dataSimpleCovarianceAnnual, self._weights)), 5)
     port_annual_volatility: float = round(np.sqrt(port_annual_var), 5)
     port_annual_simple_ret: float = round(np.sum(self._stats.SimpleReturnsNan.mean() * self._weights) * 252, 5)
     print('Port Ann Ret', str(round(port_annual_var, 5)*100)+'%')
     print('Port Ann Volatility/ Risk', str(round(port_annual_volatility, 5)*100)+'%')
     print('Port Ann Variance', str(round(port_annual_simple_ret, 5)*100)+'%')
     '''
Пример #2
0
def f1(y_hat, y_true, THRESHOLD=0.5):
    '''
    y_hat是未经过sigmoid函数激活的
    输出的f1为Macro-F1
    '''
    epsilon = 1e-7
    y_hat = y_hat > THRESHOLD
    y_hat = np.int8(y_hat)
    tp = np.sum(y_hat * y_true, axis=0)
    fp = np.sum(y_hat * (1 - y_true), axis=0)
    fn = np.sum((1 - y_hat) * y_true, axis=0)

    p = tp / (tp + fp + epsilon)  # epsilon的意义在于防止分母为0,否则当分母为0时python会报错
    r = tp / (tp + fn + epsilon)

    f1 = 2 * p * r / (p + r + epsilon)
    f1 = np.where(np.isnan(f1), np.zeros_like(f1), f1)

    return np.mean(f1)
def printst(step, seg_source,seg_ed,seg_es,labeled,labeles):
 mm = seg_source[0, 1, :, :, :] * 1 + seg_source[0, 2, :, :, :] * 2 + seg_source[0, 3, :, :, :] * 3
 pt = mm.data.cpu().numpy()
 # pt = np.transpose(pt, (2, 1, 0))
 # pt = start_seg[0, 0, :, :, :].data.cpu().numpy()
 out = sitk.GetImageFromArray(pt)
 out.SetSpacing((1, 1, 1))
 sitk.WriteImage(out, './state/seg_source' + str(step) + '.nii')
    # pt=np.argmax(pt[1],axis=1)
    # out = sitk.GetImageFromArray(seg_source)
    # out.SetSpacing((1.000, 1.000, 1.000))
    # sitk.WriteImage(out, './state/seg-source' + str(step) + '.nii')

 dice_result_ed = []
 mm1 = seg_ed[0, 1, :, :, :] * 1 + seg_ed[0, 2, :, :, :] * 2 + seg_ed[0, 3, :, :, :] * 3
 mm2 = labeled[0, 1, :, :, :] * 1 + labeled[0, 2, :, :, :] * 2 + labeled[0, 3, :, :, :] * 3
 pt1 = mm1.data.cpu().numpy()
 pt1 = threshold(pt1)
 pt2 = mm2.data.cpu().numpy()
 # print(pt1.shape)
 # print(pt2.shape)
 # labeled=labeled.data.cpu.numpy()
 diceresult_ed = dice(pt1, pt2, nargout=1)
 dice_result_ed.append(diceresult_ed)
 dice_result_ed = np.array(dice_result_ed)
 dice_sum = np.sum(dice_result_ed, axis=0)
 print('step' + str(step) + 'diceresult_ed:|' + str(dice_sum))

 dice_result_es = []
 mm1 = seg_es[0, 1, :, :, :] * 1 + seg_es[0, 2, :, :, :] * 2 + seg_es[0, 3, :, :, :] * 3
 mm2 = labeles[0, 1, :, :, :] * 1 + labeles[0, 2, :, :, :] * 2 + labeles[0, 3, :, :, :] * 3
 pt1 = mm1.data.cpu().numpy()
 pt1=threshold(pt1)
 pt2=  mm2.data.cpu().numpy()
 # print(pt1.shape)
 # print(pt2.shape)
 # labeled=labeled.data.cpu.numpy()
 diceresult_es=dice(pt1,pt2,nargout=1)
 dice_result_es.append(diceresult_es)
 dice_result_es = np.array(dice_result_es)
 dice_sum = np.sum(dice_result_es, axis=0)
 print('step'+str(step)+'diceresult_es:|'+str(dice_sum))
Пример #4
0
    def run(self):
        folders = how_many_fatherFolder(self.path)

        for experiemnt in folders:
            logging.debug("Folder under analysis -> " + str(experiemnt))
            second_path = self.path + experiemnt + "/"
            res = how_many_folder(second_path)
            num_folder = len(res)
            logging.debug("Folder to analise -> " + str(num_folder))

            for el in res:
                path_here = second_path + str(el) + "/"

                names = []
                for i in os.listdir(path_here):
                    if os.path.isfile(
                            os.path.join(path_here, i)
                    ) and 'trajectory-generate-aSs-' in i and ".zip" in i:
                        names.append(i)

                names = sorted_nicely(names)

                pops = Populations()
                # find the trajectories ID and Points
                trajectories = self.read_trajectory_info(path_here +
                                                         "trajectory.zip")
                for tra in trajectories:
                    pops.add_population(Population(tra))

                number_of_trajectories = pops.get_number_trajectories()
                total_distances = []
                numb = 0
                logging.debug("Analysing Trajectories...")
                for i in tqdm.tqdm(range(len(names))):
                    name = names[i]

                    # obtain info from the file
                    individuals = self.read_info(path_here + name)

                    # store the msd per trajectory
                    distance_per_trajectories = {}
                    for j in range(number_of_trajectories):
                        distances = []
                        for indiv in individuals:
                            if indiv.trajectoryID == pops.get_population(
                                    j).tra.trajectoryID:
                                distances.append(indiv.MSD)

                        array = np.array(distances)
                        MSD = (np.sum(array)) / len(array)
                        distance_per_trajectories.update({j: MSD})
                    total_distances.append(distance_per_trajectories)

                self.print_graph(total_distances, path_here)
def dice(img1, img2, labels=None, nargout=1):
 '''
 Dice [1] volume overlap metric

 The default is to *not* return a measure for the background layer (label = 0)

 [1] Dice, Lee R. "Measures of the amount of ecologic association between species."
 Ecology 26.3 (1945): 297-302.

 Parameters
 ----------
 vol1 : nd array. The first volume (e.g. predicted volume)
 vol2 : nd array. The second volume (e.g. "true" volume)
 labels : optional vector of labels on which to compute Dice.
     If this is not provided, Dice is computed on all non-background (non-0) labels
 nargout : optional control of output arguments. if 1, output Dice measure(s).
     if 2, output tuple of (Dice, labels)

 Output
 ------
 if nargout == 1 : dice : vector of dice measures for each labels
 if nargout == 2 : (dice, labels) : where labels is a vector of the labels on which
     dice was computed
 '''
 if labels is None:
  labels = np.unique(np.concatenate((img1, img2)))  # 输出一维数组
  labels = np.delete(labels, np.where(labels == 0))  # remove background

 dicem = np.zeros(len(labels))
 for idx, lab in enumerate(labels):
  top = 2 * np.sum(np.logical_and(img1 == lab, img2 == lab))
  bottom = np.sum(img1 == lab) + np.sum(img2 == lab)
  bottom = np.maximum(bottom, np.finfo(float).eps)  # add epsilon. 机器最小的正数
  dicem[idx] = top / bottom

 if nargout == 1:
  return dicem
 else:
  return (dicem, labels)
Пример #6
0
 def _setMatrices(self, portfolio_data: DataFrame, log_ret: DataFrame,
                  cov_mat: DataFrame):
     for i in range(self._threshold):
         weight_arr: np.ndarray = np.random.uniform(
             size=len(portfolio_data.columns))
         weight_arr = weight_arr / np.sum(weight_arr)
         # saving weights in the array
         self._weight_matrix[i, :] = weight_arr
         # Portfolio Returns
         annual_weighted_log_ret: float = (
             (np.sum(log_ret.mean() * weight_arr)) + 1)**252 - 1
         # Saving Portfolio returns
         self._annual_weighted_log_return_matrix[
             i] = annual_weighted_log_ret
         # Saving Portfolio Risk
         portfolio_sd: float = np.sqrt(
             np.dot(weight_arr.T, np.dot(cov_mat, weight_arr)))
         self._risk_matrix[i] = portfolio_sd
         # Portfolio Sharpe Ratio
         # Assuming 0% Risk Free Rate
         sr: float = annual_weighted_log_ret / portfolio_sd
         self._sharpe_ratio_matrix[i] = sr
Пример #7
0
    def __init__(self, trajectoryID, classification, generetedPoint, realPoint):
        self.trajectoryID = trajectoryID
        self.classification = classification
        self.generetedPoint = generetedPoint
        self.realPoint = realPoint
        self.max_x = 0
        self.max_y = 0
        self.min_x = 0
        self.min_y = 0
        self.xs_real = []
        self.ys_real = []
        self.xs_generated = []
        self.ys_generated = []

        # compute max and minimum of the individual
        xs = []
        ys = []
        for el in self.realPoint:
            xs.append(el.x)
            ys.append(el.y)
            if el.x not in self.xs_real:
                self.xs_real.append(el.x)
            if el.y not in self.ys_real:
                self.ys_real.append(el.y)
        for el in self.generetedPoint:
            xs.append(el.x)
            ys.append(el.y)
            if el.x not in self.xs_generated:
                self.xs_generated.append(el.x)
            if el.y not in self.ys_generated:
                self.ys_generated.append(el.y)
        self.max_x = np.amax(np.array(xs))
        self.min_x = np.amin(np.array(xs))
        self.max_y = np.amax(np.array(ys))
        self.min_y = np.amin(np.array(ys))

        # compute mse points this trajectory
        distances = []
        for i in range(len(self.generetedPoint)):
            a = np.array((self.xs_real[i], self.ys_real[i]))
            b = np.array((self.xs_generated[i], self.ys_generated[i]))
            value = np.linalg.norm(a - b) * 100000
            distances.append(pow(value, 2))

        self.array = np.array(distances)
        self.MSD = (np.sum(self.array)) / len(self.array)
def evaluate(model, X_test, y_test):
    y_pred = model.predict(X_test)
    cf_matrix = confusion_matrix(y_test, y_pred)
    categories = ['Negative', 'Positive']
    group_names = ['True Neg', 'False Pos', 'False Neg', 'True Pos']
    group_percentages = [
        '{0:.2%}'.format(value)
        for value in cf_matrix.flatten() / np.sum(cf_matrix)
    ]
    labels = [f'{v1}\n{v2}' for v1, v2 in zip(group_names, group_percentages)]
    labels = np.asarray(labels).reshape(2, 2)

    sns.heatmap(cf_matrix,
                annot=labels,
                cmap='Blues',
                fmt='',
                xticklabels=categories,
                ytick=categories)

    plt.xlabel("Predicted values", fontdict={'size': 14}, labelpad=10)
    plt.xlabel("Actual values", fontdict={'size': 14}, labelpad=10)
    plt.xlabel("Confusion Matrix", fontdict={'size': 18}, labelpad=10)
Пример #9
0
def wgn(X, snr):
    P_signal = np.sum(abs(X)**2) / (len(X))
    P_noise = P_signal / 10**(snr / 10.0)
    #return np.random.randn(X.shape[1])*np.sqrt(P_noise)
    return np.random.randn(len(X)) * np.sqrt(P_noise)
 def __init__(self, y_stocks: list):
     self._a_float = 3 * math.log(y_stocks[0].TimeSpan.MonthCount)
     self._a_suffix = y_stocks[0].Column
     self._a_ts = y_stocks[0].TimeSpan
     self._a_length = len(y_stocks)
     iso_weight: float = round(1.0 / len(y_stocks), 3)
     self._stocks = y_stocks
     self._weights = np.array(len(y_stocks) * [iso_weight], dtype=float)
     self._basics = PortfolioBasics(y_stocks, self._a_float, self._legend_place)
     self._stats = PortfolioStats(self._weights, self._basics)
     self._final = PortfolioFinal(y_stocks, self._a_float, self._legend_place)
     print('Volatility\t\t\t\t\t', self._final.Volatility)
     print('Annual Expected Return\t\t', self._final.AnnualExpectedReturn)
     print('Risk Free Rate\t\t\t\t', self._final.RiskFreeRate)
     print('Free 0.005 Sharpe Ratio\t\t', self._final.Free005SharpeRatio)
     print('Kurtosis\n', self._final.KurtosisSeries)
     print('Skewness\n', self._final.SkewnessSeries)
     print('Frequency\n', self._final.Frequency)
     self._final.Plot().show()
     exit(1234)
     self._dataSimpleCorrelation = self._stats.SimpleReturnsNan.corr()
     self._dataSimpleCovariance = self._stats.SimpleReturnsNan.cov()
     self._dataSimpleCovarianceAnnual = self._dataSimpleCovariance * 252
     self._dataSimpleSummary = self._stats.SimpleReturnsNanSummary
     self._dataWeightedReturns = self._stats.SimpleWeightedReturns
     # axis =1 tells pandas we want to add the rows
     self._portfolio_weighted_returns = round(self._dataWeightedReturns.sum(axis=1), 5)
     print('7', self._portfolio_weighted_returns.head())
     print('7', self._stats.SimpleWeightedReturnsSum.head())
     #self._dataWeightedReturns['PORTFOLIOWeighted'] = portfolio_weighted_returns
     portfolio_weighted_returns_mean = round(self._portfolio_weighted_returns.mean(), 5)
     print('port_ret mean', portfolio_weighted_returns_mean)
     print(round(self._stats.SimpleWeightedReturnsSum.mean(), 5))
     portfolio_weighted_returns_std = round(self._portfolio_weighted_returns.std(), 5)
     print('port_ret std', portfolio_weighted_returns_std)
     self._portfolio_weighted_returns_cum: Series = round((self._portfolio_weighted_returns + 1).cumprod(), 5)
     #self._dataWeightedReturns['PORTFOLIOCumulative'] = self._portfolio_weighted_returns_cum
     print('$', self._dataWeightedReturns.head())
     self._portfolio_weighted_returns_geom = round(np.prod(self._portfolio_weighted_returns + 1) ** (252 / self._portfolio_weighted_returns.shape[0]) - 1, 5)
     print('geometric_port_return', self._portfolio_weighted_returns_geom)
     self._portfolio_weighted_annual_std = round(np.std(self._portfolio_weighted_returns) * np.sqrt(252), 5)
     print('port_ret annual', self._portfolio_weighted_annual_std)
     self._portfolio_weighted_sharpe_ratio = round(self._portfolio_weighted_returns_geom / self._portfolio_weighted_annual_std, 5)
     print('port_sharpe_ratio', self._portfolio_weighted_sharpe_ratio)
     print('%', self._stats.Returns.head())
     self._data_returns_avg = self._getDataReturnsAverage(self._stats.Returns)
     print('^', self._data_returns_avg.head())
     daily_log_pct_changes: DataFrame = np.log(self._stats.Returns.pct_change() + 1) #avant portfolio
     daily_log_pct_changes.columns = daily_log_pct_changes.columns + 'LogReturn'
     print('&', daily_log_pct_changes.head())
     daily_log_volatilities: DataFrame = (daily_log_pct_changes.std() * np.sqrt(252)).to_frame()
     daily_log_volatilities.columns = ['Volatility']
     print('*', daily_log_volatilities)
     port_daily_simple_ret: float = round(np.sum(self._stats.SimpleReturnsNan.mean()*self._weights), 5)
     port_weekly_simple_ret: float = round(4.856 * port_daily_simple_ret, 5)
     port_monthly_simple_ret: float = round(21 * port_daily_simple_ret, 5)
     port_quarterly_simple_ret: float = round(63 * port_daily_simple_ret, 5)
     port_yearly_simple_ret: float = round(252 * port_daily_simple_ret, 5)
     print('port_daily_simple_ret', str(100*port_daily_simple_ret) + '%')
     print('port_weekly_simple_ret', str(100*port_weekly_simple_ret) + '%')
     print('port_monthly_simple_ret', str(100*port_monthly_simple_ret) + '%')
     print('port_quarterly_simple_ret', str(100*port_quarterly_simple_ret) + '%')
     print('port_yearly_simple_ret', str(100*port_yearly_simple_ret) + '%')
     self._setPortfolioInfo()
     self._optimizer = PortfolioOptimizer(self._legend_place, self._a_float, self._stats, self._basics.Data)
     self._stock_market_index = SnP500Index('yahoo', "^GSPC", self._a_ts)
     self._linear_reg = PortfolioLinearReg(self._stock_market_index, self._stats.Returns)
     print(f'The portfolio beta is {self._linear_reg.Beta}, for each 1% of index portfolio will move {self._linear_reg.Beta}%')
     print('The portfolio alpha is ', self._linear_reg.Alpha)
     print('_', self._basics.DataLogReturns.head())
     cov_mat_annual = self._basics.DataLogReturns.cov() * 252
     print('-', cov_mat_annual)
Пример #11
0
    def run(self):
        folders = how_many_fatherFolder(self.path)
        folders = [s for s in folders if not re.search('txt', s)]
        folders = [s for s in folders if not re.search('jpg', s)]
        folders = [s for s in folders if not re.search('png', s)]

        for experiemnt in folders:
            logging.debug("Folder under analysis -> " + str(experiemnt))
            second_path = self.path + experiemnt + "/"
            res = how_many_folder(second_path)
            folders = [s for s in folders if not re.search('txt', s)]
            folders = [s for s in folders if not re.search('jpg', s)]
            folders = [s for s in folders if not re.search('png', s)]
            num_folder = len(res)
            logging.debug("Folder to analise -> " + str(num_folder))

            for el in res:
                logging.debug("Folder under analysis -> " + str(el))
                path_here = second_path + str(el) + "/"

                names = []
                for i in os.listdir(path_here):
                    if os.path.isfile(
                            os.path.join(path_here, i)
                    ) and 'trajectory-generate-aSs-' in i and ".zip" in i:
                        names.append(i)

                names = sorted_nicely(names)

                pops = Populations()
                # find the trajectories ID and Points
                trajectories = self.read_trajectory_info(path_here +
                                                         "trajectory.zip")
                for tra in trajectories:
                    pops.add_population(Population(tra))

                # analysing the fitness
                logging.debug("Analysing the fitness...")
                max_agent, max_classifier = self.find_max_values_fitness(
                    path_here)
                agent_generations_info, classifier_generations_info = self.read_fitness(
                    path_here, max_agent, max_classifier)

                x = np.arange(len(agent_generations_info))
                y_agent = []
                std_agent = []
                for element in agent_generations_info:
                    y_agent.append(element.mean)
                    std_agent.append(element.std)
                y_classifier = []
                std_classifier = []
                for element in classifier_generations_info:
                    y_classifier.append(element.mean)
                    std_classifier.append(element.std)

                # print fitnes
                self.print_fitnes(x, y_agent, std_agent, y_classifier,
                                  std_classifier, path_here)

                total_distances = []
                total_distances_msd = []
                std_distances = []
                last_generations_values = []
                logging.debug("Analysing Trajectories...")
                for i in tqdm.tqdm(range(len(names))):
                    name = names[i]

                    # obtain info from the file
                    individuals = self.read_info(path_here + name)

                    if i == len(names) - 1:
                        for ind in individuals:
                            for el in ind.array:
                                last_generations_values.append(el)

                    msds = []
                    for ind in individuals:
                        msds.append(ind.MSD)
                    total_distances.append(np.mean(np.array(msds)))
                    std_distances.append(np.std(np.array(msds)))

                    # store the msd per trajectory
                    distance_per_trajectories = {}
                    for j in range(number_of_trajectories):
                        distances = []
                        for indiv in individuals:
                            if indiv.trajectoryID == pops.get_population(
                                    j).tra.trajectoryID:
                                distances.append(indiv.MSD)

                        array = np.array(distances)
                        MSD = (np.sum(array)) / len(array)
                        distance_per_trajectories.update({j: MSD})
                    total_distances_msd.append(distance_per_trajectories)

                # print graph msd per trajectory
                self.print_graph_msd_per_trajectory(total_distances_msd,
                                                    path_here)

                # print graph total msd
                self.print_graph_msd_total(total_distance, std_distances,
                                           path_here)

                # save the last value
                array = np.array(last_generations_values)
                MSD = (np.sum(array)) / len(array)

                with open(path_here + "/MSD.txt", "w") as text_file:
                    text_file.write(str(MSD))
Пример #12
0
 def test_sanity_check_sum_of_bitmap(self):
     image = Image([[[1, 1, 0], [2, 2, 0], [3, 3, 0]]], 32, 32)
     self.assertEqual(32 * 32 - 3, np.sum(image.return_as_np()))
Пример #13
0
def meanAllAgents(old_path):
    logging.basicConfig(format='%(asctime)s %(message)s',
                        datefmt='%m/%d/%Y %I:%M:%S %p',
                        level=logging.DEBUG)

    folders = how_many_fatherFolder(old_path)
    dff = DataFrame(columns=['exp', 'trajectories', 'MSD'])
    iii = 0
    for experiemnt in folders:
        logging.debug("Folder under analysis -> " + str(experiemnt))
        name_experiement = str(experiemnt).replace("Experiment-tgcfs-",
                                                   "").replace("-ETH", "")
        second_path = old_path + experiemnt + "/"
        res = how_many_folder(second_path)
        num_folder = len(res)
        logging.debug("Folder to analise -> " + str(num_folder))

        for el in res:
            path = second_path + str(el) + "/"
            tratt = str(el)

            names = []
            for i in os.listdir(path):
                if os.path.isfile(
                        os.path.join(path, i)
                ) and 'trajectory-generate-aSs-' in i and ".zip" in i:
                    names.append(i)

            names = sorted_nicely(names)

            total_distances = []
            numb = 0
            logging.debug("Analysing Trajectories...")
            for i in tqdm.tqdm(range(len(names))):
                name = names[i]
                numb += 1
                # name = "trajectory-generatedPoints-" + str(numb) + "-" + str(numb) + ".zip"

                trajectories_label, json_file, id_label = reanInfo(path + name)
                # number = 0
                # while number < len(id_label):

                # real points
                lat_real = []
                lng_real = []
                # generated points
                lat_generated = []
                lng_generated = []

                label_real = []
                label_generated = []
                for labels in trajectories_label:
                    for el in json_file[labels]["real"]:
                        if el[0] not in lat_real:
                            lat_real.append(el[0])
                            lng_real.append(el[1])
                            label_real.append(json_file[labels]["id"])

                    for el in json_file[labels]["generated"]:
                        if el[0] not in lat_generated:
                            lat_generated.append(el[0])
                            lng_generated.append(el[1])
                            label_generated.append(json_file[labels]["id"])

                distance_per_trajectories = {}
                # now for every trajectory compute the distance of the generated distance
                for i in range(len(label_real)):
                    index = [
                        j for j, x in enumerate(label_generated)
                        if x == label_real[i]
                    ]
                    distances = []
                    for ind in index:
                        a = np.array((lat_real[i], lng_real[i]))
                        b = np.array((lat_generated[ind], lng_generated[ind]))
                        value = np.linalg.norm(a - b) * 100000
                        value = pow(value, 2)
                        distances.append(value)

                    array = np.array(distances)
                    MSD = (np.sum(array)) / len(array)
                    distance_per_trajectories.update({i: MSD})
                total_distances.append(distance_per_trajectories)
            #
            # df = DataFrame(columns=['gen', 'tra', 'MSD'])
            #
            # x = []
            # x = np.arange(0, len(total_distances))
            # i = 0
            # for el in total_distances:
            #     for k in el.keys():
            #         d = {"gen": i, "tra": k, "MSD": el[k]}
            #         dfs = DataFrame(data=d, index=[i])
            #         df = df.append(dfs)
            #     i += 1
            # sns.set_style("darkgrid")
            # df = df[df.columns].astype(float)
            # g = sns.lmplot(x="gen", y="MSD", hue="tra", data=df, scatter_kws={"s": 1}, fit_reg=False)

            last_line = total_distances[len(total_distances) - 1]
            arr = []
            for k in last_line.keys():
                arr.append(last_line[k])

            array = np.array(arr)
            MSD = (np.sum(array)) / len(array)
            logging.debug(MSD)

            dd = {"exp": name_experiement, "trajectories": tratt, "MSD": MSD}
            dfss = DataFrame(data=dd, index=[iii])
            iii += 1
            dff = dff.append(dfss)

            # df.plot(x='gen', y='MSD')
            # sns.lmplot(x="gen", y="MSD", hue="tra", data=df)

            # a = df.loc[df['tra'] == 0]
            # ax = a.plot(x='gen', y='MSD', kind='scatter', label="0")
            # for i in range(1, 5):
            #     a = df.loc[df['tra'] == i]
            #     a.plot(x='gen', y='MSD', ax=ax, kind='scatter',label=i)

            # g.set(ylim=(0, 300))

            # for j in range(5):
            #     a = df.loc[df['tra'] == j]
            #     a.plot(x='gen', y='MSD', ylim=(0,0.00000007))

            # plt.figure(0)
            # sns.set_style("darkgrid")
            # plt.errorbar(x, mean, std)
            # plt.errorbar(x, min)
            # plt.errorbar(x, max_value)
            # # plt.plot(median)
            # plt.legend(("mean Difference", "min Difference", "max Difference"))
            # plt.xlabel("Generation")
            # plt.ylabel("Distance (metres) point generated with real point")
            # plt.legend(("Max Distance", "Min Distance", "Median Distance"))

            # save_name = path + 'msd.png'
            # plt.savefig(save_name, dpi=500, facecolor='w', edgecolor='w', orientation='portrait', papertype=None,
            #             format=None, transparent=False, bbox_inches=None, pad_inches=0.1, frameon=None)
            # plt.close()
            # logging.debug("Graph saved!")

            # os.system("rm movie.mp4")
            # os.system("ffmpeg -f image2 -r 2 -i _tmp%05d.png -vcodec mpeg4 -y movie.mp4")
            # os.system("rm _tmp*.png")
            # logging.debug("End Program")
    sns.factorplot(x="exp",
                   y="MSD",
                   hue="trajectories",
                   data=dff,
                   kind="bar",
                   palette="muted")
    plt.show()
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import KNeighborsRegressor
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Ridge
from sklearn.linear_model import Lasso
from pandas import np
import matplotlib.pyplot as plt
X, y = mglearn.datasets.load_extended_boston()
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
lasso = Lasso().fit(X_train, y_train)
lasso001 = Lasso(alpha=0.01, max_iter=100000).fit(X_train, y_train)
print("lr.coef_: {}".format(lasso.coef_))
print("lr.intercept_: {}".format(lasso.intercept_))
print("Правильность на обучающем наборе: {:.2f}".format(lasso.score(X_train, y_train)))
print("Правильность на контрольном наборе: {:.2f}".format(lasso.score(X_test, y_test)))
print("Кол-во исп. признаков: {:.2f}".format(np.sum(lasso.coef_ != 0)))
print("\nalpha=0.1, max_iter=100000:")
print("Правильность на обучающем наборе: {:.2f}".format(lasso001.score(X_train, y_train)))
print("Правильность на контрольном наборе: {:.2f}".format(lasso001.score(X_test, y_test)))
print("Кол-во исп. признаков: {:.2f}".format(np.sum(lasso001.coef_ != 0)))

lasso00001 = Lasso(alpha=0.0001, max_iter=100000).fit(X_train, y_train)
print("\nalpha=0.0001, max_iter=100000:")
print("Правильность на обучающем наборе: {:.2f}".format(lasso00001.score(X_train, y_train)))
print("Правильность на контрольном наборе: {:.2f}".format(lasso00001.score(X_test, y_test)))
print("Кол-во исп. признаков: {:.2f}".format(np.sum(lasso00001.coef_ != 0)))

plt.plot(lasso.coef_, 's', label="Ридж регрессия alpha=1")
plt.plot(lasso001.coef_, 's', label="Ридж регрессия alpha=10")
plt.plot(lasso00001.coef_, 's', label="Ридж регрессия alpha=0.1")