Example #1
0
def main():
    df = pd.read_csv('fishiris.csv')
    df['target'] = df.apply(create_target, axis=1)
    y = df['target'].to_numpy()
    df = df.drop(['Name', 'target'], axis=1)
    feature_names = df.columns.tolist()
    X = df.to_numpy()
    target_names = ['setosa', 'versicolor', 'virginica']

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.4,
                                                        shuffle=True)
    print('X_train\n', X_train)
    print('y_train\n', y_train)
    print('X_test\n', X_test)
    print('y_test\n', y_test)
    clf = ClassificationTree()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    print('-' * 40, 'print_tree', '-' * 40)
    clf.print_tree(feature_names=feature_names)
    print('-' * 40, 'print_tree', '-' * 40)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy:", accuracy)

    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Decision Tree",
                      accuracy=accuracy,
                      legend_labels=target_names)
    Plot().plot_in_3d(X_test, y_pred)
Example #2
0
	def strengthPlot(self):
		# set colorMap
		maxStrength = utils.ceilToRoundNumber(np.max(self.spring))
		jet = plt.get_cmap('Blues') 
		cNorm  = colors.Normalize(vmin = 0, vmax = maxStrength)
		colorMap = cmx.ScalarMappable(norm=cNorm, cmap=jet)

		# calculate relevant properties
		xpos = self.initialPos.x
		ypos = self.initialPos.y

		# plot
		fig = plt.figure(facecolor=(1,1,1), figsize=(10, 6), dpi=150)
		ax = fig.add_axes([0.05, 0.10, 0.85, 0.8])
		for i,j in itertools.product(range(len(xpos)), range(len(ypos))):
			if self.connections[i,j]:
				colorVal = colorMap.to_rgba(self.spring[i,j])
				plt.plot([xpos[i], xpos[j]], [ypos[i], ypos[j]], color = colorVal)
		plt.plot(xpos, ypos, 'ko', markersize = 10)
		plt.fill_between([-12, 12], -20, 0, facecolor='gray', edgecolor='gray')
		
		plt.axis('off')
		Plot.configurePlot(fig, ax, "","", False)
		ax.set_xlim( xmin = -14, xmax = 14)
		ax.set_ylim( ymin = -5, ymax = 15)

		# Add colorbar, make sure to specify tick locations to match desired ticklabels
		cax = fig.add_axes([0.90, 0.10, 0.02, 0.8])
		cbar = clb.ColorbarBase(cax, cmap = jet, ticks=[0,0.25,0.5,0.75,1])
		cbar.ax.set_yticklabels(['0', str(maxStrength / 4),str(maxStrength / 2 ), str(maxStrength/4 * 3), str(maxStrength) ])  # vertically oriented colorbar
		cbar.ax.tick_params(labelsize = 14)
		cbar.ax.set_ylabel('Spring Constant', rotation=270)
		
		Plot.save2eps(fig, 'strengthPlot')
	def plot(self , title = 'Training Scores', show = True):
		""" generate a plot of the training"""
		fig, ax = Plot.initPlot()
		for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
			ax.get_xticklabels() + ax.get_yticklabels()):
			item.set_fontsize(17)
		self.generatePlot()
		plt.title(title + "     optimum  = " + num2str(self.optimalscore) ) # EXTEND automatic names
		Plot.configurePlot(fig, ax, 'Temp', 'Temp', legend = True, legendLocation = 'lower center')
		plt.xlabel(self.xlabel)
		plt.ylabel('Distance Traveled') # TODO generalize
		if show: plt.show()
def main():
    # Load dataset
    data = datasets.load_iris()
    # X = normalize(data.data[data.target != 0])
    X = data.data[data.target != 0]
    y = data.target[data.target != 0]
    y[y == 1] = 0
    y[y == 2] = 1

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.333)

    clf = LogisticRegression(iteration=10)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    y_pred = np.reshape(y_pred, y_test.shape)

    accuracy = roc_auc_score(y_test, y_pred)
    print("Accuracy:", accuracy)
    y_pred = clf.predict_label(X_test)
    report = classification_report(y_test, y_pred)
    print(report)

    # Reduce dimension to two using PCA and plot the results
    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Logistic Regression",
                      accuracy=accuracy)
Example #5
0
def main():
    data = datasets.load_digits()
    X = normalize(data.data)
    y = data.target

    # convert the nominal y values to binary
    y = to_categorical(y)

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)

    #mlp
    clf = MultilayerPerceptron(n_hidden=16,
                               n_iterations=1000,
                               learning_rate=0.01)
    clf.fit(X_train, y_train)
    y_pred = np.argmax(clf.predict(X_test), axis=1)
    y_test = np.argmax(y_test, axis=1)

    accuracy = accuracy_score(y_test, y_pred)
    print("Accuracy:", accuracy)

    # Reduce dimension to two using PCA and plot the results
    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Multilayer Perceptron",
                      accuracy=accuracy,
                      legend_labels=np.unique(y))
def main():
    # Load dataset
    data = datasets.load_iris()
    X = normalize(data.data[data.target != 0])
    y = data.target[data.target != 0]
    y[y == 1] = 0
    y[y == 2] = 1

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.33,
                                                        seed=1)

    clf = LogisticRegression()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    y_pred = np.reshape(y_pred, y_test.shape)

    accuracy = accuracy_score(y_test, y_pred)
    print("Accuracy:", accuracy)

    # Reduce dimension to two using PCA and plot the results
    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Logistic Regression",
                      accuracy=accuracy,
                      legend_labels=data.target_names)
Example #7
0
def main():
    data = datasets.load_digits()
    X = data.data
    y = data.target

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.4,
                                                        seed=2)
    print("X_train.shape:", X_train.shape)
    print("Y_train.shape:", y_train.shape)

    clf = RandomForest(n_estimators=100)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy:", accuracy)

    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Random Forest",
                      accuracy=accuracy,
                      legend_labels=data.target_names)
Example #8
0
def main():
    data = datasets.load_digits()
    X = normalize(data.data)
    y = data.target

    # One-hot encoding of nominal y-values
    y = to_categorical(y)

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.33,
                                                        seed=8)

    # Perceptron
    clf = Perceptron(n_iterations=5000,
                     learning_rate=0.001,
                     loss=CrossEntropy,
                     activation_function=Sigmoid)
    clf.fit(X_train, y_train)

    y_pred = np.argmax(clf.predict(X_test), axis=1)
    y_test = np.argmax(y_test, axis=1)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy:", accuracy)

    # Reduce dimension to two using PCA and plot the results
    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Perceptron",
                      accuracy=accuracy,
                      legend_labels=np.unique(y))
	def plot(self, targetnum = 0, block = False):
		noTrain = len(self.trainTarget[targetnum])
		noTest = len(self.testTarget[targetnum])

		x1= np.arange(noTrain) * self.simulEnv.timeStep
		x2 = (noTrain + np.arange(noTest)) * self.simulEnv.timeStep

		fig, ax = Plot.initPlot()
		ax.set_title(r'$ \epsilon_{train} = %(num1)s \ \ \ \epsilon_{test} = %(num2)s $' % {'num1': num2str(self.trainRelError), 'num2': num2str(self.testRelError)}, fontsize = 30)
		ax.plot(np.hstack((x1,x2)), np.hstack((self.trainTarget[targetnum], self.testTarget[targetnum])), label = "target", linewidth = 3, alpha = 0.5)
		ax.plot(x1, self.trainOutput[targetnum], 'k-', label = "train")
		ax.plot(x2, self.testOutput[targetnum], 'r.', label = "test")
		ax.legend()
		Plot.configurePlot(fig,ax,"time","value", legendLocation = 'lower center')
		plt.show(block = block)

		return fig, ax
Example #10
0
 def plot(self, title='Training Scores', show=True):
     """ generate a plot of the training"""
     fig, ax = Plot.initPlot()
     for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
                  ax.get_xticklabels() + ax.get_yticklabels()):
         item.set_fontsize(17)
     self.generatePlot()
     plt.title(title + "     optimum  = " +
               num2str(self.optimalscore))  # EXTEND automatic names
     Plot.configurePlot(fig,
                        ax,
                        'Temp',
                        'Temp',
                        legend=True,
                        legendLocation='lower center')
     plt.xlabel(self.xlabel)
     plt.ylabel('Distance Traveled')  # TODO generalize
     if show: plt.show()
def painted_point_cloud(calib_path,img_path,lidar_path,out_path,num):

    print('Now tracking num:',num)
    calib_path = calib_path + str(num).zfill(6)+ '.txt'
    img_path = img_path+ str(num).zfill(6) + '.png'
    lidar_path = lidar_path + str(num).zfill(6) +'.bin'
    out_path = out_path + str(num).zfill(6) +'.npy'

    raw_pointcloud =load_velo_scan(lidar_path)
    calib = calibration_kitti.Calibration(calib_path)
    img = cv2.imread(img_path)
    img_shape = img.shape
    pts_img,pts_rect_depth = calib.lidar_to_img(raw_pointcloud) # [N,3] in lidar to [N,2] in img

    pts_img = np.round(pts_img).astype(int)#四舍五入
    #after lidar to img,filtering points in img's range ;
    val_flag_1 = np.logical_and(pts_img[:, 0] >= 0, pts_img[:, 0] < img_shape[1])
    val_flag_2 = np.logical_and(pts_img[:, 1] >= 0, pts_img[:, 1] < img_shape[0])
    val_flag_merge = np.logical_and(val_flag_1, val_flag_2)
    pts_valid_flag = np.logical_and(val_flag_merge, pts_rect_depth >= 0)

    ## in img's range ;all lidar points
    pts_img = pts_img[pts_valid_flag]
    pts_img[:, [0, 1]] = pts_img[:, [1, 0]]# height,width to width height[1242,375] to [375,1242]


    row = pts_img[:,0]
    col = pts_img[:,1]
    '''
    for i in range(1,img_shape[0]-1):
        for j in range(1,img_shape[1]-1):
            #print('before:',img[i,j])
            img[i,j] = (img[i-1,j-1]+img[i,j-1]+img[i+1,j-1]+img[i-1,j]+img[i+1,j]+img[i-1,j+1]+img[i,j+1]+img[i+1,j+1] +img[i,j])/9.
            #img[i,j] = (img[i, j - 1] + img[i - 1, j]+img[i,j]) / 3.
            #print('after,',img[i, j])
    '''
    img = img.astype(int)
    raw_pointcloud_color = img[row,col,:] # [N,3] b g r

    raw_pointcloud_color[:,[0,1,2]] = raw_pointcloud_color[:,[2,1,0]]
    painted_point_cloud_res = np.hstack((raw_pointcloud[pts_valid_flag],raw_pointcloud_color)) # [N,6] x,y,z,r,g,b
    Plot.draw_pc(painted_point_cloud_res)
    np.save(out_path,painted_point_cloud_res)
    return  painted_point_cloud_res
Example #12
0
def main():
    data = loadDataSet('../x_train.csv')
    label = loadLabel('../y_train.csv')

    # tiaocan(data,label)

    X_train, X_test, y_train, y_test = train_test_split(data,
                                                        label,
                                                        test_size=0.4,
                                                        seed=19260817)
    print("X_train.shape:", X_train.shape)
    print("Y_train.shape:", y_train.shape)

    clf = RandomForest(n_estimators=35)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy:", accuracy)

    m = len(y_test)
    tp = 0.0
    fp = 0.0
    fn = 0.0
    print(y_test)
    print(y_pred)
    for i in range(m):
        if y_pred[i] > 1 and y_test[i] > 1:
            tp += 1
        if y_pred[i] < 1 and y_test[i] > 1:
            fp += 1
        if y_pred[i] > 1 and y_test[i] < 1:
            fn += 1
    precision = tp / (tp + fp)
    recall = tp / (tp + fn)
    f1_score = 2 * precision * recall / (precision + recall)
    print(tp, fp, fn)
    print("precision is: ", precision)
    print("recall is: ", recall)
    print("f1 score is: ", f1_score)

    Plot().plot_in_2d(
        X_test, y_pred,
        title="Random Forest")  #accuracy=accuracy,#legend_labels=data.target_)
def main():
    #load the dataset
    data = datasets.load_iris()
    X = data.data
    y = data.target

    #three -> two classes
    X = X[y != 2]
    y = y[y != 2]
    X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)

    #fit and predict using LDA
    lda = LDA()
    lda.fit(X_train, y_train)
    y_pred = lda.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    print("accuracy:",accuracy)
    Plot().plot_in_2d(X_test, y_pred,title="LDA", accuracy=accuracy)
def main():
    print ('-- Grandient Boosting Classification --')
    
    data = datasets.load_iris()
    
    X = data.data
    y = data.target
    
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
    
    clf = GBDTClassifier()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    
    accuracy = accuracy_score(y_test,y_pred)
    
    print ('Accuracy:',accuracy)
    
    Plot().plot_in_2d(X_test, y_pred,
        title='GB', accuracy=accuracy, legend_labels=data.target_names)
Example #15
0
def run():
    data = datasets.load_iris()
    X = normalize(data.data)
    y = data.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

    #初始化knn
    model = KNN(k=5)
    y_pred = model.predict(X_test, X_train, y_train)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy:", accuracy)

    # 用图画出测试集的分类情况
    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="KNN",
                      accuracy=accuracy,
                      legend_labels=data.target_names)
Example #16
0
def main():
    data = datasets.load_digits()
    X = normalize(data.data)
    y = data.target

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)

    clf = NaiveBayes()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)
    print("Accuracy:", accuracy)

    #reduce dimension to two using PCA and plot the results
    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Naive Bayes",
                      accuracy=accuracy,
                      legend_labels=data.target_names)
def main():
    print("-- classification Tree --")
    data = datasets.load_iris()
    X = data.data
    y = data.target
    import pdb
    #pdb.set_trace()
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
    #clf = ClassificationTree(min_samples_split=5,min_impurity=0.05,max_depth=5,criterion="entropy")
    clf = ClassificationTree(min_impurity=0.05, criterion="gini")
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    print("Accuracy:", accuracy)

    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Decision Tree",
                      accuracy=accuracy,
                      legend_labels=data.target_names)
Example #18
0
def main():
    print("--XGBoost--")
    data = datasets.load_iris()

    X = data.data
    y = data.target

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
    clf = XGBoost(n_estimators=200, max_depth=3)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)

    print("Accuracy:", accuracy)

    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="XGBoost",
                      accuracy=accuracy,
                      legend_labels=data.target_names)
def main():

    print ("-- Classification Tree --")

    data = datasets.load_iris()
    X = data.data
    y = data.target

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)

    clf = ClassificationTree()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)

    print ("Accuracy:", accuracy)

    Plot().plot_in_2d(X_test, y_pred,
        title="Decision Tree",
        accuracy=accuracy,
        legend_labels=data.target_names)
Example #20
0
def main():
    # Load dataset
    data = datasets.load_iris()
    X = normalize(data.data[data.target != 0])
    y = data.target[data.target != 0]
    y[y == 1] = 0
    y[y == 2] = 1

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

    clf = LogisticRegression(opt_type="SGD", gradient_descent=False)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)
    print("Accuracy:", accuracy)

    # Reduce dimension to two using PCA and plot the results
    Plot().plot_in_2d(X_test,
                      y_pred,
                      title="Logistic Regression",
                      accuracy=accuracy)
Example #21
0
def main():

    print("-- Gradient Boosting Classification --")

    data = load_iris()
    X = data.data
    y = data.target

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
    #print(y_train)

    clf = GBDTClassifier()
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)

    accuracy = accuracy_score(y_test, y_pred)
    print ("Accuracy:", accuracy)

    Plot().plot_in_2d(X_test, y_pred,
        title="Gradient Boosting",
        accuracy=accuracy,
        legend_labels=data.target_names)
    print('Done!')
Example #22
0
    def run(self):

        plot = Plot(num_subplots=2, subplot_title=["average score", "entropy"])
        average_score = 0.0
        average_entropy = 0.0

        for episode in range(1, self.num_episodes+1):

            state = self.env.reset()
            state = state[np.newaxis, ...]
            done, score = False, 0
            states, actions, rewards, probs, values, dones = [], [], [], [], [], []

            while not done:
                if self.render_period > 0 and episode % self.render_period == 0:
                    self.env.render()
                state = state.astype(np.float64)
                action, prob, value = self.act(state)
                next_state, reward, done, _ = self.env.step(action)
                states.append(state)
                actions.append(action)
                rewards.append(reward)
                dones.append(done)
                probs.append(prob)
                values.append(value)
                state = next_state[np.newaxis, ...]
                score += reward

                if done:
                    loss_actor, loss_critic, entropy = self.replay(states, actions, rewards, probs, values, dones, next_state)
                    average_score, self.scores = self.update_average(self.scores, score)
                    average_entropy, self.entropies = self.update_average(self.entropies, entropy)
                    print("episode: {}/{}, score: {:.2f}, average: {:.2f} loss actor={:.4f} critic={:.4f} entropy={:.4f} num states={}"
                          .format(episode, self.num_episodes, score, average_score, loss_actor, loss_critic, average_entropy, len(states)))

                    if self.plot:
                        plot.update(value=average_score, idx=0)
                        plot.update(value=average_entropy, idx=1)

            if average_score >= self.env.spec.reward_threshold:
                print(f"solved in {episode} episodes")
                break
Example #23
0
#============================================================================
# Plot reweighted data
#============================================================================
weights_train = [weight_trainEst10, weight_trainEst20, weight_trainEst40]
weights_train_names = [
    "weight_trainEst10", "weight_trainEst20", "weight_trainEst40"
]

### Training data
for w, wn in zip(weights_train, weights_train_names):
    fig, ax = plt.subplots(2, 2, figsize=(15, 10))
    ax = ax.flatten()
    fig, ax[0] = Plot(Histogram(data_train['muo_eta'][trainMask],
                                data_train["label"][trainMask], w, 120, -4, 4),
                      fig,
                      ax[0],
                      r"$\eta$",
                      includeN=True)
    fig, ax[1] = Plot(Histogram(data_train['muo_pt'][trainMask],
                                data_train["label"][trainMask], w, 120, -5,
                                120000),
                      fig,
                      ax[1],
                      "pt",
                      includeN=False)
    fig, ax[2] = Plot(Histogram(
        data_train['correctedScaledAverageMu'][trainMask],
        data_train["label"][trainMask], w, 80, -2, 80),
                      fig,
                      ax[2],
                      r"$\langle\mu\rangle$",
	def plot(self):
		fig, ax = Plot.initPlot()
		ax.plot(self.xdata,self.ydata)
		Plot.configurePlot(fig,ax, "time","current SpringLength")
		fig.show()
Example #25
0
        draw_hump(p, (x, y), radius, True)
        x += 2 * radius
        p.lineto(100, y)


def main(p):
    num_colors = 2
    num_rows = 20
    color_assignments = []
    last_color = 1
    for r in range(num_rows):
        cur_color = random.randint(1, num_colors)
        while cur_color == last_color:
            cur_color = random.randint(1, num_colors)
        last_color = cur_color
        color_assignments.append(cur_color)
    for color in range(1, num_colors + 1):
        draw_color(p, num_rows, color_assignments, color)
        p.penup()
        input()


if __name__ == '__main__':
    p = Plot()
    p.plot_size = 4
    p.plotter_enabled = False
    p.setup()

    main(p)

    p.done()
Example #26
0
        t = t2 - t1
        non_ground_index = np.where(segmentation[:, -1] == 0)[0]
        points_up = segmentation[non_ground_index][:, :]
        ground_index = np.where(segmentation[:, -1] != 0)[0]
        points_down = segmentation[ground_index][:, :]
        points_down_labels = points_down[:, 3]
        points_up_labels = points_up[:, 3]

    else:
        print('Method have not release')
        return None, None, None, None, 0

    return points_up, points_up_labels, points_down, points_down_labels, t


have_label = True
method = 4

infile_path = '../data/velodyne/000002.npy'
raw_data = reader.load_velo_scan(infile_path)

if have_label:
    label_path = '../data/label/000002.npy'
    raw_label = np.load(label_path)
else:
    raw_label = np.zeros((raw_data.shape[0], 1)) + 1

fileter_height, fileter_height_label, ground, fileter_height_label_ground, t = filter_ground(
    raw_data, raw_label, method)
Plot.draw_pc_sem_ins(fileter_height, fileter_height_label)
Example #27
0
                                   label="Signal reweighted")

                        ax[i].set(xlim=(edges_sig[0], edges_sig[-1]),
                                  xlabel=xlab,
                                  ylabel=f"Events/{bw:4.2f}")

                        ax[i].legend()

                else:
                    for i, (var, bin, rang, xlab) in enumerate(
                            zip(variables, bins, ranges, xlabel)):

                        fig, ax[i] = Plot(Histogram(
                            data_train[var][mask], data_train["label"][mask],
                            data_train[weightType + "_" + weightName][mask],
                            bin, rang[0], rang[1]),
                                          fig,
                                          ax[i],
                                          xlab,
                                          includeN=True)
                        #fig, ax[1] = Plot(Histogram(data_train['pt'][mask], data_train["label"][mask], data_train[weightType+"_"+weightName][mask], 120, -5, 120), fig, ax[1], "pt", includeN = False)
                        #fig, ax[2] = Plot(Histogram(data_train['invM'][mask], data_train["label"][mask], data_train[weightType+"_"+weightName][mask], 120, 50, 110), fig, ax[2], "invM", includeN = False)
                        #fig, ax[3] = Plot(Histogram(data_train['correctedScaledAverageMu'][mask], data_train["label"][mask], data_train[weightType+"_"+weightName][mask], 80, -2, 80), fig, ax[3], r"$\langle\mu\rangle$", includeN = False)

                counts_weight, edges_weight = np.histogram(
                    data_train[weightType + "_" + weightName][mask],
                    bins=120,
                    range=(0, 40))
                ax[4].step(x=edges_weight,
                           y=np.append(counts_weight, 0),
                           where="post",
                           color="k")
#Data location
#Images
img_folderpath = '../data/KITTI/2011_09_26/image_02/data/*.png'
img_filepaths = sorted(glob.glob(img_folderpath))
#Lidar
lidar_folderpath = '../data/KITTI/2011_09_26/velodyne_points/data/*.bin'
lidar_filepaths = sorted(glob.glob(lidar_folderpath))
#Yolo Network
labelsPath = "../data/YOLO/coco.names"
weightsPath = "../data/YOLO/yolov3.weights"
configPath = "../data/YOLO/yolov3.cfg"
modelPath = "../data/YOLO/yolov3_model.h5"

#Loop over all data sequence
dataBuffer = []
plot_results = Plot()
view = View()
LP = LidarProcessing()
CP = CameraProcessing()

#YOLO
params = dict()
params["confidence"] = 0.2
params["threshold"] = 0.4
params["shrink"] = 0.1

#LiDAR
#Kitti
x_range, y_range, z_range, scale = (-20, 20), (-20, 20), (-2, 2), 10
v_fov, h_fov, max_d = (-24.9, 2.0), (-90, 90), 70
#Udacity
Example #29
0
	def plot(self, spring = False): 
		"make a 3D plot of the current morphology"
		# plot settings
		fig = plt.figure(facecolor='white', figsize = (10, 4))

		if self.environment.threeD:
			ax = fig.gca(projection='3d')
			ax.set_zlabel('Z axis')
			ax.view_init(azim=-90, elev=90)
		else: 
			ax = fig.gca()
		ax.set_xlabel('X axis')
		ax.set_ylabel('Y axis')

		radius = 0.3
		def createSpring(xStart, yStart, xEnd, yEnd):
			radius = 0.5
			xLen = xEnd - xStart
			yLen = yEnd - yStart
			len  = np.sqrt(xLen ** 2 + yLen** 2)

			# Rescale
			xStartNew = xStart + radius * xLen / len
			yStartNew = yStart + radius * yLen / len
			xEndNew = xEnd - radius * xLen / len
			yEndNew = yEnd - radius * yLen / len

			xLen = xStartNew - xEndNew
			yLen = yStartNew - yEndNew
			len  = np.sqrt(xLen ** 2 + yLen** 2)

			heightRatio = 2
			twistLen = 0.15
			linewidth = 2
			noTwist = int(len / twistLen)
			xSpace = (xEndNew - xStartNew) * 1.0 / (noTwist)
			ySpace = (yEndNew - yStartNew) * 1.0 / (noTwist)

			X = [xStart, xStartNew]
			Y = [yStart, yStartNew]

			for i in range(noTwist):
				X.append((i + 0.5) * xSpace + xStartNew + (-1)**i * ySpace * heightRatio )
				Y.append((i + 0.5) * ySpace + yStartNew - (-1)**i * xSpace * heightRatio )
	
			X.append(xEndNew)
			X.append(xEnd)
			Y.append(yEndNew)
			Y.append(yEnd)

			plt.plot(X,Y, 'k', linewidth = linewidth)

		# plot lines
		for i, j in itertools.product(range(self.noNodes), range(self.noNodes)):
			if  i < j and self.connections[i,j]:
				mat = np.array([self.initialPos.get(i), self.initialPos.get(j)])
				if np.shape(mat)[1] == 3:
					ax.plot(mat[:,0], mat[:,1], mat[:,2])
				elif spring:
					createSpring(mat[0,0], mat[0,1], mat[1,0], mat[1,1] )
				else: ax.plot(mat[:,0], mat[:,1])
		
		if np.shape(mat)[1] == 3:
			ax.plot(self.initialPos.x, self.initialPos.y, self.initialPos.z, 'k.', markersize = 10)
		else: Plot.circles(self.initialPos.x, self.initialPos.y, radius, 'k')

		if self.environment.threeD:
			xmin , xmax = ax.get_xlim3d()
			margin = 1
			xmin -= margin
			xmax += margin
			x = np.expand_dims( np.arange(xmin, xmax), axis =0)
			z = np.expand_dims( np.arange(xmin, xmax), axis = 1)
			X = np.tile(x,np.shape(z))
			Z = np.tile(z,np.shape(x))
			Y = np.zeros_like(Z)
			Plot.setAxes3Dplot(ax, *ax.get_xlim3d()) # not general
			ax.plot_surface(X, Y, Z, rstride=400, cstride=400, color='#EEEEEE',shade = False)
		
		else:
			Plot.setAxes2Dplot(0.25)
			plt.axis("equal")
		plt.tight_layout(pad = 0)
		plt.axis('off')
		plt.show()
		return fig, ax
Example #30
0
log.info("Saving test data to {}".format(filename_test))
with h5py.File(filename_test, 'w') as hf:
    for var in column_names:
        hf.create_dataset( f'{var}', data=np.array(data_test[var]), chunks=True, maxshape= (None,), compression='lzf')

#============================================================================
# Plot reweighted data
#============================================================================
weights_train = [weight_trainEst10, weight_trainEst20, weight_trainEst40]
weights_train_names = ["weight_trainEst10", "weight_trainEst20", "weight_trainEst40"]

### Training data
for w, wn in zip(weights_train, weights_train_names):
    fig, ax = plt.subplots(2,2,figsize=(15,10))
    ax = ax.flatten()
    fig, ax[0] = Plot(Histogram(data_train['pho_eta'][trainMask], data_train["label"][trainMask], w, 120, -4, 4), fig, ax[0], r"$\eta$", includeN = True)
    ax[0].set_yscale('log')
    fig, ax[1] = Plot(Histogram(data_train['pho_et'][trainMask], data_train["label"][trainMask], w, 90, 0, 50), fig, ax[1], r"$E_T$", includeN = False)
    ax[1].set_yscale('log')
    fig, ax[2] = Plot(Histogram(data_train['correctedScaledActualMu'][trainMask], data_train["label"][trainMask], w, 90, 0, 90), fig, ax[2], r"$\langle\mu\rangle$", includeN = False)
    ax[2].set_yscale('log')

    counts_weight, edges_weight = np.histogram(w, bins=120, range=(0, 40))
    ax[3].step(x=edges_weight, y=np.append(counts_weight, 0), where="post", color = "k");
    ax[3].set_yscale('log', nonposy='clip')
    ax[3].set(xlabel = wn, ylabel = "Events per bin")
    fig.savefig(args.outdir + "train_reweighted" + wn + ".pdf")


### Validation data
weights_valid = [weight_validEst10, weight_validEst20, weight_validEst40]
Example #31
0
    def run(self):

        plot = Plot(num_subplots=2, subplot_title=["average score", "entropy"])

        if self.render_period > 0:
            r = self.render()
            print(f"reward={r}")

        for episode in range(self.num_episodes):

            states_history = []
            actions_history = []
            critic_values_history = []
            action_probs_history = []
            rewards_history = []
            episode_steps = 0
            done = False

            t_start = time.time()
            state = self.env.reset()
            self.policy.reset_states()
            episode_reward = 0
            episode += 1

            # play and record states, actions, action probabilities, rewards
            with tf.GradientTape() as tape:
                while not done:
                    # state shape is (number_of_timesteps, state_size) with number_of_timesteps=1
                    state = state[np.newaxis, ...]
                    action, action_probs, critic_value = self.action(state)
                    next_state, reward, done, _ = self.env.step(action)
                    states_history.append(state)
                    actions_history.append(action)
                    rewards_history.append(reward)

                    # using index 0 because of output batch size of 1
                    action_probs_history.append(action_probs[0])
                    critic_values_history.append(critic_value[0])

                    state = next_state
                    episode_reward += reward
                    episode_steps += 1

                # train policy with recorded values
                actor_loss, critic_loss = self.replay(actions_history,
                                                      action_probs_history,
                                                      critic_values_history,
                                                      rewards_history)
                loss = actor_loss + critic_loss
                grads = tape.gradient(loss, self.policy.trainable_variables)
                self.optimizer.apply_gradients(
                    zip(grads, self.policy.trainable_variables))

            # update average entropy and reward
            entropy = self.entropy(np.array(action_probs_history))
            average_entropy, self.entropies = self.update_average(
                self.entropies, entropy)
            average_reward, self.episode_rewards = self.update_average(
                self.episode_rewards, episode_reward)

            # plot averages
            if self.plot:
                plot.update(value=average_reward, idx=0)
                plot.update(value=average_entropy, idx=1)

            # update learning rate
            if (average_reward > self.best_average_reward):
                self.best_average_reward = average_reward
                lr = self.optimizer._decayed_lr('float32').numpy()
                if (average_reward > self.lr_update_threshold):
                    self.lr_update_threshold = average_reward * 1.5
                    lr = lr * 0.99
                    K.set_value(self.optimizer.learning_rate, lr)
                    print(
                        f"new max average={average_reward}/{self.env.spec.reward_threshold}; learning rate={lr:.7f}"
                    )

            # render test
            if self.render_period > 0 and (episode % self.render_period) == 0:
                r = self.render()
                print(f"test episode={episode} reward={r}")

            # print episode summary
            t_end = time.time()
            if episode % 10 == 0:
                print("episode: {}/{} fps={:.2f} reward last/avg/max_avg: {:.2f}/{:.2f}/{:.2f} loss actor={:.5f} critic={:.5f} entropy={:.5f} sequence len={:d}"\
                      .format(episode,  self.num_episodes, len(rewards_history) / (t_end - t_start),\
                              episode_reward, average_reward, self.best_average_reward,
                              actor_loss, critic_loss, average_entropy, len(rewards_history)))

            # exit if solved
            if self.best_average_reward > self.env.spec.reward_threshold:
                print(
                    f"completed in {episode} episodes;  average score={self.best_average_reward} threshold={self.env.spec.reward_threshold}"
                )
                break
Example #32
0
BSMO_NET, BSMO_CHID, R, Trash = Optimizer_Array[2](INIT_NET,
                                                   Alive_Node,
                                                   R=R,
                                                   First=True,
                                                   Update=True)
SSMOMH_NET, SSMOMH_CHID, R, Trash = Optimizer_Array[3](INIT_NET,
                                                       Alive_Node,
                                                       R=R,
                                                       First=True,
                                                       Update=True)

CHID_Arr = [INIT_CHID, LEACHC_CHID, PSOC_CHID, BSMO_CHID, SSMOMH_CHID]

network_array = [INIT_NET, LEACHC_NET, PSOC_NET, BSMO_NET, SSMOMH_NET]
plt.figure(1)
Plot.Topology(network_array, CHID_Arr, [2, 3])

## Plot Energy Consume Data
plt.figure(2)
FHL_ALL = []
PACKET_ALL = []
Time = []
for i in range(0, len(Optimizer_Array)):
    FHL = []
    FHL, PACKET, _TIME = Plot.EnergyConsume(INIT_NET,
                                            Optimizer_Array[i],
                                            Alive_Node,
                                            Label=cf.Optimizer[i + 1],
                                            Alive=1,
                                            Total=0,
                                            Average=1,
Example #33
0
from utils import Plot
import noise
import math

SQRT_2 = 1.41421356237

p = Plot()


def rotate(x, y, angle):
    x -= 50
    y -= 50
    point = (
        x * math.cos(angle) - y * math.sin(angle),
        x * math.sin(angle) + y * math.cos(angle),
    )
    return (
        point[0] + 50,
        point[1] + 50,
    )


def in_canvas(point):
    return (point[0] >= 0 and point[0] <= 100 and point[1] >= 0
            and point[1] <= 100)


def draw_color(angle, threshold, noise_scale, noise_offset):
    num_rows = 100
    num_cols = 100
    grid_size = 100 * SQRT_2