iterations = 50 k = 11 kernel = "linear" knn_array = np.empty(0) knn_array_2 = np.empty(0) knnprob_array = np.empty(0) wknn_array = np.empty(0) wknnprob_array = np.empty(0) lr_array = np.empty(0) svm_array = np.empty(0) test_var = 2 for j in range(iterations): trainsets, testsets = CrossValidation(df, TrainTestdiv) feat = ["radius_mean", "texture_mean", "perimeter_mean", "area_mean", "smoothness_mean", "compactness_mean", "concavity_mean", "concave points_mean", "symmetry_mean", "fractal_dimension_mean", "radius_se", "texture_se", "perimeter_se", "area_se", "smoothness_se", "compactness_se", "concavity_se", "concave points_se", "symmetry_se", "fractal_dimension_se", "radius_worst", "texture_worst", "perimeter_worst", "area_worst", "smoothness_worst", "compactness_worst", "concavity_worst", "concave points_worst", "symmetry_worst", "fractal_dimension_worst"] wfeat = ["texture_worst", "perimeter_worst", "smoothness_worst", "concave points_worst", "symmetry_worst"] #feat = wfeat for i in range(len(testsets)): train = trainsets[i] test = testsets[i] knn = score(train, test, feat, "knn", k, kernel)[test_var] knn_array = np.append(knn_array, knn)
traintestdiv) # Dividing Dataframes into train, validation, and test set test_class = test["diagnosis"] test_class = np.array(test_class) test_features = test[[ "radius_mean", "texture_mean", "perimeter_mean", "area_mean", "smoothness_mean", "compactness_mean", "concavity_mean", "concave points_mean", "symmetry_mean", "fractal_dimension_mean", "radius_se", "texture_se", "perimeter_se", "area_se", "smoothness_se", "compactness_se", "concavity_se", "concave points_se", "symmetry_se", "fractal_dimension_se", "radius_worst", "texture_worst", "perimeter_worst", "area_worst", "smoothness_worst", "compactness_worst", "concavity_worst", "concave points_worst", "symmetry_worst", "fractal_dimension_worst" ]] for j in range(iterations): trains, validations = CrossValidation(trainset, crossvalidationdiv) accuracy_list = [] for i in range(len(trains)): train = trains[i] validation = validations[i] # Specifiying Features train_features = np.array(train[[ "radius_mean", "texture_mean", "perimeter_mean", "area_mean", "smoothness_mean", "compactness_mean", "concavity_mean", "concave points_mean", "symmetry_mean", "fractal_dimension_mean", "radius_se", "texture_se", "perimeter_se", "area_se", "smoothness_se", "compactness_se", "concavity_se", "concave points_se", "symmetry_se", "fractal_dimension_se", "radius_worst", "texture_worst", "perimeter_worst", "area_worst",
correct += 1 else: incorrect += 1 accuracy = (correct / (correct + incorrect)) * 100 print( "The accuracy of one iteration through a Train-Test split in the Wisconsin database is: ", accuracy) ############################################# UNCOMMENT FOR FINDING SCORE WITH CROSSFOLD TESTING data = pd.read_csv("data.csv") df = pd.DataFrame(data) # Dividing Dataframes into train and test set trains, tests = CrossValidation(df, 20) accuracy_list = [] for i in range( len(trains)): # Looping through each train and test set together train = trains[i] test = tests[i] # Specifiying Features train_features = np.array(train[[ "radius_mean", "texture_mean", "perimeter_mean", "area_mean", "smoothness_mean", "compactness_mean", "concavity_mean", "concave points_mean", "symmetry_mean", "fractal_dimension_mean", "radius_se", "texture_se", "perimeter_se", "area_se", "smoothness_se", "compactness_se", "concavity_se", "concave points_se", "symmetry_se", "fractal_dimension_se", "radius_worst", "texture_worst",
def joint_score_prev_current_next(training, features, classes, labels, crossvalidationdiv, iterations, k, w1, w2, w3, method1, method2): y1_accuracy = 0 y2_accuracy = 0 y3_accuracy = 0 y1_loss = 0 y2_loss = 0 y3_loss = 0 y1_specificity = 0 y2_specificity = 0 y3_specificity = 0 y1_sensitivity = 0 y2_sensitivity = 0 y3_sensitivity = 0 y1_accuracy_means_list = np.empty(0) y2_accuracy_means_list = np.empty(0) y3_accuracy_means_list = np.empty(0) y1_loss_means_list = np.empty(0) y2_loss_means_list = np.empty(0) y3_loss_means_list = np.empty(0) y1_specificity_means_list = np.empty(0) y2_specificity_means_list = np.empty(0) y3_specificity_means_list = np.empty(0) y1_sensitivity_means_list = np.empty(0) y2_sensitivity_means_list = np.empty(0) y3_sensitivity_means_list = np.empty(0) for i in range(iterations): training_sets, validation_sets = CrossValidation( training, crossvalidationdiv) y1_accuracy_list = np.empty(0) y2_accuracy_list = np.empty(0) y3_accuracy_list = np.empty(0) y1_loss_list = np.empty(0) y2_loss_list = np.empty(0) y3_loss_list = np.empty(0) y1_specificity_list = np.empty(0) y2_specificity_list = np.empty(0) y3_specificity_list = np.empty(0) y1_sensitivity_list = np.empty(0) y2_sensitivity_list = np.empty(0) y3_sensitivity_list = np.empty(0) for j in range(len(training_sets)): training_set = training_sets[j] validation_set = validation_sets[j] if (w1 != 0).all(): y1 = joint_score(training_set, validation_set, features, classes, labels, k, w1, method1, method2) #print(y1) y1_accuracy_list = np.append(y1_accuracy_list, y1[1]) y1_loss_list = np.append(y1_loss_list, y1[0]) y1_sensitivity_list = np.append(y1_sensitivity_list, y1[2]) y1_specificity_list = np.append(y1_specificity_list, y1[3]) if (w2 != 0).all(): y2 = joint_score(training_set, validation_set, features, classes, labels, k, w2, method1, method2) #print(y2) y2_accuracy_list = np.append(y2_accuracy_list, y2[1]) y2_loss_list = np.append(y2_loss_list, y2[0]) y2_sensitivity_list = np.append(y2_sensitivity_list, y2[2]) y2_specificity_list = np.append(y2_specificity_list, y2[3]) if (w3 != 0).all(): y3 = joint_score(training_set, validation_set, features, classes, labels, k, w3, method1, method2) #print(y3) y3_accuracy_list = np.append(y3_accuracy_list, y3[1]) y3_loss_list = np.append(y3_loss_list, y3[0]) y3_sensitivity_list = np.append(y3_sensitivity_list, y3[2]) y3_specificity_list = np.append(y3_specificity_list, y3[3]) if (w1 != 0).all(): y1_accuracy_means_list = np.append(y1_accuracy_means_list, mean(y1_accuracy_list)) y1_loss_means_list = np.append(y1_loss_means_list, mean(y1_loss_list)) y1_sensitivity_means_list = np.append(y1_sensitivity_means_list, mean(y1_sensitivity_list)) y1_specificity_means_list = np.append(y1_specificity_means_list, mean(y1_specificity_list)) if (w2 != 0).all(): y2_accuracy_means_list = np.append(y2_accuracy_means_list, mean(y2_accuracy_list)) y2_loss_means_list = np.append(y2_loss_means_list, mean(y2_loss_list)) y2_sensitivity_means_list = np.append(y2_sensitivity_means_list, mean(y2_sensitivity_list)) y2_specificity_means_list = np.append(y2_specificity_means_list, mean(y2_specificity_list)) if (w3 != 0).all(): y3_accuracy_means_list = np.append(y3_accuracy_means_list, mean(y3_accuracy_list)) y3_loss_means_list = np.append(y3_loss_means_list, mean(y3_loss_list)) y3_sensitivity_means_list = np.append(y3_sensitivity_means_list, mean(y3_sensitivity_list)) y3_specificity_means_list = np.append(y3_specificity_means_list, mean(y3_specificity_list)) if (w1 != 0).all(): y1_accuracy = mean(y1_accuracy_means_list) y1_loss = mean(y1_loss_means_list) y1_sensitivity = mean(y1_sensitivity_means_list) y1_specificity = mean(y1_specificity_means_list) if (w2 != 0).all(): y2_accuracy = mean(y2_accuracy_means_list) y2_loss = mean(y2_loss_means_list) y2_sensitivity = mean(y2_sensitivity_means_list) y2_specificity = mean(y2_specificity_means_list) if (w3 != 0).all(): y3_accuracy = mean(y3_accuracy_means_list) y3_loss = mean(y3_loss_means_list) y3_sensitivity = mean(y3_sensitivity_means_list) y3_specificity = mean(y3_specificity_means_list) if (w1 == 0).all(): y1_accuracy = 0 y1_loss = 0 y1_sensitivity = 0 y1_specificity = 0 if (w2 == 0).all(): y2_accuracy = 0 y2_loss = 0 y2_sensitivity = 0 y2_specificity = 0 if (w3 == 0).all(): y3_accuracy = 0 y3_loss = 0 y3_sensitivity = 0 y3_specificity = 0 return [[y1_loss, y2_loss, y3_loss], [y1_accuracy, y2_accuracy, y3_accuracy], [y1_sensitivity, y2_sensitivity, y3_sensitivity], [y1_specificity, y2_specificity, y3_specificity]]
incorrect += 1 accuracy = (correct / (correct + incorrect)) * 100 accuracy_list.append(accuracy) accuracy_average = mean(accuracy_list) print("The accuracy of", iterations, "iterations through a Train-Test split in the Wisconsin database is: ", accuracy_average) ########################################### Uncomment for finding average score with Crossfold Testing data = pd.read_csv("data.csv") df = pd.DataFrame(data) trains, tests = CrossValidation( df, 20) # Dividing Dataframes into train and test set k = 20 accuracy_list = [] # Looping through each train and test set together for i in range(len(trains)): train = trains[i] test = tests[i] # Specifiying Features train_features = np.array(train[[ "radius_mean", "texture_mean", "perimeter_mean", "area_mean", "smoothness_mean", "compactness_mean", "concavity_mean", "concave points_mean", "symmetry_mean", "fractal_dimension_mean", "radius_se", "texture_se", "perimeter_se", "area_se", "smoothness_se", "compactness_se", "concavity_se", "concave points_se", "symmetry_se",