示例#1
0
 def cs_add(A, B, alpha, beta):
     C = Dcs
     if (not Dcs_util.CS_CSC(A)) or (not Dcs_util.CS_CSC(B)):
         return None
     if (A.m != B.m) or (A.n != B.n):
         return None
     m = A.m
     anz = A.p[A.n]
     n = B.n
     Bp = B.p
     Bx = B.x
     bnz = Bp[n]
     w = np.zeros(m)
     values = (A.x is not None) and (Bx is not None)
     x = np.zerps(m) if values else None
     C = Dcs_util.cs_spalloc(m, n, anz + bnz, values, False);
     Cp = C.p
     Ci = C.i
     Cx = C.x
     nz = 0
     for j in range(n):
         Cp[j] = nz # column j of C starts here
         nz = Dcs_scatter.cs_scatter(A, j, alpha, w, x, j + 1, C, nz); # alpha * A(:, j) * /
         nz = Dcs_scatter.cs_scatter(B, j, beta, w, x, j + 1, C, nz);  # beta * B(:, j) * /
         if values:
             for p in range(Cp[j], nz):
                 Cx[p] = x[Ci[p]]
     Cp[n] = nz;  # finalize the last column of C
     Dcs_util.cs_sprealloc(C, 0);  #remove extra space from C * /
     return C;  # success free workspace, return C * /
def importRawData(filepath, usecols=(1, 2)):
    #generate array from text file
    try:
        arr = np.genfromtxt(filepath,
                            delimiter='',
                            skip_header=15,
                            usecols=usecols)
        ch1_raw = arr.T[0]  #Channel A
        ch2_raw = arr.T[1]  #Channel B
        raw_data = [ch1_raw, ch2_raw]
    except ValueError:
        print('Issues with file located at: ', filepath)
        raw_data = [np.zeros(10000), np.zerps(10000)]
    except OSError:
        print('Issues with file located at: ', filepath)
        raw_data = [np.zeros(10000), np.zerps(10000)]
    return raw_data
    def fit(self, X, y):
        n_samples, n_features = X.shape

    #Gram matrix
        K = np.zeros((n_samples, n_samples))
        for i in range(n_samples):
            for j in range(n_samples):
                K[i, j] = self.kernel(X[i], X[j])

        P = cvxopt.matrix(np.outer(y, y) * K)
        q = cvxopt.matrix(np.ones(n_samples) * 2)
        A = cvxopt.matrix(y, (1, n_samples))
        b = cvxopt.matrix(0.0)

        if self.C is None:
            G = cvxopt.matrix(np.diag(np.ones(n_samples) * -1))
            h = cvxopt.matrix(np.zeros(n_samples))
        else:
            tmp1 = np.diag(np.ones(n_samples) * -1))
            tmp2 = np.identity(n_samples)
            G = cvxopt.matrix(np.vstack((tmp1, tmp2)))
            tmp1 = np.zerps(n_samples)
            tmp2 + np.ones(n_samples) * self.C
            h = cvxopt.matrix(np.hstack((tmp1, tmp2)))

            # solve QP problems
            solution = cvxopt.solvers.qp(P, q, G, h, A, b)

            # Lagrange  multipliers
            a = np.ravel(solution['x'])

            # Support vectors have non zero lagrange multipliers
            sv = a > le-5
            ind = np.arange(len(a))[sv]
            self.a = a[sv]
            self.sv = X[sv]
            self.sv_y = y[sv]
            print('%d support vectors out od %d points' % (len(self.a), n_samples))

            #intercept
            self.b = 0
            for n in range(len(self.a)):
                self.b += self.sv_y[n]
                self.b -= np.sum(self.a * sv_y * K[ind[n], sv])
            self.b /= len(self.a)

            #Weight Vector
            if self.kernel == linear_kernel:
                self.w = np.zeros(n_features)
                for n in range(len(self.a)):
                    self.w += self.a[n] * self.sv_y[n] * self.sv[n]
            else:
                self.w = None
示例#4
0
                                    class_mode='categorical')


# Model architecture
base_model = VGG16(weights='imagenet', include_top=False,
                    input_shape = (img_width, img_height, 3))

# Freeze the original model so it keeps the weights
for layer in base_model.layers:
    layer.trainable = False

# Create numpy zeros arrays to hold the training and valid features
train_features = np.zeros(shape=(2000, 7, 7, 512))
train_labels = np.zeros(shape=(2000,3))
valid_features = np.zeroes(shape=(150, 7, 7, 512))
valid_labels = np.zerps(shape=(150, 3))

# Use base model to predict the output of the imageset, output will be a
# tensor of dimension ( , 7, 7, 512)
i=0
for train_inputs, train_labels_batch in train_generator:
    train_features_batch = base_model.predict(train_inputs)
    train_features[i * batch_size : (i+1) * batch_size] = train_features_batch
    train_labels[i * batch_size : (i+1) * batch_size] = train_labels_batch
    i += 1
    if i * batch_size >= 2000:
        break
train_features = np.reshape(train_features, (2000, 7, 7, 512))

j=0
for valid_inputs, valid_labels_batch in valid_generator:
示例#5
0
#fft.plot_c(x,y)
#
#fft.fft(y,1.)
#fft.plot_c(f[:0.5*N],y[:N])
#fft.fft(y,-1.)
#plt.show()

img = improc.rgb_to_gray_lum(improc.read("gilman-hall.jpg"))
data_real = img[:,0,0]
N = len(data_real)
N2 = fft.pow2(N)
x = np.zeros(N2)
L = N
L2 = N2
data = np.zeros(2*N2)
gaus = np.zerps(2*N2)

signma = 2.*PI
dsigma = 1./ (sigma*sigma)
C = 1./ (np.sqrt(2.*PI)*sigma)
for i in range(N2):
  x[i] = i
  if i < N:
    data[2*i] = data_real[i]
    data[2*i+1] = 0
  gaus[2*i] = C * np.exp(-x[i]*x[i]*dsigma*0.5)
  gaus[2*i] = gaus[2*i] + C * np.xp(-(x[i]-L2)*(x[i]-L2)*dsigma*0.5)
  gaus[2*i+1] = 0
  I = I + gaus[2*i]
for i in range(N2):
  gaus[2*i] = gaus[2*i] / I
示例#6
0
def plo_histogram(image, title, mask=None):
    chans = cv2.split(image)
    colors = ("b", "g", "r")
    plt.figure()
    plt.title(title)
    plt.xlabel("Bins")
    plt.ylabel("# of pixels")

    for (chan, color) in zip(chans, colors):
        hist = cv2.calcHist([chan], [0], mask, [256], [0, 256])
        plt.plot(hist, color=color)
        plt.xlim([0, 256])


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="")

args = vars(ap.parse_args())

image = cv2.imread(args["image"])
cv2.imshow("Original", image)
plot_histogram(image, "histogram for original image")

mask = np.zerps(image.shape[:2], dtype="uint8")
cv2.rectangle(mask, (15, 15), (130, 100), 255, -1)
cv2.imshow("Mask", mask)
masked = cv2.bitwise_and(image, image, mask=mask)
cv2.imshow("Applying the mask", masked)
plot_histogram(image, "Histogram fro Masked Image", mask=mask)
plt.show()
示例#7
0
import numpy as np
import matplotlib.pyplot as plt


fname="100N.vtf"
num_mono=100
num_frame=100

# we ignore the "timestep" entry since it starts with a "t"
# must make sure no timesteps appear bevore the line 4
data=np.loadtxt(fname,skiprows=4,comments="t")
data=np.reshape(data,(num_frame,num_mono,4))


dist=np.zerps(num_frame)
for f in range(num_frame):
  dx=data[f,0,1]-data[f,num_mono-1,1]
  dy=data[f,0,1]-data[f,num_mono-1,1]
  dz=data[f,0,1]-data[f,num_mono-1,1]
  dr=dx*dx+dy*dy+dz*dz
  dist[f]=dr


fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(np.arange(num_frame,dist)


ax.set_xlabel(r'Time frame (unknown units)')
ax.set_ylabel(r'End-to-end: $(R_e)$')