def plot(loss_history, accuracies, f1_scores): plt.axis([0, 8000, 0, 1.0]) plt.plot(loss_history, label='loss') iter_ = np.arange(250, 8250, 250) plt.plot(iter_, accuracies, label='accuracy') plt.plot(iter_, f1_scores, label='f1_scores') plt.xlabel('Epochs') plt.ylabel('Percentage') plt.legend(bbox_to_anchor=(1.125, 1), prop={'size': 6}) plt.savefig('mix_plot.png')
def experiment_lr(): global max_accuracy, max_epoch, max_fscore, max_lr learning_rates = np.arange(0.001, 0.011, 0.001) plt.axis([0, 8000, 0, 1]) i = 1 iter_ = np.arange(500, 8500, 500) for lr in learning_rates: losses, accuracies, f1_scores = train_test(lr) plt.plot(losses, label='loss' + str(i)) plt.plot(iter_, accuracies, label='accuracy' + str(i)) plt.plot(iter_, f1_scores, label='fscores' + str(i)) i += 1 plt.xlabel('Epochs') plt.ylabel('Percentage') plt.legend(bbox_to_anchor=(1.125, 1), prop={'size': 6}) plt.savefig('mix_plot.png')
def best_3features_viz(occupied, non_occupied): # Apply normalization onto [0-1] range x_train = normalize() x_t = sklearn.feature_selection.SelectKBest(sklearn.feature_selection.chi2, k=3).fit_transform( x_train, y_train) fig = plt.figure() ax = Axes3D(fig) ax.scatter(x_t[non_occupied][:, 0], x_t[non_occupied][:, 1], x_t[non_occupied][:, 2], c='red') ax.scatter(x_t[occupied][:, 0], x_t[occupied][:, 1], x_t[occupied][:, 2], c='green') angles = np.arange(0, 380, 20) for angle in angles: ax.view_init(30, angle) plt.draw() plt.pause(.001) plt.savefig('best_3features.jpg')
return res def evaluate(arr): sz = len(arr) acc = 0 for i in range(sz): if y_test[i][0] == arr[i]: acc += 1 return acc / sz if __name__=='__main__': kernel = 'rbf' c = 0.125000 # Best C found in CV stage gamma = 2.000000 # Best gamma found in CV stage scaler = preprocessing.StandardScaler().fit(x_train) # As proposed in SVM Guide, use the same linear scaling for both training and test inputs. x_scaled_train = scaler.transform(x_train) x_scaled_test = scaler.transform(x_test) C = np.arange(c-c,c+c,c/5) Gamma = np.arange(gamma-gamma,gamma+gamma,gamma/5) for i in range(1,len(C)): for j in range(1,len(Gamma)): clf = svm.SVC(C=C[i], cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma=Gamma[j], kernel=kernel, max_iter=-1, probability=True, random_state=None, shrinking=True, verbose=False) clf.fit(x_scaled_train,prepare_data(y_train)) acc = evaluate(clf.predict(x_scaled_test)) print("C = %lf, Gamma = %lf => Accuracy: %lf,F1: %lf"%(C[i],Gamma[j],acc,f1_score(prepare_data(y_test), clf.predict(x_scaled_test), average='macro')))
t.join(timeout=150) if t.isAlive(): print("Thread with C= %lf, Gamma = %lf is timed out"%(t.c,t.gamma)) threads.clear() print("Proceeding with Grid Search") threadLock = threading.Lock() # Grid search if good_c - 0.5>0: min_c = good_c - 0.5 else: min_c = 0 if good_gamma - 0.5>0: min_gamma = good_gamma - 0.5 else: min_gamma = 0 C = [2**exp for exp in np.arange(min_c,good_c+0.5,0.1)] Gamma = [2**exp for exp in np.arange(min_gamma,good_gamma+0.5,0.1)] best_c = None best_gamma = None for i in range(0,len(C)): threads = [] for j in range(0,len(Gamma)): #print("At thread with C= %lf, Gamma = %lf"%(C[i],Gamma[j])) thread = CrossValidationThread(C[i],Gamma[j],x_scaled_train) thread.start() threads.append(thread) for t in threads: t.join() if t.isAlive(): print("Thread with C= %lf, Gamma = %lf is timed out"%(t.c,t.gamma)) threads.clear()
for i in pcs: plt.bar(r+i*bw, V[:,i], width=bw, ) plt.xticks(r+bw, pcaNames) plt.xlabel('Attributes') plt.ylabel('Component coefficients') plt.legend(legendStrs) plt.grid() plt.title('PCA Component Coefficients') plt.show() """ attributes = ['PC' + str(e + 1) for e in pcs] # attributes as legend legendStrs = pcaNames c = ['r', 'g', 'b'] bw = 0.1 r = np.arange(1, M + 1) for i, val in enumerate(pcaNames): plt.bar( r + i * bw, V[:, i], width=bw, ) plt.xticks(r + bw, attributes) plt.xlabel('Attributes') plt.ylabel('Component coefficients') plt.legend(legendStrs) plt.grid() plt.title('PCA Component Coefficients') plt.show()