def draw_decision_boundary(theta, X, Y, orX, mu, sigma): plt.figure() x0_min, x0_max = np.min(orX), np.max(orX) arrayX = np.arange(x0_min, x0_max, 0.05) arrayX = np.reshape(arrayX, (np.shape(arrayX)[0], 1)) arrayXaux = Normalization.normalize2(generate_polynom_data(arrayX, 8), mu, sigma) arrayXaux = Data_Management.add_column_left_of_matrix(arrayXaux) theta = np.reshape(theta, (np.shape(theta)[0], 1)) arrayY = h(arrayXaux, theta.T) plt.plot(arrayX, arrayY) plt.scatter(orX, Y, 20,marker='$F$',color= "red") plt.show()
def paint_graphic(X, y, true_score, theta1, theta2, mu, sigma): figure, ax = plt.subplots() pos = (y == 1).ravel() neg = (y == 0).ravel() plt.scatter(X[pos, 0], X[pos, 1], color='blue', marker='o', label = "Legendary") plt.scatter(X[neg, 0], X[neg, 1], color='black', marker='x', label = "Non legendary") x0_min, x0_max = X[:,0].min(), X[:,0].max() x1_min, x1_max = X[:,1].min(), X[:,1].max() xx1, xx2 = np.meshgrid(np.linspace(x0_min, x0_max), np.linspace(x1_min, x1_max)) aux = np.c_[ xx1.ravel(), xx2.ravel()] p, aux = polinomial_features(aux, 5) aux = Normalization.normalize(aux[:, 1:], mu, sigma) sigm = forward_propagate(aux, theta1, theta2)[4] sigm = np.reshape(sigm, np.shape(xx1)) plt.contour(xx1, xx2, sigm, [0.5], linewidths = 1, colors=['red', 'purple']) #formatting the graphic with some labels plt.xlabel("weight_kg") plt.ylabel("speed") plt.suptitle(("Score: " + str(float("{0:.3f}".format(true_score))))) figure.legend() #set the labels to non-normalized values figure.canvas.draw() labels = [item for item in plt.xticks()[0]] for i in range(len(labels)): labels[i] = int(round((labels[i] * sigma[0, 0]) + mu[0, 0], -1)) ax.xaxis.set_ticklabels(labels) labels = [item for item in plt.yticks()[0]] for i in range(len(labels)): labels[i] = int(round((labels[i] * sigma[0, 1]) + mu[0, 1], -1)) ax.yaxis.set_ticklabels(labels) plt.show()
return (poly.fit_transform(X)) # X, y = Data_Management.load_csv_types_features("pokemon.csv",['against_bug', 'against_dark','against_dragon','against_electric', # 'against_fairy','against_fight','against_fire','against_flying', # 'against_ghost','against_grass','against_ground','against_ice','against_normal', # 'against_poison','against_psychic','against_rock','against_steel','against_water']) X, y = Data_Management.load_csv_types_features("pokemon.csv", [feature1, feature2]) # TODO: usar el tipo2 para sacar el score tambien (si mi svm predice 1 y una de las dos y es 1, es truePositive++) y dar el resultado con solo # 1 tipo, todo lo del entrenamiento se queda igual (se entrena para un solo tipo). Luego en el score se hace eso y para predecir el tipo se queda igual. # Tambien puedo sacar dos svm, tipo primario y tipo secundario pero mas lio ? X = polynomial_features(X, grado) X, mu, sigma = Normalization.normalize_data_matrix(X[:, 1:]) X = Data_Management.add_column_left_of_matrix(X) trainX, trainY, validationX, validationY, testingX, testingY = divideRandomGroups( X, y) svms = [] for j in range(18): currentTrainY = (trainY == j) * 1 currentValidationY = (validationY == j) * 1 currentTestingY = (testingY == j) * 1 current_svm, C, s = eleccion_parametros_C_y_Sigma(trainX, currentTrainY, validationX, currentValidationY, mu, sigma)
cost = np.array([], dtype=float) auxCost = sys.maxsize while True: minimize(X, Y, m, n, theta) cost = np.append(cost, J(X, Y, theta)) if abs(auxCost - cost[-1]) < 1e-4: break #Stops the loop when we reach the convergence value of 10^-4 auxCost = cost[-1] return theta, cost data = Data_Management.load_csv(sys.argv[1]) X, Y, m, n = Data_Management.get_parameters_from_training_examples(data) X_norm, mu, sigma = Normalization.normalize_data_set(X) theta, cost = gradient_descent_loop(X_norm, Y, m, n) draw_cost(cost) if n == 1: #provisional draw_points_plot(X_norm, Y, theta) A, B, Z = make_paint_data(X, Y) draw_cost_3D(A, B, Z) draw_contour(A, B, Z) #Print theta's values and ask for predictions print("Values of theta: " + str(theta) + "\n") while True: user_values = np.array(list( map(float, input("Enter query values: ").split())),
elif checker[i] == 0 and y[i] == 1: falseNegatives += 1 if truePositives == 0: return 0 recall = (truePositives / (truePositives + falseNegatives)) precision = (truePositives / (truePositives + falsePositives)) score = 2 * (precision * recall / (precision + recall)) return score graphic_attr_names = ["capture_rate", "base_egg_steps"] X, y = Data_Management.load_csv_svm("pokemon.csv", graphic_attr_names) X, mu, sigma = Normalization.normalize_data_matrix(X) X, y, trainX, trainY, validationX, validationY, testingX, testingY = Data_Management.divide_legendary_groups( X, y) #------------------------------------------------------------------------------------------------- allMaxPercent = [] allMaxElev = [] allMaxPoly = [] allMaxThetas = [] Xused = validationX Yused = validationY for t in range(NUM_TRIES):
def normalize_matrix(X): return Normalization.normalize_data_matrix(X)
plt.plot(arrayX, arrayY) plt.scatter(orX, Y, 20,marker='$F$',color= "red") plt.show() def draw_plot(X, Y): plt.plot(X, Y) data = loadmat('ex5data1.mat') X, y, Xval, yval, Xtest, ytest = data['X'], data['y'], data['Xval'], data['yval'], data['Xtest'], data['ytest'] XPoly = generate_polynom_data(X, 8) XPoly, mu, sigma = normalize_matrix(XPoly) mu = np.reshape(mu, (1, np.shape(mu)[0])) sigma = np.reshape(sigma, (1, np.shape(sigma)[0])) XPoly = Data_Management.add_column_left_of_matrix(XPoly) XPolyVal = Normalization.normalize2(generate_polynom_data(Xval, 8), mu, sigma) XPolyVal = Data_Management.add_column_left_of_matrix(XPolyVal) XPolyTest = Normalization.normalize2(generate_polynom_data(Xtest, 8), mu, sigma) XPolyTest = Data_Management.add_column_left_of_matrix(XPolyTest) lambdaAux = [ 0, 0.001, 0.003, 0.01, 0.03, 0.1, 0.3, 1, 3, 10 ] error_array = np.array([], dtype=float) error_array_val = np.array([], dtype=float) thetas = np.array([], dtype=float) for l in range(len(lambdaAux)): theta = np.ones(XPoly.shape[1], dtype=float) theta_min = sciMin(fun=minimizar, x0=theta, args=(XPoly, y, lambdaAux[l]), method='TNC', jac=True,