Esempio n. 1
0
def find_max_score_value(y_true, y_pred, metric_name, showplot=False):
    """
    y_true : numpy array of true values (0,1)
    y_pred: values of prediction, can be floats/probabilities since they are mapped to ints
    metric_name -->> a sklearn metric name
        * so far I've tested this with metrics.f1_score, metrics.precision_score, metrics.accuracy_score,
                                       metrics.matthews_corrcoef

    A poor-man's grid search over the evaluation metrics to identify
    the best offset values (i.e. point where desired metric score is
    highest)
    """
    scores = []
    offsets = []
    # stage 1: Note if the metric is outside of [0,1] this will not work.
    for offset in np.arange(0,10)/float(10):
        score = metric_name(y_true,map(np.int,y_pred+offset))
        scores.append(score)
        offsets.append(offset)
    # find the max
    mxscore = max(scores)
    mx_idx = scores.index(mxscore)
    #now offset below and above
    if mx_idx == 0:
        for offset in np.arange(0,16)/float(100):
            offset+=0.0
            score = metric_name(y_true,map(np.int,y_pred+offset))
            scores.append(score)
            offsets.append(offset)
    elif mx_idx == 9:
        for offset in np.arange(0,21)/float(100):
            offset+=0.8
            score = metric_name(y_true,map(np.int,y_pred+offset))
            scores.append(score)
            offsets.append(offset)
    else:
        base_offset = offsets[mx_idx-1]
        for offset in np.arange(0,21)/float(100):
            offset+=base_offset
            score = metric_name(y_true,map(np.int,y_pred+offset))
            scores.append(score)
            offsets.append(offset)

    if showplot:
        plt.plot(offsets,scores,'d',color='indigo')
        plt.xlabel('offset value')
        plt.ylabel(metric_name)

    mxmxscore = max(scores)
    mxmx_idx = scores.index(mxmxscore)
    print mxmxscore, offsets[mxmx_idx]
    #if verbose:
    #    print "__________"
    #    print scores
    #return mcc_scores, mcc_offsets
    return offsets[mxmx_idx]
Esempio n. 2
0
    epochs=100,
    validation_data=validation_generator,
    validation_steps=50)

model.save('cats_and_dogs_small_2.h5')




acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(acc) + 1)


plt.plot(epochs, acc, 'bo', label='training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Traing and validation accuracy')
plt.legend()

plt.figure()

plt.plot(epochs, loss, 'bo', label='Traing loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Traing and validation loss')
plt.legend()

plt.show()
Esempio n. 3
0
print(rtndf.std())

#write to a csv file
voldf = rtndf.std()
voldf.to_csv('stkvol.csv')
voldf.to_pickle('stkvol.pkl')
                 
pltflag = True

if pltflag:
  #pandas fig 1
  pltdf[stks].pct_change().plot()
  
  #pandas fig 2
  pltdf[stks].pct_change().dropna.cumsum().plot()

  stkind = 0

  ## Matplotlib plots
  plt.figure()
  plt.plot()

  for stkind in range(len(stks)):
    plt.subplot(2, 2, stkind + 1)
    plt.plot(tmpdf.index,tmpdf.values)
    tmpdf = pltdf[stks[stkind]]
    plt.title('Prices of ' + stks[stkind] + ' from ' + start + ' to ' + end)
    plt.xlabel('Dates')
    plt.ylabel('Price')
  plt.show()
Esempio n. 4
0
import matplotlib.pyplt as plt
#import argparse

#parser = argparse.ArgumentParser(description = 'Script to generate correlation v/s duration plots of x triggers\n.
				 #The duration plotted against is of the x triggers')

#parser.add_argument('channelName', type=str, help='Name of the correlation statistic file which contains data such as correlation, duration, significance etc.')

#args = parser.parse_args()
#fileDir = args.channelName
if(len(sys.argv)<2):
  print 'Provide atleast one file name'

fileDir = sys.argv[1:]

channelName = fileDir[0].split('results/')[1].split('/')[0].split('+')[0].split('H1-')[1]
correlation = np.asarray([])
duration = np.asraay([])

for iFile in fileDir:
  corrData = np.loadtxt(fileDir[iFile]).reshape(-1, 15)
  correlation = np.append(correlation, corrData[:, 1])
  duration = np.append(duration, corrData[:, 9])
  
plt.figure()
plt.plot(duration, correlation, '.')
plt.title('Correlation vs trigger durations of %s channel' %(channelName))
plt.ylabel('correlation statistic $r$')
plt.xlabel('trigger durations')
plt.savefig('./corr_vs_duration_%s.png'%(channelName))
plt.close()
Esempio n. 5
0
#import argparse

#parser = argparse.ArgumentParser(description = 'Script to generate correlation v/s duration plots of x triggers\n.
#The duration plotted against is of the x triggers')

#parser.add_argument('channelName', type=str, help='Name of the correlation statistic file which contains data such as correlation, duration, significance etc.')

#args = parser.parse_args()
#fileDir = args.channelName
if (len(sys.argv) < 2):
    print 'Provide atleast one file name'

fileDir = sys.argv[1:]

channelName = fileDir[0].split('results/')[1].split('/')[0].split(
    '+')[0].split('H1-')[1]
correlation = np.asarray([])
duration = np.asraay([])

for iFile in fileDir:
    corrData = np.loadtxt(fileDir[iFile]).reshape(-1, 15)
    correlation = np.append(correlation, corrData[:, 1])
    duration = np.append(duration, corrData[:, 9])

plt.figure()
plt.plot(duration, correlation, '.')
plt.title('Correlation vs trigger durations of %s channel' % (channelName))
plt.ylabel('correlation statistic $r$')
plt.xlabel('trigger durations')
plt.savefig('./corr_vs_duration_%s.png' % (channelName))
plt.close()
Esempio n. 6
0
import matplotlib.pyplt as plt

from bermuda import ellipse, polygon, rectangle

plt.plot([1, 2, 3], [2, 3, 4])
ax = plg.gca()

# default choices for everything
e = ellipse(ax)

# custom position, genric interface for all shapes
e = ellipse(ax, bbox=(x, y, w, h, theta))
e = ellipse(ax, cen=(x, y), width=w, height=h, theta=theta)

# force square/circle?
e = ellipse(ax, aspect_equal=True)

# freeze properties?
e = ellipse(ax, width=1, height=2, aspect_frozen=True)
e = ellipse(ax, rotation_frozen=True)
e = ellipse(ax, center_frozen=True)
e = ellipse(ax, size_frozen=True)

# all of these kwargs should be settable properties as well
e.bbox = (x, y, w, h, theta)
e.aspect_equal = True
e.aspect_frozen = True
Esempio n. 7
0
    return solI

#initial condition
io = 0.1

#create array of time points
timePoints = np.linspace(0, 10, 11)
print(timePoints)

#solve ODE
cpi = 0.3
solution = odeint(fode,io,timePoints,args=(cpi,))
print(solution)

#set data points on plot
plt.plot(timePoints,solution)

#label x- and y- axes
plt.xlabel('time')
Text(0.5, 0, 'time')
plt.ylabel('i(t)')
Text(0, 0.5, 'i(t)')

#set the position of text and display desired text
yMax = max(solution)
xMax = max(timePoints)

plt.text(0.8*xMax,0.8*yMax,str("cpi="+str(cpi)))
plt.text(0.8*xMax,0.7*yMax,str("i(o)="+str(io)))

>>> #display the plot
Esempio n. 8
0
#Build First Network

from keras.layers import Dense # Dense layers are "fully connected" layers
from keras.models import Sequential # Documentation: https://keras.io/models/sequential/

image_size = 784 # 28*28
num_classes = 10 # ten unique digits

model = Sequential()

# The input layer requires the special input_shape parameter which should match
# the shape of our training data.
model.add(Dense(units=32, activation='sigmoid', input_shape=(image_size,)))
model.add(Dense(units=num_classes, activation='softmax'))
model.summary()

#Evaulate the Model
model.compile(optimizer="sgd", loss='categorical_crossentropy', metrics=['accuracy'])
history = model.fit(x_train, y_train, batch_size=128, epochs=5, verbose=False, validation_split=.1)
loss, accuracy  = model.evaluate(x_test, y_test, verbose=False)
import matplotlib.pyplt as plt
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['training', 'validation'], loc='best')
plt.show()
print(f'Test loss: {loss:.3}')
print(f'Test accuracy: {accuracy:.3}')
Esempio n. 9
0
import matplotlib.pyplt as plt

from bermuda import ellipse, polygon, rectangle


plt.plot([1,2,3], [2,3,4])
ax = plg.gca()

# default choices for everything
e = ellipse(ax)

# custom position, genric interface for all shapes
e = ellipse(ax, bbox = (x, y, w, h, theta))
e = ellipse(ax, cen=(x, y), width=w, height=h, theta=theta)

# force square/circle?
e = ellipse(ax, aspect_equal=True)

# freeze properties?
e = ellipse(ax, width=1, height=2, aspect_frozen = True)
e = ellipse(ax, rotation_frozen=True)
e = ellipse(ax, center_frozen=True)
e = ellipse(ax, size_frozen=True)

# all of these kwargs should be settable properties as well
e.bbox = (x, y, w, h, theta)
e.aspect_equal = True
e.aspect_frozen = True
Esempio n. 10
0
#     var_grad_val = sess.run(var_grad, feed_dict={x: 1.})
#     print (var_grad_val)

# # return [4]

# #-------------------------------------------
# uniform = tf.random_uniform([100], minval=0, maxval=1, dtype=tf.float32)

# with tf.Session() as sess:
#     print uniform.eval()
#     plt.hist(uniform_eval(), normed=True)
#     plt.show()

#--------------计算pi-------
import tensorflow as tf
trials = 100
hits = 0

x = tf.random_uniform([1], minval=-1, maxval=1, dtype=tf.float32)
y = tf.random_uniform([1], minval=-1, maxval=1, dtype=tf.float32)
pi = []

with tf.Session().as_default():
    for i in range(1, trials):
        for j in range(1, trials):
            if x.eval()**2 + y.eval()**2 < 1:
                hits = hits + 1
                pi.append((4 * float(hits) / i) / trials)
plt.plot(pi)
plt.show()
Esempio n. 11
0
	dY=rY*(y-t)*y*(1-y)
	tY=tY-dY*(-1)
	wY=wY-dY*transpose(h)#轉置h矩陣
	
	#隱藏到輸入,學習率為rY*rH,rY來自dY
	dH=rH* transpose(wY) * dY* h* (1 - h)
	tH =tY- dH * (-1)
	wH -= dH * transpose(x)
	
	# 全部資料組對於這次訓練完的模型的參數的錯誤率
	
	e.append(np.sum(((sigmoid(wY * sigmoid(wH * InputLayer - tH) - tY) - OutputLayer)**2)**0.5))
	


plt.plot(e)
print(e[m])#顯示最後錯誤率
"""訓練結束"""


#測試




#測試結束
def sigmoid(x):
	y=(1+exp(-x))**1
	return y

Esempio n. 12
0
import sys

import numpy as np
import matplotlib.pyplt as plt

fname = 


plt.matshow(D)
plt.colorbar()
plt.title('Vertex-Vertex distances for sphere(9802 vertices)')
plt.xlabel('Vertex i')
plt.ylabel('Vertex j')
plt.show()





v = scipy.linalg.eigvals(D)
first_k = 5000
plt.plot(np.abs(np.real((v[:first_k]))))
plt.xlabel('k')
plt.ylabel('k-th absolute eigenvalue REAL component')
plt.title(f'Eigen spectrum for sphere distance matrix(first {first_k} k)')
plt.yscale('log')
plt.grid(axis='y')
plt.show()