I am trying to predict a classification using logistic regression, based on training data, for a series of points in testing data. I am getting an output, without errors, but the results, I am told, are wrong (the incorrect output would be considered an error, but there are no errors when running the code). The training data is a collection of 4 different categories in 375 points, there are 3 variates per point, so plotting on a 3D graph. I ran a counting program on the set and found that over 50% of the points are in classification 2. My initial results were all testing points came up as class 2. I have tried sorting the training data into several different sets: random selection of 125 entries (which is the size of the testing data), and finding the min number of all the categories, and creating a training set using the same number of points from each category. No sorting = all class 2 random sorting = all class 2 equal class number sorting = gives me an answer that has points classified in all 4 categories, but when I plug them into the online final test form, I have an accuracy score of 26%, which is the same as random chance. So, I am not processing data correctly, and I am not sure where. I am hoping someone with more experience with regression classification can point me in the right direction. Edit: Do I need to reformat (transform) the train_X, train_y, and test_X arrays before I call LogisticRegression? If so, how? Maybe I am just feeding it malformed data. If you want to grab the code with the input data lists, you can go here: text If you just want to look at the code, here ya go: # Needed imports import datetime import os import pandas as pd from sklearn.linear_model import LogisticRegression from sklearn.preprocessing import normalize data_dir = "./data" # start process timer start_time = datetime.datetime.now() # Load data from files - training data df_train_time_series = pd.read_csv(os.path.join(data_dir, "train_time_series.csv")) df_train_labels = pd.read_csv(os.path.join(data_dir, "train_labels.csv")) # Load data from files - test data df_test_time_series = pd.read_csv(os.path.join(data_dir, "test_time_series.csv")) df_test_labels = pd.read_csv(os.path.join(data_dir, "test_labels_input.csv")) # functional processes def train_data(train_ts, train_l): """Grabbing the training data imported from files, and forming it into table that I can work with.""" train_table = [] for i in range(len(train_l)): x = 0 y = 0 z = 0 ind = None for j in range(len(train_ts)): ind = ( train_l.orig_index.iloc if train_ts.timestamp.iloc[j] == train_l.timestamp.iloc else None ) if ind: x = train_ts.x.iloc[j] y = train_ts.y.iloc[j] z = train_ts.z.iloc[j] break if ind: train_table.append([x, y, z, train_l.label.iloc]) return train_table def test_data(test_ts, test_l): """Grabbing the test data imported from files, and forming it into table that I can work with.""" test_table = [] for i in range(len(test_l)): x = 0 y = 0 z = 0 time_stamp = test_l.timestamp.iloc utc_time = test_l["UTC time"].iloc ind = None for j in range(len(test_ts)): ind = ( test_l.orig_index.iloc if test_ts.timestamp.iloc[j] == test_l.timestamp.iloc else None ) if ind: x = test_ts.x.iloc[j] y = test_ts.y.iloc[j] z = test_ts.z.iloc[j] break if ind: test_table.append([ind, time_stamp, utc_time, x, y, z, 0, 0]) return test_table def sort_results(test, predict, prob): """Grabbing the logistic predictions and probabilities, and forming them into a results table.""" results = [] for i in range(len(test)): results.append( [ test.orig_index.iloc, test.timestamp.iloc, test["UTC time"].iloc, predict, max(prob), ] ) return results def sift_training_by_classification(train): """The training table has over 50% of classification 2, so this function counts the number of entries for each classification, grabs the minimum count, and then selects that many entries from each category. Without making the counts even I can only get results of all classification 2.""" t1 = 0 t2 = 0 t3 = 0 t4 = 0 for i in range(len(train)): t1 += 1 if train.label.iloc == 1 else 0 t2 += 1 if train.label.iloc == 2 else 0 t3 += 1 if train.label.iloc == 3 else 0 t4 += 1 if train.label.iloc == 4 else 0 min_count = min(t1, t2, t3, t4) print("min count: ", min_count) s1 = 0 s2 = 0 s3 = 0 s4 = 0 train_sift = [] for j in range(len(train)): label = train.label.iloc[j] if label == 1 and s1 <= min_count: train_sift.append([train.x.iloc[j], train.y.iloc[j], train.z.iloc[j], train.label.iloc[j]]) s1 += 1 if label == 2 and s2 <= min_count: train_sift.append([train.x.iloc[j], train.y.iloc[j], train.z.iloc[j], train.label.iloc[j]]) s2 += 1 if label == 3 and s3 <= min_count: train_sift.append([train.x.iloc[j], train.y.iloc[j], train.z.iloc[j], train.label.iloc[j]]) s3 += 1 if label == 4 and s4 <= min_count: train_sift.append([train.x.iloc[j], train.y.iloc[j], train.z.iloc[j], train.label.iloc[j]]) s4 += 1 return train_sift # sifting data train_table = pd.DataFrame( train_data(df_train_time_series, df_train_labels), columns=["x", "y", "z", "label"] ) # Using the full training data set gives me results of all classification 2. So, I am trying different sifting methods to try and create better results. # creating a table with an equal number of classifications. This gives me results that include a mix of all categories, but the teacher says they are wrong. train_table = pd.DataFrame(sift_training_by_classification(train_table), columns=["x", "y", "z", "label"]) #creating a table with a random selection of entries, with a quantity that is equal to the testing data. This results in all classification 2. # train_table = train_table.sample(n=125) #Using the base table, without modification. This results in all classification 2. # train_table = train_table test_table = pd.DataFrame( test_data(df_test_time_series, df_test_labels), columns=["orig_index", "timestamp", "UTC time", "x", "y", "z", "label", "accuracy"], ) # printing tables to colsole for testing purposes print("train table", train_table) print("test table", test_table) # forming tables to push through logistic regression train_X = [] train_y = [] for i in range(len(train_table)): train_X.append( [train_table.x.iloc, train_table.y.iloc, train_table.z.iloc] ) train_y.append(train_table.label.iloc) test_X = [] for k in range(len(test_table)): test_X.append([test_table.x.iloc[k], test_table.y.iloc[k], test_table.z.iloc[k]]) # Trying with and without normalize clf = LogisticRegression().fit(normalize(train_X), train_y) # clf = LogisticRegression().fit(train_X, train_y) predict = clf.predict(test_X[:]) prob = clf.predict_proba(test_X[:]) results = pd.DataFrame( sort_results(test_table, predict, prob), columns=["", "timestamp", "UTC time", "label", "accuracy"], ) # record results to file results.to_csv(os.path.join(data_dir, "test_labels.csv"), sep=",", index=False) # end_process timer end_time = datetime.datetime.now() # print elasped time to console print("Run time: " + str(end_time - start_time)[5:] + " s.ms") # print results to console. printing for testing purposes label_list = "" for i in range(len(results)): label_list = label_list + str(results.label.iloc[i]) + "," print(label_list) [url="https://stackoverflow.com/questions/79057824/python-logisticregression-not-returning-correct-results"]Continue reading...[/url][/i]