def draw_box(heights): #创建箱形图 #第一个参数为待绘制的定量数据 #第二个参数为数据的文字说明 plt.boxplot([heights], labels=['Heights']) plt.title('Heights Of Male Students') plt.show()
def plot_graphs(history, string): plt.plot(history.history[string]) plt.plot(history.history['val_' + string]) plt.xlabel("Epochs") plt.ylabel(string) plt.legend([string, 'val_' + string]) plt.show()
def predict_prices(dates, prices, x): dates = np.reshape(dates, (len(dates), 1)) svr_len = SVR(kernel='linear', C=1e3) svr_poly = SVR(kernel='poly', C=1e3, degree=2) svr_rbf = SVR(kernel='rbf', C=1e3, gamma=0.1) svr_lin.fit(dates, prices) svr_poly.fit(dates, prices) svr_rbf.fit(dates, prices) plt.scatter(dates, prices, color='black', label='data') plt.plot(dates, svr_rbf.predict(dates), color='red', label='RBF model') plt.plot(dates, svr_lin.predict(dates), color='green', label='Linear model') plt.plot(dates, svr_poly.predict(dates), color='blue', label='Polynomial model') plt.xlabel('Date') plt.ylabel('Price') plt.title('Sipport Vector Regression') plt.legend() plt.show() return svr_rbf.predict(x)[0], svr_lin.predict(x)[0], svr_poly.predict(x)[0]
def draw_bar(grades): xticks = ['A', 'B', 'C', 'D', 'E'] gradeGroup = {} #对每一类成绩进行频数统计 for grade in grades: gradeGroup[grade] = gradeGroup.get(grade, 0) + 1 #创建柱状图 #第一个参数为柱的横坐标 #第二个参数为柱的高度 #参数align为柱的对齐方式,以第一个参数为参考标准 plt.bar(range(5), [gradeGroup.get(xtick, 0) for xtick in xticks], align='center') #设置柱的文字说明 #第一个参数为文字说明的横坐标 #第二个参数为文字说明的内容 plt.xticks(range(5), xticks) #设置横坐标的文字说明 plt.xlabel('Grade') #设置纵坐标的文字说明 plt.ylabel('Frequency') #设置标题 plt.title('Grades Of Male Students') #绘图 plt.show()
def GenerateOutcomes(x, z, num_cont, num_bin): """ Following the generating procedure defined by Madras in Algorithm 2 """ # As defined by Madras num_z = z.shape[1] w = -11 beta_a = 6 # Algorithm 2 # horizontal concatenation xz = np.concatenate((x, z), 1) W = np.ones(xz.shape[1])*.5 # lists to store generated values y_t0_a0, y_t1_a0, y_t0_a1, y_t1_a1 = list(), list(), list(), list() mu_t0_a0, mu_t1_a0, mu_t0_a1, mu_t1_a1 = list(), list(), list(), list() # loop over observations because all need individual beta sample for obs in xz: # sample new beta beta_cont = choice([0, .1, .2, .3, .4], num_cont, p=[.5, .125, .125, .125, .125]) beta_bin = choice([0, .1, .2, .3, .4], num_bin, p=[.6, .1, .1, .1, .1]) beta_z = choice([.4, .6], num_z, p=[.5, .5]) # in x, continuous variables come first beta = np.concatenate((beta_cont, beta_bin, beta_z), 0) # calculate y dist mu1 = np.matmul(np.exp(obs + W), beta) mu_t0_a0.append(mu1) mu2 = np.matmul(obs, beta)-w mu_t1_a0.append(mu2) mu3 = np.matmul(np.exp(obs + W), beta) + beta_a mu_t0_a1.append(mu3) mu4 = np.matmul(obs, beta) - w + beta_a mu_t1_a1.append(mu4) # sample new y y_t0_a0.append(np.random.normal(mu1, 1, 1)[0]) y_t1_a0.append(np.random.normal(mu2, 1, 1)[0]) y_t0_a1.append(np.random.normal(mu3, 1, 1)[0]) y_t1_a1.append(np.random.normal(mu4, 1, 1)[0]) plt_entries = {'y_t0_a0': y_t0_a0, 'y_t1_a0': y_t1_a0, 'y_t0_a1': y_t0_a1, 'y_t1_a1': y_t1_a1} plt.figure() plt.title('Generated data') for label, entry in plt_entries.items(): plt.hist(entry, label=label, alpha=0.5, bins=20) plt.legend() plt.show() y_all = np.transpose(np.vstack((y_t0_a0, y_t1_a0, y_t0_a1, y_t1_a1))) mu_all = np.transpose(np.vstack((mu_t0_a0, mu_t1_a0, mu_t0_a1, mu_t1_a1))) # column names should be consistent with above vstack y_column = 'y_t0_a0, y_t1_a0, y_t0_a1, y_t1_a1' mu_column = 'mu_t0_a0, mu_t1_a0, mu_t0_a1, mu_t1_a1' return y_all, mu_all, y_column, mu_column
def show_train_history(train_history, train, validation): plt.plot(train_history, history[train]) plt.plot(train_history, history[validation]) plt.title('Train History') plt.ylabel(train) plt.xlabel('Epoch') plt.legend(['train', 'validation'], loc='upper left') plt.show()
def draw_hist(heights): #创建直方图 #第一个参数为待绘制的定量数据,不同于定性数据,这里并没有事先进行频数统计 #第二个参数为划分的区间个数 plt.hist(heights, 100) plt.xlabel('Heights') plt.ylabel('Frequency') plt.title('Heights Of Male Students') plt.show()
def plotImpSurface(A,B,Z): fig=pl.figure() ax=fig.gca(projection='3d') suf=ax.plot_surface(A,B,Z,rstride=1,cstride=1,cmap=cm.coolwarm,linewidth=0,antialiased=False) ax.zaxis=set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) fig.colorbar(surf,shrink=0.5,aspect=5) pl.show()
def draw_scatter(heights, weights): #创建散点图 #第一个参数为点的横坐标 #第二个参数为点的纵坐标 plt.scatter(heights, weights) plt.xlabel('Heights') plt.ylabel('Weights') plt.title('Heights & Weights Of Male Students') plt.show()
def newMaze(): return [[1, 1, 1, 1, 1, 0, 0, 1, 1, 1], [0, 1, 1, 1, 1, 1, 0, 1, 0, 1], [0, 0, 1, 0, 1, 1, 1, 0, 0, 1], [1, 0, 1, 1, 1, 0, 1, 1, 0, 1], [0, 0, 0, 1, 0, 0, 0, 1, 0, 1], [1, 0, 1, 1, 1, 0, 0, 1, 1, 0], [0, 0, 0, 0, 1, 0, 0, 1, 0, 1], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 0, 0, 1, 1, 1], [0, 0, 1, 0, 0, 1, 1, 0, 0, 1]] laberinto = newMaze() plt.imshow(laberinto) plt.show()
def draw_pie(grades): labels = ['A', 'B', 'C', 'D', 'E'] gradeGroup = {} for grade in grades: gradeGroup[grade] = gradeGroup.get(grade, 0) + 1 #创建饼形图 #第一个参数为扇形的面积 #labels参数为扇形的说明文字 #autopct参数为扇形占比的显示格式 plt.pie([gradeGroup.get(label, 0) for label in labels], labels=labels, autopct='%1.1f%%') plt.title('Grades Of Male Students') plt.show()
def draw_cumulative_hist(heights): #创建累积曲线 #第一个参数为待绘制的定量数据 #第二个参数为划分的区间个数 #normed参数为是否无量纲化 #histtype参数为'step',绘制阶梯状的曲线 #cumulative参数为是否累积 plt.hist(heights, 20, normed=True, histtype='step', cumulative=True) plt.xlabel('Heights') plt.ylabel('Frequency') plt.title('Heights Of Male Students') plt.show()
def visual_inspection(raw_signal_list, filtered_signal_list, begin_sec, end_sec): import matplotlib.pylot as plt for raw_signal, filtered_signal in zip(raw_signal_list, filtered_signal_list): plt.figure(figsize=(20, 20)) plt.plot(raw_signal.T) plt.plot(filterd_signal.T) plt.xlim(begin_sec * 1000, end_sec * 1000) plt.legend(['raw', 'filtered']) plt.show()
def plot_regression_line(x, y, b): # plotting the actual points as scatter plot plt.scatter(x, y, color="m", marker="o", s=30) # predict response vector y_pred = b[0] + b[1] * x # plotting the regression line plt.plot(x, y_pred, color="g") # putting labels plt.xlabel('x') plt.ylabel('y') # function to show plot plt.show()
def plot_hull(self, show_points=False): """ Function that plots the boundaries of a convex hull using matplotlib.pyplot. Input hull must be of type: scipy.spatial.qhull.ConvexHull points input must be of the original coordinates. """ hull = self.convex_hull(self.dots) plt.figure() for simplex in hull.simplices: plt.plot(self.dots[simplex,0], \ self.dots[simplex,1], 'k-') if show_points: plt.scatter(self.dots[:,0], \ self.dots[:,1], s=10,c='g') plt.scatter(self.dots[:,0], \ self.dots[:,1], s=30,c='orange') plt.show()
import matplotlib.pylot as plt years = [ 1950, 1995, 1960, 1965, 1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010, 2015 ] pops = [2.5, 2.7, 3, 3.3, 3.6, 4, 4.4, 4.8, 5.3, 5.7, 6.1, 6.5, 6.9, 7.3] death = [1.2, 1.7, 1.8, 2.2, 2.5, 2.7, 2.9, 3, 3.1, 3.3, 3.5, 3.8, 4.0, 4.3] ''' plt.plot(years, pops,'---', color=(255/255, 100/255, 100/255)) plt.plot(years, death, color=(.6, .6, .1)) ''' lines = plt.plot(years, pops, years, death) plt.grid(True) plt.setp(lines, color=(1, .4, .4), marker='o') plt.ylabel("Population in Billions") plt.xlabel("Population growth by Year") plt.title("Population Growth") plt.show()
def dpa_setup(ser): ser = Serial("/embsec/dpa_lab/dpa_setup") datafile = h5py.File('aes_decrypt_powertraces_test_target.hdf5', 'r') datasets = datafile.keys() init = True partA_buf = [ ] # lists of traces in partition A, indexed by key candidate. Individual traces will be numpy arrays! partB_buf = [] partA_cnt = [ ] # list of number of traces in each partition, indexed by byte under examination partB_cnt = [] avg_buf = None # average of all traces avg_cnt = 0 trim = False skeycan = 0 # the index to the sub-key of the round 10 key we are examining! # The loop below iterates through all traces in the file, and performs 16 key guesses on # the key byte indexed by skeycan. So, this performs DPA for 16 key guesses of just one # byte of the (round 10) key. If you want to keep this current code structure, you will # need to manually change the for loop bounds to perform more guesses. You will also # need to change skeycan to test out other sub-key bytes. for name in datasets: # iterate through all traces in the hdf5 file print("Processing: %s" % name) ds = datafile[name] trace = np.array( ds) # this is your current trace! As **a numpy array** ciphertext_hex = ds.attrs[metaname] ciphertext = binascii.unhexlify(ciphertext_hex) # If requested, truncate the trace before analysis. # This can be used to cut out problematic noisey sections while accelerating # computation and reducing memory needs (great for key attacks) if trim: trace = trace[:trim] if init: # sets up the partition buffers initially for x in range(16): # just work on 16 possible key bytes, for now. partA_buf.append(0 * trace) # initialize all 'traces' to zero partB_buf.append(0 * trace) partA_cnt.append(0) partB_cnt.append(0) avg_buf = 0 * trace init = False for x in range( 0, 16): # just work on 16 key candidates, more is too slow. ham = hamming( ciphertext[skeycan]) # hmmm ... is this what we want? if ham > 4: partA_buf[ x] += trace # add the trace to the list of traces for that key candidate partA_cnt[ x] += 1 # increment the count for this partition and key candidate elif ham < 4: partB_buf[x] += trace partB_cnt[x] += 1 pass avg_buf += trace avg_cnt += 1 result = dict() avg_buf = avg_buf / avg_cnt result['avg trace'] = avg_buf result['trace cnt'] = avg_cnt absmax = [] for x in range(16): means = (partA_buf[x] / partA_cnt[x]) - (partB_buf[x] / partB_cnt[x]) result[x] = means absmax.append(np.max(np.abs(means))) result['absmax'] = absmax # Plot the maximum value of the absolute value of each DPA hypothesis plt.figure() plt.title("AbsMax of DPA Hypotheses (%d traces)" % result['trace cnt']) plt.plot(result['absmax']) # Plot the mean trace and all DPA Ciphertext Byte outputs plt.figure() plt.plot(result['avg trace'], label='Mean Trace') dpaPlotScale = 20 for x in range(16): plt.plot(np.abs(result[x]) * dpaPlotScale, label="CT DPA Byte %d" % x) plt.legend(loc='upper right') plt.title("Ciphertext (CT) DPA Results (%d traces)" % result['trace cnt']) plt.show() # The next couple lines are to send the key you found / get a flag (if applicable) key_answer = bytes(16) # your key you found! As a byte array ser.write(key_answer) return ser.read_until()
import matplotlib.pylot as plt #data #data = pd.read_csv('data/clustering.csv') url='https://raw.githubusercontent.com/DUanalytics/pyAnalytics/master/data/clustering.csv' data = pd.read_csv(url) data.shape data.head() data.describe() data.columns #visualise plt.scatter(data.ApplicantIncome, data.LoanAmount) plt.xlabel('Income') plt.ylabel('LoanAmt') plt.show(); #standardize data : Scaling #missing values #https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.dropna.html data.dtypes data.isnull().any() data.isnull().any(axis=1) data.index[data.isnull().any(axis=1)] data.iloc[6] data.isnull().sum().sum() #75 missing values data.isnull().sum(axis=0) #columns missing data.isnull().sum(axis=1) data1 = data.dropna()
Distribution = [] for OutcomeIndex1 in range(0, NumberFlips + 1): Distribution.append(SumTrials.count(OutcomeIndex1) / (1.0 * NumberTrials)) print(repr(Distribution)) # Print the sum of the elements in Distribution sumDistrib = 0 for item in Distribution: sumDistrib += item print(repr(sumDistrib)) OutcomeIndex2 = range(0, NumberFlips + 1) num_bins = len(OutcomeIndex2) bar_width = 0.8 XticksIndex = [(outcome + (0.5 * bar_width)) for outcome in OutcomeIndex2] opacity = 0.4 plt.bar(OutcomeIndex2, Distribution, bar_width, alpha=opacity, color='b') plt.xlabel("Value") plt.ylabel("Probability") plt.xticks(XticksIndex, OutcomeIndex2) plt.show() """ Describe what happens to the figure as you vary ParameterP from zero to one. -As ParameterP increases from zero to one, the figure shifts from left to right. What is the most likely outcome for ParameterP = 0.7 and NumberFlips = 8? -With ParameterP = 0.7 and NumberFlips = 8, the most likely outcome is 6 with 29.7% probability. """