From 34c6c99d61d81fc636ef3086920fdaf891110939 Mon Sep 17 00:00:00 2001 From: brotherzhafif Date: Sat, 12 Oct 2024 11:44:14 +0700 Subject: [PATCH] Refactoring Code With Classes for Ease of Modification --- Frequency.py | 69 ---------------- FrequencyTable.py | 89 +++++++++++++++++++++ Main.py | 22 +++-- __pycache__/Chart.cpython-38.pyc | Bin 0 -> 147 bytes __pycache__/Dataset.cpython-38.pyc | Bin 0 -> 408 bytes __pycache__/Frequency.cpython-38.pyc | Bin 0 -> 1381 bytes __pycache__/frequency_table.cpython-38.pyc | Bin 0 -> 2177 bytes 7 files changed, 104 insertions(+), 76 deletions(-) delete mode 100644 Frequency.py create mode 100644 FrequencyTable.py create mode 100644 __pycache__/Chart.cpython-38.pyc create mode 100644 __pycache__/Dataset.cpython-38.pyc create mode 100644 __pycache__/Frequency.cpython-38.pyc create mode 100644 __pycache__/frequency_table.cpython-38.pyc diff --git a/Frequency.py b/Frequency.py deleted file mode 100644 index 1daf0ba..0000000 --- a/Frequency.py +++ /dev/null @@ -1,69 +0,0 @@ -import numpy as np -import Dataset - -# Data Initiation -dataset = sorted(Dataset.data) -amount = len(dataset) -lowest = min(dataset) -highest = max(dataset) - -# Counting Data Range -ranges = highest - lowest - -# Classes is Rounding Down -# Math Log Base 10 In Python For Accurate Result -classes = 1 + (3.222 * np.log10(amount)) -classes = round(classes - 0.5) - -# Interval is Rounding Up -interval = ranges / classes -interval = round(interval + 0.5) - -# Function To Find Frequency in Dataset with Desired Range (Top and Down Limit) -def find_frequency(bot, top): - total_frequency = 0 - for i in range(bot, top + 1): - frequency = dataset.count(i) - total_frequency = total_frequency + frequency - return total_frequency - -# Base 5 Rounding -def roundy(x, base=5): - return base * round(x/base) - -# Rounding Both Limit So The Data Would Be Simple And Easier To Read -base = roundy(lowest - 3) -top = roundy(highest + 3) - -# Populate List For Base and Top Range Limit and The Frequency -def populate_frequency(base, top, interval): - # Initiating Used List - lower = [] - upper = [] - frequency = [] - data_range = [] - - # Initiating Used Parameter - interval = interval # 4 - current_number = base - 1 # 156 - old_number = 0 - - while current_number <= top-3: - # Finding Botom Limit Data - old_number = current_number + 1 - lower.append(old_number) # 155 - - # Finding Top Limit Data - current_number = current_number + interval - upper.append(current_number) - - # Finding The Frequency That Range - range_frequency = find_frequency(old_number, current_number) - frequency.append(range_frequency) - - # Adding The Number Range From Both Frequency - data_range.append(str(old_number) + " ~ " + str(current_number)) - - return lower, upper, frequency,data_range - -data_lower, data_upper, data_frequency, data_range = populate_frequency(base, top, interval) diff --git a/FrequencyTable.py b/FrequencyTable.py new file mode 100644 index 0000000..da40962 --- /dev/null +++ b/FrequencyTable.py @@ -0,0 +1,89 @@ +import numpy as np + +class FrequencyTable: + def __init__(self, dataset): + # Data Initiation + self.dataset = sorted(dataset) + self.amount = len(dataset) + self.lowest = min(dataset) + self.highest = max(dataset) + + # Counting Data Range + self.range = self.highest - self.lowest + + # Classes is Rounding Down + # Math Log Base 10 In Python For Accurate Result + self.classes = 1 + (3.222 * np.log10(self.amount)) + self.classes = round(self.classes - 0.5) + + # Interval is Rounding Up + self.interval = self.range / self.classes + self.interval = round(self.interval + 0.5) + + # Rounding Both Limit So The Data Would Be Simple And Easier To Read + self.base = self.roundy(self.lowest - 3) + self.top = self.roundy(self.highest + 3) + + def Populate(self): + # Initiating Used List + lower = [] + upper = [] + frequency = [] + data_range = [] + data_limit = [] + data_midpoint = [] + + # Initiating Used Parameter + interval = self.interval # 4 + current_number = self.base - 1 # 156 + old_number = 0 + + while current_number <= self.top-3: + # Finding Botom Limit Data + old_number = current_number + 1 + lower.append(old_number) # 155 + + # Finding Top Limit Data + current_number = current_number + interval + upper.append(current_number) + + # Finding The Frequency That Range + range_frequency = self.find_frequency(old_number, current_number) + frequency.append(range_frequency) + + # Adding The Number Range From Both Frequency + data_range.append(str(old_number) + " ~ " + str(current_number)) + + # Adding Data Range Limit Of The Class Frequency + data_limit.append(str(old_number - 0.5) + " ~ " + str(current_number + 0.5)) + + # Assign Value Each Processed Data + self.bottom_limit = lower + self.top_limit = upper + self.frequency = frequency + self.ranges = data_range + self.limit = data_limit + + # Append Processed Data into Data Attributes + self.data = ProcessedData(self.bottom_limit, self.top_limit, self.frequency, self.ranges, self.limit) + + # Base 5 Rounding + def roundy(self, x, base = 5): + return base * round(x/base) + + # Function To Find Frequency in Dataset with Desired Range (Top and Down Limit) + def find_frequency(self, bot, top): + total_frequency = 0 + for i in range(bot, top + 1): + frequency = self.dataset.count(i) + total_frequency = total_frequency + frequency + return total_frequency + +class ProcessedData: + def __init__(self, bottoms, tops, frequency, ranges, limit): + self.bottom = bottoms + self.top = tops + self.frequency = frequency # frekuensi terproses + self.ranges = ranges # rentang terproses + self.limit = limit # batas kelas terproses + diff --git a/Main.py b/Main.py index 742e558..e99d321 100644 --- a/Main.py +++ b/Main.py @@ -1,11 +1,19 @@ -import Frequency as fr -import Chart +import FrequencyTable as ft +# Misalnya, Anda punya data mentah seperti ini: +data = ( + 58, 67, 45, 89, 72, 60, 76, 93, + 55, 48, 62, 85, 79, 56, 41, 90, + 77, 54, 68, 82, 46, 73, 57, 92, + 81, 53, 66, 74, 64, 52, 91, 78, + 49, 87, 88, 50, 69, 84, 43, 65, + 83, 70, 44, 61, 75, 80, 71, 63, 47,51) -# Main Control For Statistic Tools -Range_limit = fr.data_range -Frequency_limit = fr.data_frequency +# Membuat objek dari FrequencyTable dengan data mentah +table = ft.FrequencyTable(data) -print(Range_limit) -print(Frequency_limit) +# Memproses data mentah menjadi tabel frekuensi +table.Populate() +print(table.data.ranges) # Output: [0, 10, 20, 30] (data terproses) +print(table.data.frequency) # Output: [10, 20, 30, 40] (data terproses) diff --git a/__pycache__/Chart.cpython-38.pyc b/__pycache__/Chart.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..34fc66343498d4a48302392187401fe35b3aca13 GIT binary patch literal 147 zcmWIL<>g`kf|wM}bU7gX7{oyaj6jY95Erumi4=xl22Do4l?+87VFdBZLO&xvH&s6? zu^_WR-zBv;yClCrKe!~ZB(u0AGdoc?pt2+*KTjV-fkcw^oih@PO7sdUZ*kb<=BJeA Mq}qW@{0zhl0BOG==Kufz literal 0 HcmV?d00001 diff --git a/__pycache__/Dataset.cpython-38.pyc b/__pycache__/Dataset.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..53dd5402f3935e8a8540a2ec3c66549ac64077ed GIT binary patch literal 408 zcmYk#yG{a85XSMd!~#utkd4Aoyc0y!^{$Ango{|5cs8yqZ|H6d%cX{_r>4oj3k8o;TG?I zAue7bwZ9^hMADK7>Ln8lII!2jii!{-2vNq6c$q@xWd}Ntj)>?=COOc{9>iC4lWY^w zhYjeyg4_)Z*5-~vAKnB~+Hjtr=-VyOY_GBW{cT@~v2&v3`#6#3OGV`9BvLn0b zxdmJB5H?}3;$Ma0k=Pa^@mTCg`ihME*O)yyyr#%!Pk;XYuOEL)U;gQAg#7-=l&v+- zUBLm;r%3HHWRfnKCWaU)8D7W^$k&gST+>Tt_!V6Sh8awG$IJ|bsS z{a{i_u^*Y?m&9aOloH&<&Mi|Dpe1z{O%2AZ*s^0nv~|2!bV)Qb*^0UmL@iz6IesTz z@5s;%-|emp-T19N&i4&BfeB4y;*$_Ti4Oe$tVdypRea0$zS~o-P#lUVZyKnihurd3 zt8rI3Y*mcRCPk$|eUq0qEbBL*Y`8#orBb(+`@SzXFoeZxoJ55xakHEil;QRW)%U7q08?LIQX0-TtepC>e6U6#-cwT$Bw z@g2U;)DHTMsp>hgD?&`6xe%;sM0{}b3Zk}F8l(+e*2)c?bzw5pGXs4d6b1o`2*$JD1JZt!xL-sm1!+xOVr yy*M>;2RwGnJ(2gApXB7lyq1d+K6cN%-9whp1dB(GG6{yXi*O(%Sz<&L0&iaj8Oio(SbvfPZ@x^?1oJYlz$ z!=?QBM@q1Eyk+R=aY!=)x; ze^Tdm6X%J;!{bUHYc|XF*D5|8$HV^F_tAbD_pMt?lkGsh zg5;&n!o~GpkmxV-lysFxI17bbac>Bd+)=TPJTrH_>lM(P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