Beispiel #1
0
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
Beispiel #2
0
	def binarize(self, dir):
		self.templates = [0] * len(self.letters)
		print self.templates
		plt.figure()
		for i in range(0, len(self.letters))
			img = misc.imread(self.img_dir + i + ".png"

cr = CharRecog()
cr.binarize()
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()
Beispiel #4
0
def plot_animation:
    fig = plt.figure(figsize=(5, 3.5))

    ax = fig.add_subplot(1, 1, 1)
    fig.subplots_adjust(bottom=0.15)

    objs = []

    bx = np.arange(x.iloc[:, 0].min(), x.iloc[:, 0].max(), 0.1)
Beispiel #5
0
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()
Beispiel #6
0
 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()
Beispiel #7
0
 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()
Beispiel #8
0
def generate_plot(platforms, output_file):
    """ Generates a bar chart out of the given platforms and writes the
	output into the specified file as PNG image.
	"""
    # First off we need to convert the platforms in a format that can be
    # attached to the 2 axis of our bar chart. "labels" will become the
    # x-axis and "values" the value of each label on the y-axis:
    labels = []
    values = []
    for platform in platforms:
        name = platform['name']
        adapted_price = platform['adjusted_price']
        price = platform['original_price']
        # skip prices higher than 2000 USD simply because it would make the
        # output unsuable.
        if price > 2000:
            continue

        # If the name of the platform is too long, replace it with the
        # abbreviation. list.insert(0,val) inserts val at the beginning of
        # the list.
        if len(name) > 15:
            name = platform['abbreviation']
        labels.insert(
            0, u"{0}\n$ {1}\n$ {2}".format(name, price,
                                           round(adjusted_price, 2)))
        values.insert(0, adapted_price)

        # Let's define the width of each bar and the size of the resulting graph.
        width = 0.3
        ind = np.arange(len(values))
        fig = plt.figure(figsize=(len(labels) * 1.8, 10))

        # Generate a subplot and put our values onto it.
        ax = fig.add_subplot(1, 1, 1)
        ax.bar(ind, values, width, align='center')

        # Format the x and Y axis labels. Also set the ticks on the x-axis slightly
        # farther apart and give them a slight tilting effect.
        plt.ylabel('Adjusted price')
        plt.xlabel('Year/ Console')
        ax.set_xticks(ind + 0.3)
        ax.set_xtickslabels(labels)
        fig.autofmt_xdate()
        plt.grid(True)
        plt.savefig(output_file, dpi=72)
for i in range(len(unique_province)):
    if type(unique_provices[i]) == float:
        nan_indices.append(i)
        
        
unique_provinces = list(unique_provinces)
province_confirmed_cases = list(province_confirmed_cases)

for i in nan_indices:
    unique_provices.pop(i)
    province_confirmed_cases.pop(i)     
    
    
# Plot a bar graph to see the total confirmed cases across different countries

plt.figure(figsize=(32,32))
plt.barh(unique_countries, country_confirmed_cases)
plt.title('Number of Covid-19 Confirmed Cases in Countries')
plt.xlabel('Number of Covid Confirmed Caese')
plt.show()

# Plot a bar graph to see the total confirmed cases b/w mainland china and outside mainland china

china_confirmed = latest_confirmed[confirmed_cases['Country/Region']=='China'].sum()
outside_mainland_china_confirmed = np.sum(country_confirmed_cases)-china_confirmed
plt.figure(figsize=(16, 9))
plt.barh('Mainland China',china_confirmed)
plt.barh('Outside Mainland China',outside_mainland_china_confirmed)
plt.title('Number of Confirmed Coronavirus cases')
plt.show()
from pylab import *
import matplotlib.pylot as plt

# budget, fear factor and external factor constraints
b_x, b_y = 8.0, 10.0
c_x, c_y = (x/b_x), (y/b_y)
f_x, f_y = (1.0-y/x), (1.0-y/x)
e_x, e_y = 0.75, 0.9

x, y = meshgrid(arrange(1, 4, 0.1), arrange(1, 4, 0.1))
xdot = f_x*y - c_x*x + e_x
ydot = f_y*x - c_y*y + e_y

plt.figure(figsize=(10, 10))
plt.title('Phase Plot: Aggressive vs. Aggressive', fontsize = 28)
streamplot(x, y, xdot, ydot)

show()
Beispiel #11
0
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()
Beispiel #12
0
# -*- coding: utf-8 -*
import pandas as pd
#倒入路径
catering_sale = '../data/catering_sale.xls'
#倒入Excel
data = pd.read_excel(catering_sale, index_col = u'日期')

import matplotlib.pylot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

plt.figure()
p = data.boxplot()
x = p['fliers'][0].get_xdata()
y = p['fliers'][0].get_ydata()
y.sort()
for i in range(len(x)):
    if i>0:
        plt.annotate(y[i], xy = (x[i],y[i]), xytext=(x[i]+0.05 - 0.8/(y[i]-y[i-1])))
    else:
        plt.annotate(y[i], xy= (x[i],y[i]), xytext=(x[i]+0.08, y[i]))
plt.show()
Beispiel #13
0
#Rafael Almeida

# K-MEANS

import pandas as pd
import numpy as np
import matplotlib.pylot as plt
%matplotlib inline

df = pd.DataFrame({
    'x': [12, 20, 28, 18, 29, 33, 24, 45, 45, 52, 51, 52, 55, 53, 55, 61, 64, 69, 72],
    'y': [39, 36, 30, 52, 54, 46, 55, 59, 63, 70, 66, 63, 58, 23, 14, 8, 19, 7, 24]
    })

np.random.seed(200)
k = 3

# centroids[i] = [x,y]
centroids = { 
    i +1 [np.random.randint(0, 80), np.random.randint(0, 80)]
    for i in range (k)
}

fig = plt.figure(figsize = (5,5))
plt.scatter(df['x'], df['y'], color= 'k')
colmap = {1: 'r', 2: 'g', 3: 'b'}
for i in centroids.keys():
    plt.scatter(*centroids[i], color=colmap[i])
plt.xlim(0, 80)
plt.ylim(0, 80)
plt.show()
Beispiel #14
0
pd.options.display.max_columns = None
data2.groupby([clusterNos]).mean()
plt.scatter(data2.ApplicantIncome, data2.LoanAmount, c=clusterNos)
plt.scatter(data2.ApplicantIncome, data2.Credit_History,
            c=clusterNos)  #better distinction
plt.scatter(data2.ApplicantIncome, data2.Loan_Amount_Term,
            c=clusterNos)  #better distinction

#Now use this information ;
#which customers you would like to target.

#hierarchical clustering
import scipy.cluster.hierarchy as shc
dend = shc.dendrogram(shc.linkage(data2_scaled, method='ward'))

plt.figure(figsize=(10, 7))
plt.title("Dendrogram")
dend = shc.dendrogram(shc.linkage(data2_scaled, method='ward'))
plt.axhline(y=6, color='r', linestyle='--')
plt.show()

#another method for Hcluster from sklearn
from sklearn.cluster import AgglomerativeClustering
aggCluster = AgglomerativeClustering(n_clusters=2,
                                     affinity='euclidean',
                                     linkage='ward')
aggCluster.fit_predict(data2_scaled)
aggCluster
aggCluster.labels_

#compare
Beispiel #15
0
# Get heartrate from FFT
max_val = 0
max_index = 0
for index, fft_val in enumerate(red_fft):
    if fft_val > max_val:
        max_val = fft_val
        max_index = index

heartrate = freqs[max_index] * 60        
print('Estimated Heartate: {} bpm'.format(heartrate))


# Plotting
if PLOT:
    plt.figure(figsize=(16,9))
    plt.plot(x, colors['red'], color='#fc4f30')
    plt.xlabel('Time [s]')
    plt.ylabel('Normalized Pixel Color')
    plt.title('Time-Series Red Channel Pixel Data')
    fig1 = plt.gcf()
    plt.show()
    if SAVE:
        plt.draw()
        fig1.savefig('./{}_time_series.png'.format(filename), dpi=200)
    
    # Plot the highpass data
    plt.figure(figsize=(16,9))
    plt.plot(x_filt, colors['red_filt'], color='#fc4f30')
    plt.xlabel('Time [s]')
    plt.ylabel('Normalized Pixel Color')
Beispiel #16
0
# initialize time and x and y expenditure at initial time
t_0 = 0
init_data = np.array([14, 5])

# starting RK45 integration method
sys_1 = integrate.RK45(model, t_0, init_data, 1000, 0.001)

# storing initial data
sol_x = [sys_1.y[0]]
sol_y = [sys_1.y[1]]
time = [t_0]

for i in range(5000):
    sys_1.step()  # performing integration step
    sol_x.append(
        sys_1.y[0]
    )  # storing the results in our solution list, y is the attribute current state
    sol_y.append(sys_1.y[1])
    time.append(sys_1.t)

plt.figure(figsize=(20, 10))

# plotting results in a graph
plt.plot(time, sol_x, 'b--', label='Country A')
plt.plot(time, sol_y, 'r--', label='Country B')
plt.ylabel('Military Expenditure (billions USD)', fontsize=16)
plt.xlabel('Time (years)', fontsize=16)
plt.legend(loc='best', fontsize=22)
plt.title('Simple Arms Race: Aggressive vs. Passive', fontsize=28)
plt.show()
Beispiel #17
0
import numpy as np
import matplotlib.pylot as plt
import pywt
import pywt.data
from PIL import Image

# Load image
original = pywt.data.camera()

# Wavelet transform of image, and plot approximation and details
titles = [
    'Approximation', ' Horizontal detail', 'Vertical detail', 'Diagonal detail'
]
coeffs2 = pywt.dwt2(original, 'haar')
LL, (LH, HL, HH) = coeffs2
fig = plt.figure(figsize=(12, 3))
for i, a in enumerate([LL, LH, HL, HH]):
    ax = fig.add_subplot(1, 4, i + 1)
    ax.imshow(a, interpolation="nearest", cmap=plt.cm.gray)
    ax.set_title(titles[i], fontsize=10)
    ax.set_xticks([])
    ax.set_yticks([])

fig.tight_layout()
plt.show()
    BlackHawk,
    temperature_to_mass,
    MASS_CONVERSION,
)
import matplotlib.pylot as plt
import numpy as np


def spec_geom(E, T, M):
    gam = 27 * G_NEWTON**2 * M**2 * E**2
    boltz = 1.0 / (np.exp(E / T) - 1.0)
    return gam * boltz / (2.0 * np.pi)


if __name__ == "__main__":
    mpbh_gram = 1.5e18
    mpbh_gev = mpbh_gram * MASS_CONVERSION
    blackhawk = BlackHawk(mpbh_gram)
    blackhawk.run()

    engs = blackhawk.primary["energies"]
    spec = blackhawk.primary["photon"]

    T = temperature_to_mass(mpbh_gev)
    spec2 = spec_geom(engs, T, mpbh_gev)

    plt.figure(dpi=150)
    plt.plot(engs, spec)
    plt.plot(engs, spec2)
    plt.show()
Beispiel #19
0
from sklearn.metrics import mean_squared_error, r2_score

# Get reference values
reference_data = pd.DataFrame.as_matrix(data['Ref AC'])
Y_calib = reference_data[:xxx]
Y_valid = reference_data[xxx:]
 
# Get spectra
X_calib = pd.DataFrame.as_matrix(data.iloc[:422, 2:])
X_valid = pd.DataFrame.as_matrix(data.iloc[423:, 2:])
 
# Get wavelengths (They are in the first line which is considered a header from pandas)
wl = np.array(list(data)[2:])
    
# Plot spectra
plt.figure(figsize=(8,4.5))
with plt.style.context(('ggplot')):
    plt.plot(wl, X_calib.T)
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Absorbance')    
plt.show()

# Calculate derivatives
X2_calib = savgol_filter(X_calib, 17, polyorder = 2,deriv=2)
X2_valid = savgol_filter(X_valid, 17, polyorder = 2,deriv=2)
 
# Plot second derivative
plt.figure(figsize=(8,4.5))
with plt.style.context(('ggplot')):
    plt.plot(wl, X2_calib.T)
    plt.xlabel('Wavelength (nm)')
Beispiel #20
0
print(randomArray(20))
import matplotlib.pylot as plt

maxN = 100
for n in range(1, maxN + 1):
    setup = '''
from __main__ import randomArray, buildinSort
array = randomArray({})
    '''.format(n)

    prog = '''
buildinSort(array)
    '''
    t = timeit.Timer(prog, setup)
    print(f'For an array of the length {n}: {t.timeit(1000) / 1000}')
    fig = plt.figure()
    plt.plot(range(1, maxN + 1), t, label="buildin")
    a = 1e-7
    plt.plot(range(1, maxN + 1), [a*n for n in range(1, maxN + 1)], label="a*n")
    plt.legend()
    plt.show 


#matplotlib 




#zeitmessung