Esempio n. 1
0
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()
Esempio n. 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()
Esempio n. 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")
Esempio n. 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)
Esempio n. 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__);
Esempio n. 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()
Esempio n. 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")
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)
Esempio n. 10
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()
Esempio n. 11
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()
Esempio n. 12
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'))
Esempio n. 13
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()
Esempio n. 14
0
def plot_sinad_sfdr (label, data_x, data_y, chans=[0,1,2,3],
                     titles=['SFDR','SINAD']):
    """
    x   x values of data (same for all chans)
    y   array with shape (2, chans, data)
    """
    n=len(chans)    
    n2=len(titles)
    pos=np.arange(n2*n)+1

    for t in range(n2):
        pos_val=pos[t::n2]
        for chan in chans:
            plt.subplot(n,n2,pos_val[chan])
            plt.plot(data_x,data_y[t][chan],label=label)
            if t==0:
                plt.ylabel('Chan %i' %chan)
            if chan==0:
                plt.title(titles[t])
Esempio n. 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)
Esempio n. 16
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;
Esempio n. 17
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!'
Esempio n. 18
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
Esempio n. 19
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()
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
    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):
        value_keys = f[test][sorted_keys[j]].keys()
Esempio n. 22
0
@author: DELL
"""

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib as plt
import matplotlib.pyplot as plt

data = pd.read_csv("E:\\assignment\\ass 13 ml starter\\train_HK6lq50.csv")
data.head()
count_classes = pd.value_counts(data['is_pass'], sort=True)
count_classes.plot(kind='bar', rot=0)
plt.title("Transaction Class Distribution")
plt.xlabel("Class")
plt.ylabel("Frequency")
from sklearn.utils import resample
data_majority = data[data.is_pass == 1]
data_minority = data[data.is_pass == 0]
data_majority.count()
data_minority_upsampled = resample(
    data_minority,
    replace=True,  # sample with replacement
    n_samples=50867,  # to match majority class
    random_state=123)
data_upsampled = pd.concat([data_majority, data_minority_upsampled])
data_upsampled.is_pass.value_counts()
data_upsampled.head()
data_upsampled.info()
data_upsampled.drop(['id', 'program_id', 'trainee_id'], axis=1, inplace=True)
data_upsampled['is_handicapped'].unique()
	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")
read()
Esempio n. 24
0
#coding:utf-8
import numpy as np
import pandas as pd
import matplotlib as plt  #65ans:matplotlib.pyplot

import string
dfxy = pd.read_csv(
    'xyfilm.csv',
    delimiter=';',
    names=['fn', 'date', 'bor'],
)
dfxypt = dfxy[['date', 'bor']]
dfpxy = dfxypt.sort_values(by='date',
                           ascending=1)  #66ans: 'date'#67ans:ascending=1
dfpxy['bor'] = dfpxy['bor'] / 10.0  #69ans:['bor']  #69ans:/10.0
dfpxy.index = dfpxy['date']
del dfpxy['date']
plt.title("BOR")  #70ans:title
plt.xlabel('day')
plt.ylabel('Box Office Return')  #71ans:Box Office Return
dfpxy['bor'].plot(style='rD--')  #72ans:rD--
plt.show()
Esempio n. 25
0
"""
adc_cal.clear_ogp()
adc_cal.clear_inl()

freqarray=[50,800,30]

sfdr,sinad=adc_cal.do_sfdr_sinad_cw_sweep(chans=[2,3], freqarray=freqarray)

sinad_values,freqs= dic2arr(sinad)
sfdr_values,f = dic2arr(sfdr)

""" No corrections """
plt.subplot(221)
plt.plot(freqs,sinad_values[0],'-*', label='No corrections')
plt.title('SINAD')
plt.ylabel('Chan 2')

plt.subplot(223)
plt.plot(freqs,sinad_values[1],'-*', label='No corrections')
plt.ylabel('Chan 3')

plt.subplot(222)
plt.plot(freqs,sfdr_values[0],'-*', label='No corrections')
plt.title('SFDR')

plt.subplot(224)
plt.plot(freqs,sfdr_values[1],'-*', label='No corrections')

""" OGP corrections """
adc_cal.do_ogp_cw_sweep(chans=[2,3])
ogp2 = adc_cal.get_ogp_chan(2)
Esempio n. 26
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()
std_order = orders.groupby('month').price.std().reset_index()
print std_order

#create a set of axes
ax = plt.subplot()
#create a variable with the average prices
bar_heights = avg_order['price']
#create a variable with the standard deviation
bar_errors = std_order['price']

#Create a bar chart with the following attributes: bar_heights as the bar value, bar_errors as error bars, capsize of 5, the months as x-label, a y-label and a descriptive title
plt.bar(range(len(bar_heights)), bar_heights, yerr=bar_errors, capsize=5)
ax.set_xticks(range(len(avg_order)))
#ax.set_xticklabels(avg_order.month)
ax.set_xticklabels(["April", "May", "June", "July", "August", "September"])
plt.ylabel('price')
plt.title("Average amount spent on order for each month")
plt.show()
"""How much has each customer on FoodWheel spent over the past six months? What can this tell us about the average FoodWheel customer?"""

#Calculate the sum of price spent by each customer
customer_amount = orders.groupby('customer_id').price.sum().reset_index()
#print customer_amount.head()

#create a histogram of this data with the following attributes: range from 0 to 200, 40 bins, "Total spent" as x-axis, "Number of customers" as y-axis and an appropriate title
plt.hist(customer_amount.price.values, range=(0, 200), bins=40)
plt.xlabel('Total Spent')
plt.ylabel('Number of Customers')
plt.title('Customer purchases over the past six months')
plt.show()
                   nesterovs_momentum=True,
                   power_t=0.5,
                   random_state=None,
                   shuffle=False,
                   solver='lbfgs',
                   tol=0.0001,
                   validation_fraction=0.1,
                   verbose=False,
                   warm_start=False)
mlp.fit(x_train, y_train.ravel())

predict_test = mlp.predict(x_test)
predict_train = mlp.predict(x_train)

from sklearn.metrics import r2_score, mean_squared_log_error, mean_squared_error

score = r2_score(y_test, predict_test)
print("R2 Score Test=", score)
#print("MLG SCORE " , mean_squared_log_error(y_test, predict_test))
#print("Difference in value" , y_test - predict_test)
print("MSE Train", mean_squared_error(y_train, predict_train))
print("MSE Test", mean_squared_error(y_test, predict_test))

plt.scatter(x_train, y_train, color='red')
plt.plot(x_train, predict_train, color='blue')
plt.plot(x_test, predict_test, color='blue')
plt.plot(x_test, y_test, color='red')
plt.title('Model Fit')
plt.xlabel('Days')
plt.ylabel('confirmed Adjusted')
plt.show()
"""Module.2:GraphingUtility(ver:0.001a)
Nov 1, 2014 @ 6:25PM-date of modified version upon save
Reads a file containing stock data in the correct format (LIST FORMAT CONVENTION HERE) and writes the data to an---
array"""
#Parent-Project: SSGG
#Nov 1, 2014 @ 6:25PM-date started
##Dev Notes: -Notes on current development and planned features
#
##CODE (followed by two blank lines)


import matplotlib as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
Esempio n. 30
0
         linestyle=':',
         linewidth=4)

colors = cycle(['aqua', 'darkorange', 'cornflowerblue'])
for i, color in zip(range(2), colors):
    plt.plot(fpr[i],
             tpr[i],
             color=color,
             lw=2,
             label='ROC curve of class {0} (area = {1:0.2f})'
             ''.format(i, roc_auc[i]))

plt.plot([0, 1], [0, 1], 'k--', lw=2)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Some extension of Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
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))
Esempio n. 31
0
if __name__ == '__main__':
    ''' ==================== Part 1: Basic Function ==================== '''
    print(id_matrix())  # warmUpExercise
    ''' ==================== Part 2: Plotting ========================== '''
    # Read the data file
    data = np.loadtxt('ex1data1.txt', delimiter=',')

    # Store the strings as float arrays
    x = np.array(data[:, 0], ndmin=2).reshape(len(data), 1)
    y = np.array(data[:, 1], ndmin=2).reshape(len(data), 1)
    m = len(data)

    # Plot the data
    plt.plot(x, y, 'rx')
    plt.ylabel('Profit in $10,000\'s')
    plt.xlabel('Population of City in 10,000s')
    plt.axis([0, 25, -5, 25])
    plt.show()
    ''' ==================== Part 3: Cost and Gradient descent ========= '''
    # Add x-sub-0 (vector of 1's)
    X = np.concatenate((np.ones((m, 1)), x), axis=1)

    # Test thetas
    thetas = np.zeros((2, 1))
    cost = compute_cost(X, y, thetas)
    print('Cost using thetas(2, 1): ', cost)

    # Test different thetas
    thetas = np.array([[-1.0], [2.0]])
    cost = compute_cost(X, y, thetas)
Esempio n. 32
0
# In[30]:

n = 50

P = np.dot(Z, V[:, :n])
R = np.dot(P, V[:, :n].T)
RX = R + mu

# In[31]:

fig = plt.figure()
plt.plot(range(n), P.max(axis=0), label="max")
plt.plot(range(n), P.min(axis=0), label="min")
plt.plot(range(n), P.mean(axis=0), label="mean")
plt.plot(range(n), P.std(axis=0), label="std")
plt.ylabel("Value of Principle Component")
plt.xlabel("Index of Principle Component")
plt.legend()
fig.set_size_inches(8, 8)
fName = os.path.join(pDir, 'describe_PC_n{}_{}.png'.format(n, rev))
savefig(fName, bbox_inches='tight')
plt.show()

# In[32]:

print P.max()
print P.min()
print P.shape

# Display scatter plot of a list of letters.
print(TPRi)
print(cnf_matrixi)
print(k)
print(cnf_matrixi.iloc[0,0]/ (sum(cnf_matrixi.iloc[:,0])))

#%% Plot the accuracy metrics versus number of eigenvectors
import matplotlib.pyplot as plt


plt.plot(neigvec, TPRi, 'b-', label='TPR')
plt.plot(neigvec, TNRi, 'g-', label='TNR')
plt.plot(neigvec, PPVi, 'r-', label='PPV')
plt.plot(neigvec, NPVi, 'y-', label='NPV')
plt.plot(neigvec, Acci, 'k-', label='Accuracy')
plt.xlabel('Number of eigenvectors')
plt.ylabel('Metrics of Accuracy')
plt.legend()
loc= 'lower right'


#%% BIG GAP FOR Applying the classifier to the post treatment samples!
# Here we are doing it to just the 107 Aziz sample (one of the very late post treatment samples). 
# We will combine this with the other sample, and we should also do the 30 hour time point.
    
    
    
    
    
    
 #%% Project the post treatment cells into eigenspace
dfpost107=dfpost[dfpost['sample'].str.contains("Aziz")]
Esempio n. 34
0
                                          mode='min')
history = model.fit_generator(generator=tr_gen,
                              steps_per_epoch=count,
                              nb_epoch=5,
                              validation_data=tr_gen1,
                              validation_steps=count1,
                              max_queue_size=2,
                              callbacks=[m_check])

#model.fit(np.array(data_train),np.array(label_train), batch_size=batch_size, nb_epoch=nb_epoch, verbose=1, validation_data=(np.array(data_test),np.array(label_test)))
score = model.evaluate(np.array(data_test), np.array(label_test), verbose=0)
print('Score: ', score)
score = model.evaluate(np.array(data_holdout),
                       np.array(label_holdout),
                       verbose=0)
print('Score holdout: ', score)

plt.plot(history.history[fmeasure])
plt.title('Model Fmeasure-score')
plt.ylabel('Fmeasure')
plt.xlabel('Epoch')
plt.legend(['train', 'validation'], loc='upper-left')
plt.show()

plt.plot(history.history['loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['train', 'validation'], loc='upper-left')
plt.show()
Esempio n. 35
0
lable_name = "Seodaemun"
Y_train = train[lable_name]
Y_test = test[lable_name]
print(X_train.shape)
print(X_test.shape)

lm = LinearRegression(fit_intercept=True, normalize=True, n_jobs=4)
lm.fit(X_train, Y_train)
accuracy = lm.score(X_test, Y_test)
coefs = pd.DataFrame(zip(X_train.columns,lm.coef_), columns = ['features', 'coefficients'])
coefs.reindex(coefs.coefficients.abs().sort_values(ascending=False).index)
print ("Linear Regression test file accuracy:"+str(accuracy))

print(coefs)
Y_pred = lm.predict(X_test)

plt.scatter(Y_test,Y_pred)
plt.xlabel('Dust Index: $Y_i$')
plt.ylabel("Predicted dust Index: $\hat{Y}_i$")
plt.title("Dust vs Predicted dust Index: $Y_i$ vs $\hat{Y}_i$")

# print error
print(Y_test - Y_pred)
mse = sklearn.metrics.mean_squared_error(Y_test, Y_pred)
print(mse)

plt.show()


Esempio n. 36
0
    # TODO
    """
    TASK 2: Plot metrics against frame number
    """
    # TODO: Define frame_number!
    # TODO: We can vary noise parameter and plot several noisy results in
    #   same graph, labeling them by noise param.

    frame_number = 1

    # Plot F-score
    plt.plot(fscore_original, '-o', frame_number, label='Original')
    plt.plot(fscore_noisy, '-o', frame_number, label='Noisy')
    plt.legend()
    plt.xlabel('Frame Number')
    plt.ylabel('F-score')
    plt.title('F-score in time')
    plt.savefig(os.path.join(FIGURES_DIR, 'fscore.png'))

    # Plot IoU
    plt.plot(iou_original, '-o', frame_number, label='Original')
    plt.plot(iou_noisy, '-o', frame_number, label='Noisy')
    plt.legend()
    plt.xlabel('Frame Number')
    plt.ylabel('IoU')
    plt.title('IoU in time')
    plt.savefig(os.path.join(FIGURES_DIR, 'iou.png'))

    # Plot Mapk :
    plt.plot(map_original, '-o', frame_number, label='Original')
    plt.plot(map_noisy, '-o', frame_number, label='Noisy')
Esempio n. 37
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()
Esempio n. 38
0
plt.contourf(X1,
             X2,
             svmclassifier.predict(np.array([X1.ravel(),
                                             X2.ravel()]).T).reshape(X1.shape),
             alpha=0.75,
             cmap=ListedColormap(('red', 'blue')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(Y_set)):
    plt.scatter(X_set[Y_set == j, 0],
                X_set[Y_set == j, 1],
                c=ListedColormap(('red', 'blue'))(i),
                label=j)
plt.title('SVM (Training set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salaries')
plt.legend()
plt.show()

# Visualising the Test set results
from matplotlib.colors import ListedColormap
X_set, Y_set = X_test, Y_test
X1, X2 = np.meshgrid(
    np.arange(start=X_set[:, 0].min() - 1,
              stop=X_set[:, 0].max() + 1,
              step=0.01),
    np.arange(start=X_set[:, 1].min() - 1,
              stop=X_set[:, 1].max() + 1,
              step=0.01))
plt.contourf(X1,
             X2,
Esempio n. 39
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.'
Esempio n. 40
0
def final_decision_plot(df,
                        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)

    plt.show(block=block)

    return fig, axes
Esempio n. 41
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()
def process(path):
    msev = []
    maev = []
    rsqv = []
    rmsev = []
    acyv = []

    df = pd.read_csv(path, encoding="latin-1")

    x1 = np.array(df['Lyrics'].values.astype('U'))
    y1 = np.array(df['MoodValue'])
    print(x1)
    print(y1)

    print(x1)
    print(y1)
    X_train, X_test, y_train, y_test = train_test_split(x1, y1, test_size=0.20)

    count_vectorizer = CountVectorizer(stop_words='english')
    count_train = count_vectorizer.fit_transform(
        X_train
    )  # Learn the vocabulary dictionary and return term-document matrix.
    count_test = count_vectorizer.transform(X_test)

    tfidf_vectorizer = TfidfVectorizer(
        stop_words='english', max_df=0.7
    )  # This removes words which appear in more than 70% of the articles
    tfidf_train = tfidf_vectorizer.fit_transform(X_train)
    tfidf_test = tfidf_vectorizer.transform(X_test)

    model2 = DecisionTreeClassifier()
    model2.fit(count_train, y_train)
    y_pred = model2.predict(count_test)
    print("predicted")
    print(y_pred)
    print("test")
    print(y_test)

    result2 = open("results/resultCOUNTDT.csv", "w")
    result2.write("ID,Predicted Value" + "\n")
    for j in range(len(y_pred)):
        result2.write(str(j + 1) + "," + str(y_pred[j]) + "\n")
    result2.close()

    mse = mean_squared_error(y_test, y_pred)
    mae = mean_absolute_error(y_test, y_pred)
    r2 = abs(r2_score(y_test, y_pred))

    print("---------------------------------------------------------")
    print("MSE VALUE FOR DecisionTree COUNT IS %f " % mse)
    print("MAE VALUE FOR DecisionTree COUNT IS %f " % mae)
    print("R-SQUARED VALUE FOR DecisionTree COUNT IS %f " % r2)
    rms = np.sqrt(mean_squared_error(y_test, y_pred))
    print("RMSE VALUE FOR DecisionTree COUNT IS %f " % rms)
    ac = accuracy_score(y_test, y_pred)
    print("ACCURACY VALUE DecisionTree COUNT IS %f" % ac)
    print("---------------------------------------------------------")

    msev.append(mse)
    maev.append(mae)
    rsqv.append(r2)
    rmsev.append(rms)
    acyv.append(ac * 100)

    result2 = open('results/COUNTDTMetrics.csv', 'w')
    result2.write("Parameter,Value" + "\n")
    result2.write("MSE" + "," + str(mse) + "\n")
    result2.write("MAE" + "," + str(mae) + "\n")
    result2.write("R-SQUARED" + "," + str(r2) + "\n")
    result2.write("RMSE" + "," + str(rms) + "\n")
    result2.write("ACCURACY" + "," + str(ac) + "\n")
    result2.close()

    df = pd.read_csv('results/COUNTDTMetrics.csv')
    acc = df["Value"]
    alc = df["Parameter"]
    colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"]
    explode = (0.1, 0, 0, 0, 0)

    fig = plt.figure()
    plt.bar(alc, acc, color=colors)
    plt.xlabel('Parameter')
    plt.ylabel('Value')
    plt.title(' COUNT DecisionTree Metrics Value')
    fig.savefig('results/COUNTDTMetricsValue.png')
    plt.pause(5)
    plt.show(block=False)
    plt.close()

    model2 = DecisionTreeClassifier()
    model2.fit(tfidf_train, y_train)
    y_pred = model2.predict(tfidf_test)
    print("predicted")
    print(y_pred)
    print("test")
    print(y_test)

    result2 = open("results/resultTFIDFDT.csv", "w")
    result2.write("ID,Predicted Value" + "\n")
    for j in range(len(y_pred)):
        result2.write(str(j + 1) + "," + str(y_pred[j]) + "\n")
    result2.close()

    mse = mean_squared_error(y_test, y_pred)
    mae = mean_absolute_error(y_test, y_pred)
    r2 = abs(r2_score(y_test, y_pred))

    print("---------------------------------------------------------")
    print("MSE VALUE FOR DecisionTree TFIDF IS %f " % mse)
    print("MAE VALUE FOR DecisionTree TFIDF IS %f " % mae)
    print("R-SQUARED VALUE FOR DecisionTree TFIDF IS %f " % r2)
    rms = np.sqrt(mean_squared_error(y_test, y_pred))
    print("RMSE VALUE FOR DecisionTree TFIDF IS %f " % rms)
    ac = accuracy_score(y_test, y_pred)
    print("ACCURACY VALUE DecisionTree TFIDF IS %f" % ac)
    print("---------------------------------------------------------")

    msev.append(mse)
    maev.append(mae)
    rsqv.append(r2)
    rmsev.append(rms)
    acyv.append(ac * 100)

    result2 = open('results/TFIDFDTMetrics.csv', 'w')
    result2.write("Parameter,Value" + "\n")
    result2.write("MSE" + "," + str(mse) + "\n")
    result2.write("MAE" + "," + str(mae) + "\n")
    result2.write("R-SQUARED" + "," + str(r2) + "\n")
    result2.write("RMSE" + "," + str(rms) + "\n")
    result2.write("ACCURACY" + "," + str(ac) + "\n")
    result2.close()

    df = pd.read_csv('results/TFIDFDTMetrics.csv')
    acc = df["Value"]
    alc = df["Parameter"]
    colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#8c564b"]
    explode = (0.1, 0, 0, 0, 0)

    fig = plt.figure()
    plt.bar(alc, acc, color=colors)
    plt.xlabel('Parameter')
    plt.ylabel('Value')
    plt.title(' TFIDF DecisionTree Metrics Value')
    fig.savefig('results/TFIDFCOUNTDTMetricsValue.png')
    plt.pause(5)
    plt.show(block=False)
    plt.close()

    al = ['COUNT', 'TFIDF']

    result2 = open('results/DTMSE.csv', 'w')
    result2.write("Vectorization,MSE" + "\n")
    for i in range(0, len(msev)):
        result2.write(al[i] + "," + str(msev[i]) + "\n")
    result2.close()

    colors = ["#1f77b4", "#ff7f0e", "#2ca02c"]
    explode = (0.1, 0, 0, 0, 0)

    #Barplot for the dependent variable
    fig = plt.figure(0)
    df = pd.read_csv('results/DTMSE.csv')
    acc = df["MSE"]
    alc = df["Vectorization"]
    plt.bar(alc, acc, color=colors)
    plt.xlabel('Vectorization')
    plt.ylabel('MSE')
    plt.title("DecisionTree MSE Value")
    fig.savefig('results/DTMSE.png')
    plt.pause(5)
    plt.show(block=False)
    plt.close()

    result2 = open('results/DTMAE.csv', 'w')
    result2.write("Vectorization,MAE" + "\n")
    for i in range(0, len(maev)):
        result2.write(al[i] + "," + str(maev[i]) + "\n")
    result2.close()

    fig = plt.figure(0)
    df = pd.read_csv('results/DTMAE.csv')
    acc = df["MAE"]
    alc = df["Vectorization"]
    plt.bar(alc, acc, color=colors)
    plt.xlabel('Vectorization')
    plt.ylabel('MAE')
    plt.title('DecisionTree MAE Value')
    fig.savefig('results/DTMAE.png')
    plt.pause(5)
    plt.show(block=False)
    plt.close()

    result2 = open('results/DTR-SQUARED.csv', 'w')
    result2.write("Vectorization,R-SQUARED" + "\n")
    for i in range(0, len(rsqv)):
        result2.write(al[i] + "," + str(rsqv[i]) + "\n")
    result2.close()

    fig = plt.figure(0)
    df = pd.read_csv('results/DTR-SQUARED.csv')
    acc = df["R-SQUARED"]
    alc = df["Vectorization"]

    plt.bar(alc, acc, color=colors)
    plt.xlabel('Vectorization')
    plt.ylabel('R-SQUARED')
    plt.title('DecisionTree R-SQUARED Value')
    fig.savefig('results/DTR-SQUARED.png')
    plt.pause(5)
    plt.show(block=False)
    plt.close()

    result2 = open('results/DTRMSE.csv', 'w')
    result2.write("Vectorization,RMSE" + "\n")
    for i in range(0, len(rmsev)):
        result2.write(al[i] + "," + str(rmsev[i]) + "\n")
    result2.close()

    fig = plt.figure(0)
    df = pd.read_csv('results/DTRMSE.csv')
    acc = df["RMSE"]
    alc = df["Vectorization"]
    plt.bar(alc, acc, color=colors)
    plt.xlabel('Vectorization')
    plt.ylabel('RMSE')
    plt.title('DecisionTree RMSE Value')
    fig.savefig('results/DTRMSE.png')
    plt.pause(5)
    plt.show(block=False)
    plt.close()

    result2 = open('results/DTAccuracy.csv', 'w')
    result2.write("Vectorization,Accuracy" + "\n")
    for i in range(0, len(acyv)):
        result2.write(al[i] + "," + str(acyv[i]) + "\n")
    result2.close()

    fig = plt.figure(0)
    df = pd.read_csv('results/DTAccuracy.csv')
    acc = df["Accuracy"]
    alc = df["Vectorization"]
    plt.bar(alc, acc, color=colors)
    plt.xlabel('Vectorization')
    plt.ylabel('Accuracy')
    plt.title('DecisionTree Accuracy Value')
    fig.savefig('results/DTAccuracy.png')
    plt.pause(5)
    plt.show(block=False)
    plt.close()
Esempio n. 43
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()
Esempio n. 44
0
def model(X_train,
          Y_train,
          X_test,
          Y_test,
          learning_rate=0.009,
          num_epochs=500,
          minibatch_size=64,
          print_cost=True):
    """
  Implements a three-layer ConvNet in Tensorflow:
  CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED

  Arguments:
  X_train -- training set, of shape (None, 64, 64, 3)
  Y_train -- test set, of shape (None, n_y = 6)
  X_test -- training set, of shape (None, 64, 64, 3)
  Y_test -- test set, of shape (None, n_y = 6)
  learning_rate -- learning rate of the optimization
  num_epochs -- number of epochs of the optimization loop
  minibatch_size -- size of a minibatch
  print_cost -- True to print the cost every 100 epochs

  Returns:
  train_accuracy -- real number, accuracy on the train set (X_train)
  test_accuracy -- real number, testing accuracy on the test set (X_test)
  parameters -- parameters learnt by the model. They can then be used to predict.
  """

    ops.reset_default_graph(
    )  # to be able to rerun the model without overwriting tf variables
    tf.set_random_seed(1)  # to keep results consistent (tensorflow seed)
    seed = 3  # to keep results consistent (numpy seed)
    (m, n_H0, n_W0, n_C0) = X_train.shape
    n_y = Y_train.shape[1]
    costs = []  # To keep track of the cost

    # Create Placeholders of the correct shape
    # START CODE HERE ### (1 line)
    X, Y = create_placeholders(n_H0, n_W0, n_C0, n_y)
    ### END CODE HERE ###

    # Initialize parameters
    # START CODE HERE ### (1 line)
    parameters = initialize_parameters()
    ### END CODE HERE ###

    # Forward propagation: Build the forward propagation in the tensorflow graph
    # START CODE HERE ### (1 line)
    Z3 = forward_propagation(X, parameters)
    ### END CODE HERE ###

    # Cost function: Add cost function to tensorflow graph
    # START CODE HERE ### (1 line)
    cost = compute_cost(Z3, Y)
    ### END CODE HERE ###

    # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer that minimizes the cost.
    # START CODE HERE ### (1 line)
    optimizer = tf.train.AdamOptimizer(
        learning_rate=learning_rate).minimize(cost)
    ### END CODE HERE ###

    # Initialize all the variables globally
    init = tf.global_variables_initializer()

    # Start the session to compute the tensorflow graph
    with tf.Session() as sess:

        # Run the initialization
        sess.run(init)

        # Do the training loop
        for epoch in range(num_epochs):

            minibatch_cost = 0.
            num_minibatches = int(
                m / minibatch_size
            )  # number of minibatches of size minibatch_size in the train set
            seed = seed + 1
            minibatches = random_mini_batches(X_train, Y_train, minibatch_size,
                                              seed)

            for minibatch in minibatches:

                # Select a minibatch
                (minibatch_X, minibatch_Y) = minibatch
                # IMPORTANT: The line that runs the graph on a minibatch.
                # Run the session to execute the optimizer and the cost, the feedict should contain a minibatch for (X,Y).
                # START CODE HERE ### (1 line)
                _, cost_ = sess.run([optimizer, cost],
                                    feed_dict={
                                        X: minibatch_X,
                                        Y: minibatch_Y
                                    })
                ### END CODE HERE ###

                minibatch_cost += cost_ / num_minibatches

            # Print the cost every epoch
            if print_cost == True and epoch % 5 == 0:
                print("Cost after epoch %i: %f" % (epoch, minibatch_cost))
            if print_cost == True and epoch % 1 == 0:
                costs.append(minibatch_cost)

        # plot the cost
        plt.plot(np.squeeze(costs))
        plt.ylabel('cost')
        plt.xlabel('iterations (per tens)')
        plt.title("Learning rate =" + str(learning_rate))
        plt.show()

        # Calculate the correct predictions
        predict_op = tf.argmax(Z3, 1)
        true_op = tf.argmax(Y, 1)
        correct_prediction = tf.equal(predict_op, true_op)

        # Calculate accuracy on the test set
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        print(accuracy)
        train_accuracy = accuracy.eval({X: X_train, Y: Y_train})
        test_accuracy = accuracy.eval({X: X_test, Y: Y_test})
        print("Train Accuracy:", train_accuracy)
        print("Test Accuracy:", test_accuracy)

        return train_accuracy, test_accuracy, parameters
        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()
Esempio n. 46
0
linabundpost.to_csv("linabundpost.csv")
filename = "linabundpost.csv"
#%%Map the lineages in dchange to the cells in the anndata object
# each cell now is labeled by the amount its lineage changes post treatment
adata.obs['linabundchange']= adata.obs['lineage'].map(dchange)
# Set the threshold for calling cells sensitive or resistant, pulling from only
# the lineages with greatest increase (R) or decrease (S) in abundance 
S_threshold = 0.05
R_threshold = 0
# Plot the change in lineage abundance distribution                                       
plt.figure()
plt.hist(adata.obs.loc[adata.obs.timepoint == 't=0 wks','linabundchange'], bins = 100)
plt.plot([R_threshold,R_threshold], [0,900], c='r', alpha = 1)
plt.plot([-S_threshold, -S_threshold],[0,900], c='g', alpha = 1)
plt.xlabel(' Change in lineage abundance')
plt.ylabel('Number of cells in the lineage')
plt.title('Distribution of lineage abundance change')
#plt.legend(loc=1, prop={'size': 15})     
plt.ylim(0, 900)
plt.xlim(-0.5, 0.5)
plt.grid(b=None)
                                    

#%% Make the sensitive and resistant labels within your anndata object

classLabel = np.array(['res', 'sens', 'unknown', 'res_est', 'sens_est'])
adata.obs.loc[adata.obs.timepoint=='t=0 wks','classLabel'] = 'unknown'
adata.obs.loc[adata.obs.timepoint=='t=7 wks','classLabel'] = 'unknown'
adata.obs.loc[adata.obs.timepoint=='t=10 wks','classLabel'] = 'unknown'
adata.obs.loc[(adata.obs.linabundchange>R_threshold)&(adata.obs.timepoint=='t=0 wks'), 'classLabel'] = 'res'
adata.obs.loc[(adata.obs.linabundchange<-S_threshold)&(adata.obs.timepoint=='t=0 wks'), 'classLabel'] = 'sens'
Esempio n. 47
0
import numpy as np
import matplotlib as plt
import sys

path = sys.argv
a = np.loadtxt('result/')

plt.plot()
plt.xlabel('长度')
plt.ylabel('Translation Error')
plt.show()
Esempio n. 48
0
                           kind='linear')
            window_size, poly_order = 101, 2
            yy_sg = savgol_filter(itp(xx), window_size, poly_order)
            plt.plot(wavelengths[1:-1],
                     100.0 * (QE[index_chip, 1:-1, this_area_y, this_area_x]),
                     'o--',
                     color=colors[color_tmp],
                     label='Chip: ' + str(sensor_name_list[index_chip]) +
                     ', X: ' + str(frame_x_divisions[this_area_x]) + ', Y: ' +
                     str(frame_y_divisions[this_area_y]))
            color_tmp = color_tmp + 1
            plt.plot(xx,
                     100.0 * yy_sg,
                     'k',
                     label='Fit for chip: ' +
                     str(sensor_name_list[index_chip]) + ', X: ' +
                     str(frame_x_divisions[this_area_x]) + ', Y: ' +
                     str(frame_y_divisions[this_area_y]))
lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.xlabel('Wavelength [nm] ')
plt.ylabel('Quantum Efficiency [%] ')
plt.savefig(figure_dir + "qe_wl.pdf",
            format='pdf',
            bbox_extra_artists=(lgd, ),
            bbox_inches='tight')
plt.savefig(figure_dir + "qe_wl.png",
            format='png',
            bbox_extra_artists=(lgd, ),
            bbox_inches='tight',
            dpi=1000)
Esempio n. 49
0
def pos_plot(iPartType, box_size):
    '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')
    f = h5py.File(base + '/snapshot_555.0.hdf5')

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

    ##############################################   PLOT   ########################################################

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

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

    plt.scatter(xyz[:, 0], xyz[:, 1], marker='.')

    plt.axis('equal')
    plt.xlim(box_size)
    plt.ylim(box_size)
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('xy')
    #ax.text(-lim+1, lim+1, str(time)+'Gyr', fontsize=15)

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

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

    plt.scatter(xyz[:, 0], xyz[:, 2], marker='.')

    plt.axis('equal')
    plt.xlim(box_size)
    plt.ylim(box_size)
    plt.xlabel('x')
    plt.ylabel('z')
    plt.title('xz')
    #ax.text(-lim+1, lim+1, str(time)+'Gyr', fontsize=15)

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

    plt.scatter(xyz[:, 1], xyz[:, 2], marker='.')

    plt.axis('equal')
    plt.xlim(box_size)
    plt.ylim(box_size)
    plt.xlabel('y')
    plt.ylabel('z')
    plt.title('yz')
    #ax.text(-lim+1, lim+1, str(time)+'Gyr', fontsize=15)

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

    save_fig_file = '/data8/data/mercadf1/scratch/Plots/' + tag_type + '_pos_run_ELVIS_RomeoJuliet_snap_555_0.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)
axes_label_font_size = 12
linewidth = 2
contour_font = 10

fig = plt.figure(figsize=(6, 5))
ax_ray = fig.add_subplot(111)
dist = [20, 60, 85, 95]
dist = [8, 27, 48, 67, 89, 99]
dist = [10, 25, 30, 35, 40]  # 15,20,25,30,35,40,45,50,55,60]
for i in range(len(dist)):
    s = str(str(dist[i]) + "_SURF96.inp")
    data = np.loadtxt(s, usecols=(5, 6, 7))
    ax_ray.plot(data[:, 0], data[:, 1], label=str(dist[i] * 10) + "km")
ax_ray.grid(True, linestyle='--', color='black', linewidth=0.15)

plt.xticks(fontsize=axes_label_font_size)
plt.yticks(fontsize=axes_label_font_size)
plt.xlabel('Time period ($s$)',
           fontsize=axes_label_font_size,
           fontweight='bold')
plt.ylabel('Phase velocity ($km/s$)',
           fontsize=axes_label_font_size,
           fontweight='bold')
plt.legend(fancybox=True,
           shadow=True,
           framealpha=0.5,
           loc='lower right',
           fontsize=legend_font_size)
plt.savefig('Rayleigh_Phase_dispersion.jpg', dpi=400)
plt.show()