def animate_plotting(subdir_path,):
    average_filename = 'averaged_out.txt'  
    if os.path.exists( os.path.join(subdir_path,average_filename) ):
            print(subdir_path+average_filename+' already exists please use hotPlot.py')        
            #import existing data for average at the end           
#            data_out = numpy.genfromtxt(os.path.join(subdir_path,average_filename))
#            averaged_data = numpy.array(data_out[:,1])
#            angles = data_out[:,0]
            #os.remove( os.path.join(subdir_path,average_filename))
    else:
        files = os.listdir(subdir_path)     
            #files = [d for d in os.listdir(subdir_path) if os.path.isdir(os.path.join(subdir_path, d))]
        onlyfiles_path = [os.path.join(subdir_path,f) for f in files if os.path.isfile(os.path.join(subdir_path,f))]
        onlyfiles_path = natsort.natsorted(onlyfiles_path)          
        averaged_data = []
        angles = []
        for f in onlyfiles_path:
            data = numpy.genfromtxt(f,delimiter = ',')       
            #data = pandas.read_csv(f)
            averaged_data.append(numpy.mean(data))
            angle = os.path.basename(f).split('_')[0]
            angles.append(float(angle))
        fig = plt.plot(angles, averaged_data,'o')
        plt.yscale('log')
        plt.xscale('log')
        plt.legend(loc='upper right')
        plt.title(base_path)
        plt.grid(True)
        plt.xlabel(r'$\theta$ $[deg.]}$')
        #plt.xlabel(r'$\mathrm{xlabel\;with\;\LaTeX\;font}$')
        plt.ylabel(r'I($\theta$) $[a.u.]$')
def genCurve(dataSet, tree):
	x = [] # stores the x axis of the graph
	trainList = [] # the list of accuracies derived from training data
	valList = [] # the list of accuracies derived from validation data
	i = 0
	while i < 1: 
		i = i+0.1
		a = 0
		b = 0
		for trial in range(3):
			newData = sortData(dataSet, i) # MAKE THIS
			tree = getTree(newData) # NEED TO GET THIS FUNCTION WHEN TREEGEN WORKS
			a = a + model_validation.validateTree(tree, newData)
			b = b + model_validation.validateTree(tree, newData)
		a = float(a)/3
		b = float(b)/3

		trainList.append(a)
		valList.append(b)
		x.append(i)

	plt.plot(x, trainList)
	plt.plot(x, valList)
	plt.xlabel('percent training used')
	plt.ylabel('percent accuracy')
	plt.title('learning curve')
	plt.show()
Beispiel #3
0
 def plotDataFrame(self, variables):
     try:
         import matplotlib.pyplot as plt
     except ImportError:
         print "Unable to import matplotlib"
     plt.plot(self.df[variables[0]], self.df[variables[1]])
     plt.xlabel(r"{}".format(variables[0]))
     plt.ylabel(r"$P$")
     plt.minorticks_on()
     plt.show()
Beispiel #4
0
def plot2D(x,y,x_ex,y_ex,ylabl):

    #static variable counter
    plot2D.fig_num += 1

    plt.subplot(2,2,plot2D.fig_num)
    plt.xlabel('$x$ (cm)')
    plt.ylabel(ylabl)
    plt.plot(x,y,"b+-",label="Lagrangian")
    plt.plot(x_ex,y_ex,"r--",label="Exact")
    plt.savefig("var_"+str(plot2D.fig_num)+".pdf")
Beispiel #5
0
def generate_plot(array, vmin, vmax, figNumber=1):
    plt.figure(figNumber)
    plt.subplot(2,3,i)
    print i
    plt.imshow(array, vmin = vmin, vmax= vmax, interpolation = None) 
    plt.xlabel('Sample')
    plt.ylabel('Line')
    plt.title(row[0])
    cb = plt.colorbar(orientation='hor', spacing='prop',ticks = [vmin,vmax],format = '%.2f')
    cb.set_label('Reflectance / cos({0:.2f})'.format(incAnglerad*180.0/math.pi))
    plt.grid(True)
Beispiel #6
0
	def plot_f_score(self, disag_filename):
		plt.figure()
		from nilmtk.metrics import f1_score
		disag = DataSet(disag_filename)
		disag_elec = disag.buildings[building].elec
		f1 = f1_score(disag_elec, test_elec)
		f1.index = disag_elec.get_labels(f1.index)
		f1.plot(kind='barh')
		plt.ylabel('appliance');
		plt.xlabel('f-score');
		plt.title(type(self.model).__name__);
Beispiel #7
0
 def plot(self, output):
     plt.figure(figsize=output.fsize, dpi=output.dpi)
     for ii in range(0, len(self.v)):
         imsize = [self.t[0], self.t[-1], self.x[ii][-1], self.x[ii][0]]
         lim = amax(absolute(self.v[ii])) / output.scale_sat
         plt.imshow(self.v[ii], extent=imsize, vmin=-lim, vmax=lim, cmap=cm.gray, origin='upper', aspect='auto')
         plt.title("%s-Velocity for Trace #%i" % (self.comp.upper(), ii))
         plt.xlabel('Time (s)')
         plt.ylabel('Offset (km)')
         #plt.colorbar()
         plt.savefig("Trace_%i_v%s.pdf" % (ii, self.comp))
         plt.clf()
Beispiel #8
0
def plot2D(x,y,ylabl,x_ex=None,y_ex=None):

    #static variable counter
    plot2D.fig_num += 1

    plt.subplot(2,2,plot2D.fig_num)
    plt.xlabel('$x$ (cm)')
    plt.ylabel(ylabl)
    plt.plot(x,y,"b+-",label="Numerical")
    if (x_ex != None):
        plt.plot(x_ex,y_ex,"r-x",label="Exact")

    plt.savefig("var_"+str(plot2D.fig_num)+".pdf")
Beispiel #9
0
def viz_losses(filename, losses):
  if '.' not in filename: filename += '.png'

  x = history['epoch']
  legend = losses.keys

  for v in losses.values: plt.plot(np.arange(len(v)) + 1, v, marker='.')

  plt.title('Loss over epochs')
  plt.xlabel('Epochs')
  plt.xticks(history['epoch'], history['epoch'])
  plt.legend(legend, loc = 'upper right')
  plt.savefig(filename)
def plot_confusion_matrix(cm, labels, title='Confusion matrix', cmap=plt.cm.Blues, save=False):
    plt.figure()
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(labels))
    plt.xticks(tick_marks, labels, rotation=45)
    plt.yticks(tick_marks, labels)
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    plt.show()
    if save:
        plt.savefig(save)
Beispiel #11
0
def print_scatter_data():
	import matplotlib.pylab as plt
	filename = Par.dirname + ('/Scatter.dat')
	fitnesses = []
	self_reliences = []
	life_times = []
	self_reliences_dead = []

	for Agent in Par.Agents:
		if Agent.dead != True:
			fitnesses.append(Agent.fitness)
			Needs = Agent.needs
			Production = Agent.production
			selfReli = [0.0]*Par.num_resources
			for i in range(Par.num_resources):
				selfReli[i] = Production[i]*Needs[i]
			self_reliences.append(abs(sum(selfReli)))
		else:
			life_time = Agent.t_death - Agent.t_discovery 
			life_times.append(life_time)
			Needs = Agent.needs
			Production = Agent.production
			selfReli = [0.0]*Par.num_resources
			for i in range(Par.num_resources):
				selfReli[i] = Production[i]*Needs[i]
			self_reliences_dead.append(abs(sum(selfReli)))
			
	
	file =open(filename, 'w')

	for i in range(len(fitnesses)):
		s= str(fitnesses[i]) +'		'+ str(self_reliences[i])
		file.write(s)
		file.write('\n')
	file.close()	


	plt.scatter(self_reliences, fitnesses)
	plt.ylabel('Fitness')
	plt.xlabel('self_reliences')
	plt.savefig('FitnessVSR.png')
	plt.close()
	plt.scatter(self_reliences_dead, life_times)
	plt.ylabel('LifeTimes')
	plt.xlabel('self_reliences')
	plt.savefig('LifeTimeVSR.png')
	plt.close()
Beispiel #12
0
def plot_data():
	import matplotlib.pylab as plt
	t, suffering = np.loadtxt('suffering.dat', unpack= True, usecols = (0,1))
	t, fitness = np.loadtxt('fitness.dat', unpack = True, usecols = (0,1))


	plt.plot(t, suffering)
	plt.xlabel('t')
	plt.ylabel('suffering')
	plt.savefig('suffering.png')
	plt.close()

	plt.plot(t, fitness)
	plt.xlabel('t')
	plt.ylabel('fitness')
	plt.savefig('fitness.png')
	plt.close()
Beispiel #13
0
def blca(datafile,numprocs): 
	#run c++ blocking routine, saves txt data file with blocking data
	os.system("make --silent")
	os.system("mpirun -n %i blocking.out 100 3000 2 %i %s"%(nprocs,numprocs,datafile))#VMC
#os.system("mpirun -n %i blocking.out 5000 100 20000 %i %s"%(nprocs,numprocs,datafile))#DMC
	#read txt file and save plot
	data = np.genfromtxt(fname=datafile+'.txt')
	fig=plt.figure()
	plt.plot(data[:,0],data[:,2],'k+')
	plt.xlabel(r'$\tau_{trial}$', size=20)
	plt.ylabel(r'$\epsilon$', size=20)
	plt.xlim(np.min(data[:,0]),np.max(data[:,0]))
	plt.ylim(np.min(data[:,2]),np.max(data[:,2]))
	fig.savefig(datafile+'.eps',format='eps')
	#open plot if -p in argv
	if plot_res:
		os.system('evince %s%s '%(datafile+'.eps','&'))
	print("plot saved : %s"%(datafile+'.eps'))
Beispiel #14
0
 def __save(self,n,plot,sfile):
     p.figure(figsize=sfile)
     p.xlabel(plot.xlabel)
     p.ylabel(plot.ylabel)
     p.xscale(plot.xscale)
     p.yscale(plot.yscale)
     p.grid()
     for curve in plot.curves: 
         if curve[1] == None: p.plot(curve[0],curve[2], label=curve[3])
         else: p.plot(curve[0],  curve[1], curve[2], label=curve[3])
     p.rc('legend', fontsize='small')
     p.legend(shadow=0, loc='best')
     p.axes().set_aspect(plot.aspect)
     if not plot.dir: plot.dir = './plots/'
     if not plot.name: plot.name = self.__global_name+'_%0*i'%(2,n)
     if not os.path.isdir(plot.dir): os.mkdir(plot.dir)
     if plot.pgf: p.savefig(plot.dir+plot.name+'.pgf')
     else: p.savefig(plot.dir+plot.name+'.pdf', bbox_inches='tight')
     p.close()
Beispiel #15
0
def plot_confusion_matrix(cm, classes,
    normalize=False, title='Confusion matrix',
    cmap=plt.cm.Blues, filename='viz\\confusion_matrix.png'):
  plt.figure()
  plt.imshow(cm, interpolation='nearest', cmap=cmap)
  plt.title(title)
  plt.colorbar()
  tick_marks = np.arange(len(classes))
  plt.xticks(tick_marks, classes, rotation=45)
  plt.yticks(tick_marks, classes)

  if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

  thresh = cm.max() / 2.
  for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
      plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")

  plt.tight_layout()
  plt.ylabel('True label')
  plt.xlabel('Predicted label')
  plt.savefig(filename)
Beispiel #16
0
    def plot(self, model, output):
        Z = linspace(0, model.number[2] * model.spacing[2], model.number[2]) + model.spacing[2]
        imsize = [model.origin[0], model.size[0] + model.origin[0], model.origin[1], model.size[1] + model.origin[1]]
        fig = plt.figure(figsize=(output.hres / output.sres, output.hres / (output.sres * output.hratio)),
                         dpi=output.sres)
        vimg = plt.imshow(transpose(self.v[:, :, 0]), extent=imsize, vmin=round(amin(self.v), 1) - 0.05, vmax=round(amax(self.v) + 0.05, 1),
                          cmap=cm.jet)
        vtitle = plt.title('')
        plt.xlabel('X')
        plt.ylabel('Y')
        plt.colorbar()

        def animate(ii):
            vimg.set_array(transpose(self.v[:, :, ii]))
            vtitle.set_text("%s Plot (Z = %1.2fkm)" % (self.name, Z[ii]))
            return vimg, vtitle

        try:
            ani = animation.FuncAnimation(fig, animate, frames=len(Z), interval=20, blit=False, repeat=False)
            ani.save("./%s.mp4" % self.type, fps=30, codec='libx264', bitrate=1800)
        except IndexError:
            print 'To render movies, make sure that ffmpeg is installed!'
Beispiel #17
0
    def display(self, data, candidates, fname, display):
        
        finallist=[]
        for c in candidates:
            finallist.append(c[0])
        #print finallist
        part1 = finallist[:len(finallist)/2]
        part2 = finallist[len(finallist)/2:]
        
        meandiff=int(np.sqrt(np.power(np.mean(part2),2)-np.power(np.mean(part1),2)))
        rangeA = max(part1)-min(part1)
        rangeB = max(part2)-min(part2)
        span = int((rangeA+rangeB)/2)
        dspan = int(meandiff/span)
        theta = float(meandiff/(rangeA+rangeB))
        oneortwo=""
        if dspan >3 and meandiff > 20 or meandiff>36:
            oneortwo = "Two distributions \n\n MD: %d \n Span: %d \n Dspan: %d \n theta: %d" % (meandiff, span, dspan, theta) 
        else:
            oneortwo = "One distribution \n\n MD: %d \n Span: %d \n Dspan: %d \n theta: %d" % (meandiff, span, dspan, theta)

        cans = np.array(candidates)
        plt.plot(cans[:,0],cans[:,1],'ro')
        plt.axhline(max(cans[:,1])/4, color='r')
        plt.axhline(max(cans[:,1]/2), color='r')
        plt.axhline(int(max(cans[:,1]))*0.75, color='r')
        red_patch = mpatches.Patch(color='red', label='75%, 50% and 25% \nof maximum frequency')
        plt.legend(handles=[red_patch])
        plt.ylabel('Frequency of occurence')
        plt.xlabel('separate items')
        plt.title('Frequency distribution estimation graph: %s' %(fname))
        plt.text(max(data)*1.1, max(cans[:,1])*0.62, oneortwo, fontsize = 11, color = 'r')
        plt.hist(data,range(int(min(data)),int(max(data)),1))
        ofile = fname[0:-3]+"png"
        print ("Writing outfile: %s") % (ofile)
        plt.savefig(ofile, bbox_inches='tight')
        if display == True: 
            plt.show()
        return;
Beispiel #18
0
def execute():
    plt.rcParams['font.sans-serif'] = ['SimHei']
    plt.rcParams['axes.unicode_minus'] = False
    x = random.normal(5, .5, 1000)
    y = random.normal(3, 1, 1000)
    a = x*cos(pi/4) + y*sin(pi/4)
    b = -x*sin(pi/4) + y*cos(pi/4)
    plt.plot(a, b, '.')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('原数据集')
    data = zeros((1000, 2))
    data[:, 0] = a
    data[:, 1] = b
    x, y, evals, evecs = pca(data, 1)
    print(y)
    plt.figure()
    plt.plot(y[:, 0], y[:, 1], '.')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('重新构造数据')
    plt.show()
Beispiel #19
0
    def plot(self, output):
        # Create adaptive scale
        scale_len = 100
        scale_fix = 250
        nframes = self.v.shape[2]
        scale = zeros((nframes, 1))
        win = ones((scale_len, 1))
        for ii in range(0, nframes):
            scale[ii] = amax(absolute(self.v[:, :, ii]))
        scale = convolve(squeeze(scale), squeeze(win), mode='same') / output.scale_sat
        if (self.writestep == 0):
            scale[:scale_fix] = scale[scale_fix]

        # Initialize figure
        comp = {'x': ['Y', 'Z'], 'y': ['X', 'Z'], 'z': ['X', 'Y']}
        fig = plt.figure(figsize=(output.hres / output.sres, output.hres / (output.sres * output.hratio)),
                         dpi=output.sres)
        imsize = [self.x[0], self.x[-1], self.y[-1], self.y[0]]
        vimg = plt.imshow(transpose(self.v[:, :, 0]), extent=imsize, vmin=-scale[0], vmax=scale[0], cmap=cm.RdBu)
        vtitle = plt.title('')
        plt.xlabel(comp[self.dir][0])
        plt.ylabel(comp[self.dir][1])
        plt.colorbar()

        def animate(ii):
            vimg.set_array(transpose(self.v[:, :, ii]))
            vimg.set_clim(-scale[ii], scale[ii])
            vtitle.set_text("%s for %s=%s km (t=%1.2e s)" % (self.type, self.dir, self.loc, self.t[ii]))
            return vimg, vtitle

        try:
            ani = animation.FuncAnimation(fig, animate, frames=self.v.shape[2], interval=20, blit=False, repeat=False)
            if (self.writestep == 0):
                ani.save("./%s_%s_%s.mp4" % (self.dir, self.loc, self.type), fps=30, codec='libx264', bitrate=1800)
            else:
                ani.save("./%s_%s_%s_%i.mp4" % (self.dir, self.loc, self.type, self.writestep), fps=30, codec='libx264', bitrate=1800)
        except IndexError:
            print 'To render movies, make sure that ffmpeg is installed!'
        self.writestep += 1
def plotdatatree(treeID, scale1, mass1):
	plot_title="Mass Accretion History Tree " + str(treeID)   #Can code the number in with treemax
	x_axis="scale time"
	y_axis="total mass"
	figure_name=os.path.expanduser('~/figureTree' + str(treeID))
	#Choose which type of plot you would like: Commented out.
	plt.plot(scale1, mass1, linestyle="-", marker="o")
	#plt.scatter(scale1, mass1, label="first tree")

	plt.title(plot_title)
	plt.xlabel(x_axis)
	plt.ylabel(y_axis)
	#plt.yscale("log")

	plt.savefig(figure_name)

	#In order to Plot only a single tree on a plot must clear lists before loop. 
	#Comment out to over plot curves.			
	plt.clf()

	clearmass = []
	clearscale = []

	return clearmass, clearscale
Beispiel #21
0
    eye_sample, eye_line = row[1:3]
    nose_sample, nose_line = row[3:5]
    print eye_sample, eye_line, nose_sample, nose_line
    print "opening",bg_file[0]
    cube = gdal.Open(bg_file[0], GA_ReadOnly )
    xOff = int(eye_sample) - size/2 -1
    yOff = int(eye_line) - size/2 -1
    array = cube.ReadAsArray(xOff, yOff, size, size)
    array = array / math.cos(incAnglerad)
    means.append(array.mean())
    if i == 1 and vmin == None:
        vmin = array.min()
        vmax = array.max()
        print vmin,vmax
    generate_plot(array, vmin, vmax)
    xOff = int(nose_sample) - size/2 -1
    yOff = int(nose_line) - size/2 -1
    array = cube.ReadAsArray(xOff, yOff, size, size)
    array = array/math.cos(incAnglerad)
    means2.append(array.mean())
    generate_plot(array, vmin,vmax, 3)
    

plt.figure(2)
plt.plot(times,means,'bo')
plt.xlabel('L_s [deg]')
plt.ylabel('Reflectance / cos(i) for {0}x{0} pixels'.format(size))
plt.title('mean(BG) vs L_s, eye-crater')
plt.plot(times,means2,'ro')
plt.show()
Beispiel #22
0
import matplotlib as plt

# X-axis sorted for making the graph clean from crazy lines all over and just a single line

print(plt.style.available)

plt.style.use("dark_background")
forest_fires = forest_fires.sort(["rain"])
plt.plot(forest_fires["rain"], forest_fires["area"])
# Set the x axis label
plt.xlabel("Amount of Rain")
# Set the y axis label
plt.ylabel("Area")
# Set the title
plt.title("Rain quantity vs fire area")
plt.show()
Beispiel #23
0
def create_figure_surface(figid, aspect, xx, yy, cmin, cmax, levels, slices, v3d):
    ''' creates a plot of the surface of a tracer data
    Parameters
    ----------
    figid : int  
            id of figure
    aspect: float
            aspect ratio of figure
    xx    : array 
            scale of x axis
    yy    : array 
            scale of y axis     
    cmin,cmax : array
                minimum and maxminm of the color range
    levels : array
            range of contourlines
    slices : array
            location of slices
    v3d    : vector data in geometry format
    Returns
    -------
    plot :  of surface data
    '''
    # prepare matplotlib
    import matplotlib
    matplotlib.rc("font",**{"family":"sans-serif"})
    matplotlib.rc("text", usetex=True)
    #matplotlib.use("PDF")
    import matplotlib.pyplot as plt
    # basemap
    from mpl_toolkits.basemap import Basemap
    # numpy
    import numpy as np

    # data
    vv = v3d[0,:,:,0]
    # shift
    vv = np.roll(vv, 64, axis=1)

    # plot surface
    plt.figure(figid)
    # colormap
    cmap = plt.cm.bone_r
    # contour fill
    p1 = plt.contourf(xx, yy, vv, cmap=cmap, levels=levels, origin="lower")#, hold="on")
    plt.clim(cmin, cmax)
    # contour lines
    p2 = plt.contour(xx, yy, vv, levels=levels, linewidths = (1,), colors="k")#, hold="on")
    plt.clabel(p2, fmt = "%2.1f", colors = "k", fontsize = 14)
    #plt.colorbar(p2,shrink=0.8, extend='both')
    # slices
    #s1 = xx[np.mod(slices[0]+64, 128)]
    #s2 = xx[np.mod(slices[1]+64, 128)]
    #s3 = xx[np.mod(slices[2]+64, 128)]
#    print s1, s2, s3
    #plt.vlines([s1, s2, s3], -90, 90, color='k', linestyles='--')
    # set aspect ratio of axes
    plt.gca().set_aspect(aspect)

    # basemap
    m = Basemap(projection="cyl")
    m.drawcoastlines(linewidth = 0.5)

    # xticks
    plt.xticks(range(-180, 181, 45), range(-180, 181, 45))
    plt.xlim([-180, 180])
    plt.xlabel("Longitude [degrees]", labelpad=8)
    # yticks
    plt.yticks(range(-90, 91, 30), range(-90, 91, 30))
    plt.ylim([-90, 90])
    plt.ylabel("Latitude [degrees]")


    # write to file
    plt.savefig("solution-surface", bbox_inches="tight")
    plt.show()
    filename = assembly + '-trackspacing-errors.h5'
    if os.path.isfile('results/' + filename):
        f = h5py.File('results/' + filename, 'r')
    else:
        f = h5py.File('results/' + assembly + '-errors.h5', 'r')
    for j, x in enumerate(x_axis):
        value_keys = f[test][sorted_keys[j]].keys()
        for key in value_keys:
            if 'Kinf_Error' in key:
                kinf_list.append(f[test][sorted_keys[j]][key][...]*10**5)
    plt.plot(x_axis,kinf_list, colors[i] + 'o-', ms = 10, lw = 2)
    f.close()

plt.axis([max(x_axis), 0, 0, 400])
plt.title('Error in K-Infinity')
plt.xlabel('Track Spacing [cm]')
plt.ylabel('K-Infinity Error [pcm]')
plt.grid()
plt.legend(legend)
plt.show()
fig.savefig('K-Infinity-Error-TS.png')

fig = plt.figure()
for i, assembly in enumerate(assembly_list):
    mean_list = []
    filename = assembly + '-trackspacing-errors.h5'
    if os.path.isfile('results/' + filename):
        f = h5py.File('results/' + filename, 'r')
    else:
        f = h5py.File('results/' + assembly + '-errors.h5', 'r')
    for j, x in enumerate(x_axis):
Beispiel #25
0
# TODO: Display an image along with the top 5 classes
model.eval()
class_names = []
# Process Image
image_path = 'input2.png'

# Give image to model to predict output
probs, classes, image = predict(image_path, model)
print('probs: {} and classes: {}'.format(probs, classes))

print(probs, classes)
print(cat_to_name)
print(model.class_to_idx)
#probs=probs.detach.numpy()
#classes=classes.detach.numpy()
print(probs, classes)
print(classes[0])
for i in classes[0]:
    class_names.append(model.class_to_idx.item(int(classes[0, i])))
print(class_names)
for c in classes:
    class_names.append(cat_to_name[c])
print('classnames: {}'.format(class_names))
# Show the image
ax = imshow(image)
plt.barh(probs, class_names)
plt.xlabel('Probability')
plt.title('Predicted Flower Names')
plt.show()
#%% Make a column that is the logfoldchange from post to pre
dfpre['foldchange'] =  (dfpre['linabundpost']-dfpre['linabundpre'])
#dfpre['foldchange'] =  ((dfpre['linabundpost']/npost)-(dfpre['linabundpre']/npre))/(dfpre['linabundpre']/npre)
print(dfpre['foldchange'].unique())
foldchangevec = dfpre['foldchange']
#%% Look at fold change and log fold change for each cell and its correpsonding lineage
dfpre['logfoldchange'] = np.log(dfpre['foldchange'])
dfpre['logfoldchange']= dfpre['logfoldchange'].fillna(0)


print(dfpre['logfoldchange'].unique())

# Figures of logfold change and fold change
plt.figure()
plt.hist(dfpre['logfoldchange'], range = [3, 7], bins = 100)
plt.xlabel('log(foldchange) of lineage abundance')
plt.ylabel('number of cells')

plt.figure()
plt.hist(dfpre['foldchange'],range = [0, 500], bins = 100)
plt.xlabel('foldchange of lineage abundance')
plt.ylabel('number of cells')



#%% Make the survivor column, but don't label it yet. 
#dfpre['survivor'] =np.where(dfpre['linabundpost'] >1000, 'res','sens'
# Want to call cells that have an increase in lineage abundance resistant
dfpre.loc[dfpre.foldchange>0, 'survivor'] = 'res'

dfpre.loc[dfpre.foldchange<-200, 'survivor']= 'sens'
        height_shift_range=0,
        shear_range=0,
        zoom_range=0,
        horizontal_flip=True,
        fill_mode='nearest',
        #             preprocessing_function = contrast_adjusment,
        #             preprocessing_function = HE,
        preprocessing_function=AHE)
    datagen.fit(x_train)
    print("Running augmented training now, with augmentation")
    history = model.fit_generator(
        datagen.flow(x_train, y_train, batch_size=batch_size),
        steps_per_epoch=x_train.shape[0],  # batch_size,
        epochs=epochs,
        validation_data=(x_test, y_test))
else:
    print("Running regular training, no augmentation")
    history = model.fit(x_train,
                        y_train,
                        batch_size=batch_size,
                        epochs=epochs,
                        verbose=1,
                        validation_data=(x_test, y_test))

plt.plot(history.epoch, history.history['val_acc'], '-o', label='validation')
plt.plot(history.epoch, history.history['acc'], '-o', label='training')
plt.legend(loc=0)
plt.xlabel('epochs')
plt.ylabel('accuracy')
plt.grid(True)
Beispiel #28
0
print(confusion_matrix(labels2, prediction2))

print('Classification Report')
target_names = ['Benign', 'Malignant']
print(classification_report(labels2, prediction2, target_names=target_names))

#%%

fpr_keras, tpr_keras, thresholds_keras = roc_curve(test_labels,
                                                   predictions_all)
auc_keras = auc(fpr_keras, tpr_keras)

plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr_keras, tpr_keras, label='Keras (area = {:.3f})'.format(auc_keras))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.show()

#%%
prediction2 = model.predict_classes(data3)
from sklearn.metrics import classification_report, confusion_matrix
print('Confusion Matrix')
print(confusion_matrix(labels2, prediction2))

print('Classification Report')
target_names = ['Benign', 'Malignant']
print(classification_report(labels2, prediction2, target_names=target_names))
Beispiel #29
0
def show_target_scatter_plot(colum_name):
    plt.scatter(train[colum_name], train['price'])
    plt.xlabel(colum_name)
    plt.ylabel('price')
    plt.title('Price VS ' + colum_name)
transformed parameters {
  vector[nholes] d_hat;
  for (i in 1:nholes)
    d_hat[i] <- avg  + p[i] + holes[i];
}
model {
  dist ~ normal(d_hat, sigma);
}"""

unpooled_data = {
    'nholes': 2000,
    'players': df['Player'],  # Stan counts starting at 1
    'holes': df['Hole'],
    'dist': df['Distance']
}

sm = pystan.StanModel(model_code=unpooled_model)
unpooled_fit = sm.sampling(data=unpooled_data, iter=1000, chains=2)

unpooled_estimates = pd.Series(unpooled_fit['a'].mean(0), index=player_names)
unpooled_se = pd.Series(unpooled_fit['a'].std(0), index=player_names)
order = unpooled_estimates.sort_values().index
plt.figure(figsize=(18, 6))
plt.scatter(range(len(unpooled_estimates)), unpooled_estimates[order])
for i, m, se in zip(range(len(unpooled_estimates)), unpooled_estimates[order],
                    unpooled_se[order]):
    plt.plot([i, i], [m - se, m + se], 'b-')
    plt.xlim(-1, 690)
plt.ylabel('Price estimate (log scale)')
plt.xlabel('Ordered category')
plt.title('Variation in category price estimates')
Beispiel #31
0
df['violation_bins'] = first_word_violation.apply(assign_vio_bin)

# create dummy variables for each 'violation'
#dummy_df = pd.get_dummies(df['violation_bins'], prefix = 'violation')
# Combine the two datasets
#df = pd.concat([df, dummy_df], axis =1)

df.boxplot(column='driver_age',
           by='driver_race',
           showfliers=False,
           figsize=(6, 6))

import matplotlib.pyplot as plt

plt.hist(df['driver_age'], bins=30, histtype='stepfilled')
plt.xlabel('Age of Driver')
plt.title('Distribution of Age')
plt.show()

pd.crosstab(df['month_label'], df['is_arrested']).plot(kind='bar')
plt.title('Arrest Frequency for Month')
plt.xlabel('Month')
plt.ylabel('Frequency of Arrest')

arrested = df[df['is_arrested'] == 1]
pd.crosstab(df['month_label'], arrested['is_arrested']).plot(kind='bar')

df.groupby('driver_gender').boxplot(column='driver_age',
                                    by='month_label',
                                    showfliers=False,
                                    figsize=(14, 6))
###I installed pip and matplotlib. matplotlib is recognized, but doesn't do anything yet.###
import matplotlib as plt
# Scatter plot
plt.scatter(gdp_cap, life_exp)

# Previous customizations
plt.xscale('log')
plt.xlabel('GDP per Capita [in USD]')
plt.ylabel('Life Expectancy [in years]')
plt.title('World Development in 2007')

# Definition of tick_val and tick_lab
tick_val = [1000, 10000, 100000]
tick_lab = ['1k', '10k', '100k']

# Adapt the ticks on the x-axis
plt.xticks(tick_val, tick_lab)

# After customizing, display the plot
plt.show()
Beispiel #33
0
    Y = list(data_subset[to_plot])
    Y_err = [0] * len(Y)

#    Y_err = list(data_subset[to_plot + '_std'])
#    plt.errorbar(T[:], Y[:], yerr = Y_err, markersize = 5, marker = 'o', label = type)

    plt.plot(T[3:], Y[3:], markersize = 5, lw = 3, marker = 'o', label = type[4:-8] + ' spins')

    # if i == 0:
    #      plt.plot(T[:], Y[:], markersize = 5, lw = 3, marker = 'o', label = '1D')
    # elif i == 1 :
    #      plt.plot(T[1:], Y[1:], markersize = 5, lw = 3, marker = 'o', label = '1.5D')
    # elif i == 2 :
    #      plt.plot(T[1:], Y[1:], markersize = 5, lw = 3, marker = 'o', label = '2D')
    # else:
    #      plot(T[7:], Y[7:], markersize = 5, lw = 3, marker = 'o', label = '2.5D')

plt.xlabel('$T$', fontsize = 20)
plt.ylabel('$E$', fontsize = 20, rotation = 'horizontal', labelpad = 25)

#plt.axvline(x = 2.2, lw = 5, color = 'k', alpha = 0.2)

plt.subplots_adjust(left = 0.15, right = 0.92, top = 0.92, bottom = 0.15)
plt.tick_params(axis = 'both', which = 'major', labelsize = 20)


plt.xlim(left = 0, right = 5)
plt.ylim(bottom = -2.1, top = 0)
legend = plt.legend(fontsize = 18, loc = 2)
show()
Beispiel #34
0
        plt.plot(tStart, fStart)
        plt.plot(tStart, NStart)
    else:
        plt.plot(t, f)
        plt.plot(t, N)
    
    plt.ylabel("kraft[N]")
    plt.legend(["Rullefriksjon", "Normalkraft"])

elif thingToPlot == "vel":
    vAbs = []
    #for el in v:
    vAbs= np.abs(v)
    tData = []
    vData = []
    for i in range(len(malingData)):
        tData.append(malingData[i][0])
        vData.append(malingData[i][3])
    
    plt.plot(t, vAbs)
    plt.plot(tData, vData)
    
    plt.ylabel("fart[m/s]")
    plt.legend(["Teoretisk", "Eksperimentelt"])

plt.xlabel("tid[s]")

#plt.show()
plt.savefig("test.png", dpi=900)

print(x[len(x)-1], xData[len(xData)-1])
Beispiel #35
0
    def boton_vector_normalf(self):
        Pop_Up = tk.Tk()
        Pop_Up.title("Rango Tiempo")
        Pop_Up.minsize(400, 300)
        L1 = tk.Label(Pop_Up, text="Eliga Tiempo a Evaluar")
        posicion = ttk.Frame(L1)
        posicion.pack(side=tk.LEFT, expand=True, padx=5, pady=5)
        E1 = ttk.Entry(posicion, justify=tk.CENTER)
        E1.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
        E1.insert(tk.END, "5")
        L1.pack()
        label = tk.Label(Pop_Up)
        label.pack()
        frame_arriba = ttk.Frame(Pop_Up)
        hola = round(tiempo_maximov1, 2)
        strhola = str(hola)
        inter = "Intervalo de tiempo= [0, "
        suma = inter + strhola + "]"

        frame_arriba.pack(side=tk.TOP,
                          fill=tk.BOTH,
                          expand=True,
                          padx=5,
                          pady=5)
        tiempo_init = ttk.Label(frame_arriba, text=suma)
        tiempo_init.pack(side=tk.TOP)
        tiempo_init_x = ttk.Entry(frame_arriba,
                                  state='readonly',
                                  justify='center')
        tiempo_init_x.insert(0, "0")
        button = ttk.Button(Pop_Up,
                            text='Evaluar',
                            width=10,
                            command=Pop_Up.destroy)

        button.pack(side=tk.BOTTOM)

        #  inicializa la ventana popup
        tiempofinal = 20
        xo = int(self.entrada_posicion_x0.get())
        yo = int(self.entrada_posicion_y0.get())
        vxo = 15
        vyo = 90
        angulo_inicial = self.entrada_angulo_inicial.get()
        mpl.title("Vector Normal")
        mpl.xlabel("-X-")
        mpl.ylabel("-Y-")
        x = np.arange(0, tiempofinal, 0.001)
        print(E1.get())
        x1 = 5
        h = math.sin(math.degrees(angulo_inicial))
        j = math.cos(math.degrees(angulo_inicial))
        print(h)
        x1 = 2
        y = yo + vyo * x + (1 / 2) * -9.8 * x**2
        z = xo + vxo * x + (1 / 2) * 0 * x**2
        y1 = yo + vyo * x1 + (1 / 2) * -9.8 * x1**2
        z1 = xo + vxo * x1 + (1 / 2) * 0 * x1**2

        vector_velocidadx = (vxo * x1)
        vector_velocidady = (vyo * h - (9.8 * x1))
        mpl.plot(z, y, "-")
        mpl.plot(vector_velocidadx + z1, vector_velocidady + y1, "-o")
        mpl.plot((vector_velocidady + z1), (vector_velocidadx), "-o")
        mpl.plot(z1, y1, "-o")
        mpl.show()
        pass
gs.fit(X_train, y_train)
'''

xgb_model.fit(X_train, y_train)

#model.fit(X_train,y_train)

y_pred = xgb_model.predict(X_test)
predictions = [value for value in y_pred]
print("Predictions!!!!!!!!!!!!!!!!!!!!!!!!!!")
print(predictions)

pickle.dump(xgb_model, open("xgbmodel.pickle", "wb"))
'''
with open('xgbmodel.pickle', mode='wb') as f:
   pickle.dump(model, f)
'''

#loaded_model = pickle.load(open("xgbmodel.pickle.dat", "rb"))
plt.plot(y_test, color='red', label='percentage_teacher')
plt.plot(predictions, color='blue', label='percentage')
plt.plot(y_real_test, color='green', label='Real Price')
plt.title('Cripto currency prediction')
plt.xlabel('Time')
plt.ylabel('green:Real Price blue: prediction(%) red: teacher(%)')
plt.legend()
plt.show()

print("FINAL money")
print(trade.simulate_trade(y_real_test, X_test, model))
Beispiel #37
0
    def boton_aceleracionf(self):

        #funcion para la obtencion de tiempo impacto final
        def time_impact(self):
            t = ((self.velocidad_inicial * sin(self.angulo)) /
                 (2 * self.gravedad)) + (
                     (1 / self.gravedad) *
                     (sqrt(((self.velocidad_inicial * sin(self.angulo))**2) +
                           (2 * self.y0 * self.gravedad))))
            print(t)
            return t

            # funcion para el calculo de la coordenada horizontal
        def cord_x(self, t):
            x = self.x0 + ((self.velocidad_inicial * cos(self.angulo)) * t)
            return x

            # funcion para el calculo de la coordenada vertical
        def cord_y(self, t):
            y = self.y0 + (((self.velocidad_inicial *
                             (cos(self.angulo))) * t) - ((self.gravedad / 2) *
                                                         (t**2)))
            return y

            # funcion altura maxima para graficar
        def altura_max(self):
            r = self.y0 + (((self.velocidad_inicial * (sin(self.angulo)))**2) /
                           (2 * self.gravedad))
            return r

            # funcion alcance maximo para graficar
        def alcance_max(self):
            alc = self.x0 + ((self.velocidad_inicial*sin(2*self.angulo))/(2*self.gravedad)) + \
                             ((self.velocidad_inicial*cos(self.angulo)) /
                              (self.gravedad))*sqrt(((self.velocidad_inicial*sin(self.angulo))**2) + 2*self.y0*self.gravedad)
            return alc

        # pop up de ingreso de datos
        Pop_Up = tk.Tk()
        Pop_Up.title("Aceleracion")

        frame_arriba = ttk.Frame(Pop_Up)
        frame_centro = ttk.Frame(Pop_Up)
        frame_abajo = ttk.Frame(Pop_Up)
        frame_evaluar = ttk.Frame(Pop_Up)
        frame_salir = ttk.Frame(Pop_Up)

        frame_arriba.pack(side=tk.TOP,
                          fill=tk.BOTH,
                          expand=True,
                          padx=5,
                          pady=5)
        frame_centro.pack(side=tk.TOP,
                          fill=tk.BOTH,
                          expand=True,
                          padx=5,
                          pady=5)
        frame_abajo.pack(side=tk.TOP,
                         fill=tk.BOTH,
                         expand=True,
                         padx=5,
                         pady=5)
        frame_evaluar.pack(side=tk.TOP,
                           fill=tk.BOTH,
                           expand=True,
                           padx=5,
                           pady=5)
        frame_salir.pack(side=tk.TOP,
                         fill=tk.BOTH,
                         expand=True,
                         padx=5,
                         pady=5)

        tiempo = ttk.Label(frame_abajo, text="Tiempo(s):")
        aceptar = ttk.Button(frame_evaluar, text="ACEPTAR")
        tiempo_init = ttk.Label(frame_arriba, text="Intervalo de tiempo")
        tiempo_init_x = ttk.Entry(frame_arriba,
                                  state='readonly',
                                  justify='center')
        tiempo_init_y = ttk.Entry(frame_arriba, state='readonly')
        tiempo_init.pack(side=tk.TOP)
        tiempo_init_x.pack(side=tk.LEFT,
                           fill=tk.BOTH,
                           expand=True,
                           padx=5,
                           pady=5)
        tiempo_init_y.pack(side=tk.LEFT,
                           fill=tk.BOTH,
                           expand=True,
                           padx=5,
                           pady=5)
        tiempo_init_x.configure(state='normal')
        tiempo_init_x.delete(0, 'end')
        tiempo_init_x.insert(0, "0")
        tiempo_init_x.configure(state='readonly')
        # inicializa el punto de interseccion del eje Y
        tiempo_init_y.configure(state='normal')
        tiempo_init_y.delete(0, 'end')
        tiempo_init_y.insert(0, time_impact(self))
        tiempo_init_y.configure(state='readonly')

        # Separador de datos
        separador = ttk.Separator(frame_centro, orient="horizontal")
        separador.pack(side=tk.TOP, expand=False, fill=tk.X)
        # Crea formularios para entrada de datos
        entrada_tiempo = ttk.Entry(frame_abajo, validate="key")
        # entrada_y = ttk.Entry(frame_derecha, validate="key", validatecommand=validacion_y)

        tiempo.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5, pady=5)
        # posicion_y.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5, pady=5)

        entrada_tiempo.pack(side=tk.LEFT,
                            fill=tk.BOTH,
                            expand=True,
                            padx=5,
                            pady=5)
        # entrada_y.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=5, pady=5)

        button = ttk.Button(frame_evaluar,
                            text='Salir',
                            width=8,
                            command=Pop_Up.destroy)
        button.pack(side=tk.BOTTOM)
        button2 = ttk.Button(frame_evaluar,
                             text='Evaluar',
                             width=8,
                             command=Pop_Up.mainloop)
        button2.pack(side=tk.BOTTOM)
        mpl.show()

        #tiempo ingresado por el usuario(temporal)
        time_usuario = 1
        # generamiento de la grafica

        #generacion de la grafica del tiempo ingresado
        time = np.arange(0, time_usuario, 0.01)
        x = cord_x(self, time)
        y = cord_y(self, time)

        #grafica completa del lanzamiento
        time_complete = np.arange(0, time_impact(self) + 4, 0.01)
        x2 = cord_x(self, time_complete)
        y2 = cord_y(self, time_complete)

        #generacion del punto de posicion a medir
        x3 = cord_x(self, time_usuario)
        y3 = cord_y(self, time_usuario)

        #estetica de la grafica
        mpl.title("Aceleracion")
        mpl.xlim(0, alcance_max(self) + self.x0)
        mpl.ylim(0, altura_max(self) + self.y0)
        mpl.xlabel("-Distancia-")
        mpl.ylabel("-Altura-")

        #generamiento de las curvas
        mpl.plot(self.x0, self.y0, "k-o")  #punto pos inicial
        mpl.plot(x, y, "y-")  #curva del usuario
        mpl.plot(x2, y2, "k--")  #lanzamiento completo
        mpl.plot(x3, y3, "r-o")  #punto del usuario
        mpl.grid()  #cuadriculado

        #generacion del vector con origen en el punto de posicion
        mpl.plot(x3, y3 - time_impact(self), "g-o")

        # inicializa el punto de interseccion del eje Y
        tiempo_init_y.configure(state='normal')
        tiempo_init_y.delete(0, 'end')
        tiempo_init_y.insert(0, time_impact(self))
        tiempo_init_y.configure(state='readonly')
Beispiel #38
0
    def boton_vector_normalf(self):

        velocidad = int(self.entrada_Rapidez_inicial.get())
        angulo_inicial = int(self.entrada_angulo_inicial.get())
        nose = (self.gravedad * int(self.entrada_posicion_y0.get()) /
                math.pow(velocidad, 2))
        tiempo_maximov1 = (velocidad / self.gravedad) * (
            (math.sin(math.radians(angulo_inicial))) + math.sqrt(
                math.pow(math.sin(math.radians(angulo_inicial)), 2) +
                2 * nose))
        tiempo_maximo = tiempo_maximov1
        print(tiempo_maximov1)

        label = tk.Label(Pop_Up)
        label.pack()

        # Crea un cuadro que contiene los datos

        # Organiza los datos
        separador = ttk.Separator(Pop_Up, orient="horizontal")
        separador.pack(side=tk.TOP, expand=False, fill=tk.X)

        # Tiempo Impacto
        def time_impact(self):
            t = ((self.velocidad_inicial * sin(self.angulo)) /
                 (2 * self.gravedad)) + (
                     (1 / self.gravedad) *
                     (sqrt(((self.velocidad_inicial * sin(self.angulo))**2) +
                           (2 * self.y0 * self.gravedad))))
            print(t)
            return t

            # Coordenada X

        def cord_x(self, t):
            x = self.x0 + ((self.velocidad_inicial * cos(self.angulo)) * t)
            return x

            # # Coordenada Y

        def cord_y(self, t):
            y = self.y0 + (((self.velocidad_inicial *
                             (cos(self.angulo))) * t) - ((self.gravedad / 2) *
                                                         (t**2)))
            return y

            # Altura máxima gráfica

        def altura_max(self):
            r = self.y0 + (((self.velocidad_inicial * (sin(self.angulo)))**2) /
                           (self.gravedad))
            return r

            # Alcance maximo gráfica

        def alcance_max(self):
            alc = self.x0 + ((self.velocidad_inicial * sin(2 * self.angulo)) / (self.gravedad)) + \
                  ((self.velocidad_inicial * cos(self.angulo)) /
                   (self.gravedad)) * sqrt(
                ((self.velocidad_inicial * sin(self.angulo)) ** 2) + 2 * self.y0 * self.gravedad)
            return alc

        time_usuario = 1  # Tiempo ingresado(arreglar)

        # Grafica del tiempo ingresado
        time = np.arange(0, time_usuario, 0.01)
        x = cord_x(self, time)
        y = cord_y(self, time)

        # Grafica completa lanzamiento
        time_complete = np.arange(0, time_impact(self) + 4, 0.01)
        x2 = cord_x(self, time_complete)
        y2 = cord_y(self, time_complete)

        # Punto de posicion a medir
        x3 = cord_x(self, time_usuario)
        y3 = cord_y(self, time_usuario)

        # Detalles gráfica
        mpl.title("Aceleracion normal y tangencial")
        mpl.xlim(0, alcance_max(self) + self.x0)
        mpl.ylim(0, altura_max(self) + self.y0)
        mpl.xlabel("Distancia")
        mpl.ylabel("Altura")

        # Generación curvas
        mpl.plot(self.x0, self.y0, "k-o")  # Posición inicial
        mpl.plot(x, y, "y-")  # Curva
        mpl.plot(x2, y2, "k--")  # Lanzamiento
        # mpl.plot(x3, y3, "r-o")  # Punto ingresado  #Punto rojo de altura maxima
        mpl.grid()  # Cuadriculacion del grafico

        # Generación de las flechas para aceleración normal y tangencial
        # >>>>Genera bien las flechas y texto, pero tira un warning. Falta arreglar<<<<
        ax = mpl.axes()
        ax1 = mpl.axes()
        ax.text(15.4, 10.8, 'an', fontsize=9)  # Texto aceleracion normal
        ax.arrow(x3, y3, 0, -4, head_width=0.5, head_length=1, fc='k',
                 ec='k')  # Flecha de aceleracion normal
        ax.text(18.7, 13.7, 'at', fontsize=9)  # Texto aceleracion tangencial
        ax1.arrow(x3, y3, 5, 0, head_width=0.5, head_length=1, fc='k',
                  ec='k')  # Flecha de aceleracion tangencial

        # Muestra el grafico
        mpl.plot()
        mpl.show()
        pass
         
data.head()

data['title'] = [t[0:-7] for t in data.title]
data.head()

data[['score', 'runtime', 'year', 'votes']].describe()

print len(data[data.runtime == 0])

data.runtime[data.runtime==0] = np.nan

data.runtime.describe()

plt.hist(data.year, bins=np.arange(1950, 2013), color='#cccccc')
plt.xlabel("Release Year")
remove_border()
# Received following message: 
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# AttributeError: 'module'object has not attribute 'hist'
# AND
# AttributeError: 'module'object has not attribute 'xlabel'

plt.hist(data.score, bins=20, color='#cccccc')
plt.xlabel("IMDB rating")
remove_border()
# Again, I'm receiving AttributeError messages. Is there an issue with the matplotlib that is not allowing me to produce a histogram?

plt.scatter(data.year, data.score, lw=0, alpha=.08, color='k')
plt.xlabel("Year")
Beispiel #40
0
def merlin ( dataset , pos1 , pos2 , act_fun , sov_fun ) :

 print("Dataset Given" , dataset.head())
 x = dataset.iloc[:, -pos1].values
 y = dataset.iloc[:, -pos2].values
 print("x \n" , x )
 print("y \n" , y )
 x = x.reshape(-1, 1)
 y = y.reshape(-1, 1)

  # Test/Trin Split with shuffle OFF
 from sklearn.model_selection import train_test_split

 x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=5, shuffle=False)

#datset plotting module
 plt.scatter(x_train, y_train, color='black')
 plt.plot(x_test, y_test, color='black')
 plt.title('Full Data')
 plt.xlabel('Days From 1st Case')
 plt.ylabel('Cases')
 plt.show()

# creating a score dataset
 cnames = ['Accuracy', 'Size', 'alpha','Activation_Function','Solver']
 acc_matrix = pd.DataFrame(columns=cnames)
 print(acc_matrix.head())
 acc_lst = []
 i_lst = []
 nr_list = []
 fun1=[]
 fun2=[]

 iterate_list = [0.0000000001, 0.0000000002, 0.0000000003, 0.0000000004, 0.0000000005, 0.0000000006, 0.0000000007,
                    0.0000000008, 0.0000000009,
                    0.000000001, 0.000000002, 0.000000003, 0.000000004, 0.000000005, 0.000000006, 0.000000007,
                    0.000000008, 0.000000009,
                    0.00000001, 0.00000002, 0.00000003, 0.00000004, 0.00000005, 0.00000006, 0.00000007, 0.00000008,
                    0.00000009,
                    0.0000001, 0.0000002, 0.0000003, 0.0000004, 0.0000005, 0.0000006, 0.0000007, 0.0000008, 0.0000009,
                    0.000001, 0.000002, 0.000003, 0.000004, 0.000005, 0.000006, 0.000007, 0.000008, 0.000009,
                    0.00001, 0.00002, 0.00003, 0.00004, 0.00005, 0.00006, 0.00007, 0.00008, 0.00009,
                    0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008, 0.0009,
                    0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009,
                    0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09
                    ]

# importing the nural net

 from sklearn.neural_network import MLPRegressor
 from sklearn.metrics import r2_score

# model Testing Module

 for nr in range(100, 110, 10):
  print("Nural Size = ", nr)
  for i in iterate_list:
   mlp = MLPRegressor(activation=act_fun, alpha=i, batch_size='auto', beta_1=0.9,
                               beta_2=0.999, early_stopping=False, epsilon=1e-08,
                               hidden_layer_sizes=(nr), learning_rate='constant', max_iter=90000000, momentum=0.9,
                               n_iter_no_change=10, nesterovs_momentum=True, power_t=0.5,
                               random_state=5, shuffle=False, solver=sov_fun, tol=0.0001,
                               validation_fraction=0.1, verbose=False, warm_start=True )
   mlp.fit(x_train, y_train.ravel())

   predict_test = mlp.predict(x_test)
   scr = r2_score(y_test, predict_test)
   acc_lst.append(scr)
   i_lst.append(i)
   nr_list.append(nr)
   fun1.append(act_fun)
   fun2.append(sov_fun)
   print(" i = ", i, "Score = ", scr)

 print("Training Complete")
 print()
 acc_matrix['Accuracy'] = acc_lst
 acc_matrix['Size'] = nr_list
 acc_matrix['alpha'] = i_lst
 acc_matrix['Activation_Function'] = fun1
 acc_matrix['Solver'] = fun2

 acc_matrix.reset_index()

 print(acc_matrix.head())
 for i in acc_matrix.index:
  if acc_matrix['Accuracy'][i] == max(acc_matrix['Accuracy']):
   print("Best Parameters For The Model Is\n")
   print("Accuracy ", acc_matrix["Accuracy"][i])
   print("Nural Size ", acc_matrix['Size'][i])
   print("aplha =", acc_matrix['alpha'][i])
   print("Activation Function =", acc_matrix['Activation_Function'][i])
   print("Solver =", acc_matrix['Solver'][i])
lr_b = 0
lr_w = 0
# Iterations
for i in range(iteration):

    b_grad = 0.0
    w_grad = 0.0
    for n in range(len(x_data)):
        b_grad = b_grad - 2.0 * (y_data[n] - b - w * x_data[n]) * 1.0
        w_grad = w_grad - 2.0 * (y_data[n] - b - w * x_data[n]) * x_data[n]

    lr_b = lr_b + b_grad**2
    lr_w = lr_w + w_grad**2

    # Update parameters
    b = b - lr / np.sqrt(lr_b) * b_grad
    w = w - lr / np.sqrt(lr_w) * w_grad

    # Store parameters for plotting
    b_history.append(b)
    w_history.append(w)

#plot the figure
plt.contourf(x, y, z, 50, alpha=0.5, cmap=plt.get_cmap('jet'))
plt.plot([-188.4], [2.67], 'x', ms=12, markeredgewidth=3, color='orange')
plt.plot(b_history, w_history, 'o-', ms=3, lw=1.5, color='black')
plt.xlim(-200, -100)
plt.ylim(-5, 5)
plt.xlabel(r'$b$', fontsize=16)
plt.ylabel(r'$w$', fontsize=16)
plt.show()
#total number of missing values in dataframe
loan_file.isnull().sum(axis=0)

# In[7]:

loan_file['LoanAmount'].mean()

# In[ ]:

# In[8]:

#visualization of distribution
#lets plot the loan amount and income
loan_file['ApplicantIncome'].hist(bins=50)
plt.title('ApplicantIncome Analysis')
plt.xlabel('Amount')

# In[9]:

#boxplot to understand the destribution
loan_file.boxplot(['ApplicantIncome'])
plt.show()
'''it shows there are too many outliers present in this columns. it shows the income disparity'''

# In[10]:

#income check on the basis of education level
loan_file.boxplot(['ApplicantIncome'], by=['Education'])
plt.show()

# In[11]:
	t = pickle.load(pickle_file)
	v = pickle.load(pickle_file)
	print t
	print v

initialize(v, s, t, dt, n)
calculate(v, s, t, dt, n)
store(v, t, n)

#plot
plt.figure(1)
plt.subplot(211)
plt.plot(t, v,"g-", linewidth=2.0)
plt.scatter(t, v)
plt.title('The Velocity of a Free Falling Object')
plt.xlabel('Time($t$)', fontsize=14)
plt.ylabel('Velocity($m/s$)', fontsize=14)
plt.text(3,-60,r'$g = 9.8 m/s^2$', fontsize=16)
plt.grid(True)

plt.subplot(212)
plt.plot(t, s,"g-", linewidth=2.0)
plt.scatter(t, s)
plt.title('The Displacement of a Free Falling Object')
plt.xlabel('Time($t$)', fontsize=14)
plt.ylabel('Displacement($m$)', fontsize=14)
plt.text(3,-300,r'$g = 9.8 m/s^2$', fontsize=16)
plt.grid(True)

plt.show()
plt.savefig("ex1.jpg")
Beispiel #44
0
    line = line.strip()
    A, B, C, D, E, F = line.split(',')
    if (A == "ConcurrentHashMap" and E == "write"):
        sum1 += int(F)
        count += 1
    if (A == "SynchronizedMap" and E == "write"):
        sum2 += int(F)
        count2 += 1
    if (count == 200):
        x.append(B)
        y.append(sum1 / 1000)
        count = 0
        sum1 = 0
    if (count2 == 200):
        x1.append(B)
        y1.append(sum2 / 1000)
        count2 = 0
        sum2 = 0

dataset8.close()
plt.xlabel('Threads')
plt.ylabel('Tempo Médio (ns)')
line_up, = plt.plot(x, y, label='ConcurrentHashMap')
line_down, = plt.plot(x1, y1, label='Collections.synchronizedMap')
plt.legend([line_up, line_down],
           ['ConcurrentHashMap', 'Collections.synchronizedMap'])

plt.title('Grafico para operação de escrita, usando 0.2 de carga na operação')

plt.show()
Beispiel #45
0
rorder = {}
rorder_len = 0
for k, v in sorted(res, key = lambda x: x[1], reverse=True):
    rorder[k] = rorder_len + 1
    rorder_len += 1


ntop = int(threshold_ratio * rorder_len)
output = open(outfile, 'w')
nhit = 0
ntot = 0
output.write('EventId,RankOrder,Class\n')
for k, tmp in res:
    if rorder[k] <= ntop:
        lb = 's'
        nhit += 1
    else:
        lb = 'b'

    output.write('%s,%d,%s\n' % (k,  len(rorder) + 1 - rorder[k], lb))
    ntot += 1
output.close()



feature_names = list(data_test[1:].columns)
for feature in feature_names:
    tmp = data_train.DER_mass_MMC[data_train.DER_mass_MMC != -999.0]
    tmp.hist(bins=100)
    mplt.xlabel(feature)
    mplt.show()
import matplotlib as plt
from 数据分析处理 import price, area
linear = LinearRegression()
area = np.array(area).reshape(-1, 1)
price = np.array(price).reshape(-1, 1)
# 训练模型
model = linear.fit(area, price)
# 打印截距和回归系数
print(model.intercept_, model.coef_)

# 线性回归可视化(数据拟合)
linear_p = model.predict(area)
plt.figure(figsize=(12, 6))
plt.scatter(area, price)
plt.plot(area, linear_p, 'red')
plt.xlabel("area")
plt.ylabel("price")
plt.show()

# 使用train_test_split进行交叉验证
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.2,
                                                    random_state=12)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)

# 模型训练
linear = LinearRegression()
model = linear.fit(x_train, y_train)
Beispiel #47
0
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 23 11:07:06 2017

@author: Paige
"""

import matplotlib as plt
import numpy as np


x = np.linspace(-10, 10, 400)
cos = np.cos(x)
sin = np.sin(x)
plt.plot(x, cos, "r", label="cos(x)")        # r for red line
plt.plot(x, sin, "b--", label="sin(x)")      # b-- for blue dashed line
# colors include red(r), green(g), yellow(y), blue(b), cyan(c),
# magenta(m), black(k), white(w)
# line styles include solid line("-"), dashed line("--"),
# solid line("-"), square("s"), dots("o"), smaller dots(".")
plt.xlim(-10, 10)
plt.legend()
plt.xlabel("x")
plt.title("Trigonometric Functions")
plt.ylabel("cos(x) or sin(x)")
# plt.savefig("1-23-2017.png")
plt.show()
Beispiel #48
0

def print_statements(soln, i):
    if i > 0:  # Solution found
        print("Number of function calls: %d" % (2 + i))
        print("A solution is: %f" % (soln))
    else:
        print("Solution not found!")


solution, no_iter, vals, objectfs = n_m(0.8, 1e-1)
print_statements(solution, no_iter)
print("\n\n")

plt.scatter(vals, objectfs)
plt.xlabel("X")
plt.ylabel("f(X)")
plt.title("Newton's method")
plt.show()

x0 = 0.3
x1 = 0.8
solution, no_iter, ob, vals = secant_mth(x0, x1, eps=1e-1)
print_statements(solution, no_iter)

plt.scatter(vals, ob)
plt.xlabel("X")
plt.ylabel("f(X)")
plt.title("Secant method")
plt.show()
Beispiel #49
0
        cos += ((-1)**i)*x**(2*i)/math.factorial(2*i)
    return cos


x0 = time.clock()

for i in range(1, 100):
    x = tcos(1, i)
    x = time.clock()
    y = numpy.cos(1)
    y = time.clock()
    print 'tcos :' + str(x - x0) + '  numpy: ' + str(y - x)
    x0 = y

t = numpy.arange(0.0, 1.1, 0.01)
plt.plot(t, tcos(2*pi*t, 10))
plt.plot(t, tcos(2*pi*t, 9))
plt.plot(t, numpy.cos(t*2*pi))

plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.show()

print '\nExercise 1.3.2:\n'
print '    As you can see in the plot, the 10th order gives a good approximation of the cos-function.'

print '\nExercise 1.3.3:\n'
print '    My method does not determine any significant difference betwwen the two implementations.'
print '    I would expect a better performance of the numpy implementation.'
Beispiel #50
0
def pos_plot(isnap, irun, iPartType,  lim):
    'Function plots position plots of particles for three projections'
    #... Time
    time = isnap*0.05
    
    #...Tag for Saving
    tag_list = ['Gas', 'DM', '?', '',  'Stars', '' ]
    tag_type = tag_list[iPartType]

    #...Load File
    f = h5py.File(base + '/snapshot_' + str(isnap).zfill(3) +'.hdf5')

    #...Load Stellar Data
    xyz = np.array(f['PartType'+str(iPartType)+'/Coordinates'])
    
    ##############################################   PLOT   ########################################################

    #...Some Parameters:
    cbar = 'magma'
    nbins = 100
    cmin,cmax = 7.0, 11.0 #0, 1.5e4
    mperp = 800

    ############# Calculate counts

    counts1, xedges, yedges, img = plt.hist2d(xyz[:,0],xyz[:,1],bins=nbins,range=[[-lim,lim],[-lim,lim]])
    counts2, xedges, yedges, img = plt.hist2d(xyz[:,0],xyz[:,2],bins=nbins,range=[[-lim,lim],[-lim,lim]])
    counts3, xedges, yedges, img = plt.hist2d(xyz[:,1],xyz[:,2],bins=nbins,range=[[-lim,lim],[-lim,lim]])

    #...Set up figure box:
    plt.style.use('dark_background')
    fig = plt.figure(figsize = [20,6])

    #########################  Plot xy
    ax = fig.add_subplot(1, 3, 1)

    #counts, xedges, yedges, img = plt.hist2d(xyz[:,0],xyz[:,1],bins=nbins)
    counts1 = np.log(counts1*mperp)
    plt.imshow(counts1, origin='lower',extent=[-lim, lim, -lim, lim], cmap=cbar, vmin=cmin, vmax=cmax)
    
    ax.axis([-lim,lim,-lim,lim])
    plt.axis('equal')
    #plt.xlim(box_size)
    #plt.ylim(box_size)
    plt.xlabel('x (kpc)')
    plt.ylabel('y (kpc)')
    plt.title('xy')
    #plt.colorbar()
    ax.text(-lim, lim, str(time)+'Gyr', fontsize=20)

    #########################  Plot xz

    ax = fig.add_subplot(1, 3, 2)

    #plt.hist2d(xyz[:,0],xyz[:,2],cmap=cbar,bins=nbins,weights=mass,vmin=cmin,vmax=cmax)
    
    #counts, xedges, yedges, img = plt.hist2d(xyz[:,0],xyz[:,2],bins=nbins)
    counts2 = np.log(counts2*mperp)
    plt.imshow(counts2, origin='lower',extent=[-lim, lim, -lim, lim], cmap=cbar, vmin=cmin, vmax=cmax)
    
    ax.axis([-lim,lim,-lim,lim])
    plt.axis('equal')
    #plt.xlim(box_size)
    #plt.ylim(box_size)
    plt.xlabel('x (kpc)')
    plt.ylabel('z (kpc)')
    plt.title('xz')
    #plt.colorbar()
    #ax.text(-lim+1, lim+.5, str(time)+'Gyr', fontsize=20)

    #########################  Plot yz
    ax = fig.add_subplot(1, 3, 3)

    #plt.hist2d(xyz[:,1],xyz[:,2],cmap=cbar,bins=nbins,weights=mass,vmin=cmin,vmax=cmax)

    #counts, xedges, yedges, img = plt.hist2d(xyz[:,1],xyz[:,2],bins=nbins)
    counts3 = np.log(counts3*mperp)
    plt.imshow(counts3, origin='lower',extent=[-lim, lim, -lim, lim], cmap=cbar, vmax=cmax)

    ax.axis([-lim,lim,-lim,lim])
    plt.axis('equal')
    #plt.xlim(box_size)
    #plt.ylim(box_size)
    plt.xlabel('y (kpc)')
    plt.ylabel('z (kpc)')
    plt.title('yz')
    cb = plt.colorbar()
    cb.set_label('log(Msun/bin)',fontsize = 20)
    #ax.text(-lim+1, lim+.5, str(time)+'Gyr', fontsize=20)

    #################################################  SAVING ########################################################

    save_fig_file = '/data8/data/mercadf1/output/Pegasus/high_res/aruns/run_'+ str(irun)+'/Plots/pos_plots/'+tag_type+'_2Dhist_run'+str(irun)+'_snap'+str(isnap)+'.png'
    #save_fig_file = '/data25/rouge/gonzaa11/francisco/outputs/runmed'+str(irun)+'/Plots/pos_plots/'+tag_type+'_pos_run'+str(irun)+'_snap'+str(isnap)+'.png'
    #...Report saving:
    print "Saving : "
    print str(save_fig_file)

    #...Save Figure:
    plt.savefig(save_fig_file)

    return 'Snapshot '+str(isnap)+' complete'
def linear_regression(x_data, y, tag=""):
    # Splitting the dataset into the Training set and Test set
    # Here I am using the train_test_split function present in the SKlearn library.
    # This function splits the data into training testing and validation datasets as shown below.
    # we can specify the percentage of test size compared to the input matrix size.

    from sklearn.model_selection import train_test_split
    x_train, x_test, y_train, y_test = train_test_split(x_data,
                                                        y,
                                                        test_size=0.2,
                                                        random_state=0)

    # The rows are randomly sorted into the groups to form M initial clusters. An exchange algorithm is applied to
    # this initial configuration which searches for the rows of data that would produce a maximum decrease in a
    # least-squares penalty function.
    kmeans = KMeans(n_clusters=M, random_state=0).fit(x_train)
    Mu = kmeans.cluster_centers_

    def big_sigma_matrix(Data):
        """
        Summation of all the variance values along the diagonal. This is also called the co-variance matter.
        :param Data: This will be the training data that we are using to generate the covariance matrix
        :return: Covariance matrix
        """
        BigSigma = np.zeros((Data.shape[1], Data.shape[1]))
        DataT = np.transpose(Data)
        for i in range(0, len(DataT)):
            BigSigma[i][i] = np.var(DataT[i])
        BigSigma = np.dot(200, BigSigma)
        return BigSigma

    def phi_matrix(Data, MuMatrix, BigSigma):
        """
        This will generate the design matrix phi
        :param Data: Training/Testing/Validation sets
        :param MuMatrix: Matrix with centroid values for the clusters
        :param BigSigma: Covariance matrix
        :return: Design matrix Phi
        """
        phi = np.zeros((int(len(Data)), len(MuMatrix)))
        BigSigInv = np.linalg.inv(BigSigma)
        for C in range(0, len(MuMatrix)):
            for R in range(0, int(len(Data))):
                sub = np.subtract(Data.iloc[R], MuMatrix[C])
                sub_1 = np.dot(BigSigInv, np.transpose(sub))
                phi[R][C] = np.math.exp(-0.5 * np.dot(sub, sub_1))
        return phi

    def closed_form_weights(phi_matrix, lambda_value, targets):
        """
        Calculate the weights (W/theta Matrix)
        :param phi_matrix: Design matrix Phi
        :param lambda_value: term that governs the relative importance of the regularization term
        :param targets: Target vector
        :return: Weight matrix W/theta
        """
        lamba_identity = np.identity(M)
        for i in range(M):
            lamba_identity[i][i] = lambda_value
        weight = np.linalg.inv(
            np.add(lamba_identity,
                   np.dot(phi_matrix.transpose(), phi_matrix))).dot(
                       phi_matrix.transpose().dot(targets))
        return weight

    def root_mean_squared_error_SGD(data, output):
        """
        Method to find the RMS value for SGD method
        :param data: Data matrix
        :param output: Actual Output matrix retrieved through regression
        :return: RMS value
        """
        sum = 0.0
        for i in range(0, len(output)):
            sum = sum + np.math.pow((data[i] - output[i]), 2)
        return str(np.math.sqrt(sum / len(output)))

    # Generate the big sigma, training/testing/validation design matrices and the weights using the training design
    # matrix
    big_sigma = big_sigma_matrix(x_data)
    training_phi = phi_matrix(x_train, Mu, big_sigma)
    testing_phi = phi_matrix(x_test, Mu, big_sigma)
    weights_cf = closed_form_weights(training_phi, lambda_value, y_train)

    # Choose a random initial weight
    initial_weight = np.dot(220, weights_cf)
    initial_weight = initial_weight.transpose()

    # Iterate over 500 times to find the minima using the gradient descent approach
    # To find a local minimum of a function using gradient descent, we take steps proportional to the negative of the
    # gradient of the function at the current point.
    testing_error = []
    for i in range(0, epochs):
        delta_error = np.add(
            -np.dot(training_phi[i] - np.dot(initial_weight, training_phi[i]),
                    (training_phi[i])), np.dot(2, initial_weight))
        new_weight = -np.dot(alpha, delta_error) + initial_weight
        initial_weight = new_weight

        # Find the RMS value at each iteration to check the learning of the model. We try to approach the minima which is
        # ideally 0. In our case with 500 iterations I was able to reach 0.05-0.06. With higher number of iterations,
        # we can decrease the RMS value further.
        training_sgd_output_rms = root_mean_squared_error_SGD(
            np.dot(new_weight, np.transpose(training_phi)),
            training_phi.transpose()[0])
        testing_sgd_output_rms = root_mean_squared_error_SGD(
            np.dot(new_weight, np.transpose(testing_phi)),
            testing_phi.transpose()[0])
        testing_error.append(float(testing_sgd_output_rms))
    print(testing_error[len(testing_error) - 1])
    figure = plt.figure()
    plt.plot(np.arange(epochs), testing_error)
    plt.suptitle('Error RMS')
    plt.xlabel('Number of iterations')
    plt.ylabel('Testing RMS error rate')
    figure.savefig(tag)
Beispiel #52
0
        if i%60 == 0 or i%100 == 0 or i == 1 or i == 2 or i == 3:
            train_accuracy = accuracy.eval(feed_dict={x: np.array(X_train1).reshape(-1,224*224*3)
            ,y_: np.array(y_train1).reshape(-1,4),keep_prob: 1})
            print('step %d, training accuracy %g' % (i, train_accuracy))

            if train_accuracy >0.75 or i%120 == 0  or i == 1 or i == 2 or i == 3:
                confusion_mc=sess.run(tf.confusion_matrix( tf.argmax(y_, 1),tf.argmax(y_conv, 1)),
                                      feed_dict = {x: np.array(X_test_cv).reshape(-1,224*224*3),
                                                   y_: np.array(y_test_cv).reshape(-1,4),keep_prob: 1})
                df_cm = pd.DataFrame(confusion_mc,
                                     index =y_train1.columns, columns = y_train1.columns )
                plt.figure()
                sns.heatmap(df_cm, annot=True)
                plt.ylabel('True label %d ' %i)
                plt.xlabel('Predicted label')
                plt.show()
                print(np.median(sess.run(W_conv1_1)),np.mean(sess.run(W_conv1_1)),np.var(sess.run(W_conv1_1)))
                print(np.median(sess.run(W_conv3_2)),np.mean(sess.run(W_conv3_2)),np.var(sess.run(W_conv3_2)))
                print(np.median(sess.run(W_fc2)),np.mean(sess.run(W_fc2)),np.var(sess.run(W_fc2)))
                print((sess.run(cross_entropy,feed_dict={x: np.array(X_train1).reshape(-1,224*224*3),
                                                            y_: np.array(y_train1).reshape(-1,4),keep_prob: 1})))
            toc = time.time()
            print((toc-tic)/60)
        merge = tf.summary.merge_all()
        summary = sess.run(merge,feed_dict={x: np.array(X_train1).reshape(-1,224*224*3),
                                                y_:np.array(y_train1).reshape(-1,4),
                                                keep_prob: 1})
        if i%100==0:
            save_path = saver.save(sess, "C:/Users/woill/.spyder-py3/tmp/model.ckpt")
        writer.add_summary(summary, i)
Beispiel #53
0
# B) What does the distribution of petal lengths look like?

sns.distplot(iris.Petal_Length)

# C) Is there a correlation between petal length and petal width?

sns.heatmap(iris.corr(), annot=True, cmap='OrRd')

sns.relplot(data=iris, x="Petal_Length", y="Petal_Width")

# D) Would it be reasonable to predict species based on sepal width and sepal length?

iris.head()
sns.relplot(data=iris, x="Sepal_Width", y="Sepal_Length", hue="Species")
plt.title('Species by Septal')
plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')

# E) Which features would be best used to predict species?

sns.relplot(data=iris, x="Petal_Length", y="Petal_Width", hue="Species")
plt.title('Species by Petal')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')

sns.pairplot(data=iris, hue='Species')

# 1.) Use seaborn's load_dataset function to load the anscombe data set.
# Use pandas to group the data by the dataset column,
# and calculate summary statistics for each dataset. What do you notice?
Beispiel #54
0
        if (j < i):
            tag1 = keys_tags_pop[i]
            tag2 = keys_tags_pop[j]
            Count = np.asarray((CrossTab_artistsID_tagID[tag1] > 0)
                               & (CrossTab_artistsID_tagID[tag2] > 0))
            sm = Count.sum()
            DistanceMatrix[i, j] = np.exp(-sm)

DistanceMatrix_sym = (DistanceMatrix.T + DistanceMatrix)

Z = linkage(DistanceMatrix_sym, 'ward')
max_d = 50

plt.figure()
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('Tags ID')
plt.ylabel('Distance')
dendrogram(
    Z,
    leaf_rotation=90.,  # rotates the x axis labels
    leaf_font_size=8.,  # font size for the x axis labels
    labels=keys_tags_pop,
    color_threshold=max_d,
)
plt.savefig("Dendrogram_1.png", bbox_inches='tight')
plt.show()

clusters = fcluster(Z, max_d, criterion='distance')

#Interpretation
Profile = {}
        options_data.loc[option]['PRICE'],
        sigma_est=2.,   #Estimate for implied volatility
        it=100)
options_data['IMP_VOL'].loc[option] = imp_vol

futures_data['MATURITY']
    #Select column with name MATURITY
options_data.loc[46170]
#Select Data row for index 46170
options_data.loc[46710]['STRIKE']
#Select only value in column STRIKE
plot_data = options_data[options_data['IMP_VOL'] > 0]
maturities = sorted(set(options_data['MATURITY']))

maturities

#Reiterate over all maturities and plot
import matplotlib as plt
#%matplotlib inline
plt.figure(figsize=(8,6))
for maturity in maturities:
    data = plot_data[options_data.MATURITY == maturity]
    #Select data for this maturity
    plt.plot(data['STRIKE'], data['IMP_VOL'], label=maturity.date(), lw=1.5)
    plt.plot(datadata['STRIKE'], data['IMP_VOL'], 'r.')
plt.grid(True)
plt.xlabel('strike')
plt.ylabel('implied volatility of volatility')
plt.legend()
plt.show()
Beispiel #56
0
def final_decision_plot(df,
                        figureName,
                        z_s=10,
                        z_b=10,
                        show=False,
                        block=False,
                        trafoD_bins=True,
                        bin_number=15):
    """Plots histogram decision score output of classifier"""

    nJets = df['nJ'].tolist()[1]

    if trafoD_bins == True:
        bins, arg2, arg3 = trafoD_with_error(df)
        print(len(bins))
    else:
        bins = np.linspace(-1, 1, bin_number + 1)

    # Initialise plot stuff
    plt.ion()
    plt.close("all")
    fig = plt.figure(figsize=(8.5, 7))
    plot_range = (-1, 1)
    plot_data = []
    plot_weights = []
    plot_colors = []
    plt.rc('font', weight='bold')
    plt.rc('xtick.major', size=5, pad=7)
    plt.rc('xtick', labelsize=10)

    plt.rcParams["font.weight"] = "bold"
    plt.rcParams["axes.labelweight"] = "bold"
    plt.rcParams["mathtext.default"] = "regular"

    df = setBinCategory(df, bins)

    bins = np.linspace(-1, 1, len(bins))

    decision_value_list = df['bin_scaled'].tolist()
    post_fit_weight_list = df['post_fit_weight'].tolist()
    sample_list = df['sample'].tolist()

    # Get list of hists.
    for t in class_names_grouped[::-1]:
        class_names = class_names_map[t]
        class_decision_vals = []
        plot_weight_vals = []
        for c in class_names:
            for x in range(0, len(decision_value_list)):
                if sample_list[x] == c:
                    class_decision_vals.append(decision_value_list[x])
                    plot_weight_vals.append(post_fit_weight_list[x])

        plot_data.append(class_decision_vals)
        plot_weights.append(plot_weight_vals)
        plot_colors.append(colour_map[t])

    # Plot.
    if nJets == 2:

        multiplier = 20
    elif nJets == 3:
        multiplier = 100

    plt.plot([], [],
             color='#FF0000',
             label=r'VH $\rightarrow$ Vbb x ' + str(multiplier))

    plt.hist(plot_data,
             bins=bins,
             weights=plot_weights,
             range=plot_range,
             rwidth=1,
             color=plot_colors,
             label=legend_names[::-1],
             stacked=True,
             edgecolor='none')

    df_sig = df.loc[df['Class'] == 1]

    plt.hist(df_sig['bin_scaled'].tolist(),
             bins=bins,
             weights=(df_sig['post_fit_weight'] * multiplier).tolist(),
             range=plot_range,
             rwidth=1,
             histtype='step',
             linewidth=2,
             color='#FF0000',
             edgecolor='#FF0000')

    x1, x2, y1, y2 = plt.axis()
    plt.yscale('log', nonposy='clip')
    plt.axis((x1, x2, y1, y2 * 1.2))
    axes = plt.gca()
    axes.set_ylim([5, 135000])
    axes.set_xlim([-1, 1])
    x = [-1, -0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1]
    plt.xticks(x, x, fontweight='normal', fontsize=20)
    y = [r"10", r"10$^{2}$", r"10$^{3}$", r"10$^{4}$", r"10$^{5}$"]
    yi = [10, 100, 1000, 10000, 100000]
    plt.yticks(yi, y, fontweight='normal', fontsize=20)

    axes.yaxis.set_ticks_position('both')
    axes.yaxis.set_tick_params(which='major',
                               direction='in',
                               length=10,
                               width=1)
    axes.yaxis.set_tick_params(which='minor',
                               direction='in',
                               length=5,
                               width=1)

    axes.xaxis.set_ticks_position('both')
    axes.xaxis.set_tick_params(which='major',
                               direction='in',
                               length=10,
                               width=1)
    axes.xaxis.set_tick_params(which='minor',
                               direction='in',
                               length=5,
                               width=1)

    axes.xaxis.set_minor_locator(AutoMinorLocator(4))
    handles, labels = axes.get_legend_handles_labels()

    #Weird hack thing to get legend entries in correct order
    handles = handles[::-1]
    handles = handles + handles
    handles = handles[1:12]

    plt.legend(loc='upper right',
               ncol=1,
               prop={'size': 12},
               frameon=False,
               handles=handles)

    plt.ylabel("Events", fontsize=20, fontweight='normal')
    axes.yaxis.set_label_coords(-0.07, 0.93)
    plt.xlabel(r"BDT$_{VH}$ output", fontsize=20, fontweight='normal')
    axes.xaxis.set_label_coords(0.89, -0.07)
    an1 = axes.annotate("ATLAS Internal",
                        xy=(0.05, 0.91),
                        xycoords=axes.transAxes,
                        fontstyle='italic',
                        fontsize=16)

    offset_from = OffsetFrom(an1, (0, -1.4))
    an2 = axes.annotate(r'$\sqrt{s}$' + " = 13 TeV , 36.1 fb$^{-1}$",
                        xy=(0.05, 0.91),
                        xycoords=axes.transAxes,
                        textcoords=offset_from,
                        fontweight='normal',
                        fontsize=12)

    offset_from = OffsetFrom(an2, (0, -1.4))
    an3 = axes.annotate("1 lepton, " + str(nJets) + " jets, 2 b-tags",
                        xy=(0.05, 0.91),
                        xycoords=axes.transAxes,
                        textcoords=offset_from,
                        fontstyle='italic',
                        fontsize=12)

    offset_from = OffsetFrom(an3, (0, -1.6))
    an4 = axes.annotate("p$^V_T \geq$ 150 GeV",
                        xy=(0.05, 0.91),
                        xycoords=axes.transAxes,
                        textcoords=offset_from,
                        fontstyle='italic',
                        fontsize=12)

    fig = plt.gcf()
    #fig.set_size_inches(10, 4)
    plt.savefig(figureName, bbox_inches='tight',
                dpi=300)  # should before plt.show method
    plt.show(block=block)

    return fig, axes
Beispiel #57
0
    # Change the axis units font
    plt.setp(ax.get_ymajorticklabels(), fontsize=18)
    plt.setp(ax.get_xmajorticklabels(), fontsize=18)

    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')

    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

    # Turn on the plot grid and set appropriate linestyle and color
    ax.grid(True, linestyle=':', color='0.75')
    ax.set_axisbelow(True)

    # Define the X and Y axis labels
    plt.xlabel('Impact Velocity (m/s)', fontsize=22, weight='bold', labelpad=5)
    plt.ylabel('Drop Height (m)', fontsize=22, weight='bold', labelpad=10)
    plt.plot(impact_velocity,
             height,
             linewidth=2,
             linestyle='-',
             label=r'Height (m)')

    # uncomment below and set limits if needed
    # plt.xlim(0,5)
    # plt.ylim(0,10)

    # Adjust the page layout filling the page using the new tight_layout command
    plt.tight_layout(pad=0.5)

    # save the figure as a high-res pdf in the current folder
Beispiel #58
0
import pandas as pd
import matplotlib as plt
import seaborn as sns

flight_filepath = "../input/flight_delays.csv"
flight_data = pd.read_csv(flight_filepath, index_col="Month")

# Bar
plt.figure(figsize=(10, 6))
plt.title("Average Arrival Delay for Spirit Airlines Flights, by Month")

sns.barplot(x=flight_data.index, y=flight_data['NK'])
plt.ylabel("Arrival delay (in minutes)")

plt.figure(figsize=(14, 7))
plt.figure(figsize=(14, 7))
sns.heatmap(data=flight_data, annot=True)
plt.xlabel("Airline")
Beispiel #59
0
import numpy
import matplotlib as plt

### Displace Random Weight DIMER affect on Saddle Force Calls ###
SADDLE_E = [[
    .299, .324, .325, .326, .337, .348, .357, .358, .359, .360, .368, .370,
    .389, .390, .391, 1.276, 1.278, 2.076, 2.156, 2.484
]]
x = [0, 0.3, 0.6, 0.9]
plt.gca().set_color_cycle(['red', 'green', 'blue', 'yellow', 'black'])
plt.plot(x, SADDLE1)
plt.scatter(x, SADDLE1)
plt.scatter(x, Saddle2)
plt.scatter(x, Saddle3)
plt.scatter(x, Saddle4)
plt.scatter(x, Saddle5)
plt.plot(x, Saddle2)
plt.plot(x, Saddle3)
plt.plot(x, Saddle4)
plt.plot(x, Saddle5)
plt.legend([
    'Saddle 1 (0.588)', 'Saddle 2 (0.607)', 'Saddle 3 (0.983)',
    'Saddle 4 (.985)', 'Saddle 5 (0.986)'
],
           loc='lower left')
plt.xlabel('Probability of Displacing Randomly')
plt.ylabel('Avg. Force Calls', color='b')
#plt.show()
savefig('Dimer_Displace_Random_Weight.png')
Beispiel #60
0
# In[29]:

y = data['Purchase']

# In[35]:

from sklearn.linear_model import Ridge
ridge_regressor = Ridge(alpha=1e-10)
ridge_regressor.fit(datax, y)

X_grid = np.arange(min(datax), max(datax), 0.01)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(datax, y, color='red')
plt.plot(X_grid, regressor.predict(X_grid), color='blue')
plt.title('Truth or Bluff (Random Forest Regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

# In[38]:

tdata = pd.read_csv("E:\\assignment\\ass 3 analytic vidhya\\test.csv")

# In[39]:

tdata.isnull().values.any()

# In[40]:

features_with_na = [
    features for features in tdata.columns