def plot_conf_matrix(y_true, y_pred, normed=True, heatmap_color ='Blues', **kwargs): ## check to make sure that y_pred is an array of integers if y_true is a bunch of integers true_int_check = all(isinstance(a,int) for a in y_true) pred_int_check = all(isinstance(a,int) for a in y_pred) if true_int_check and not pred_int_check: # convert the y_pred values to integers if isinstance(y_pred, pd.Series): y_pred = y_pred.astype(int) my_c = metrics.confusion_matrix(y_true, y_pred) print metrics.matthews_corrcoef(y_true, y_pred) if normed: cm_normalized = my_c.astype('float') / my_c.sum(axis=1)[:, np.newaxis] my_c = cm_normalized plt.title('Normalized RF Classifier Confusion Matrix') else: plt.title('Random Forest Classifier Confusion Matrix') sns.heatmap(my_c, annot=True, fmt='',cmap=heatmap_color, **kwargs) plt.ylabel('True') plt.xlabel('Assigned') plt.show() return
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]
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()
cm = confusion_matrix(y_test, y_pred) #Visualising the results from matplotlib.colors import ListedColormap X_set, y_set = X_train, y_train X1, X2 = np.meshgrid( np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01), np.arange(start=X_set[:, 1].min() - 1, stop=X_set[:, 1].max() + 1, step=0.01)) plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha=0.75, cmap=ListedColormap(('red', 'green'))) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c=ListedColormap(('red', 'green'))(i), label=j) plt.title('Decision Tree Classifier (Training set)') plt.xlabel('Age') plt.ylabel('Estimated Salary') plt.legend() plt.show()
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()
#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()
#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 >>> plt.show()
#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}')
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()