Esempio n. 1
0
def tryFits1(fName):
    distances, heights = getTrajectoryData(fName)
    distances = pylab.array(distances) * 36
    totHeights = pylab.array([0] * len(distances))
    for h in heights:
        totHeights = totHeights + pylab.array(h)
    pylab.title('Trajectory of Projectile (Mean of 4 Trials)')
    pylab.xlabel('Inches from Launch Point')
    pylab.ylabel('Inches Above Launch Point')
    meanHeights = totHeights / float(len(heights))
    pylab.plot(distances, meanHeights, 'bo')
    a, b = pylab.polyfit(distances, meanHeights, 1)
    altitudes = a * distances + b
    pylab.plot(distances,
               altitudes,
               'r',
               label='Linear Fit' + ', R2 = ' +
               str(round(rSquare(meanHeights, altitudes), 4)))
    a, b, c = pylab.polyfit(distances, meanHeights, 2)
    altitudes = a * (distances**2) + b * distances + c
    pylab.plot(distances,
               altitudes,
               'g',
               label='Quadratic Fit' + ', R2 = ' +
               str(round(rSquare(meanHeights, altitudes), 4)))
    pylab.legend()
def fitline(x, y, lo=0, hi=1, doplot=1, quiet=1, linewidth=2):
    """Fitline takes the position of low and high fit limits
    and returns the slope. Integer indices in the x data set can
    be specified for the low and high limits, otherwise use a
    fraction between 0 and 1.
    
    In application to fractal dimension estimation, if
    x = log d (radius, a.k.a. inter-point distance) and
    y = log v (index, a.k.a. estimate of volume at a given radius)
    then the slope is the dimension estimate of the data set.
    """
    lendat = len(x)
    assert hi > lo
    if lo >= 0 and hi <= 1:
        loix = int(lendat*lo)
        hiix = int(lendat*hi)
    elif lo >=0 and hi > 0:
        loix = int(lo)
        hiix = int(hi)
    else:
        raise ValueError, "Invalid low and high fit limits"
    if quiet==0:
        print "lo ix %d, hi ix %d, max ix %d"%(loix, hiix, lendat-1)
    pfit = polyfit(x[loix:hiix],y[loix:hiix],1)
#    print pfit
    if doplot==1:
        plot([x[loix],x[hiix]],
             [x[loix]*pfit[0]+pfit[1],x[hiix]*pfit[0]+pfit[1]],
             linewidth=linewidth)
    return pfit[0]
Esempio n. 3
0
def fitData1(fileName):
    xVals, yVals = getData(fileName)
    xVals = pylab.array(xVals)
    yVals = pylab.array(yVals)
    xVals = xVals * 9.81  # convert mass to force (F = mg)
    pylab.plot(xVals, yVals, 'bo', label='Measured displacements')
    pylab.title('Measured Displacement of Spring')
    pylab.xlabel('|Force| (Newtons)')
    pylab.ylabel('Distance (meters)')
    a, b = pylab.polyfit(xVals, yVals, 1)
    estYVals = a * xVals + b
    pylab.plot(xVals, estYVals, label='Linear fit')
    a, b, c, d = pylab.polyfit(xVals, yVals, 3)
    estYVals = a * (xVals**3) + b * xVals**2 + c * xVals + d
    pylab.plot(xVals, estYVals, label='Cubic fit')
    pylab.legend(loc='best')
Esempio n. 4
0
def fitData(inputFile):
    masses, distances = getData(inputFile)
    #newMasses = masses + [x/100 for x in range(105, 155, 5)]
    masses = np.array(masses[:-6])
    distances = np.array(distances[:-6])
    forces = masses * 9.81
    #newForces = np.array(newMasses)*9.81
    plt.plot(forces, distances, 'bo', label="Measured displacements")
    plt.title("测得的弹簧位移")
    plt.xlabel('|阻力|(牛顿)')
    plt.ylabel("距离 (米)")
    #最小二乘法线性拟合(1次)
    a, b = plt.polyfit(forces, distances, 1)
    predictDistances = a*forces + b
    k = 1/a
    plt.plot(forces, predictDistances, label="预测线性拟合k="+str(round(k,5))+"的位移")

    '''
    # 最小二乘法线性拟合(3次)
    a, b, c, d = plt.polyfit(forces, distances, 3)
    predictDistances = a*(forces**3) + b*(forces**2) + c*forces + d
    plt.plot(forces, predictDistances, "r:", label="三次拟合")
    '''

    plt.legend(loc='best')
    plt.savefig('chapter15_5.jpg', dpi=100)
    plt.show()
    '''
Esempio n. 5
0
def fitline(x, y, lo=0, hi=1, doplot=1, quiet=1, linewidth=2):
    """Fitline takes the position of low and high fit limits
    and returns the slope. Integer indices in the x data set can
    be specified for the low and high limits, otherwise use a
    fraction between 0 and 1.
    
    In application to fractal dimension estimation, if
    x = log d (radius, a.k.a. inter-point distance) and
    y = log v (index, a.k.a. estimate of volume at a given radius)
    then the slope is the dimension estimate of the data set.
    """
    lendat = len(x)
    assert hi > lo
    if lo >= 0 and hi <= 1:
        loix = int(lendat * lo)
        hiix = int(lendat * hi)
    elif lo >= 0 and hi > 0:
        loix = int(lo)
        hiix = int(hi)
    else:
        raise ValueError, "Invalid low and high fit limits"
    if quiet == 0:
        print "lo ix %d, hi ix %d, max ix %d" % (loix, hiix, lendat - 1)
    pfit = polyfit(x[loix:hiix], y[loix:hiix], 1)
    #    print pfit
    if doplot == 1:
        plot([x[loix], x[hiix]],
             [x[loix] * pfit[0] + pfit[1], x[hiix] * pfit[0] + pfit[1]],
             linewidth=linewidth)
    return pfit[0]
Esempio n. 6
0
    def _fit_leastsq(self, volume_lst, energy_lst, fittype="birchmurnaghan"):
        """
        Internal helper function for the least square fit

        Args:
            volume_lst (list/numpy.dnarray/None): vector of volumes
            energy_lst (list/numpy.dnarray/None): vector of energies
            fittype (str): on of the following ['birch', 'birchmurnaghan', 'murnaghan', 'pouriertarantola', 'vinet']

        Returns:
            list: [E0, B0, BP, V0], [E0_err, B0_err, BP_err, V0_err]
        """
        try:
            import matplotlib.pylab as plt
        except ImportError:
            import matplotlib.pyplot as plt
        vol_lst = np.array(volume_lst).flatten()
        eng_lst = np.array(energy_lst).flatten()
        a, b, c = plt.polyfit(vol_lst, eng_lst, 2)
        v0 = -b / (2 * a)
        ev_angs_to_gpa = (
            1e21 / scipy.constants.
            physical_constants["joule-electron volt relationship"][0])
        pfit_leastsq, perr_leastsq = self._fit_leastsq_funct(
            [a * v0**2 + b * v0 + c, 2 * a * v0 * ev_angs_to_gpa, 4, v0],
            vol_lst,
            eng_lst,
            fitfunction,
            fittype,
        )
        return pfit_leastsq, perr_leastsq  # [e0, b0, bP, v0]
def plot_zipf_law_on_corpus(corpus):

    words = getWordsFromCorpus(corpus)
    words = remove_stopwords_from_corpus_words(words)
    fdist = FreqDist(words)
    words = fdist.most_common()

    x = [math.log(i[1]) for i in words]
    y = [math.log(i) for i in range(1, len(x) + 1)]

    (m, b) = pylab.polyfit(x, y, 1)
    yp = pylab.polyval([m, b], x)

    slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)

    pylab.plot(x, y, 'r')
    pylab.plot(x, yp, 'b')

    pylab.ylim([min(y), max(y)])
    pylab.xlim([min(x), max(x)])
    pylab.text(x=1,
               y=1,
               s="Best Fit Line (Blue) \nslope = {slope}".format(
                   slope=np.round(slope, 2)))
    pylab.grid(True)
    pylab.ylabel('Counts of words (log)')
    pylab.xlabel('Ranks of words (log)')
    pylab.title(
        'ZIPF LAW TEST ON CORPUS. IDEALLY SLOPE OF THE LINE MUST BE = -1 for IDEAL ZIPF CASE'
    )
    pylab.show()
Esempio n. 8
0
    def test_generate_models(self):

        degs_msg = "generate_models should return one model for each given degree"
        list_type_msg = "generate_models should return a list of models"
        array_type_msg = "each model returned by generate_models should be of type pylab.array"
        coefficient_mismatch = "coefficients of returned model are not as expected"

        # simple y = x case.
        x = pylab.array(range(50))
        y = pylab.array(range(50))
        degrees = [1]
        models = ps5.generate_models(x, y, degrees)

        self.assertEquals(len(models), len(degrees), degs_msg)
        self.assertIsInstance(models, list, list_type_msg)
        self.assertIsInstance(models[0], pylab.ndarray, array_type_msg)
        self.assertListEqual(list(models[0]), list(pylab.polyfit(x, y, 1)),
                             coefficient_mismatch)

        # two models for y = 2x case
        y = pylab.array(range(0, 100, 2))
        degrees = [1, 2]
        models = ps5.generate_models(x, y, degrees)
        self.assertEquals(len(models), len(degrees), degs_msg)
        self.assertIsInstance(models, list, list_type_msg)
        for m in models:
            self.assertIsInstance(m, pylab.ndarray, array_type_msg)
        for i in range(2):
            self.assertListEqual(list(models[i]),
                                 list(pylab.polyfit(x, y, degrees[i])),
                                 coefficient_mismatch)

        # three models
        degrees = [1, 2, 20]
        models = ps5.generate_models(x, y, degrees)
        self.assertEquals(len(models), len(degrees), degs_msg)
        self.assertIsInstance(models, list, list_type_msg)
        for m in models:
            self.assertIsInstance(m, pylab.ndarray, array_type_msg)
        for i in range(3):
            self.assertListEqual(list(models[i]),
                                 list(pylab.polyfit(x, y, degrees[i])),
                                 coefficient_mismatch)
 def plot_regression(self):
     # sample plot of points
     x = self.NaN_filtered['tmax']
     y = self.NaN_filtered['tmin']
     fit = polyfit(x,y,1)
     fit_fn = poly1d(fit)
     # takes in x and returns an estimate for y
     plt.plot(x,y, 'yo', x, fit_fn(x), '--k')
     plt.title("Temperature pattern regression plot")
     plt.xlabel("tmax")
     plt.ylabel("tmin")
     plt.show()        
Esempio n. 10
0
 def plot_regression(self):
     # sample plot of points
     x = self.NaN_filtered['tmax']
     y = self.NaN_filtered['tmin']
     fit = polyfit(x, y, 1)
     fit_fn = poly1d(fit)
     # takes in x and returns an estimate for y
     plt.plot(x, y, 'yo', x, fit_fn(x), '--k')
     plt.title("Temperature pattern regression plot")
     plt.xlabel("tmax")
     plt.ylabel("tmin")
     plt.show()
Esempio n. 11
0
 def _fit_leastsq(self, volume_lst, energy_lst, fittype='birchmurnaghan'):
     try:
         import matplotlib.pylab as plt
     except ImportError:
         import matplotlib.pyplot as plt
     vol_lst = np.array(volume_lst).flatten()
     eng_lst = np.array(energy_lst).flatten()
     a, b, c = plt.polyfit(vol_lst, eng_lst, 2)
     v0 = -b / (2 * a)
     pfit_leastsq, perr_leastsq = self.fit_leastsq(
         [a * v0**2 + b * v0 + c, 2 * a * v0 * 160.21766208, 4, v0],
         vol_lst, eng_lst, self.fitfunction, fittype)
     return pfit_leastsq, perr_leastsq  # [e0, b0, bP, v0]
def fitDataWithCubic(inputFile):
    masses, distances = getData(inputFile)
    distances = pylab.array(distances)
    masses = pylab.array(masses)
    forces = masses * 9.81
    pylab.plot(forces, distances, 'bo', label='Measured displacements')
    pylab.title('Measured Displacement of Spring')
    pylab.xlabel('|Force| (Newtons)')
    pylab.ylabel('Distance (meters)')
    # find linear fit
    a, b = pylab.polyfit(forces, distances, 1)
    predictedDistances = a * pylab.array(forces) + b
    k = 1.0 / a
    pylab.plot(forces,
               predictedDistances,
               label='Displacements predicted by\nlinear fit, k = ' +
               str(round(k, 5)))
    a, b, c, d = pylab.polyfit(forces, distances, 3)
    predictedDistances = a * (forces**3) + b * forces**2 + c**forces + d
    pylab.plot(forces, predictedDistances, 'b:', label='cubic fit')
    pylab.legend(loc='best')
    pylab.show()
Esempio n. 13
0
def processTrajectories(fileName):
    distances, heights = getTrajectoryData(fileName)
    numTrials = len(heights[0])
    distances = np.array(distances)
    meanHeights = list(map(lambda x: sum(x)/len(x), heights))
    meanHeights = np.array(meanHeights)
    plt.title("抛弹的弹道("+str(numTrials)+"条弹道的平均路径)")
    plt.xlabel("距离抛射点的英寸数")
    plt.ylabel("抛射点上方的英寸数")
    plt.plot(distances, meanHeights, 'bo', label="实验点")
    a, b = plt.polyfit(distances, meanHeights, 1)
    altitudes = a*distances + b
    print("RSquare of linear fit =", rSquared(meanHeights, altitudes))
    plt.plot(distances, altitudes, 'b', label="线性拟合")
    a, b, c = plt.polyfit(distances, meanHeights, 2)
    altitudes = a*(distances**2) + b*distances + c
    getHorizontalSpeed(a, b, c, distances[-1], distances[0])
    print("RSquare of quadratic fit =", rSquared(meanHeights, altitudes))
    plt.plot(distances, altitudes, 'b:', label="二次拟合")
    plt.legend()
    plt.savefig("chapter15_6.jpg", dpi=100)
    plt.show()
Esempio n. 14
0
def plot_msd(I_m, r_average, r_error, rx_av, rx_error, ry_av, ry_error,
             folder):
    x = range(len(I_m))

    # Linear fit of msd data, the slot should be 1 in random walk
    m, b = pl.polyfit(x, r_average, 1)

    plt.rcParams["figure.figsize"] = [15, 5]
    plt.subplot(121)
    plt.plot(x, r_average, 'r')  #Data
    plt.plot(x, x, '--b')  #What should be
    plt.plot(x, m * x + b, '--k')  #Fitting
    plt.fill_between(x,
                     np.asarray(r_average) - np.asarray(r_error),
                     np.asarray(r_average) + np.asarray(r_error),
                     alpha=0.2,
                     facecolor='r',
                     linewidth=4,
                     linestyle='dashdot',
                     antialiased=True)

    plt.xlabel('Iteration')
    plt.ylabel('MSD')
    plt.legend(['Data', 'm = 1', 'm = %.2f' % m])
    plt.subplot(122)
    plt.plot(x, rx_av, 'b', label='MSD X')
    plt.plot(x, ry_av, 'r', label='MSD Y')
    plt.fill_between(x,
                     np.asarray(rx_av) - np.asarray(rx_error),
                     np.asarray(rx_av) + np.asarray(rx_error),
                     alpha=0.2,
                     facecolor='b',
                     linewidth=4,
                     linestyle='dashdot',
                     antialiased=True)
    plt.fill_between(x,
                     np.asarray(ry_av) - np.asarray(ry_error),
                     np.asarray(ry_av) + np.asarray(ry_error),
                     alpha=0.2,
                     facecolor='r',
                     linewidth=4,
                     linestyle='dashdot',
                     antialiased=True)

    plt.legend()
    plt.xlabel('Iteration')
    plt.ylabel('MSD')
    plt.savefig(folder + '/MSD.png')
    plt.show()
Esempio n. 15
0
def fitData(fileName):
    xVals, yVals = getData(fileName)
    xVals = pylab.array(xVals)
    yVals = pylab.array(yVals)
    xVals = xVals * 9.81  # convert mass to force (F = mg)
    pylab.plot(xVals, yVals, 'bo', label='Measured points')
    pylab.title('Measured Displacement of Spring')
    pylab.xlabel('Force (Newtons)')
    pylab.ylabel('Distance (meters)')
    a, b = pylab.polyfit(xVals, yVals, 1)  # fit y = ax + b
    # use line equation to graph predicted values
    estYVals = a * xVals + b
    k = 1 / a
    pylab.plot(xVals, estYVals, label='Linear fit, k = ' + str(round(k, 5)))
    pylab.legend(loc='best')
Esempio n. 16
0
 def _fit_leastsq(self, volume_lst, energy_lst, fittype='birchmurnaghan'):
     try:
         import matplotlib.pylab as plt
     except ImportError:
         import matplotlib.pyplot as plt
     vol_lst = np.array(volume_lst).flatten()
     eng_lst = np.array(energy_lst).flatten()
     a, b, c = plt.polyfit(vol_lst, eng_lst, 2)
     v0 = -b / (2 * a)
     ev_angs_to_gpa = 1e21 / scipy.constants.physical_constants[
         'joule-electron volt relationship'][0]
     pfit_leastsq, perr_leastsq = self.fit_leastsq(
         [a * v0**2 + b * v0 + c, 2 * a * v0 * ev_angs_to_gpa, 4, v0],
         vol_lst, eng_lst, self.fitfunction, fittype)
     return pfit_leastsq, perr_leastsq  # [e0, b0, bP, v0]
Esempio n. 17
0
def plotScatter(pearsonStats,data,args,color='b'):
    """"""
    fig = pl.figure()
    ax  = fig.add_subplot(111)
    if args.log:
        ax.set_xscale('log')
        ax.set_yscale('log')
    
    
    ax.scatter(data[0],data[1], s=15, c=color, marker='o', alpha=1)
    if not args.log:
        ax.set_autoscale_on(False)
    ax.set_xlabel(args.label1)
    ax.set_ylabel(args.label2)
    upperLim = max(data[0]+data[1])
    

    
    m,b  = pl.polyfit(data[0],data[1],1)
    bfYs = pl.polyval([m,b], [1,max(data[0])])
    
    ax.plot([1,max(data[0])],bfYs,'r-')
    
    pl.text(0.02,0.95,'Pearson: %.4f, %s\nBest Fit: y=%.3f*x+%.3f' % (pearsonStats[0],pearsonStats[1],m,b),
            bbox=dict(facecolor='#87AACD', alpha=1),
            horizontalalignment='left',
            verticalalignment='top',
            transform = ax.transAxes)
    
    if args.pdf:
        pdf_or_png = 'pdf'
    else:
        pdf_or_png = 'png'
        
    # construct outfile name
    if not args.log:
        outName = '%s_%s_vs_%s.%s' % (args.out,args.label1.replace(' ','_'),args.label2.replace(' ','_'),pdf_or_png)
    else:
        outName = '%s_%s_vs_%s.log.%s' % (args.out,args.label1.replace(' ','_'),args.label2.replace(' ','_'),pdf_or_png)
        
    pl.savefig(outName)
    
    if args.galaxy:
        os.rename(outName,args.out)
        
    print 'Show?  %s' % (args.show)
    if args.show:
        pl.show()
Esempio n. 18
0
def plot_scores():
    from matplotlib import pylab
    import numpy
    savedsco = open("savedsco","r")
    scores = pickle.load(savedsco)
    scores[0] = 0
    savedsco.close()
    pylab.clf()
    figure = pylab.figure()
    pylab.plot(scores)
    x = numpy.arange(len(scores))
    scores = numpy.array(scores)
    m,b = pylab.polyfit(x,scores,1)
    print m
    #pylab.plot(x, m*x+b)
    pylab.savefig("averagescores.png")
Esempio n. 19
0
def lc_linfit(x, y, breaks):

    bks = np.hstack((min(x) - 1, breaks, max(x)))
    yfit = []
    for i in range(len(bks) - 1):
        w = np.where((x > bks[i]) & (x <= bks[i + 1]))
        m, b = plot.polyfit(np.log10(x[w]), np.log10(y[w]), 1)
        if i == 0:
            norm = 10**b
            p = np.array(norm)
            pnames = np.array('norm')
        yfit = np.append(yfit, 10**b * x[w]**m)
        p = np.hstack((p, -m, bks[i + 1]))
        pnames = np.hstack((pnames, 'pow' + str(i + 1), 'break' + str(i + 1)))

    p = p[0:len(p) - 1]
    return p, yfit, pnames
Esempio n. 20
0
def get_level(data, slope_threshold=1.5, window_size=15):
    """
    Automatically determine the fluid level from the slope change
    """
    found = False
    n = data.shape[0]
    for i in range(window_size, n - window_size):
        window_ind = range(i - window_size, i + window_size)
        window_data = data[i - window_size:i + window_size]
        fit = pylab.polyfit(window_ind, window_data, 1)
        if fit[0] > slope_threshold:
            found = True
            break
    if found:
        return i, data[i]
    else:
        return None
Esempio n. 21
0
def fit_data(input):
    masses, distances = get_data(input)
    masses = pylab.array(masses)
    distances = pylab.array(distances)

    forces = masses * 9.81
    pylab.plot(forces, distances, 'bo', label='Measured displacements')
    pylab.xlabel('|Force|(Newtons)')
    pylab.ylabel('Distance(meters')

    a, b = pylab.polyfit(forces, distances, 1)
    predict_distances = a * pylab.array(forces) + b
    k = 1.0 / a
    pylab.plot(forces,
               predict_distances,
               label='Displacement pridicted by \nlinear fit k=' +
               str(round(k, 5)))
    pylab.legend(loc='best')
Esempio n. 22
0
    def markup_image(self, image):
        if not self.tracker.markup_image:
            return

        text = "#{0} ({1}, {2})".format(self.tracker.cnt, self.__width, self.__height)
        text += " {0}%".format(self.tracker.middle_percent)

        if self.__contours is not None:
            all_x = []
            all_y = []
            for i in range(len(self.__contours)):
                box_x, box_y, box_w, box_h = cv2.boundingRect(self.__contours[i])
                center_x = self.__moments[i][2]
                center_y = self.__moments[i][3]
                all_x.append(center_x)
                all_y.append(center_y)

                if self.draw_box:
                    cv2.rectangle(image, (box_x, box_y), (box_x + box_w, box_y + box_h), BLUE, 2)

                if self.draw_contour:
                    cv2.drawContours(image, [self.__contours[i]], -1, GREEN, 2)

                # Draw center
                cv2.circle(image, (center_x, center_y), 4, RED, -1)
                # text += " Avg: ({0}, {1})".format(self.avg_x, self.avg_y)

            if self.draw_line and len(all_x) >= 2:
                m, b = polyfit(all_x, all_y, 1)
                cv2.line(image, (0, int(b)), (self.__width, int((self.__width * m) + b)), BLUE, 2)

        # Draw the alignment lines
        # if self.vertical_lines:
        #    cv2.line(image, (mid_x - mid_inc, 0), (mid_x - mid_inc, self.height), x_color, 1)
        #    cv2.line(image, (mid_x + mid_inc, 0), (mid_x + mid_inc, self.height), x_color, 1)

        # if self.horizontal_lines:
        #    cv2.line(image, (0, mid_y - mid_inc), (self.width, mid_y - mid_inc), y_color, 1)
        #    cv2.line(image, (0, mid_y + mid_inc), (self.width, mid_y + mid_inc), y_color, 1)

        if self.display_text:
            cv2.putText(image, text, defs.TEXT_LOC, defs.TEXT_FONT, defs.TEXT_SIZE, RED, 1)
def generate_models(x, y, degs):
    """
    Generate regression models by fitting a polynomial for each degree in degs
    to points (x, y).

    Args:
        x: an 1-d pylab array with length N, representing the x-coordinates of
            the N sample points
        y: an 1-d pylab array with length N, representing the y-coordinates of
            the N sample points
        degs: a list of degrees of the fitting polynomial

    Returns:
        a list of pylab arrays, where each array is a 1-d array of coefficients
        that minimizes the squared error of the fitting polynomial
    """
    result = []
    for d in degs:
        result.append(pylab.polyfit(x, y, d))
    return result
 def plot_regression(self):
     # sample plot of points
     x = self.filtered.tmax
     y = self.filtered.tmin
     fit = polyfit(x,y,1)
     fit_fn = poly1d(fit)
     # takes in x and returns an estimate for y
     # fig 1
     plt.plot(x,y, 'yo', x, fit_fn(x), '--k')
     plt.title("Temperature pattern regression plot")
     plt.xlabel("tmax")
     plt.ylabel("tmin")
     # fig 2
     plt.figure()
     andrews_curves(self.filtered[[2,3]], 'tmin')
     plt.title("Andrews curve plot for tmin")
     plt.show()
     # fig 3
     plt.figure()
     self.df[[2,3]].boxplot()
     # fig 4
     self.filtered.plot()
     plt.show()
Esempio n. 25
0
 def plot_regression(self):
     # sample plot of points
     x = self.filtered.tmax
     y = self.filtered.tmin
     fit = polyfit(x, y, 1)
     fit_fn = poly1d(fit)
     # takes in x and returns an estimate for y
     # fig 1
     plt.plot(x, y, 'yo', x, fit_fn(x), '--k')
     plt.title("Temperature pattern regression plot")
     plt.xlabel("tmax")
     plt.ylabel("tmin")
     # fig 2
     plt.figure()
     andrews_curves(self.filtered[[2, 3]], 'tmin')
     plt.title("Andrews curve plot for tmin")
     plt.show()
     # fig 3
     plt.figure()
     self.df[[2, 3]].boxplot()
     # fig 4
     self.filtered.plot()
     plt.show()
Esempio n. 26
0
def plotScatter(pearsonStats,normedTxCntsList,opts):
    """"""
    fig = pl.figure()
    ax  = fig.add_subplot(111)
    if opts.log:
        ax.set_xscale('log')
        ax.set_yscale('log')
    
    
    ax.scatter(normedTxCntsList[0],normedTxCntsList[1], s=15, c='b', marker='o', alpha=1)
    if not opts.log:
        ax.set_autoscale_on(False)
    ax.set_xlabel(opts.name_a)
    ax.set_ylabel(opts.name_b)
    upperLim = max(normedTxCntsList[0]+normedTxCntsList[1])
    

    
    m,b  = pl.polyfit(normedTxCntsList[0],normedTxCntsList[1],1)
    bfYs = pl.polyval([m,b], [1,max(normedTxCntsList[0])])
    
    ax.plot([1,max(normedTxCntsList[0])],bfYs,'r-')
    
    pl.text(0.01,0.99,'Pearson: %.4f, %s\nBest Fit: y=%.3f*x+%.3f' % (pearsonStats[0],pearsonStats[1],m,b),
            bbox=dict(facecolor='#87AACD', alpha=1),
            horizontalalignment='left',
            verticalalignment='top',
            transform = ax.transAxes)
    
    mkdirp(opts.dir)
    if not opts.log:
        pl.savefig('%s%s_vs_%s.png' % (opts.dir,opts.name_a,opts.name_b))
    else:
        pl.savefig('%s%s_vs_%s.log.png' % (opts.dir,opts.name_a,opts.name_b))
    print 'Show?  %s' % (opts.show)
    if opts.show:
        pl.show()
Esempio n. 27
0
        ax  = fig.add_subplot(111)
        if opts.log:
            ax.set_xscale('log')
            ax.set_yscale('log')
        
        
        ax.scatter(vecFile[1],vecFile[2], s=15, c='b', marker='o', alpha=1)
        if not opts.log:
            ax.set_autoscale_on(False)
        ax.set_xlabel(vecFile[0][0])
        ax.set_ylabel(vecFile[0][1])
        upperLim = max(vecFile[1]+vecFile[2])
        

        
        m,b  = pl.polyfit(vecFile[1],vecFile[2],1)
        bfYs = pl.polyval([m,b], [1,max(vecFile[1])])
        
        ax.plot([1,max(vecFile[1])],bfYs,'r-')
        
        pl.text(0.01,0.99,'Pearson: %.4f, %s\nBest Fit: y=%.3f*x+%.3f' % (pearson[0],pearson[1],m,b),
                bbox=dict(facecolor='#87AACD', alpha=1),
                horizontalalignment='left',
                verticalalignment='top',
                transform = ax.transAxes)
        
        
        pl.savefig('%s_vs_%s.png' % (vecFile[0][0],vecFile[0][1]))
        print 'Show?  %s' % (opts.show)
        if opts.show:
            pl.show()
Esempio n. 28
0
'''

def plot_author_success(successlist):
    xvals = [value[0] for key, value in successlist.iteritems()]
    yvals = [value[2] for key, value in successlist.iteritems()]
    labellist = [key for key, value in successlist.iteritems()]

    fig, ax = plt.subplots()
    ax.scatter(xvals, yvals)

    for i, txt in enumerate(labellist):
        ax.annotate(txt, (xvals[i],yvals[i]))
    plt.title("Active Users with their success rate")
    plt.xlabel("No. distinct users")
    plt.ylabel("fraction of users with multiple posts")
    #remove_border()

plot_author_success(authorstats)



#regression line
m_fit,b_fit = plt2.polyfit(big_table.comments, big_table.score, 1)
plt2.plot(big_table.comments, big_table.score, 'yo', big_table.comments, m_fit*big_table.comments+b_fit, color='purple', alpha=0.3)
plt.title("Comments versus Score")
plt.xlabel("price")
plt.ylabel("title")
plt.xlim(-10, max(big_table.comments) * 1.05)
plt.ylim(-10, max(big_table.score) * 1.05 )

Esempio n. 29
0
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pylab as plb
from scipy.stats import linregress

labels, values = zip(*aggCount.most_common(50))

indexes = np.arange(len(labels))
width = 1

plt.bar(indexes, values, width)

plt.xticks(indexes + width * 0.5, labels)
plt.show()

X = indexes + 1
Y = values
plt.plot(X, Y, 'bo')
plt.savefig('rawplot.png')

plt.loglog(X, Y, 'bo')
plt.savefig('loglogplot.png')

m, b = plb.polyfit(plb.log10(X), plb.log10(Y), 1)
slope, intercept, r_value, p_value, std_err = linregress(
    plb.log10(X), plb.log10(Y))
rsquared = r_value**2
plt.plot(plb.log10(X), plb.log10(Y), 'bo', plb.log10(X),
         plb.log10(X) * slope + intercept, '--k')
plt.savefig('loglogplot_fitted.png')
Esempio n. 30
0
 def draw_regression_line(self, x_data, y_data):
     m, b = plb.polyfit(x_data.ravel(), y_data.ravel(), 1)
     x_lim = plt.xlim()
     data_y = [x_lim[0] * m + b, x_lim[1] * m + b]
     line = mpl_lines.Line2D(x_lim, data_y, color='blue', linewidth=0.4)
     plt.axes().add_line(line)
Esempio n. 31
0
    'Mid_Call'
]
puts.columns = [
    'UnderlyingPrice', 'Expiration', 'Strike', 'Ask_Put', 'Bid_Put', 'Mid_Put'
]

# join
result = pd.merge(calls, puts, on=['UnderlyingPrice', 'Expiration', 'Strike'])
del calls
del puts
del prices

result = result.apply(compute_expiration, axis=1)
# C-P = S0-Kexp(-rT)
# K = F -> C-P=0
result['cp'] = result['Mid_Call'] - result['Mid_Put']
T = result.groupby('T')['cp'].apply(list).index
Strike = result.groupby('T')['Strike'].apply(list).values
cp = result.groupby('T')['cp'].apply(list).values
result['F'] = 0
for t in range(len(T)):
    a, b = plb.polyfit(Strike[t], cp[t], 1)
    result.loc[result['T'] == T[t], 'F'] = b
result = result.drop(['cp', 'Expiration'], axis=1)

result = result.apply(f, axis=1)
result.to_csv('imp_vol_yahoo_vix 1602.csv',
              sep=',',
              encoding='utf-8',
              index=False)
Esempio n. 32
0
def control_dif2D(file, folder):

    # Relevant parameters of the simulation
    data = np.load(file)
    U_matrix = data['U_m']
    dt = data['dt']
    save = data['save']
    d = data['var'][0]
    shape = U_matrix.shape
    mid = int(shape[1] / 2)

    # Wide at half width, total concentration, maximum peak
    wide_x, wide_y, area, maximum = [], [], [], []

    time_it = []
    plt.rcParams["figure.figsize"] = [15, 4]
    plt.subplot(121)
    color1 = pl.cm.Blues(np.linspace(1, 0, len(U_matrix)))

    for i, elements in enumerate(U_matrix):
        # The total concetration is the sum in every voxel
        area.append(np.sum(elements))
        # Maximum peak for each time iter
        maximum.append(np.amax(elements))

        # Chemical profile through a middle line
        ux = elements[mid, :]
        uy = elements[:, mid]

        # Calculates FWHM
        arr = np.linspace(0, shape[1] - 1, shape[1])  #Array x

        wide_x.append(FWHM(ux, arr))
        wide_y.append(FWHM(uy, arr))

        plt.plot(arr, ux, alpha=.5, color=color1[i])

        # Time of the iteration
        time_it.append(i * save)

    plt.xlabel('Position')
    plt.ylabel('Concentration')

    # Deletes the initial conditions
    del wide_x[0], time_it[0], wide_y[0]
    # Linear fitting of parameters

    wide_x = np.asarray(wide_x)
    wide_y = np.asarray(wide_y)
    time_it = np.asarray(time_it)
    mx, bx = pl.polyfit(time_it, wide_x**2, 1)
    my, by = pl.polyfit(time_it, wide_y**2, 1)
    # Teoretical and fitted difussion coefficients
    dif_x = mx * 10000 * dt / 2
    dif_y = my * 10000 * dt / 2
    dif_teo = d * 2 / (10000 * dt)  # Teoretical slot

    plt.subplot(122)
    plt.plot(time_it, wide_x**2, 'r', time_it, mx * time_it + bx, '--r')
    plt.plot(time_it, wide_y**2, 'b', time_it, my * time_it + by, '--b')
    plt.plot(time_it, dif_teo * time_it, '--k')
    plt.xlabel('Iteration')
    plt.ylabel('FWHM**2')
    plt.legend([
        'x',
        'Dx = %.2f' % dif_x, 'y',
        'Dy = %.2f' % dif_y,
        'Dteo = %.1f' % d
    ])
    plt.savefig(folder + '/Difussion.png')
    plt.show()

    plt.rcParams["figure.figsize"] = [15, 4]
    # Linear fittinf of maximum vs sqrt(iteration)
    intime = np.array(range(1, len(maximum))) * dt
    del maximum[0]
    maximum = np.asarray(maximum)
    ml, bl = pl.polyfit(intime, 1 / maximum, 1)
    # Toretical and fitted difussion coefficients
    dif = (ml * area[0]) / (4 * np.pi * save)
    d_teo = (d * (4 * np.pi * save)) / area[0]  # Teoretical slot

    plt.subplot(121)
    plt.plot(range(len(area)) * save, area)
    plt.xlabel('Iteration')
    plt.ylim(0, 2 * area[0])
    plt.ylabel('Total Concentration')
    plt.subplot(122)
    plt.plot(intime, 1 / maximum, 'r', intime, ml * intime + bl, '--k')
    plt.plot(intime, d_teo * intime, '--b')
    plt.xlabel('Iteration^(0.5)')
    plt.ylabel('Max')
    plt.legend(['Data', 'D = %.2f' % dif, 'Dteo = %.1f' % d])
    plt.savefig(folder + '/Difussion2.png')
    plt.show()
Esempio n. 33
0
    yvals = [value[2] for key, value in successlist.iteritems()]
    labellist = [key for key, value in successlist.iteritems()]

    fig, ax = plt.subplots()
    ax.scatter(xvals, yvals)

    for i, txt in enumerate(labellist):
        ax.annotate(txt, (xvals[i], yvals[i]))
    plt.title("Active Users with their success rate")
    plt.xlabel("No. distinct users")
    plt.ylabel("fraction of users with multiple posts")
    #remove_border()


plot_author_success(authorstats)

#regression line
m_fit, b_fit = plt2.polyfit(big_table.comments, big_table.score, 1)
plt2.plot(big_table.comments,
          big_table.score,
          'yo',
          big_table.comments,
          m_fit * big_table.comments + b_fit,
          color='purple',
          alpha=0.3)
plt.title("Comments versus Score")
plt.xlabel("price")
plt.ylabel("title")
plt.xlim(-10, max(big_table.comments) * 1.05)
plt.ylim(-10, max(big_table.score) * 1.05)
def fitExpData(xVals, yVals):
    logVals = []
    for y in yVals:
        logVals.append(math.log(y, 2.0))
    a, b = plt.polyfit(xVals, logVals, 1)
    return a, b, 2.0
Esempio n. 35
0
        #     continue
        line = line[:-1]
        elems = line.rsplit(' ')
        X.append(float(elems[0]))
        Y.append(float(elems[1]))
    return plt.array(X), plt.array(Y)


if __name__ == '__main__':
    times, speeds = getData('springData.txt')

    # Analyze data
    plt.scatter(times, speeds)
    plt.xlabel('Time (seconds)')
    plt.ylabel('Speed')

    a, b = plt.polyfit(times, speeds, 1)
    yVals = a * times + b
    plt.plot(times, yVals, color='g', linewidth=2, label='Linear')
    plt.title('Speed Versus Time')
    plt.legend()

    a, b, c = plt.polyfit(times, speeds, 2)
    yVals = a * (times**2) + b * times + c
    plt.plot(times, yVals, c='r', linewidth=4, label='Quadratic')
    plt.legend()

    plt.show()

    print(input('Enter anything to finish the program'))
Esempio n. 36
0
        result = pd.merge(calls,
                          puts,
                          on=['UnderlyingPrice', 'Expiration', 'Strike'])
        del calls
        del puts
        del prices

        result = result.apply(compute_expiration, axis=1)
        # C-P = S0-Kexp(-rT)
        # K = F -> C-P=0
        result['cp'] = result['Mid_Call'] - result['Mid_Put']
        result['cpab'] = result['Ask_Call'] - result['Bid_Put']
        result['cpba'] = result['Bid_Call'] - result['Ask_Put']
        T = result.groupby('T')['cp'].apply(list).index
        Strike = result.groupby('T')['Strike'].apply(list).values
        cp = result.groupby('T')['cp'].apply(list).values
        cpab = result.groupby('T')['cpab'].apply(list).values
        cpba = result.groupby('T')['cpba'].apply(list).values
        result['F'] = 0
        result['Fab'] = 0
        result['Fba'] = 0
        for t in range(len(T)):
            a, b = plb.polyfit(Strike[t], cp[t], 1)
            result.loc[result['T'] == T[t], 'F'] = b
            a, b = plb.polyfit(Strike[t], cpab[t], 1)
            result.loc[result['T'] == T[t], 'Fab'] = b
            a, b = plb.polyfit(Strike[t], cpba[t], 1)
            result.loc[result['T'] == T[t], 'Fba'] = b
        result = result.drop(['cp', 'Expiration'], axis=1)
        result.to_csv('month\mydata\options2_{}.csv'.format(str(j)))
infile1 = DATDIR + 'g2g_ccpa_' + vday + '.dat'
infile2 = DATDIR + 'g2g_ccpax_' + vday + '.dat'
infile3 = DATDIR + 'g2g_ST4_' + vday + '.dat'
# plot diagonal line:
x = arange(0., 51., 1)
plt.plot(x, x, color='black', linestyle='dotted')
# make x/y scales equal (without the line below, python plot's y-scale tend
# to be smaller than its x-scale, to fit a 'landscape' page):
plt.gca().set_aspect('equal', adjustable='box')
#
tbl = ascii.read(infile1)
tbl.colnames
plt.scatter(tbl['col4'], tbl['col5'], s=1, color='red')  #'s' is size of dots
#
# draw regression line:
fit = plt.polyfit(tbl['col4'], tbl['col5'], deg=1)
plt.plot(x, fit[0] * x + fit[1], linewidth=2, color='red')
label1 = "CCPA: %6.2f" % rmse(tbl['col4'], tbl['col5'])
print(label1)
#print RMSE of RTMA: rmse(tbl['col4'], tbl['col5'])
print "fit0, fit1 of CCPA regression line:", fit[0], fit[1]
#
# Now do the same for the second dataset:
tbl = ascii.read(infile2)
tbl.colnames
plt.scatter(tbl['col4'], tbl['col5'], s=1, color='blue',
            alpha=0.2)  #'s' is size of dots
fit = plt.polyfit(tbl['col4'], tbl['col5'], deg=1)
print rmse(tbl['col4'], tbl['col5'])
print "fit0, fit1 of CCPAX regression line:", fit[0], fit[1]
plt.plot(x, fit[0] * x + fit[1], linewidth=2, color='blue')
Esempio n. 38
0
    Both lists should be `numSteps` items long.
    """

    ret_rabbit = [CURRENTRABBITPOP]
    ret_fox = [CURRENTFOXPOP]
    for i in range(numSteps):
        if CURRENTFOXPOP >= 10 and CURRENTRABBITPOP >= 10:
            rabbitGrowth()
            foxGrowth()
        ret_rabbit.append(CURRENTRABBITPOP)
        ret_fox.append(CURRENTFOXPOP)

    return ret_rabbit, ret_fox


rp, fp = runSimulation(200)

rl, = pyplot.plot(rp, label="Rabbit population")
fl, = pyplot.plot(fp, label="Fox population")
pyplot.legend(handles=[rl, fl])
pyplot.show()

rcoeff = pylab.polyfit(range(len(rp)), rp, 2)
rcl, = pyplot.plot(pylab.polyval(rcoeff, range(len(rp))),
                   label="Rabbit Coefficients")
fcoeff = pylab.polyfit(range(len(fp)), fp, 2)
fcl, = pyplot.plot(pylab.polyval(fcoeff, range(len(fp))),
                   label="Fox Coefficients")
pyplot.legend(handles=[rcl, fcl])
pyplot.show()
Esempio n. 39
0
        print "Drawing..."
        fig = pl.figure()
        ax  = fig.add_subplot(111)
        #ax.set_xscale('log')
        #ax.set_yscale('log')
        
        ax.scatter(vector0,vector1, s=10, c='b', marker='o', alpha=0.6)
        
        # -- set axis labels --
        xLab = DEGseqParser._file.name.split('_')[0]
        yLab = DEGseqParser._file.name.split('_')[2]
        ax.set_xlabel(xLab)
        ax.set_ylabel(yLab)

        
        m,b  = pl.polyfit(vector0,vector1,1)
        min_xVec = min(vector0)
        max_xVec = max(vector0)
        bfYs = pl.polyval([m,b], [min_xVec,max_xVec])
        
        ax.plot([min_xVec,max_xVec],bfYs,'r-')
        
        pl.text(0.01,0.99,'Pearson: %.4f, %s\nBest Fit: y=%.3f*x+%.3f' % (pearson[0],pearson[1],m,b),
                bbox=dict(facecolor='#87AACD', alpha=1),
                horizontalalignment='left',
                verticalalignment='top',
                transform = ax.transAxes)
        
        

        pl.savefig('%s_vs_%s.indvDEG.png' % (xLab,yLab))
from geobricks_raster_correlation.core.raster_correlation_core import get_correlation
from matplotlib import pyplot as plt
from matplotlib.pylab import polyfit, polyval

# input to your raster files
raster_path1 = "../../tests/data/geoserver_data_dir/data/workspace/wheat_actual_biomprod_201010_doukkala/wheat_actual_biomprod_201010_doukkala.geotiff"
raster_path2 = "../../tests/data/geoserver_data_dir/data/workspace/wheat_potential_biomprod_201010_doukkala/wheat_potential_biomprod_201010_doukkala.geotiff"

# Number of sampling bins
bins = 150

corr = get_correlation(raster_path1, raster_path2, bins)
x = []
y = []
colors = []
#print corr['series']
for serie in corr['series']:
    colors.append(serie['color'])
    for data in serie['data']:
        x.append(data[0])
        y.append(data[1])

# Adding regression line
(m, b) = polyfit(x, y, 1)
yp = polyval([m, b], x)
plt.plot(x, yp)

# plotting scatter
plt.scatter(x, y, c=colors)
plt.show()
Esempio n. 41
0
 def draw_regression_line(self, x_data, y_data):
     m, b = plb.polyfit(x_data.ravel(), y_data.ravel(), 1)
     x_lim = plt.xlim()
     data_y = [x_lim[0] * m + b, x_lim[1] * m + b]
     line = mpl_lines.Line2D(x_lim, data_y, color='blue', linewidth=0.4)
     plt.axes().add_line(line)
from geobricks_raster_correlation.core.raster_correlation_core import get_correlation
from matplotlib import pyplot as plt
from matplotlib.pylab import polyfit, polyval

# input to your raster files
raster_path1 = "../../tests/data/geoserver_data_dir/data/workspace/wheat_actual_biomprod_201010_doukkala/wheat_actual_biomprod_201010_doukkala.geotiff"
raster_path2 = "../../tests/data/geoserver_data_dir/data/workspace/wheat_potential_biomprod_201010_doukkala/wheat_potential_biomprod_201010_doukkala.geotiff"


# Number of sampling bins
bins = 150

corr = get_correlation(raster_path1, raster_path2, bins)
x = []
y = []
colors = []
#print corr['series']
for serie in corr['series']:
    colors.append(serie['color'])
    for data in serie['data']:
        x.append(data[0])
        y.append(data[1])

# Adding regression line
(m, b) = polyfit(x, y, 1)
yp = polyval([m, b], x)
plt.plot(x, yp)

# plotting scatter
plt.scatter(x, y, c=colors)
plt.show()
Esempio n. 43
0
      END of each time step, and fox_populations is a record of the fox population
      at the END of each time step.

    Both lists should be `numSteps` items long.
    """

    ret_rabbit = [CURRENTRABBITPOP]
    ret_fox = [CURRENTFOXPOP]
    for i in range(numSteps):
        if CURRENTFOXPOP >= 10 and CURRENTRABBITPOP >= 10:
            rabbitGrowth()
            foxGrowth()
        ret_rabbit.append(CURRENTRABBITPOP)
        ret_fox.append(CURRENTFOXPOP)

    return ret_rabbit, ret_fox

rp, fp = runSimulation(200)

rl, = pyplot.plot(rp, label="Rabbit population")
fl, = pyplot.plot(fp, label="Fox population")
pyplot.legend(handles=[rl, fl])
pyplot.show()

rcoeff = pylab.polyfit(range(len(rp)), rp, 2)
rcl, = pyplot.plot(pylab.polyval(rcoeff, range(len(rp))), label="Rabbit Coefficients")
fcoeff = pylab.polyfit(range(len(fp)), fp, 2)
fcl, = pyplot.plot(pylab.polyval(fcoeff, range(len(fp))), label="Fox Coefficients")
pyplot.legend(handles=[rcl, fcl])
pyplot.show()
Esempio n. 44
0
from collections import Counter
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pylab as plb
from scipy.stats import linregress

labels, values = zip(*aggCount.most_common(50))

indexes = np.arange(len(labels))
width = 1

plt.bar(indexes, values, width)

plt.xticks(indexes + width * 0.5, labels)
plt.show()

X=indexes +1
Y=values
plt.plot(X,Y, 'bo')
plt.savefig('rawplot.png')

plt.loglog(X,Y,'bo')
plt.savefig('loglogplot.png')

m,b=plb.polyfit(plb.log10(X),plb.log10(Y),1)
slope, intercept, r_value, p_value, std_err=linregress(plb.log10(X),plb.log10(Y))
rsquared=r_value**2
plt.plot(plb.log10(X),plb.log10(Y),'bo',plb.log10(X), plb.log10(X)*slope +intercept, '--k')
plt.savefig('loglogplot_fitted.png')