def plotPoints3D(self): colors = cm.rainbow(np.linspace(0, 1, self.cl_cnt)) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') for c in range(self.cl_cnt): centroid = self.centroids[c] x = [] y = [] z = [] cluster_size = len(self.cl_points[c]) #for each point assorted to the cluster for p in range(cluster_size): index = self.cl_points[c][p] #index of the point point = self.points[index] #print point x.append(point[0]) y.append(point[1]) z.append(point[2]) clrs = cm.rainbow(np.linspace(0, 1, self.cl_cnt)) #colors ax.scatter(x, y, z, color=clrs[c], marker='o') ax.scatter(centroid[0], centroid[1], centroid[2], color=clrs[c], marker='*', s = 50) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show()
def LogScaleASP(LRange, Data, unlogged = False, scale = False, tau = 0, D = 0, tauEst = 0, DEst = 0): """ Plots log-binned avalanche size probabilities (if logbin == True, also plots unprocessed data) """ plt.figure() for d in range(len(Data)): colors = cm.rainbow(np.linspace(0, 1, len(Data))) if unlogged: plt.scatter(np.arange(0,len(Data[d][0])),Data[d][0],s = 0.5, label = 'Raw Data') for i in range(len(Data[d][1])): #plt.plot(Data[d][1][i][0],Data[d][1][i][1],color = colors[d],label = 'L = ' + str(LRange[d])) plt.plot(Data[d][1][i][0],Data[d][1][i][1],color = colors[d],label = 'Log-Binned Data') #plt.plot(np.arange(0,len(Data[d][1][i])),Data[d][1][i],color = colors[d]) if tauEst != 0: #print 't' plt.plot([1,10**10],[1,(10**10)**(-tauEst)],linestyle = 'dashed',label = r'$P_N(s;L)\propto s^{-\tau_s}$') plt.xscale('log') plt.yscale('log') #ax = plt.axes() #ax.grid(True) plt.xlim((10**0,10**(round(math.log(len(Data[-1][0]),10)) + 1))) plt.ylim((10**(round(math.log(Data[-1][1][0][1][-1],10)) - 1), 10**0)) plt.xlabel(r'$s$', fontsize = 20) plt.ylabel(r'$\tilde{P}_N(s;L)$', fontsize = 20) plt.title(r'$N = 10^9$',fontsize = 25) plt.legend(prop = {'size':25}) #fig.savefig('Pvs.eps', format = 'eps', dpi = 1000) logp = [i[1][0] for i in Data] pscaled = [[p*math.pow(s,tau) for (p,s) in zip(logp[i][1],logp[i][0])] for i in range(len(logp))] sscaled = [[s/LRange[i]**D for s in logp[i][0]] for i in range(len(logp))] if scale: plt.figure() plt.xscale('log') plt.yscale('log') ax = plt.axes() ax.grid(True) #plt.xlim((10**0,10**(round(math.log(len(Data[-1][0]),10)) + 1))) #plt.ylim((10**(-2), 10**0)) plt.ylabel(r'$s^{\tau_s}P(s;L)$',fontsize = 20) plt.xlabel(r'$s$', fontsize = 20) for d in range(len(Data)): colors = cm.rainbow(np.linspace(0, 1, len(Data))) plt.plot(logp[d][0],pscaled[d],color = colors[d],label = 'L =' + str(LRange[d])) plt.legend(prop = {'size':15},loc =3) plt.figure() ax = plt.axes() ax.grid(True) plt.xscale('log') plt.yscale('log') #plt.xlim((10**(-6),10**2)) #plt.ylim((10**(-2), 10**0)) plt.ylabel(r'$s^{\tau_s}P(s;L)$',fontsize = 20) plt.xlabel(r'$s/L^D$', fontsize = 20) for d in range(len(Data)): colors = cm.rainbow(np.linspace(0, 1, len(Data))) plt.plot(sscaled[d][1:],pscaled[d][1:],color = colors[d], label = 'L =' + str(LRange[d])) plt.legend(prop = {'size':15},loc = 3)
def plot_epam_proton_lcurves(t1, t2): #convert time to datetime format dt1 = datetime.datetime.strptime(t1, "%d-%b-%Y %H:%M") dt2 = datetime.datetime.strptime(t2, "%d-%b-%Y %H:%M") #set up figure nl = 3 xc = 255/nl f, ax = plt.subplots(figsize=(8,4)) #ACE EPAM protons epam_dates0, epam_lcurve = parse_epam_proton_range(t1, t2) mxepam = np.amax(epam_lcurve) l1 = ax.plot(epam_dates0, epam_lcurve[:,0], c = cm.rainbow(1 * xc + 1) ,label='EPAM 0.540 - 0.765 MeV') l2 = ax.plot(epam_dates0, epam_lcurve[:,1], c = cm.rainbow(3 * xc + 1) ,label='EPAM 0.765 - 1.22 MeV') l3 = ax.plot(epam_dates0, epam_lcurve[:,2], c = cm.rainbow(5 * xc + 1) ,label='EPAM 1.22 - 4.94 MeV') #format of tick labels hrsFmt = mdates.DateFormatter('%d') ax.xaxis.set_major_formatter(hrsFmt) ax.set_xlabel("Start Time "+t1+" (UTC)") ax.set_xlim([dt1, dt2]) ymax = np.max([mxepam, mxepam]) ax.set_ylim(top = ymax) ax.set_ylim(bottom = 1.e-4) #auto orientate the labels so they don't overlap #f.autofmt_xdate() #set yaxis log ax.set_yscale('log') #Axes labels ax.set_title("ACE EPAM Protons") ax.set_ylabel('H Intensity $\mathrm{(cm^{2}\,sr\,s\,MeV)^{-1}}$') #legend fontP = FontProperties() fontP.set_size('x-small') leg = ax.legend(loc='best', prop = fontP, fancybox=True ) leg.get_frame().set_alpha(0.5) #month year = dt1.year month = dt1.month plt.show() return None
def draw_ranges_for_parameters(data, title='', save_path='./pictures/'): parameters = data.columns.values.tolist() # remove flight name parameter for idx, parameter in enumerate(parameters): if parameter == 'flight_name': del parameters[idx] flight_names = np.unique(data['flight_name']) print len(flight_names) for parameter in parameters: plt.figure() axis = plt.gca() # ax.set_xticks(numpy.arange(0,1,0.1)) axis.set_yticks(flight_names) axis.tick_params(labelright=True) axis.set_ylim([94., 130.]) plt.grid() plt.title(title) plt.xlabel(parameter) plt.ylabel('flight name') colors = iter(cm.rainbow(np.linspace(0, 1,len(flight_names)))) for flight in flight_names: temp = data[data.flight_name == flight][parameter] plt.plot([np.min(temp), np.max(temp)], [flight, flight], c=next(colors), linewidth=2.0) plt.savefig(save_path+title+'_'+parameter+'.jpg') plt.close()
def showBoundaries(ax, parser, dim, text): ax.set_aspect('equal') if dim==3: # calculate how many boundary terms we have # determines how many shades of colors we need and how the legend is drawn bounds = set([b[0] for b in parser.cells['quad'].values()]) bounds_min = min(bounds) bounds_max = max(bounds) bounds_range = bounds_max-bounds_min # I riffle shuffle the colors to try to make sure that boundary terms # near each other will get contrasting colors colors = riffleShuffle(rainbow(np.linspace(0,1,bounds_range+1))) for i,e in parser.cells['quad'].iteritems(): verts = e[1:] material = e[0] coords = [parser.nodes[verts[n]] for n in range (4)] tri = Poly3DCollection([coords]) tri.set_color(colors[material-bounds_min]) tri.set_edgecolor('k') ax.add_collection(tri) # create the legend manually, by createing and filling in the rectangles # and then appending the material number guide = [plt.Rectangle((0, 0), 1, 1, fc=color) for color in colors] plt.legend(guide, bounds, loc='best') return fig
def plot_3d(dataset): """TODO: Docstring for plot_3d. :returns: TODO """ from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') iso = Isomap(n_components=3) projected = iso.fit_transform(dataset.data.toarray()) print 'projected: sample: %s, feature: %s'\ % (projected.shape[0], projected.shape[1]) all_scatter = [] colors = cm.rainbow(np.linspace(0, 1, len(dataset.target_names)), alpha=0.5) for i in range(len(dataset.target_names)): points = projected[dataset.target==i,:] cur = ax.scatter(points[:,0], points[:,1], points[:,2], color=colors[i], edgecolor='k', lw=0.1, vmin=0, vmax=len(dataset.target_names)) all_scatter.append(cur) ax.legend(all_scatter, dataset.target_names, loc='lower left', scatterpoints=1) plt.savefig('isomap3d', dpi=500) plt.show() return True
def compare_subcarrier_location(alpha, M, K, overlap, oversampling_factor): import matplotlib.pyplot as plt import matplotlib.cm as cm goofy_ordering = False taps = gfdm_filter_taps('rrc', alpha, M, K, oversampling_factor) A0 = gfdm_modulation_matrix(taps, M, K, oversampling_factor, group_by_subcarrier=goofy_ordering) n = np.arange(M * K * oversampling_factor, dtype=np.complex) colors = iter(cm.rainbow(np.linspace(0, 1, K))) for k in range(K): color = next(colors) f = np.exp(1j * 2 * np.pi * (float(k) / (K * oversampling_factor)) * n) F = abs(np.fft.fft(f)) fm = np.argmax(F) / M plt.plot(F, '-.', label=k, color=color) data = get_zero_f_data(k, K, M) x0 = gfdm_gr_modulator(data, 'rrc', alpha, M, K, overlap, compat_mode=goofy_ordering) * (2. / K) f0 = 1. * np.argmax(abs(np.fft.fft(x0))) / M plt.plot(abs(np.fft.fft(x0)), label='FFT' + str(k), color=color) xA = A0.dot(get_data_matrix(data, K, group_by_subcarrier=goofy_ordering).flatten()) * (1. / K) fA = np.argmax(abs(np.fft.fft(xA))) / M plt.plot(abs(np.fft.fft(xA)), '-', label='matrix' + str(k), color=color) print fm, fA, f0 plt.legend() plt.show()
def scatter(file_name, modes, operations): log_file = LogFile(file_name) series = {} for mode in modes: for operation in operations: label = mode + '_' + operation data = log_file.readLogAsDict(mode, operation) series[label] = data import matplotlib.pyplot as plt import matplotlib.cm as cm import numpy as np fig = plt.figure() ax1 = fig.add_subplot(111) num = len(modes) + len(operations) + 1 colours = cm.rainbow(np.linspace(0, 1, num)) c = 0 # these are for setting up colours for label, data in series.iteritems(): xs = list(data.iterkeys()) ys = list(data.itervalues()) ax1.scatter(xs, ys, label=label, color=colours[c]) c += 1 legend = ax1.legend(loc='upper left', shadow=True) plt.show()
def sendTSNE(self, people): d = self.getData() if d is None: return else: (X, y) = d X_pca = PCA(n_components=50).fit_transform(X, X) tsne = TSNE(n_components=2, init='random', random_state=0) X_r = tsne.fit_transform(X_pca) yVals = list(np.unique(y)) colors = cm.rainbow(np.linspace(0, 1, len(yVals))) # print(yVals) plt.figure() for c, i in zip(colors, yVals): name = "Unknown" if i == -1 else people[i] plt.scatter(X_r[y == i, 0], X_r[y == i, 1], c=c, label=name) plt.legend() imgdata = StringIO.StringIO() plt.savefig(imgdata, format='png') imgdata.seek(0) content = 'data:image/png;base64,' + \ urllib.quote(base64.b64encode(imgdata.buf)) msg = { "type": "TSNE_DATA", "content": content } self.sendMessage(json.dumps(msg))
def kmeans_showprogress(self, centroids, iter, plt_progress=False): ''' Compute K-Means algorithm and plot progress. Arguments: centroids (m x n float matrix): Centroids values. iter (int): Max number of iterations. plt_progress (Boolean): 'True' if show progress on the graph. Returns: ci (m x n float matrix): Centroid values. cc (int vector): The indices of the closest centroids. ''' K = centroids.shape[0] self._colors = cm.rainbow(np.linspace(0, 1, K)) cc = None for i in range(iter): # For each example in X, assign it to the # closest centroid. cc = self.closest_centroid(centroids); if plt_progress: self._plot_progress_kmeans(centroids, cc, K, i) centroids = self.centroids_means(cc, K) return centroids, cc.astype(int)
def plot_xyt_rotate(s): xyts = np.fromfile(pdir + 'lcr/rotate.dat') xs = xyts[1::7][::s] ys = xyts[2::7][::s] ts = xyts[6::7][::s] ds = [1-((x/350)**2 + (y/350)**2)**0.5 for x,y in zip(xs,ys)] cs = cm.rainbow(ds) fig = plt.figure() ax = fig.gca(projection='3d') ax.scatter(xs, ys, ts, color=cs, cmap=cm.hsv) ax.set_xlim3d(0, 300) ax.set_ylim3d(0, 300) ax.set_zlim3d(0, 16000) ax.set_xlabel('area length (m)') ax.set_ylabel('area width (m)') ax.set_zlabel('time (min)') ax.set_xticks([0,100,200,300]) ax.set_yticks([0,100,200,300]) ax.set_zticks([0,4000,8000,12000,16000]) ax.ticklabel_format(axis='z', style='sci', scilimits=(-2,2)) ax.view_init(elev=13., azim=-70) ax.grid(True) plt.show()
def generate_rt_curves(dataTrials, simulTrials, pdfPages, valueDiffRange=np.arange(-3,4,1)): """ Plots the response times for data and simulations. Args: dataTrials: a list of DDMTrial objects corresponding to the experimental data. simulTrials: a list of DDMTrial objects corresponding to the simulations. pdfPages: matplotlib.backends.backend_pdf.PdfPages object. valueDiffRange: a numpy array corresponding to the value differences to be used in the x axis of the choice plot; should be sorted in ascending order. """ numValueDiffs = valueDiffRange.size RTsPerValueDiff = dict() for valueDiff in valueDiffRange: RTsPerValueDiff[valueDiff] = list() for trial in dataTrials: valueDiff = trial.valueLeft - trial.valueRight RTsPerValueDiff[valueDiff].append(trial.RT) meanRTs = np.zeros(numValueDiffs) stdRTs = np.zeros(numValueDiffs) for valueDiff in valueDiffRange: idx = (np.abs(valueDiffRange - valueDiff)).argmin() meanRTs[idx] = np.mean(RTsPerValueDiff[valueDiff]) stdRTs[idx] = (np.std(RTsPerValueDiff[valueDiff]) / np.sqrt(len(RTsPerValueDiff[valueDiff]))) colors = cm.rainbow(np.linspace(0, 1, 9)) fig = plt.figure() plt.errorbar(valueDiffRange, meanRTs, yerr=stdRTs, label=u"Data", color=colors[0]) RTsPerValueDiff = dict() for valueDiff in valueDiffRange: RTsPerValueDiff[valueDiff] = list() for trial in simulTrials: valueDiff = trial.valueLeft - trial.valueRight RTsPerValueDiff[valueDiff].append(trial.RT) meanRTs = np.zeros(numValueDiffs) stdRTs = np.zeros(numValueDiffs) for valueDiff in valueDiffRange: idx = (np.abs(valueDiffRange - valueDiff)).argmin() meanRTs[idx] = np.mean(RTsPerValueDiff[valueDiff]) stdRTs[idx] = (np.std(RTsPerValueDiff[valueDiff]) / np.sqrt(len(RTsPerValueDiff[valueDiff]))) plt.errorbar(valueDiffRange, meanRTs, yerr=stdRTs, label=u"Simulations", color=colors[5]) plt.xlabel(u"Value difference") plt.ylabel(u"Response time") plt.legend(loc=u"upper left") pdfPages.savefig(fig) plt.close(fig)
def plot_dispatch(bus_to_plot): # plotting: later as multiple pdf with pie-charts and topology? import numpy as np import matplotlib as mpl import matplotlib.cm as cm plot_data = renew_sources+transformers # data preparation x = np.arange(len(timesteps)) y = [] labels = [] for c in plot_data: if bus_to_plot in c.results['out']: y.append(c.results['out'][bus_to_plot]) labels.append(c.uid) # plotting fig, ax = plt.subplots() sp = ax.stackplot(x, y, colors=cm.rainbow(np.linspace(0, 1, len(plot_data)))) proxy = [mpl.patches.Rectangle((0, 0), 0, 0, facecolor= pol.get_facecolor()[0]) for pol in sp] ax.legend(proxy, labels) ax.grid() ax.set_xlabel('Timesteps in h') ax.set_ylabel('Power in MW') ax.set_title('Dispatch')
def Visualize(Tlist,sigmalist, nsamples = 100): """ Visualize an estimation (a point will be used to represent the translation position of a transformation) @param Tlist: a list of Transformations @param sigmalist: a list of corresponding sigmas @param nsamples: the number of samples generated for each (T,sigma) """ import matplotlib.cm as cm fig = plt.figure() ax = fig.add_subplot(111, projection='3d') cholsigmalist = [] colors = iter(cm.rainbow(np.linspace(0, 1, len(Tlist)))) for i in range(len(sigmalist)): color = next(colors) cholsigma = np.linalg.cholesky(sigmalist[i]).T Tsample = [] for k in range(nsamples): vecsample = np.dot(cholsigma,np.random.randn(6,1)) #vecsample = np.dot(cholsigma, np.random.uniform(-1,1,size = 6)) vecsample.resize(6) Tsample = np.dot(VecToTran(vecsample), Tlist[i]) ax.scatter(Tsample[0,3],Tsample[1,3],Tsample[2,3], c = color) ax.set_autoscaley_on(False) ax.set_xlim([-0.5, 0.5]) ax.set_ylim([-0.5, 0.5]) ax.set_zlim([-0.5, 0.5]) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show(False) return True
def plot_kin_all(expt_dh,imsd_fhs): fig=plt.figure(figsize=(8,3)) colors=cm.rainbow(np.linspace(0,1,len(imsd_fhs))) ylimmx=[] for i,imsd_fh in enumerate(imsd_fhs): imsd_flt_fh='%s.imsd_flt' % imsd_fh imsd_flt=pd.read_csv(imsd_flt_fh).set_index('lag time [s]') params_flt_fh='%s.params_flt' % imsd_fh params_flt=pd.read_csv(params_flt_fh) ax1=plt.subplot(131) ax1=plot_kin(imsd_flt, params_flt.loc[:,['power law exponent','power law constant']], ctime='lag $t$', fit_eqn='power', smp_ctrl=None, ylabel='MSD ($\mu m^{2}$)', color=colors[i], ymargin=0.2, ax1=ax1, label=basename(imsd_flt_fh).split('_replicate', 1)[0].replace('_',' '), plot_fh=None) ylimmx.append(np.max(imsd_flt.max())) ax1.set_ylim([0,np.max(ylimmx)]) ax1.legend(bbox_to_anchor=[1,1,0,0],loc=2) fig.savefig('%s/plot_flt_%s.pdf' % (expt_dh,dirname(expt_dh)))
def _DrawEmpiricalCdf(axis, results): colors = cm.rainbow(numpy.linspace( # pylint: disable=no-member 1, 0, len(results) + 1)) for (commit, values), color in zip(results, colors): # Empirical distribution function. levels = numpy.linspace(0, 1, len(values) + 1) axis.step(sorted(values) + [max(values)], levels, label='%s (n=%d)' % (commit, len(values)), color=color) # Dots denoting the percentiles. axis.plot(numpy.percentile(values, tuple(p * 100 for p in _PERCENTILES)), _PERCENTILES, '.', color=color) axis.set_yticks(_PERCENTILES) # Vertical lines denoting the medians. values_per_commit = [values for _, values in results] medians = tuple(numpy.percentile(values, 50) for values in values_per_commit) axis.set_xticks(medians, minor=True) axis.grid(which='minor', axis='x', linestyle='--') # Axis labels and legend. #axis.set_xlabel(step.metric_name) axis.set_ylabel('Cumulative probability') axis.legend(loc='lower right')
def plotAstar(): length = 18 width = 13 height = 7 paths = [[[12, 12], [2, 3], [0, 0]], [[13, 12, 11, 11], [7, 7, 7, 8], [0, 0, 0, 0]], [[6, 5, 4, 3, 2, 1], [1, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0]], [[9, 8, 7, 6, 6, 6], [10, 10, 10, 10, 9, 8], [0, 0, 0, 0, 0, 0]], [[9, 10, 11, 12, 13, 13, 13, 13], [10, 10, 10, 10, 10, 9, 8, 7], [0, 0, 0, 0, 0, 0, 0, 0]], [[9, 9, 10, 10, 10, 10, 11, 11], [10, 9, 9, 8, 7, 6, 6, 5], [0, 0, 0, 0, 0, 0, 0, 0]], [[11, 12, 12, 12, 12, 12, 12, 12, 12, 12], [5, 5, 6, 6, 7, 8, 9, 10, 11, 11], [0, 0, 0, 1, 1, 1, 1, 1, 1, 0]], [[15, 14, 14, 14, 14, 14, 14, 14, 13, 12, 12, 12], [8, 8, 7, 6, 5, 4, 3, 3, 3, 3, 2, 2], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0]], [[4, 5, 6, 7, 8, 9, 10, 10, 11, 12, 12], [5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 3], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[15, 15, 15, 15, 15, 15, 15, 15, 14, 13, 13, 12, 11, 10, 10], [8, 7, 6, 5, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0]], [[15, 15, 14, 13, 12, 11, 10, 9, 8, 7, 7, 7, 6, 5, 4, 3], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 8], [11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 9, 8, 7, 6, 5, 4, 4, 4], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]], [[1, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 13, 13], [5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0]], [[2, 3, 4, 5, 5, 6, 7, 7, 7, 7, 7, 7, 8, 9, 10, 11, 11, 11, 12], [8, 8, 8, 8, 7, 7, 7, 6, 6, 5, 4, 3, 3, 3, 3, 3, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]], [[10, 9, 8, 8, 8, 7, 6, 5, 5, 5, 5, 4, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 2, 2, 2, 2, 2, 3, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 8, 9, 9, 9, 9], [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 2, 1, 0]], [[15, 14, 13, 12, 12], [1, 1, 1, 1, 2], [0, 0, 0, 0, 0]], [[12, 13, 13, 13, 13, 13], [3, 3, 4, 5, 6, 7], [0, 0, 0, 0, 0, 0]], [[1, 1, 1, 2, 3, 4, 5, 6, 7, 7, 8, 8, 9, 10, 11, 12, 13, 14, 15, 15, 15, 15], [9, 10, 10, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0]], [[2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2], [8, 7, 7, 7, 6, 6, 6, 7, 8, 9, 9, 9, 9, 10, 10], [0, 0, 0, 1, 1, 2, 3, 3, 3, 3, 2, 1, 0, 0, 0]], [[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4], [9, 9, 8, 8, 8, 8, 7, 6, 5, 5, 5, 5, 5, 4, 3, 3, 3, 4, 5, 5], [0, 0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1, 0, 0, 0, 0]], [[1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3], [11, 11, 11, 11, 10, 10, 9, 8, 7, 6, 6, 6, 5, 4, 3, 2, 2, 2, 2, 2], [0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 3, 2, 1, 0]], [[15, 15, 14, 14, 13, 12, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 10, 10, 9, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1], [1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 9, 10, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 11], [0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], [[4, 4, 4, 3, 2, 2, 2, 2, 3], [5, 4, 4, 4, 4, 3, 2, 2, 2], [0, 0, 1, 1, 1, 1, 1, 0, 0]], [[2, 1, 1, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8, 8, 8, 9], [8, 8, 7, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0]], [[12, 11, 10, 9, 8, 7, 6, 5, 4, 4, 4, 4, 4, 3, 2, 1, 1, 1, 1, 1], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 3, 3, 3, 3, 2, 1, 0, 0]], [[2, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 7, 6, 5, 4, 4, 4, 4], [0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 2, 1, 0]], [[15, 16, 16, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]], [[15, 15, 16, 16, 16, 16, 16, 15, 14, 13, 12, 11, 10, 10, 10, 9, 8, 7, 6, 5, 4, 4, 4, 4, 3, 2, 2], [1, 2, 2, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8], [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0]]] fig = plt.figure() ax = fig.add_subplot(111, projection='3d') colors = cm.rainbow(np.linspace(0, 1, len(paths))) for path, c in zip(paths, colors): x = path[0] y = path[1] z = path[2] ax.plot(x, y, z, color=c, marker='o', zorder=-1) ax.set_xlim3d([0, length-1]) ax.set_ylim3d([0, width-1]) ax.set_zlim3d([0, height-1]) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') title = "First iteration " + str(len(paths)) + " Connections" plt.title(title) plt.show()
def _DrawHistogram(axis, results): values_per_commit = [values for _, values in results] # Calculate bounds and bins. combined_values = sum(values_per_commit, []) lower_bound = min(combined_values) upper_bound = max(combined_values) if lower_bound == upper_bound: lower_bound -= 0.5 upper_bound += 0.5 bins = numpy.linspace(lower_bound, upper_bound, math.log(len(combined_values)) * 4) # Histograms. colors = cm.rainbow(numpy.linspace( # pylint: disable=no-member 1, 0, len(results) + 1)) for (commit, values), color in zip(results, colors): axis.hist(values, bins, alpha=0.5, normed=True, histtype='stepfilled', label='%s (n=%d)' % (commit, len(values)), color=color) # Vertical lines denoting the medians. medians = tuple(numpy.percentile(values, 50) for values in values_per_commit) axis.set_xticks(medians, minor=True) axis.grid(which='minor', axis='x', linestyle='--') # Axis labels and legend. #axis.set_xlabel(step.metric_name) axis.set_ylabel('Relative probability') axis.legend(loc='upper right')
def main(): articles, ids = get_data(1000) f_names, X = tfidf_featurize(articles) # Choose K using "Elbow method"/ F-Test # #plot_elbow_method(X) # Number of clusters k = 9 # DO CLUSTERING HERE (LEGACY CODE) X = X.toarray() estimater, labels = cluster_n(X, k) classification = dict(zip(ids, labels)) div_lbls = get_output(labels, X, k) # GRAPHING num_feats = len(f_names) reduced_X = PCA(n_components=2).fit_transform(X) est_graph, lbls_graph = cluster_n(reduced_X, k) div_lbls_graph = get_output(lbls_graph, reduced_X, k) # Get 10 diff colors x = np.arange(10) ys = [i+x+(i*x)**2 for i in range(10)] colors = cm.rainbow(np.linspace(0, 1, len(ys))) for i in range(len(div_lbls)): cur = np.array(div_lbls_graph[i]) plt.scatter(cur[:,0], cur[:,1], c=colors[i]) centroids = est_graph.cluster_centers_ plt.scatter(centroids[:, 0], centroids[:, 1], marker='x', s=169, linewidths=3, color='black', zorder=10) plt.show()
def scatterPlotForFeatures(df, f1,f2,legend1,legend2): f1=f1.split(".") f2=f2.split(".") handler=getTrainingHandler() darr=df.map(lambda s: ( handler.computeClassification(s),(\ reduce(lambda x,y: getattr(x,y) if isinstance(x, Row) else getattr(getattr(s,x),y), f1) if len(f1)>1 else getattr(s,f1[0]),\ reduce(lambda x,y: getattr(x,y) if isinstance(x, Row) else getattr(getattr(s,x),y), f2) if len(f2)>1 else getattr(s,f2[0])\ )))\ .reduceByKey(lambda x,y: makeList(x) + makeList(y))\ .collect() numClasses=getTrainingHandler().numClasses() citer=iter(cm.rainbow(np.linspace(0, 1, numClasses))) colors = [next(citer) for i in range(0, numClasses)] legends= [getTrainingHandler().getClassLabel(i) for i in range(0,numClasses)] sets=[] for t in darr: sets.append((plt.scatter([x[0] for x in t[1]],[x[1] for x in t[1]], color=colors[t[0]],alpha=0.5),legends[t[0]])) params = plt.gcf() plSize = params.get_size_inches() params.set_size_inches( (plSize[0]*3, plSize[1]*2) ) plt.ylabel(legend2) plt.xlabel(legend1) plt.legend([x[0] for x in sets], [x[1] for x in sets], scatterpoints=1, loc='lower left', ncol=numClasses, fontsize=12) plt.show()
def plot_data(data0, label0, title, subtitle, *args): colors = iter(cm.rainbow(np.linspace(0,1,5))) plt.clf() fig = plt.figure() ax = plt.gca() if (len(args) == 2): means, covmats = args elif (len(args) == 4): means, covmats, traindata, trainlabel = args trainlabel = trainlabel.reshape(trainlabel.size) for i in range(1,6): cl = next(colors) plt.scatter(data0[(label0==i).reshape((label0==i).size),0], data0[(label0==i).reshape((label0==i).size),1],color=cl) # Draw ellipse with 1 standard deviation if (len(args) >= 2): # Compute eigen vectors and value for covmat to plot an ellipse lambda_, v = np.linalg.eig(covmats[i-1]) lambda_ = np.sqrt(lambda_) ell = Ellipse(xy=(means[0,i-1], means[1,i-1]), width=lambda_[0]*2, height=lambda_[1]*2, angle=np.rad2deg(np.arccos(v[0, 0]))) ell.set_facecolor('none') ax.add_artist(ell) #Add mean points plt.scatter(means[0,i-1], means[1,i-1],c='black',s=30); if (len(args) == 4): plt.scatter(traindata[trainlabel==i,0], traindata[trainlabel==i,1],color=cl, edgecolor='black') fig.suptitle(title) ax.set_title(subtitle,fontsize='10') fig.show() return
def pca_3d(X, component1, component2, component3, class_indices, path, name, data_legend): C, y, Z = pca(X, class_indices) colors = cm.rainbow(np.linspace(0, 1, C)) markers = ["o", "v", "8", "s", "p", "*", "h", "H", "+", "x", "D"] while True: if C <= len(markers): break markers += markers # Plot PCA of the data f = plt.figure(figsize=(15, 15)) ax = f.add_subplot(111, projection='3d', axisbg='white') ax._axis3don = False for c in range(C): # select indices belonging to class c: class_mask = y.A.ravel() == c xs = Z[class_mask, component1 - 1] xs = xs.reshape(len(xs)).tolist()[0] ys = Z[class_mask, component2 - 1] ys = ys.reshape(len(ys)).tolist()[0] zs = Z[class_mask, component3 - 1] zs = zs.reshape(len(zs)).tolist()[0] ax.scatter(xs, ys, zs, s=20, c=colors[c], marker=markers[c]) # plt.figtext(0.5, 0.93, 'PCA 3D', ha='center', color='black', weight='light', size='large') f.savefig(path + '/' + name + '.png', dpi=200) plt.show()
def fingerprint(disp_sim_spin = True,n_sim_spins = 13,xrange = [0,20],): ################### # Add simulated spins # ################### if disp_sim_spin == True: HF_par = [hf['C1']['par'],hf['C2']['par'],hf['C3']['par'], hf['C4']['par'], hf['C5']['par'], hf['C6']['par'], hf['C7']['par'], hf['C8']['par'], hf['C9']['par'], hf['C10']['par'], hf['C11']['par'], hf['C12']['par']] HF_perp = [hf['C1']['perp'],hf['C2']['perp'],hf['C3']['perp'], hf['C4']['perp'], hf['C5']['perp'], hf['C6']['perp'], hf['C7']['perp'], hf['C8']['perp'], hf['C9']['perp'], hf['C10']['perp'], hf['C11']['perp'], hf['C12']['perp']] #msmp1_f from hdf5 file # msm1 from hdf5 file # ZFG g_factor from hdf5file B_Field = 304.12 # use magnet tools Bz = (msp1_f**2 - msm1_f**2)/(4.*ZFS*g_factor) tau_lst = np.linspace(0,72e-6,10000) Mt16 = SC.dyn_dec_signal(HF_par,HF_perp,B_Field,16,tau_lst) FP_signal16 = ((Mt16+1)/2) ## Data location ## timestamp ='20140419_005744' if os.name =='posix': ssro_calib_folder = '//Users//Adriaan//Documents//teamdiamond//data//20140419//111949_AdwinSSRO_SSROCalibration_Hans_sil1' else: ssro_calib_folder = 'd:\\measuring\\data\\20140419\\111949_AdwinSSRO_SSROCalibration_Hans_sil1' a, folder = load_mult_dat(timestamp, number_of_msmts = 140, ssro_calib_folder=ssro_calib_folder) ############### ## Plotting ### ############### fig = a.default_fig(figsize=(35,5)) ax = a.default_ax(fig) # ax.set_xlim(15.0,15.5) ax.set_xlim(xrange) start, end = ax.get_xlim() ax.xaxis.set_ticks(np.arange(start, end, 0.5)) ax.set_ylim(-0.05,1.05) ax.plot(a.sweep_pts, a.p0, '.-k', lw=0.4,label = 'data') #N = 16 if disp_sim_spin == True: colors = cm.rainbow(np.linspace(0, 1, n_sim_spins)) for tt in range(n_sim_spins): ax.plot(tau_lst*1e6, FP_signal16[tt,:] ,'-',lw=.8,label = 'spin' + str(tt+1))#, color = colors[tt]) if False: tot_signal = np.ones(len(tau_lst)) for tt in range(n_sim_spins): tot_signal = tot_signal * Mt16[tt,:] fin_signal = (tot_signal+1)/2.0 ax.plot(tau_lst*1e6, fin_signal,':g',lw=.8,label = 'tot') plt.legend(loc=4) print folder plt.savefig(os.path.join(folder, str(disp_sim_spin)+'fingerprint.pdf'), format='pdf') plt.savefig(os.path.join(folder, str(disp_sim_spin)+'fingerprint.png'), format='png')
def scatter_matrix_with_categoricals(data, col1, col2, categorical_param, figsize=(10, 6), alpha=0.75): categorials = data[categorical_param].unique() colors = cm.rainbow(np.linspace(0, 1, len(categorials))) color_cycle = cycle(colors) plt.figure(figsize=figsize) for c in categorials: plt.scatter(data[col1][data[categorical_param] == c], data[col2][data[categorical_param] == c], alpha=alpha, color=color_cycle.next(), label=c) plt.xlabel(col1) plt.ylabel(col2) plt.legend(loc='best') plt.show()
def displayPowerSpectrum(*args): fig = plt.figure(1) ax = fig.add_subplot(111) colors = iter(cm.rainbow(np.linspace(0, 1, len(args)))) for a in args: dataPowerSpectrum = calculatePowerSpectrum(a.matrix) n = dataPowerSpectrum.size xpoly = np.array(range(1,n + 1)) p = ax.plot(xpoly, dataPowerSpectrum, color=next(colors), label=a.name) Fs = 1/a.data_step[0] tmp = 1/( Fs/2 * np.linspace(1e-2, 1, int(xpoly.size/6)) ) ax.set_xticks( np.linspace(1,xpoly.size,tmp.size) ) ax.set_xticklabels( ["%.1f" % member for member in tmp] ) del tmp plt.yscale('log') plt.grid(linestyle='dotted') plt.ylabel('Power Spectrum (|f|^2)') plt.xlabel('Frequency') plt.legend(loc=3) plt.show()
def hists(dgrp,dfs): "dgrp = MAIN,DC,EC,SC,MC" #set up some cosmetics colors = cm.rainbow(np.linspace(0,1,len(dfs))) labels = ['h10_%d'%i for i in range(len(dfs))] ncols=len(dgrp['COLS']) gs ='' if ncols > 4: gs = gridspec.GridSpec(2,4) else: gs = gridspec.GridSpec(1,ncols) for icol in np.arange(ncols): ax=plt.subplot(gs[icol]) col=dgrp['COLS'][icol] nbins=dgrp['NBINS'][icol] xmin=dgrp['XMIN'][icol] xmax=dgrp['XMAX'][icol] #print 'col=%s:nbins=%d:xmin=%d:xmax%d'%(col,nbins,xmin,xmax) ax.set_title(col) ax.set_xlabel(col) for c,l,df in zip(colors,labels,dfs): #print df[col] plt.hist(df[col],nbins,(xmin,xmax), histtype='step',color=c,label=l) plt.legend(loc=2,prop={'size':8})
def _get_colours(self, colours, rainbow=False): # pragma: no cover num_chains = len(self.chains) if rainbow or num_chains > len(colours): colours = cm.rainbow(np.linspace(0, 1, num_chains)) else: colours = colours[:num_chains] return colours
def compute_graph(players, start, end, resolution, delta, position="allround"): import collections import matplotlib.cm as cm import numpy q = Archiv.objects.filter( Q(game__time__gt = start), Q(game__time__lt = end), reduce(lambda q1, q2: q1 | q2, map(lambda p: Q(player = p), players)) ).order_by('game__time') start=q[0].game.time data_plot = dict() colors = [c for c in cm.rainbow(numpy.linspace(0,1,len(players)))] for skill_record in q: if skill_record.player not in data_plot: data_plot[skill_record.player] = dict() data_plot[skill_record.player]['color'] = colors.pop() data_plot[skill_record.player]["skill"] = collections.OrderedDict() data_plot[skill_record.player]["skill"][start-datetime.timedelta(**{'days': delta})] = 700 #data_plot[skill_record.player]["skill"][resolution(skill_record.game.time)] = skill_record.player.skill(position=position) if position=="offensiv": data_plot[skill_record.player]["skill"][resolution(skill_record.game.time)] = compute_skill(skill_record.mu_off,skill_record.sigma_off) elif position=="defensiv": data_plot[skill_record.player]["skill"][resolution(skill_record.game.time)] = compute_skill(skill_record.mu_def,skill_record.sigma_def) else: data_plot[skill_record.player]["skill"][resolution(skill_record.game.time)] = compute_skill(skill_record.mu,skill_record.sigma) #colors = cm.rainbow(numpy.linspace(0,1,len(players))) #print(data_plot) return {p: ds for p, ds in data_plot.items()}
def _plot_proto_symbol_space(coordinates, target_names, name, args): # Reduce to 2D so that we can plot it. coordinates_2d = TSNE().fit_transform(coordinates) n_samples = coordinates_2d.shape[0] x = coordinates_2d[:, 0] y = coordinates_2d[:, 1] colors = cm.rainbow(np.linspace(0, 1, n_samples)) fig = plt.figure(1) plt.clf() ax = fig.add_subplot(111) dots = [] for idx in xrange(n_samples): dots.append(ax.plot(x[idx], y[idx], "o", c=colors[idx], markersize=15)[0]) ax.annotate(target_names[idx], xy=(x[idx], y[idx])) lgd = ax.legend(dots, target_names, ncol=4, numpoints=1, loc='upper center', bbox_to_anchor=(0.5,-0.1)) ax.grid('on') if args.output_dir is not None: path = os.path.join(args.output_dir, name + '.pdf') print('Saved plot to file "%s"' % path) fig.savefig(path, bbox_extra_artists=(lgd,), bbox_inches='tight') else: plt.show()
def plotMeanVsAvForAllLines(self, bs = 50, norm = False): colors = cm.rainbow(np.linspace(0, 1, self.numLines)) for i in xrange(self.numLines): a, s = self.getBinnedDataForFlyLine(i+1, bs, norm) plt.scatter(a, s, color=colors[i], alpha = 0.5) plt.show()
import numpy as np import random import sklearn.cluster as clstr import pylab as plt import matplotlib.cm as cm def init_board(N): X = np.array([(random.uniform(-1, 1), random.uniform(-1, 1)) for i in range(N)]) return X points = init_board(100) clusterizator = clstr.MiniBatchKMeans(n_clusters=7, init_size=21) # clusterizator = clstr.Birch clusterizator.fit(points, y=None) p = points.transpose() plt.scatter(p[0], p[1]) centers = clusterizator.cluster_centers_ c = centers.transpose() colors = cm.rainbow(np.linspace(0, 1, 7)) for i in range(100): label = clusterizator.labels_[i] plt.scatter(p[0, i], p[1, i], color=colors[label]) plt.grid() plt.show()
print("Brier scores: (the smaller the better)") clf_score = brier_score_loss(y_test, prob_pos_clf, sw_test) print("No calibration: %1.3f" % clf_score) clf_isotonic_score = brier_score_loss(y_test, prob_pos_isotonic, sw_test) print("With isotonic calibration: %1.3f" % clf_isotonic_score) clf_sigmoid_score = brier_score_loss(y_test, prob_pos_sigmoid, sw_test) print("With sigmoid calibration: %1.3f" % clf_sigmoid_score) # ############################################################################# # Plot the data and the predicted probabilities plt.figure() y_unique = np.unique(y) colors = cm.rainbow(np.linspace(0.0, 1.0, y_unique.size)) for this_y, color in zip(y_unique, colors): this_X = X_train[y_train == this_y] this_sw = sw_train[y_train == this_y] plt.scatter(this_X[:, 0], this_X[:, 1], s=this_sw * 50, c=color[np.newaxis, :], alpha=0.5, edgecolor='k', label="Class %s" % this_y) plt.legend(loc="best") plt.title("Data") plt.figure() order = np.lexsort((prob_pos_clf, ))
# And, we can add a new field to our dataframe to show the cluster of each row: # In[24]: pdf['cluster_'] = agglom.labels_ pdf.head() # In[25]: import matplotlib.cm as cm n_clusters = max(agglom.labels_)+1 colors = cm.rainbow(np.linspace(0, 1, n_clusters)) cluster_labels = list(range(0, n_clusters)) # Create a figure of size 6 inches by 4 inches. plt.figure(figsize=(16,14)) for color, label in zip(colors, cluster_labels): subset = pdf[pdf.cluster_ == label] for i in subset.index: plt.text(subset.horsepow[i], subset.mpg[i],str(subset['model'][i]), rotation=25) plt.scatter(subset.horsepow, subset.mpg, s= subset.price*10, c=color, label='cluster'+str(label),alpha=0.5) # plt.scatter(subset.horsepow, subset.mpg) plt.legend() plt.title('Clusters') plt.xlabel('horsepow') plt.ylabel('mpg')
# groupby month and day and compute standard deviation month_day_str = xr.DataArray(ds_obs.indexes['time'].strftime("%m-%d"), coords=[ds_obs.coords["time"]], name="month_day_str") obs_50_sd = ds_obs.groupby(month_day_str).std("time") obs_50_sd.to_netcdf(PATH_DATA + 'ERAi_GPH_SD_50.nc4') else: obs_50_sd = xr.open_dataset(PATH_DATA + 'ERAi_GPH_SD_50.nc4') titles = [ 'Beginning of Aug', 'mid Aug', 'Beginning of Sep', 'mid Sep', 'Beginning of Oct', 'mid Oct', 'Beginning of Nov', 'mid Nov' ] fig, ax = plt.subplots(nrows=2, ncols=4, figsize=(21, 8)) plt.suptitle(' Z50 standard deviation (1999-2010, excl 2002)') for k in range(8): color = iter(cm.rainbow(np.linspace(0, 1, len(models) * 2))) if k < 4: ii = 0 jj = k else: ii = 1 jj = k - 4 CS = ax[ii, jj].plot(obs_50_sd.month_day_str.values, obs_50_sd.z.values / 9.8, color='black', linewidth=2) ax[ii, jj].set_ylabel('Amplitude (m)', fontsize=12) ax[ii, jj].set_xlabel('Date', fontsize=12) ax[ii, jj].set_title('IC: ' + titles[k]) fechas = [ datetime.strptime(obs_50_sd.month_day_str.values[j],
def get_colors(num: int) -> Iterable[str]: return cm.rainbow(np.linspace(0, 1, num))
def scatter_plot_by_category(feat, x, y): #数据的可视化 alpha = 0.5 gs = df.groupby(feat) cs = cm.rainbow(np.linspace(0, 1, len(gs))) for g, c in zip(gs, cs): plt.scatter(g[1][x], g[1][y], color=c, alpha=alpha)
plt.xlabel("Micrograms") plt.ylabel("Score") #plt.subplot(131) plt.show() #cost function linear4 = MyLR(np.array([[89.0], [-14]])) theta0 = 89.0 fig = plt.figure() axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) plt.title("Cost for different theta0") plt.xlabel("theta1") plt.ylabel("cost (j(theta0, theta1))") #plt.axis([-14, 20, -3, 140]) n_samples = 6 colors = cm.rainbow(np.linspace(0, 1, n_samples)) for j in range(0, n_samples): costs = [] for theta1 in range(-13, -2): #every curve costs.append(linear4.cost_(Xpill, Yscore)) linear4.theta = np.array([[theta0], [theta1]]) theta0 += 1 print(costs) plt.plot(range(-13, -2), costs, color=colors[j]) # print("------") #plt.margins(0) axes.set_xlim([-14, -4]) axes.set_ylim([20, 140]) plt.show()
##SUPPORT VECTOR CLASSIFICATIONS ##checking for different kernels from sklearn.svm import SVC svc_scores = [] kernels = ['linear', 'poly', 'rbf', 'sigmoid'] for i in range(len(kernels)): svc_classifier = SVC(kernel=kernels[i]) svc_classifier.fit(X_train, Y_train) svc_scores.append(svc_classifier.score(X_test, Y_test)) from matplotlib.cm import rainbow ##%matplotlib inline colors = rainbow(np.linspace(0, 1, len(kernels))) plt.bar(kernels, svc_scores, color=colors) for i in range(len(kernels)): plt.text(i, svc_scores[i], svc_scores[i]) plt.xlabel('Kernels') plt.ylabel('Scores') plt.title('Support Vector Classifier scores for different kernels') #dataset.describe() # Fitting SVM to the Training set classifier = SVC(kernel='rbf', random_state=0, probability=True) classifier.fit(X_train, Y_train) from sklearn.externals import joblib
encoder_y = encoder_y + np.sin(encoder_yaw) * dydt * dt_s encoder_yaw += dpdt * dt_s encoder_xs.append(encoder_x) encoder_ys.append(encoder_y) encoder_yaws.append(encoder_yaw) # In[6]: plt.figure() plt.plot(encoder_yaws) plt.ylabel("degrees") plt.show() plt.figure(figsize=(10, 10)) colors = cm.rainbow(np.linspace(0, 1, len(encoder_xs))) plt.scatter(encoder_xs, encoder_ys, s=1, color=colors) plt.title("robot position based just on encoders") plt.axis("equal") plt.show() # ## Plot the Yaw() versus FusedHeading() # In[5]: plt.figure(figsize=(15, 15)) plt.plot(sensor_data[:, 6], label="FusedHeading") plt.plot(sensor_data[:, 5], linestyle=":", linewidth=3, label="Yaw") plt.title("Yaw of robot") plt.xlabel("samples") plt.ylabel("Yaw (degrees)")
import numpy as np from mds import mds import matplotlib.pyplot as plt import matplotlib.cm as cm if __name__ == "__main__": Cities = np.array([ 'BeiJing', 'ShangHai', 'HeZe', 'GuangZhou', 'DaTong', 'XiAn', 'HerBin', 'NanJing' ]) D = np.matrix( '0 2.16 4.68 3.33 5.85 2.08 1.91 2.08;0 0 12.95 2.41 9.33 2.58 2.91 1.08;0 0 0 17.91 11.76 9.56 21 8;0 0 0 0 36 2.58 4.16 2.25;0 0 0 0 0 16.26 24 16.88;0 0 0 0 0 0 2.91 2;0 0 0 0 0 0 0 2.83 ;0 0 0 0 0 0 0 0' ) X = mds((D + np.transpose(D)) / 2) ArrayX = np.array(X) colors = cm.rainbow(np.linspace(0, 1, len(ArrayX))) for x, c, name in zip(ArrayX, colors, Cities): plt.scatter(x[0], x[1], label=name, color=c) plt.legend() plt.title('City locations under MDS') plt.show()
def plot_graph_networkx(graph, ax, colors=None, use_unique_colors=True, use_pos_node=False, pos=None, title=None): node_labels = { node: "{}".format(np.round(data["features"][:], 2)) for node, data in graph.nodes(data=True) if data["features"] is not None } edge_labels = {(sender, receiver): "{}".format(data["features"][:]) for sender, receiver, data in graph.edges(data=True) if data["features"] is not None} global_label = ("{}".format(graph.graph["features"][:]) if graph.graph["features"] is not None else None) if use_pos_node == False and pos == None: pos = nx.spring_layout(graph) elif use_pos_node == False: pos = pos elif use_pos_node == True: pos = nx.get_node_attributes(graph, name='features') # make the label such that it is not printend right onto the node but a slight offset in the y dimenion. pos_higher = {} offset = 0.1 for i, j in pos.items(): pos_higher[i] = (float(j[0]), float(j[1]) + offset) print('node labels: ', node_labels) print('edge labels: ', edge_labels) if use_unique_colors: colors = cm.rainbow(np.linspace(0, 1, len(graph.nodes))) nx.draw_networkx(graph, pos, ax=ax, node_color=colors, with_labels=False, labels=None) # nx.draw_networkx(graph, pos, ax=ax, node_color=colors, with_labels=False, labels=None, connectionstyle='arc3, rad = 0.4') nx.draw_networkx_labels(graph, ax=ax, pos=pos, labels=node_labels) # edit pos to print all edge labels pos2 = pos.copy() cnt = 0 for i in range(len(pos2), 2 * (len(pos2)) - 1): pos2[i] = pos2[cnt] cnt += 1 if edge_labels: nx.draw_networkx_edge_labels(graph, pos2, edge_labels, ax=ax, label_pos=0.2) if global_label: plt.text(0.05, 0.95, global_label, transform=ax.transAxes) if title: ax.set_title(title) ax.yaxis.set_visible(False) ax.xaxis.set_visible(False) return pos
Toronto_merged['Cluster Labels']=Toronto_merged['Cluster Labels'].astype(int) # In[560]: import matplotlib.cm as cm import matplotlib.colors as colors # create map map_clusters = folium.Map(location=[latitude, longitude], zoom_start=11) # set color scheme for the clusters x = np.arange(kclusters) ys = [i + x + (i*x)**2 for i in range(kclusters)] colors_array = cm.rainbow(np.linspace(0, 1, len(ys))) rainbow = [colors.rgb2hex(i) for i in colors_array] # add markers to the map markers_colors = [] for lat, lon, poi, cluster in zip(Toronto_merged['Latitude'], Toronto_merged['Longitude'], Toronto_merged['Neighborhood'], Toronto_merged['Cluster Labels']): label = folium.Popup(str(poi) + ' Cluster ' + str(cluster), parse_html=True) folium.CircleMarker( [lat, lon], radius=5, popup=label, color=rainbow[cluster-1], fill=True, fill_color=rainbow[cluster-1], fill_opacity=0.7).add_to(map_clusters)
### unweighted dir_data = "/Users/saito/data/phangs/co_ratio/ngc4321/" # plot 1 data = np.loadtxt(dir_data + "w_bm_vs_ap_r21_median.txt") plt.figure(figsize=(10, 8)) plt.rcParams["font.size"] = 18 for i in range(16): r21_tmp = data[data[:, 0] == 4 + i * 2] r21 = r21_tmp[r21_tmp[:, 1].argsort(), :] plt.plot(r21[:, 1] * scale, r21[:, 2], linewidth=2, c=cm.rainbow(i / 15.), marker="o", alpha=0.6) plt.scatter( [ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 ], np.array([4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32]) * scale, c=np.array([4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32]) * scale, cmap="rainbow") cbar = plt.colorbar()
def ff_img(filename): result = get_ncaa_data() df = DataFrame(result) df.columns = [ 'id', 'tourney_year', 'winner_seed', 'winner_team', 'loser_seed', 'loser_team', 'other_seed_1', 'other_team_1', 'other_seed_2', 'other_team_2', 'final_four_sum' ] x = df['tourney_year'] #x = np.arange(1979,2016,1) y = df['final_four_sum'] #y = x**2 + 2*x - 5 fig = Figure() # Plotting canvas = FigureCanvas(fig) ax1 = fig.add_subplot(311) ax2 = fig.add_subplot(312) ax3 = fig.add_subplot(313) colors = cm.rainbow(np.linspace(0, 1, len(df))) ax1.scatter(x, y, c=colors) ax1.set_xlabel('') ax1.set_ylabel('SUM OF SEEDS') ax2.hist(y, bins=22) ax2.set_xlabel('SUM OF SEEDS') ax2.set_ylabel('COUNT') db_url = 'da602.sqlite' conn = sqlite3.connect(db_url) c = conn.cursor() c.execute('select seed, team, percent_picked from PICKS_2016') picks_results = c.fetchall() seeds = [] teams = [] probs = [] team_sample = [] for picks_row in picks_results: seeds.append(int(picks_row[0])) teams.append(picks_row[1]) probs.append(float(picks_row[2])) print(probs) # make probs add to 1 multiplier = (1.0 / sum(probs)) numpy_probs = np.asarray(probs) numpy_probs = numpy_probs * multiplier print(sum(numpy_probs)) seed_sums = [] for i in range(0, 99000): four_seeds = np.random.choice(seeds, 4, p=numpy_probs) four_seeds_sum = sum(four_seeds) seed_sums.append(four_seeds_sum) #print four_seeds_sum ax3.hist(seed_sums, normed=1, bins=15) #ax3.set_title("Peoples Real Picks Sums Histogram") ax3.set_xlabel("Sum") ax3.set_ylabel("Frequency") conn.commit() conn.close() canvas.print_figure(filename) return static_file(filename, root='./', mimetype='image/png')
def plotParam(dataset, exp_id, indList, labels): ''' Plot the parameters found by models against a the ground truth parameters It produces a scatter plot where the x-axis represents the ground-truth values and the y-axis represents the value found by models. This plot is produced for every epoch for which the model had its parameters saved during training Args: dataset (str): the dataset name. The dataset should be artificial because this plot requires ground truth parameters. exp_id (str): the experience name. indList (list): the list of model ids to plot ''' paramsPaths = sorted( glob.glob("../models/{}/model{}_epoch*".format(exp_id, indList[0]))) colors = cm.rainbow(np.linspace(0, 1, len(indList))) for key in paramKeys: tensor = np.zeros( (len(indList), len(paramsPaths), len( np.genfromtxt("../results/{}/model{}_epoch0_{}.csv".format( exp_id, indList[0], key))))) for k, indModel in enumerate(indList): tensorPathList = sorted(glob.glob( "../results/{}/model{}_epoch*_{}.csv".format( exp_id, indModel, key)), key=findNumbers) for i, tensorPath in enumerate(tensorPathList): tensor[k, i] = np.genfromtxt(tensorPath)[:, 0].reshape(-1) xMin = np.min(tensor) xMax = np.max(tensor) xRangeDict = (xMin - 0.1, xMax + 0.1) maxErr = 0 if not labels: labels = indList for k, indModel in enumerate(indList): paramsPaths = sorted(glob.glob( "../models/{}/model{}_epoch*".format(exp_id, indModel)), key=findNumbers) #print(k,indModel,len(paramsPaths),tensor.shape) #for path in paramsPaths: # print(path) #Plot the param error as a function of the absolute value of the param for j in range(len(tensor[k])): epoch = findNumbers( os.path.basename(paramsPaths[j]).replace( "model{}".format(indModel), "")) param_gt = np.genfromtxt("../data/{}_{}.csv".format( dataset, key)) plt.figure(j, figsize=(10, 5)) plt.plot(param_gt, tensor[k, j], "*", label=labels[k], color=colors[k]) x = np.arange(xRangeDict[0], xRangeDict[1], 0.01) plt.plot(x, x) plt.xlabel("Ground truth") plt.ylabel("Estimation") plt.xlim(xRangeDict) plt.ylim(xRangeDict) if k == len(indList) - 1: plt.legend() plt.savefig("../vis/{}/model{}_{}VSgt_epoch{}_.png".format( exp_id, indList, key, epoch)) plt.close()
def plot_tdump(tdump, outfile, goes_source, latlon_range): def plot_single(t, m=None, c=None, highlight=None, i=None): m.plot(t.lon.values, t.lat.values, c=c, latlon=True, label=t.tnum[0]) m.plot(t.lon.values[::6], t.lat.values[::6], '.', c=c, latlon=True) m.plot(t.lon.values[0], t.lat.values[0], '*', c=c, latlon=True, ms=12) m.plot(t.lon.values[-1], t.lat.values[-1], 's', c=c, latlon=True, ms=8) if i is not None: plt.annotate(str(i), xy=(t.lon.values[0] + .5, t.lat.values[0] + .5)) if highlight is None: pass elif (highlight == 0): m.plot(t.lon.values[0], t.lat.values[0], '*', c='black', latlon=True, ms=12) # start stars elif (highlight == t.age[-1]): m.plot(t.lon.values[-1], t.lat.values[-1], 's', c='black', latlon=True, ms=8) # end squares else: try: offset = np.where(t.age == highlight)[0][0] except IndexError: print('nothing to highlight!') pass m.plot(t.lon.values[offset], t.lat.values[offset], '.', c='black', latlon=True, ms=10) return m T = read_tdump(tdump) start = T.index.min().to_pydatetime() end = T.index.max().to_pydatetime() tlen = (end - start).total_seconds() / 3600 goes_date = dt.datetime.strptime( goes_source.split(os.sep)[-1], 'g15.%Y%j.%H%M.nc') goes_offset = int((goes_date - start).total_seconds() / 3600) highlight = goes_offset if 0 <= goes_offset <= tlen else None fig = plt.figure(figsize=(7, 8)) ax = fig.add_axes([0, 0, 1, 1]) m = bmap(ax=ax, latlon_range=latlon_range) t = T.groupby('tnum') goesname = add_goes_to_map(m, goes_source) colors = cm.rainbow(np.linspace(0, 1, len(list(t.groups.keys())))) for i, k in enumerate(t.groups.keys()): m = plot_single(t.get_group(k), m=m, c=colors[i], highlight=highlight, i=i) m.drawgreatcircle(-121.3, 38.6, -156, 19.8, linestyle='--', c='black') m.plot(-121.3, 38.6, 's', ms=8, c='black', latlon=True) m.plot(-156, 19.8, '*', ms=12, c='black', latlon=True) m.plot(-118.2, 33.77, 's', ms=8, c='red', latlon=True) hrs = np.ptp(T.age.values) plotname = '{:2.0f}-hour trajectories from {} to {}'\ .format(hrs, start.strftime('%Y-%m-%d %HZ'), end.strftime('%Y-%m-%d %HZ'))+goesname ax.set_title('{}'.format(plotname), y=1.08) fig.savefig(outfile, dpi=300, bbox_inches='tight') fig.clf() plt.close('all') return
import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm import secondGameFirstPolicyFunctions as mf rewardMatrix = np.array([[1, -1], [-1, 1]]) ## Reward Matrix epsilon = 0.15 ## epsilon firstPlayerPolicy = mf.policyInitializer() secondPlayerPolicy = mf.policyInitializer() policyTrack1Player1 = list() policyTrack1Player2 = list() for i in range(4000): learningRate = 1 / (i + 1) policyTrack1Player1.append(firstPlayerPolicy[0]) policyTrack1Player2.append(secondPlayerPolicy[0]) firstPlayerAction = mf.actionTaker(firstPlayerPolicy, epsilon) secondPlayerAction = mf.actionTaker(secondPlayerPolicy, epsilon) firstPlayerPolicy, secondPlayerPolicy = mf.policyImprover( firstPlayerPolicy, secondPlayerPolicy, firstPlayerAction, secondPlayerAction, learningRate, rewardMatrix) colors = cm.rainbow(np.linspace(0, 1, len(policyTrack1Player1))) plt.scatter(policyTrack1Player1, policyTrack1Player2, s=5, color=colors) plt.scatter(firstPlayerPolicy[0], secondPlayerPolicy[0], s=50, color=(1, 0, 0)) plt.title("Matching Pennies") plt.xlabel('Player 1 head') plt.ylabel('Player 2 head') plt.show()
# There are several kernels for Support Vector Classifier. I'll test some of them and check which has the best score. # In[16]: svc_scores = [] kernels = ['linear', 'poly', 'rbf', 'sigmoid'] for i in range(len(kernels)): svc_classifier = SVC(kernel=kernels[i]) svc_classifier.fit(X_train, y_train) svc_scores.append(svc_classifier.score(X_test, y_test)) # I'll now plot a bar plot of scores for each kernel and see which performed the best. # In[17]: colors = rainbow(np.linspace(0, 1, len(kernels))) plt.bar(kernels, svc_scores, color=colors) for i in range(len(kernels)): plt.text(i, svc_scores[i], svc_scores[i]) plt.xlabel('Kernels') plt.ylabel('Scores') plt.title('Support Vector Classifier scores for different kernels') # The `linear` kernel performed the best, being slightly better than `rbf` kernel. # In[18]: print("The score for Support Vector Classifier is {}% with {} kernel.".format( svc_scores[0] * 100, 'linear')) # #### Decision Tree Classifier
def plot_predictions(ax, predictions): colors = cm.rainbow(np.linspace(0, 1, len(predictions))) for idx, pred in enumerate(predictions): plot_prediction(ax, predictions[pred], colors[idx], pred)
def add_tdump_to_map_with_heights(m, axh, tdump, highlight=None, fixed_color=None): def plot_single(t, m, axh, c, i=None, highlight=None): m.plot(t.lon.values, t.lat.values, c=c, latlon=True, label=t.tnum[0]) m.plot(t.lon.values[::6], t.lat.values[::6], '.', c=c, latlon=True) m.plot(t.lon.values[0], t.lat.values[0], '*', c=c, latlon=True, ms=12) m.plot(t.lon.values[-1], t.lat.values[-1], 's', c=c, latlon=True, ms=8) if i is not None: x, y = m(t.lon.values[0] + .5, t.lat.values[0] + .5) m.ax.annotate(str(i), xy=(x, y), xytext=(x, y), xycoords='data', textcoords='data', fontsize=6) axh.plot(t.age, t.height, '-', c=c) axh.plot(t.age[::6], t.height[::6], '.', c=c) axh.plot(t.age[0], t.height[0], '*', c=c) if highlight is None: pass elif (highlight == 0): m.plot(t.lon.values[0], t.lat.values[0], '*', c='black', latlon=True, ms=12) # start stars elif (highlight == t.age[-1]): m.plot(t.lon.values[-1], t.lat.values[-1], 's', c='black', latlon=True, ms=8) # end squares else: try: offset = np.where(t.age == highlight)[0][0] m.plot(t.lon.values[offset], t.lat.values[offset], '.', c='black', latlon=True, ms=10) axh.plot(t.age[offset], t.height[offset], '.', c='black') except IndexError: print('nothing to highlight!') pass T = read_tdump(tdump) start = T.index.min().to_pydatetime() end = T.index.max().to_pydatetime() t = T.groupby('tnum') if fixed_color is None: colors = cm.rainbow(np.linspace(0, 1, len(t.groups.keys()))) else: colors = [fixed_color] * len(t.groups.keys()) for i, k in enumerate(t.groups.keys()): plot_single(t=t.get_group(k), m=m, axh=axh, c=colors[i], highlight=highlight, i=i) hrs = np.ptp(T.age.values) tdump_name = '{:2.0f}-hour trajectories ({} to {})'.format( hrs, start.strftime('%Y-%m-%d %HZ'), end.strftime('%Y-%m-%d %HZ')) return tdump_name
def plot_test(self, batch_x, manifold): # plot_folder: path to save the plot # plot_name: name of the save plot # plot_mode: 'save' or 'show' x_recons, z_mu, z_log_sigma2, x_true = self.sess.run( [self.x_recons, self.latent_mu, self.latent_log_sigma2, self.x], feed_dict={ self.x: batch_x, self.is_training: True }) # just to check the values z_sigma2 = np.exp(z_log_sigma2) marker_size = 120 if manifold: colors = manifold.points_colors else: colors = cm.rainbow(np.linspace(0, 1, np.shape(batch_x)[0])) manifold_time = range(np.shape(batch_x)[0]) colors_latent = colors[manifold_time, :] fig = plt.figure(figsize=[12, 9]) if self.dim_data > 2: ax = fig.add_subplot(231, projection='3d') ax.scatter3D(x_true[manifold_time, 0], x_true[manifold_time, 1], x_true[manifold_time, 2], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('dim 1') ax.set_ylabel('dim 2') ax.set_zlabel('dim 3') ax.set_title('true data points on manifold') if manifold and manifold.manifold_type == 'torus': ax.view_init(elev=64., azim=100.) ax = fig.add_subplot(232, projection='3d') ax.scatter3D(x_recons[manifold_time, 0], x_recons[manifold_time, 1], x_recons[manifold_time, 2], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('dim 1') ax.set_ylabel('dim 2') ax.set_zlabel('dim 3') ax.set_title('learned data points on manifold') if manifold and manifold.manifold_type == 'torus': ax.view_init(elev=64., azim=100.) elif self.dim_data == 2: ax = fig.add_subplot(231) ax.scatter(x_true[manifold_time, 0], x_true[manifold_time, 1], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('dim 1') ax.set_ylabel('dim 2') ax.set_title('true data points on manifold') ax = fig.add_subplot(232) ax.scatter(x_recons[manifold_time, 0], x_recons[manifold_time, 1], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('dim 1') ax.set_ylabel('dim 2') ax.set_title('learned data points on manifold') latent_time = range(np.shape(batch_x)[0]) colors_latent = colors[latent_time, :] if self.dim_latent == 1: ax = fig.add_subplot(234) ax.scatter(latent_time, z_mu[latent_time, 0], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('point') ax.set_title('latent 1') ax = fig.add_subplot(235) ax.scatter(np.cos(z_mu[latent_time, 0]), np.sin(z_mu[latent_time, 0]), s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('cos(latent1)') ax.set_xlabel('sin(latent1)') ax.set_title('latent 1 in cos and sin') if self.dim_latent == 2: # remove this later (just to test) if np.shape(x_true)[1] > 2: ax = fig.add_subplot(233) ax.scatter(x_true[latent_time, 2], LA.norm(z_mu[latent_time, :], axis=1), s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('z dim (manifold)') ax.set_ylabel('r (latent)') ax.set_title('latent 1&2') ax = fig.add_subplot(234) ax.scatter(z_mu[latent_time, 0], z_mu[latent_time, 1], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('point') ax.set_title('latent 1&2') ax = fig.add_subplot(235) ax.scatter(latent_time, z_mu[latent_time, 0], s=200, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('point') ax.set_title('latent 1') ax = fig.add_subplot(236) ax.scatter(latent_time, z_mu[latent_time, 1], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('point') ax.set_title('latent 2') if self.dim_latent == 3: ax = fig.add_subplot(234) ax.scatter(z_mu[latent_time, 0], z_mu[latent_time, 1], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('point') ax.set_title('latent 1&2') ax = fig.add_subplot(235) ax.scatter(z_mu[latent_time, 0], z_mu[latent_time, 2], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('point') ax.set_title('latent 1&3') ax = fig.add_subplot(236) ax.scatter(z_mu[latent_time, 1], z_mu[latent_time, 2], s=marker_size, marker='x', color=colors_latent, linewidth=3) ax.set_xlabel('point') ax.set_title('latent 2&3') plt.pause(0.05) plt.show() pass
def _mutation_count_plots(pipeline, x_label): printlabel = 'Distance to Center of Mass' if x_label == 'distance_to_COM' else 'Sample Size' pipeline.print2('Special Plot being conducted.') df = pd.DataFrame(pipeline.stats[1:], columns=pipeline.stats[0]) radii = np.unique(df['radius'].values) plt.figure(figsize=(11, 10)) colors = itertools.cycle(cm.rainbow(np.linspace(0, 1, len(radii)))) plt.subplot(2, 2, 1) plt.title('Mean number of SNPs per cell') for i in radii: subsample = df[df['radius'] == i] plt.scatter(subsample[x_label], subsample['mean_SNPs'], color=next(colors), label='Radius=' + str(i)) plt.xlabel(printlabel) plt.ylabel('Mean SNPs per Cell') plt.subplot(2, 2, 2) plt.title('Mean number of Drivers per cell') for i in radii: subsample = df[df['radius'] == i] plt.scatter(subsample[x_label], subsample['mean_drivers'], color=next(colors), label='Radius=' + str(i)) plt.xlabel(printlabel) plt.ylabel('Mean Drivers per Cell') plt.subplot(2, 2, 3) plt.title('Number of SNPs in the sample vs Distance') for i in radii: subsample = df[df['radius'] == i] plt.scatter(subsample[x_label], subsample['num_SNPs'], color=next(colors), label='Radius=' + str(i)) plt.xlabel(printlabel) plt.ylabel('Number of SNPs') plt.subplot(2, 2, 4) plt.title('Number of drivers in the sample vs Distance') for i in radii: subsample = df[df['radius'] == i] plt.scatter(subsample[x_label], subsample['num_drivers'], color=next(colors), label='Radius=' + str(i)) plt.xlabel(printlabel) plt.ylabel('Number of Drivers') plt.savefig( pipeline.FILES['out_directory']+'/mutation_count_plots_'+x_label+'.pdf',\ bbox_inches='tight')
def GMM_classification(directory): params = [] for i in range(1): params.extend( stat_utlis.get_significant_parameters(directory, verbose=True)) params = np.unique(params) print params params = [ 'T0p0', 'Fb', 'Ap0', 'Aa0', 'Aa1', 'T1a0', 'T2a0', 'T1a1', 'T2a1' ] data = utils.load_obj(directory + 'fuj_params_data.pkl') data = pd.DataFrame(data) # filter out sample with nan in params names = data['name'] data.drop('name', axis=1, inplace=True) nan_fraction = {} for c in list(data): nan_idx = pd.isnull(data[c]) print sum(nan_idx) nan_fraction[c] = float(sum(nan_idx)) / len(data) print sorted(nan_fraction.items(), key=operator.itemgetter(1)) i = [ i for i in range(len(data)) if not any(np.isnan(v) for v in data[params][i]) ] # names = data['name'][i] for n in data.dtype.names: nan_len = len(np.isnan(data[n])) data[n][np.isnan(data[n])] = 0.0 mean = sum(data[n]) / len(data[n] - nan_len) data[n][np.isnan(data[n])] = mean # filtered[n][np.isnan(filtered[n])] = 0.0 data[n] = (data[n] - min(data[n])) / (max(data[n]) - min(data[n])) * 1 # data = filtered # np.array(filtered, dtype=data.dtype) # create output column labels = ['sa', 'su', 'an', 'fe', 'di', 'ha'] def get_label(name, labels): for i in range(len(labels)): if labels[i] in name: return i return None label = np.array([get_label(names[j], labels) for j in range(len(data))]) print len(label), len(data) data_array = data[params].view(np.float32).reshape(data.shape + (-1, )) print data_array.shape # data = append_fields(data, 'label', label) iris = datasets.load_iris() # Break up the dataset into non-overlapping training (75%) and testing # (25%) sets. skf = StratifiedKFold(label, n_folds=4, shuffle=True) # Only take the first fold. train_index, test_index = next(iter(skf)) X_train = data_array[train_index] y_train = label[train_index] X_test = data_array[test_index] y_test = label[test_index] n_classes = len(np.unique(y_train)) # Try GMMs using different types of covariances. classifiers = dict((covar_type, GMM(n_components=n_classes, covariance_type=covar_type, init_params='wc', n_iter=20)) for covar_type in ['tied', 'full']) n_classifiers = len(classifiers) # plt.figure(figsize=(3 * n_classifiers / 2, 6)) # plt.subplots_adjust(bottom=.01, top=0.95, hspace=.15, wspace=.05, # left=.01, right=.99) X_train[y_train == 0].mean() for index, (name, classifier) in enumerate(classifiers.items()): # Since we have class labels for the training data, we can # initialize the GMM parameters in a supervised manner. classifier.means_ = np.array( [X_train[y_train == i].mean(axis=0) for i in xrange(n_classes)]) # Train the other parameters using the EM algorithm. classifier.fit(X_train) h = plt.subplot(1, n_classifiers, index + 1) make_ellipses(classifier, h) for n, color in enumerate(cm.rainbow(np.linspace(0, 1, len(labels)))): data = X_train[y_train == n] plt.scatter(data[:, 0], data[:, 1], 0.8, color=color, label=labels[n]) # Plot the test data with crosses for n, color in enumerate(cm.rainbow(np.linspace(0, 1, len(labels)))): data = X_test[y_test == n] plt.plot(data[:, 0], data[:, 1], 'x', color=color) y_train_pred = classifier.predict(X_train) train_accuracy = np.mean(y_train_pred.ravel() == y_train.ravel()) * 100 plt.text(0.05, 0.9, 'Train accuracy: %.1f' % train_accuracy, transform=h.transAxes) y_test_pred = classifier.predict(X_test) test_accuracy = np.mean(y_test_pred.ravel() == y_test.ravel()) * 100 plt.text(0.05, 0.8, 'Test accuracy: %.1f' % test_accuracy, transform=h.transAxes) plt.xticks(()) plt.yticks(()) plt.title(name) plt.legend(loc='lower right', prop=dict(size=12)) plt.show() return
def plot(self, run_configurations, axes, cuda, compare): def get_function_prefix(compute_type): if '32_r' in compute_type: return 's' elif '64_r' in compute_type: return 'd' elif '32_c' in compute_type: return 'c' elif '64_c' in compute_type: return 'z' elif 'bf16_r' in compute_type: return 'bf' elif 'f16_r' in compute_type: return 'h' else: print('Error - Cannot detect precision preFix: ' + compute_type) num_argument_sets = len(self.argument_sets) if num_argument_sets == 0: return sorted_argument_sets = self.sort_argument_sets( isolate_keys=[]) # No sort applied, but labels provided argument_diff = cr.ArgumentSetDifference( self.argument_sets, ignore_keys=self._get_sweep_keys()) differences = argument_diff.get_differences() test = [] xLabel = [] for key in differences: xLabel.append(key) for argument_set_hash, argument_sets in sorted_argument_sets.items(): argument_set = argument_sets[0] precision = argument_set.get("compute_type").get_value() function = argument_set.get("function").get_value() for key in differences: argument = argument_set.get(key) test.append( argument.get_value() if argument.is_set() else 'DEFAULT') break grouped_run_configurations = run_configurations.group_by_label() num_groups = len(grouped_run_configurations) metric_labels = [ key for key in self.argument_sets[0].collect_timing( run_configurations[0]) ] num_metrics = len(metric_labels) if num_metrics == 0: return # loop over independent outputs y_scatter_by_group = OrderedDict() for group_label, run_configuration_group in grouped_run_configurations.items( ): # x_scatter_by_group[group_label] = [] y_scatter_by_group[group_label] = [] # loop over argument sets that differ other than the swept variable(s) for subset_label, partial_argument_sets in sorted_argument_sets.items( ): if len(partial_argument_sets) != 1: raise ValueError( 'Assumed that sorting argument sets with no keys has a single element per sort.' ) argument_set = partial_argument_sets[0] y_list_by_metric = OrderedDict( ) # One array of y values for each metric # loop over number of coarse grain runs and concatenate results for run_configuration in run_configuration_group: results = argument_set.collect_timing(run_configuration) for metric_label in results: if not metric_label in y_list_by_metric: y_list_by_metric[metric_label] = [] y_list_by_metric[metric_label].extend( results[metric_label]) # For each metric, add a set of bars in the bar chart. for metric_label, y_list in y_list_by_metric.items(): y_scatter_by_group[group_label].extend(sorted(y_list)) for group_label, run_configuration_group in grouped_run_configurations.items( ): for run_configuration in run_configuration_group: mhz_str = "Mhz" mem_clk_str = "mclk" sys_clk_str = "sclk" mclk = run_configuration.load_specifications()['Card0'][ "Start " + mem_clk_str].split(mhz_str)[0] sclk = run_configuration.load_specifications()['Card0'][ "Start " + sys_clk_str].split(mhz_str)[0] theoMax = 0 precisionBits = int(re.search(r'\d+', precision).group()) if (function == 'gemm' and precisionBits == 32): #xdlops theoMax = float( sclk ) / 1000.00 * 256 * 120 #scaling to appropriate precision elif ( function == 'trsm' or function == 'gemm' ): #TODO better logic to decide memory bound vs compute bound theoMax = float( sclk ) / 1000.00 * 128 * 120 * 32.00 / precisionBits #scaling to appropriate precision elif self.flops and self.mem: try: n = 100000 m = 100000 flops = eval(self.flops) mem = eval(self.mem) theoMax = float(mclk) / float(eval(self.mem)) * eval( self.flops) * 32 / precisionBits / 4 except: print("flops and mem equations produce errors") if theoMax: theoMax = round(theoMax) x_co = (test[0], test[len(test) - 1]) y_co = (theoMax, theoMax) axes.plot(x_co, y_co, label="Theoretical Peak Performance: " + str(theoMax) + " GFLOP/s") color = iter(cm.rainbow(np.linspace(0, 1, len(y_scatter_by_group)))) for group_label in y_scatter_by_group: c = next(color) axes.scatter( # x_bar_by_group[group_label], test, y_scatter_by_group[group_label], # gap_scalar * width, color='#000000', #c, # label = group_label, ) axes.plot( # x_scatter_by_group[group_label], test, y_scatter_by_group[group_label], # 'k*', '-ok', color='#000000', #c, label=get_function_prefix(precision) + function + ' Performance', #group_label, ) axes.xaxis.set_minor_locator(AutoMinorLocator()) axes.yaxis.set_minor_locator(AutoMinorLocator()) axes.set_ylabel(metric_labels[0] if len(metric_labels) == 1 else 'Time (s)') axes.set_xlabel('='.join(xLabel)) return True
pd_reads.to_excel('data-histograms.xlsx') # ## Figure S3 histograms # In[33]: fig, axes = plt.subplots(1, 1, figsize=(10, 5), dpi=100, sharex=True, sharey=True) kwargs = dict(alpha=0.5, bins=20, density=True, stacked=True) axis_font = {'fontname': 'Arial', 'size': '12'} colors = cm.rainbow(np.linspace(0, 1, len(pd_reads['Names']))) keys = pd_reads['Names'] i = 0 for y, c in zip(keys, colors): plt.hist(pd_reads['log10'][i], bins=20, density=True, label=pd_reads['Names'][i], stacked=True, alpha=0.5, color=c) i = i + 1 #plt.hist(pd_reads['log10'],**kwargs,label=pd_reads['Names']) plt.legend(prop={'size': 10}, loc='upper left') plt.xlabel('log(10)_Intensities', **axis_font)
def plot_train(self, iteration, iteration_plot, num_images, batch_x, plot_mode, plot_folder, plot_name, manifold): # plot_folder: path to save the plot # plot_name: name of the save plot # plot_mode: 'save' or 'show' if iteration % iteration_plot == 0: # plot prediction if manifold: colors = manifold.points_colors else: colors = cm.rainbow(np.linspace(0, 1, np.shape(batch_x)[0])) x_recons, z_mu, x_true = self.sess.run( [self.x_recons, self.latent_mu, self.x], feed_dict={ self.x: batch_x, self.is_training: True }) fig = plt.figure(figsize=[12, 9]) if self.dim_data > 2: ax = fig.add_subplot(231, projection='3d') ax.scatter3D(x_true[:, 0], x_true[:, 1], x_true[:, 2], s=100, marker='x', color=colors, linewidth=3) ax.set_xlabel('dim 1') ax.set_ylabel('dim 2') ax.set_zlabel('dim 3') ax.set_title('true data points on manifold') if self.manifold_train and self.manifold_train.manifold_type == 'torus': ax.view_init(elev=64., azim=100.) ax = fig.add_subplot(232, projection='3d') ax.scatter3D(x_recons[:, 0], x_recons[:, 1], x_recons[:, 2], s=100, marker='x', color=colors, linewidth=3) ax.set_xlabel('dim 1') ax.set_ylabel('dim 2') ax.set_zlabel('dim 3') ax.set_title('learned data points on manifold') if self.manifold_train and self.manifold_train.manifold_type == 'torus': ax.view_init(elev=64., azim=100.) elif self.dim_data == 2: ax = fig.add_subplot(231) ax.scatter(x_true[:, 0], x_true[:, 1], s=100, marker='x', color=colors, linewidth=3) ax.set_xlabel('dim 1') ax.set_ylabel('dim 2') ax.set_title('true data points on manifold') ax = fig.add_subplot(232) ax.scatter(x_recons[:, 0], x_recons[:, 1], s=100, marker='x', color=colors, linewidth=3) ax.set_xlabel('dim 1') ax.set_ylabel('dim 2') ax.set_title('learned data points on manifold') ax = fig.add_subplot(234) ax.plot(range(np.shape(z_mu)[0]), z_mu[:, 0]) ax.set_xlabel('point') ax.set_title('latent 1') if self.dim_latent == 1: ax = fig.add_subplot(235) ax.scatter(np.cos(z_mu), np.sin(z_mu), color=colors) ax.set_xlabel('point') ax.set_title('latent sin and cosine') if self.dim_latent > 1: ax = fig.add_subplot(235) ax.plot(range(np.shape(z_mu)[0]), z_mu[:, 1]) ax.set_xlabel('point') ax.set_title('latent 2') if self.dim_latent > 2: ax = fig.add_subplot(236) ax.plot(range(np.shape(z_mu)[0]), z_mu[:, 2]) ax.set_xlabel('point') ax.set_title('latent 3') if plot_mode is 'show': plt.pause(0.05) plt.show() if plot_mode is 'save': if not os.path.exists(plot_folder): os.makedirs(plot_folder) print('This directory was created: {}'.format(plot_folder)) plt.savefig('{}/{}'.format(plot_folder, plot_name)) plt.close('all') pass
#print type(data) # Define max_distance (eps parameter in DBSCAN()) max_distance = 175 #max_distance = 1000 db = DBSCAN(eps=max_distance, min_samples=10).fit(data) # Extract a mask of core cluster members core_samples_mask = np.zeros_like(db.labels_, dtype=bool) core_samples_mask[db.core_sample_indices_] = True # Extract labels (-1 is used for outliers) labels = db.labels_ n_clusters = len(set(labels)) - (1 if -1 in labels else 0) unique_labels = set(labels) #print unique_labels # Plot up the results! # The following is just a fancy way of plotting core, edge and outliers colors = cm.rainbow(np.linspace(0, 1, len(unique_labels))) for k, col in zip(unique_labels, colors): #ax.clear() if k == -1: # Black used for noise. #col = [0, 0, 0, 1] col =(0,0,0,1) class_member_mask = (labels == k) xy2 = data[class_member_mask & core_samples_mask] #print xy2 #return xy2 #print test #print tuple(col)
def evaluate_wavefront_performance(N_zern, test_coef, guessed_coef, zern_list, show_predic=False): """ Evaluates the performance of the ML method regarding the final RMS wavefront error. Compares the initial RMS NCPA and the residual after correction """ # Transform the ordering to match the Zernike matrix new_test_coef = transform_zemax_to_noll(test_coef) new_guessed_coef = transform_zemax_to_noll(guessed_coef) x = np.linspace(-1, 1, 512, endpoint=True) xx, yy = np.meshgrid(x, x) rho, theta = np.sqrt(xx**2 + yy**2), np.arctan2(xx, yy) pupil = rho <= 1.0 rho, theta = rho[pupil], theta[pupil] zernike = zern.ZernikeNaive(mask=pupil) _phase = zernike(coef=np.zeros(new_test_coef.shape[1] + 3), rho=rho, theta=theta, normalize_noll=False, mode='Jacobi', print_option='Silent') H_flat = zernike.model_matrix[:, 3:] # remove the piston and tilts H_matrix = zern.invert_model_matrix(H_flat, pupil) # Elliptical mask ellip_mask = (xx / 0.5)**2 + (yy / 1.)**2 <= 1.0 H_flat = H_matrix[ellip_mask] # print(H_flat.shape) N = test_coef.shape[0] initial_rms = np.zeros(N) residual_rms = np.zeros(N) for k in range(N): phase = np.dot(H_flat, new_test_coef[k]) residual_phase = phase - np.dot(H_flat, new_guessed_coef[k]) before, after = np.std(phase), np.std(residual_phase) initial_rms[k] = before residual_rms[k] = after average_initial_rms = np.mean(initial_rms) average_residual_rms = np.mean(residual_rms) improvement = (average_initial_rms - average_residual_rms) / average_initial_rms * 100 print('\nWAVEFRONT PERFORMANCE DATA') print('\nNumber of samples in TEST dataset: %d' % N) print('Average INITIAL RMS: %.3f waves (%.1f nm @1.5um)' % (average_initial_rms, average_initial_rms * wave_nom)) print('Average RESIDUAL RMS: %.3f waves (%.1f nm @1.5um)' % (average_residual_rms, average_residual_rms * wave_nom)) print('Improvement: %.2f percent' % improvement) if show_predic == True: plt.figure() plt.scatter(range(N), initial_rms * wave_nom, c='blue', s=6, label='Initial') plt.scatter(range(N), residual_rms * wave_nom, c='red', s=6, label='Residual') plt.xlabel('Test PSF') plt.xlim([0, N]) plt.ylim(bottom=0) plt.ylabel('RMS wavefront [nm]') plt.title(r'$\lambda=1.5$ $\mu$m (defocus: 0.20 waves)') plt.legend() for k in range(N_zern): guess = guessed_coef[:, k] coef = test_coef[:, k] colors = wave_nom * residual_rms colors -= colors.min() colors /= colors.max() colors = cm.rainbow(colors) plt.figure() ss = plt.scatter(coef, guess, c=colors, s=20) x = np.linspace(-0.15, 0.15, 10) # plt.colorbar(ss) plt.plot(x, x, color='black', linestyle='--') title = zern_list[k] plt.title(title) plt.xlabel('True Value [waves]') plt.ylabel('Predicted Value [waves]') plt.xlim([-0.15, 0.15]) plt.ylim([-0.15, 0.15]) return initial_rms, residual_rms
def plot_model_2d_classification(estimator, X, y, ax=None, xlim=None, ylim=None, title=None, new_window=True, levels=None, s=30): plt.style.use('seaborn') if isinstance(X, np.ndarray): labels = ['X' + str(i) for i in range(X.shape[1])] else: labels = X.columns X = X.values() if new_window: plt.figure() if ax is None: ax = plt.axes() if ylim: ax.set_ylim(ylim[0], ylim[1]) if xlim: ax.set_xlim(xlim[0], xlim[1]) if xlim and ylim: xx, yy = np.meshgrid(np.linspace(xlim[0], xlim[1], 500), np.linspace(ylim[0], ylim[1], 500)) else: x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), np.arange(y_min, y_max, 0.1)) Z = estimator.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) if levels: ax.contour(xx, yy, Z, levels=levels, linewidths=2, colors='red', alpha=1) else: ax.contourf(xx, yy, Z, cmap=plt.cm.Pastel1, alpha=1) n_classes = set(y) colors = cm.rainbow(np.linspace(0, 1, len(n_classes))) class_labels = [str(i) for i in n_classes] for i, color in zip(n_classes, colors): idx = np.where(y == i) ax.scatter(X[idx, 0], X[idx, 1], c=color, label=class_labels[i], cmap=plt.cm.coolwarm, edgecolor='black', s=s) ax.set_xlabel(labels[0]) ax.set_ylabel(labels[1]) ax.set_title(title) ax.legend() plt.tight_layout() return ax
from time import sleep from mobility import * import matplotlib.pyplot as plt from matplotlib import animation import matplotlib.cm as cm import itertools import numpy as np x = np.arange(5) ys = [i + x + (i * x)**3 for i in range(10)] colors = iter(cm.rainbow(np.linspace(0, 1, len(ys)))) colors = itertools.cycle(["red", "black", "black", "black", "black"]) height = 500 width = 500 grupos = 2 # gm = gauss_markov(20, dimensions=(height, width)) #gm = reference_point_group(50, dimensions=(height, width), aggregation=0.8) #gm2 = reference_point_group(50, dimensions=(height, width), aggregation=0.47) #gm3 = reference_point_group(10, dimensions=(height, width), aggregation=0.49) #gm4 = reference_point_group(10, dimensions=(height, width), aggregation=0.45) #gm5 = reference_point_group(10, dimensions=(height, width), aggregation=0.45) gm = tvc([10, 5, 5, 5, 5, 5], dimensions=(height, width), velocity=(0.1, 1.), aggregation=[1, 0.5], epoch=[100, 100], safepoint=[410, 300]) gm2 = tvc(10, dimensions=(height, width),