def adaline_test_i_b(): input_file = "bitmap_data.csv" df = pd.read_csv(input_file, header=None) df.head() # X: values, y: targets # extract features X = df.iloc[:, 0:77].values X = add_biases(X) # extract the label column y = df.iloc[:, 77].values # Number 5: y_5 = np.where(y == 5, 1, 0) # Number 6: y_6 = np.where(y == 6, 1, 0) # Number 8: y_8 = np.where(y == 8, 1, 0) # Number 9: y_9 = np.where(y == 9, 1, 0) sets = [y_5, y_6, y_8, y_9] predictions = [] y_test = [] i = 0 for set in sets: # split data into train and test sets X_train = np.concatenate((X[:8], X[11:19], X[22:30], X[33:41])) X_test = np.concatenate((X[8:11], X[19:22], X[30:33], X[41:])) y_train = np.concatenate((set[:8], set[11:19], set[22:30], set[33:41])) y_test_tmp = np.concatenate( (set[8:11], set[19:22], set[30:33], set[41:])) if(i == 0): # only print the first time print_bitmap(X_test) y_test.append(y_test_tmp) # print(X_train) # print(y_train) # live plot plot = True # 3d plot d3 = False adaline_implementation(y_train, y_test_tmp, X_train, X_test, plot, d3)
def adaline_test_i_b(): # import and ready house prediction data train_df = pd.read_csv( 'https://firebasestorage.googleapis.com/v0/b/bible-project-2365c.appspot.com/o/train.csv?alt=media&token=9c5d17c2-0589-43ea-b992-e7c2ad02d714', index_col='ID') train_df.head() test_df = pd.read_csv( 'https://firebasestorage.googleapis.com/v0/b/bible-project-2365c.appspot.com/o/test.csv?alt=media&token=99688b27-9fdb-4ac3-93b8-fa0e0f4d7540', index_col='ID') test_df.head() predictors = [ 'crim', 'zn', 'indus', 'chas', 'nox', 'rm', 'age', 'dis', 'rad', 'tax', 'ptratio', 'black', 'lstat' ] target = 'zn' scaler = MinMaxScaler(feature_range=(0, 1)) # Scale both the training inputs and outputs scaled_train = scaler.fit_transform(train_df) # scaled_test = scaler.transform(test_df) # Print out the adjustment that the scaler applied to the total_earnings column of data # print("Note: median values were scaled by multiplying by {:.10f} and adding {:.6f}".format( # scaler.scale_[13], scaler.min_[13])) multiplied_by = scaler.scale_[13] ded = scaler.min_[13] scaled_train_df = pd.DataFrame(scaled_train, columns=train_df.columns.values) # x: values, y: targets X = scaled_train_df.drop(target, axis=1).values Y = scaled_train_df[[target]].values X_train, X_test, y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=123) # live plot plot = True # 3d plot d3 = True adaline_implementation(y_train, Y_test, X_train, X_test, plot, d3)
def adaline_test_i_b(): # import and ready input file input_file = "iris.csv" df = pd.read_csv(input_file, header=None) df.head() # X: values, y: targets # extract features X = df.iloc[:, 0:4].values # extract the label column y = df.iloc[:, 4].values # normalize the data attributes X = preprocessing.normalize(X) # TODO # add biases X = add_biases(X) # Setosa: y_setosa = np.where(y == 'Setosa', 1, 0) # Versicolor: y_versicolor = np.where(y == 'Versicolor', 1, 0) # Virginica: y_virginica = np.where(y == 'Virginica', 1, 0) sets = [y_setosa, y_versicolor, y_virginica] predictions = [] y_test = [] i = 0 for set in sets: # split data into train and test sets X_train, X_test, y_train, y_test_tmp = train_test_split( X, set, test_size=0.2, random_state=123) y_test.append(y_test_tmp) # live plot plot = True # 3d plot d3 = True adaline_implementation(y_train, y_test_tmp, X_train, X_test, plot, d3)
def main(): plotting = int(input("0. Live Plot\n1. Only Results\n") or 0) live_plotting = plotting == 0 X, y, is3d, isIris = load_file() clusters_num = int(input("Δώσε αριθμό κέντρων: ") or 2) max_iter = int(input('Δώσε αριθμό μέγιστων επαναλύψεων: ') or 300) update_plot = int(input("Update Plot ανά πόσες εποχές: ") or max_iter / 10) tol = float(input('Δώσε tolerance: ') or 0.001) km = KMeans(k=clusters_num, max_iter=max_iter, tol=tol) km.fit(X, y, live_plotting, update_plot, is3d, isIris) dMax = max([np.abs(c1 - c2) for c1 in km.centroids for c2 in km.centroids]) std = dMax / np.sqrt(2 * clusters_num) patterns = [] targets = [] for classification in km.classifications: for featureset in km.classifications[classification]: tmp = [] for centroid in km.centroids: r = rbf(featureset, km.centroids[centroid], std) tmp.append(r) tmp = np.array(tmp) patterns.append(tmp) targets.append(classification) patterns = np.array(patterns) targets = np.array(targets) patterns = add_biases(patterns) patterns_train, patterns_test, targets_train, targets_test = train_test_split( patterns, targets, test_size=0.2, random_state=123) adaline_implementation(targets_train, targets_test, patterns_train, patterns_test, plot=live_plotting, d3=False)
def adaline_test_i_b(): file = input("Δώσε input file(a,b,c,d): ") input_file = 'data_package_%s.csv' % file df = pd.read_csv(input_file, header=0) df = df._get_numeric_data() # targets targets_file = 'data_package_values_%s.csv' % file targets_df = pd.read_csv(targets_file, header=0) targets_df = targets_df._get_numeric_data() values = df.values targets = targets_df.values # plot_data(targets) values = add_biases(values) patterns_train, patterns_test, targets_train, targets_test = train_test_split( values, targets, test_size=0.2, random_state=123) # plot_testing_data(targets_train, targets_test) plot = True d3 = False adaline_implementation(targets_train, targets_test, patterns_train, patterns_test, plot, d3)