Пример #1
0
def plot(data, weights):
    data_mat = array(df['density', 'radio_suger'].values[:,:])
    label_mat = mat(df['label'].values[:]).transpose()
    m = shape(data_mat)[0]
    xcord1 = []
    ycord1 = []
    xcord2 = []
    ycord2 = []
    for i in xrange(m):
        if label_mat[i] == 1:
            xcord1.append(data_mat[i])
            ycore1.append(label_mat[i])
        else:
            xcord2.append(data_mat[i])
            ycord2.append(label_mat[i])
    plt.figure(1)
    ax = plt.subplot(111)
    ax.scatter(xcord1, ycord1, s=30, c='red', marker='s')
    ax.scatter(xcord2, ycord2, s=30, c='greeen')
    x = arange(-0.2, 0.8, 1)
    y = array((-w[0,0]*x)/w[0,1])
    print shape(x)
    print shape(y)
    plt.sca(ax)
    
    plt.plot(x,y)
    plt.xlabel('density')
    plt.ylabel('radio_suger')
    plt.title('LDA')
    plt.show()
Пример #2
0
def visualize_data():
  """"""
  df = pd.read_csv('sp500_joined_closes.csv')
  df_corr = df.corr()
  
  data = df_corr.values
  fig = plt.figure()
  ax = fig.add_subplot(1, 1, 1) # An one by one plot.
  
  heatmap = ax.pcolor(data, cmap = plt.cm.RdYlGn)
  fig.colorbar(heatmap)
  ax.set_xticks(np.arange(data.shape[0])+ 0.5, minor=False)
  ax.set_yticks(np.arange(data.shape[1])+0.5, minor=False)
  ax.inverst_yaxis()
  ax.xaxis.tick_top()
  
  column_labels = df_corr.columns
  row_labels = df_corr.index
  
  ax.set_xticklables(column_labels)
  ax.set_yticklabels(row_labels)
  plt.xticks(rotation=90)
  heatmap.set_clim(-1, 1)
  plt.tight_layout()
  plt.show()
Пример #3
0
def plot_multiple(plot_function, args, filename=None, figsize=(14,9), label_left = "Density", 
                  label_bottom="Coarse Grain Measure Value", label_right="Energy", 
                  legend_pos=(1.40,0.90), legend_labels=None):
    
    fig = plt.figure(figsize=figsize)
    #figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
    num_structs = len(args)
    rows = int(1.5 * math.sqrt(num_structs))
    cols = int(math.ceil(num_structs / float(rows)))
    ls,lb = None,None
    
    #figsize(cols * 4, rows * 3)
    
    for i, arg in enumerate(args):
        ax = fig.add_subplot(rows, cols, i+1)
        try:
            (ls, lb) = plot_function(arg, ax=ax)
        except Exception:
            import traceback
            print >>sys.stderr, traceback.format_exc()
            #print >>sys.stderr, "Error:", str(e)
            continue
            
    fig.add_subplot(rows, cols, 1)
    ax = fig.add_subplot(rows, cols, num_structs)
    #ls1, lb1 = ax1.get_legend_handles_labels()
    
    if label_bottom is not None:
        fig.text(0.5, 0.00, label_bottom, ha='center', va='center', fontsize=13)
        
    if label_left is not None:
        fig.text(0.00, 0.60, label_left, ha='center', va='center', rotation='vertical', fontsize=13)
    
    if label_right is not None:
        fig.text(1., 0.60, label_right, ha='center', va='center', rotation='vertical', fontsize=13)
    
    plt.tight_layout()
    plt.subplots_adjust(top=1.30)
    
    if legend_labels is not None:
        lb = legend_labels
    
    if ls is not None and lb is not None:
        plt.legend(ls, lb, bbox_to_anchor=legend_pos, loc=2, borderaxespad=0.)
    
    if filename is not None:
        # blah
        plt.savefig(filename, bbox_inches='tight')
        
    return ax
Пример #4
0
def plot_the_loss_curve(epochs, mae_training, mae_validation):
  """Plot a curve of loss vs. epoch."""

  plt.figure()
  plt.xlabel("Epoch")
  plt.ylabel("Root Mean Squared Error")

  plt.plot(epochs[1:], mae_training[1:], label="Training Loss")
  plt.plot(epochs[1:], mae_validation[1:], label="Validation Loss")
  plt.legend()
  
  # We're not going to plot the first epoch, since the loss on the first epoch
  # is often substantially greater than the loss for other epochs.
  merged_mae_lists = mae_training[1:] + mae_validation[1:]
  highest_loss = max(merged_mae_lists)
  lowest_loss = min(merged_mae_lists)
  delta = highest_loss - lowest_loss
  print(delta)

  top_of_y_axis = highest_loss + (delta * 0.05)
  bottom_of_y_axis = lowest_loss - (delta * 0.05)
   
  plt.ylim([bottom_of_y_axis, top_of_y_axis])
  plt.show()  
Пример #5
0
    temp_list = []

    filtered = include_keys(info, keep)
    filtered.update({'ticker': ticker})
    temp_list.append(filtered)

    # filtered = filtered.update({'ticker': ticker})
    df = pd.DataFrame(temp_list)
    # df['lastDividendDate'] = datetime.datetime.fromtimestamp(df['lastDividendDate']).isoformat()
    # df['lastDividendDate'] = pd.to_datetime(df['lastDividendDate'], format='%Y-%m-%dT%H:%M:%S')

    return df, info, hist_div, mean_dividend


for i, ticker in enumerate(tech):
    df, info, hist_div, mean_dividend = get_data(ticker)

    if len(hist_div) == 0:
        print("No dividends to graph")
        pass
    else:
        # plt.figure(i)
        # ax = sns.distplot(hist_div, bins = 50)
        # ax.set_title("Stock " + ticker)

        fig = plt.figure()
        ax = plt.axes()
        ax.plot(hist_div)
        ax.set_title("Stock " + ticker)
Пример #6
0
                                                    target_size=(150, 150),
                                                    batch_size=20,
                                                    class_mode='binary')
validation_generator = test_datagen.flow_from_directory(validation_dir,
                                                        target_size=(150, 150),
                                                        batch_size=20,
                                                        class_mode='binary')
model.compile(loss='binary_crossentropy',
              optimizer=optimizers.RMSprop(lr=2e-5),
              metrics=['acc'])
history = model.fit_generator(train_generator,
                              steps_per_epoch=100,
                              epochs=30,
                              validation_data=validation_generator,
                              validation_steps=50)
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('Training and validation accuracy')
plt.legend()
plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()
Пример #7
0
verts = []
for row in csv_reader:
	verts.append(row)
	if float(row[0]) > bigx:
		bigx = float(row[0]
		if float(row[1]) > bigy:
			bigy = float(row[1]
			if float(row[0]) > smallx:
				smallx = float(row[0]
				if float(row[1]) > smally:
					smally = float(row[1]
verts.sort()
x_arr = []
y_arr = []
for vert in verts:
	x_arr.append(vert[0])
	y_arr.append(vert[1])

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8)

ax.set_xlabel('x data')
ax.set_ylabel('y data')
ax.set_xlim(smallx,bigx)
ax.set_ylim(smally,bigy)

ax.plot(x_arr,y_arr,color='blue',lw=2)
plt.show()
fig.savefig('test.png')
Пример #8
0
        'K Nearest Neighbors (KNN)': KNN(contamination=outliers_fraction),
        'Average KNN': KNN(method='mean',contamination=outliers_fraction)
}

xx , yy = np.meshgrid(np.linspace(0,1 , 200), np.linspace(0, 1, 200))

for i, (clf_name, clf) in enumerate(classifiers.items()):
    clf.fit(X)
    # predict raw anomaly score
    scores_pred = clf.decision_function(X) * -1
        
    # prediction of a datapoint category outlier or inlier
    y_pred = clf.predict(X)
    n_inliers = len(y_pred) - np.count_nonzero(y_pred)
    n_outliers = np.count_nonzero(y_pred == 1)
    plt.figure(figsize=(10, 10))
    
    # copy of dataframe
    dfx = df
    dfx['outlier'] = y_pred.tolist()
    
    # IX1 - inlier feature 1,  IX2 - inlier feature 2
    IX1 =  np.array(dfx['Item_MRP'][dfx['outlier'] == 0]).reshape(-1,1)
    IX2 =  np.array(dfx['Item_Outlet_Sales'][dfx['outlier'] == 0]).reshape(-1,1)
    
    # OX1 - outlier feature 1, OX2 - outlier feature 2
    OX1 =  dfx['Item_MRP'][dfx['outlier'] == 1].values.reshape(-1,1)
    OX2 =  dfx['Item_Outlet_Sales'][dfx['outlier'] == 1].values.reshape(-1,1)
         
    print('OUTLIERS : ',n_outliers,'INLIERS : ',n_inliers, clf_name)