Skip to content

Commit

Permalink
feat: menambahkan statistika (#6)
Browse files Browse the repository at this point in the history
* feat: menambahkan statistika

rumus statistika entropy adalah `H(X) = -∑p(x)log(p(x))`
dengan fungsi yang bisa dilihat pada OpenSeries/statistika.py

request documentation: @dherlyar

Signed-off-by: slowy07 <[email protected]>

* fix: memperbaiki dari codeowners

Signed-off-by: slowy07 <[email protected]>

* fix: menambahkan requirements.txt pada unitesting

Signed-off-by: slowy07 <[email protected]>

* fix: memperbaiki dari CODEOWNERS

Signed-off-by: slowy07 <[email protected]>

* feat: menambahkan fungsi standar deviasi

fungsi untuk mengukur penyebaran data terhadap nilai rata-rata
dengan rumus `σ = √∑(x - μ)^2 / n`

Signed-off-by: slowy07 <[email protected]>

* chore: merestruktur folder contoh

Signed-off-by: slowy07 <[email protected]>

---------

Signed-off-by: slowy07 <[email protected]>
  • Loading branch information
slowy07 authored Jan 23, 2024
1 parent 6221e4b commit 3c1559d
Show file tree
Hide file tree
Showing 31 changed files with 253 additions and 115 deletions.
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
# berpegang penuh dalam repositori

# tanda * merupakan yang meninjau segala aspek pada repositori
* @slowy07
* @slowy07 @abnvlf
*.md @dherlyar @random-prog
1 change: 1 addition & 0 deletions .github/workflows/pythontesting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- name: install requirement
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install -r dev-requirements.txt
- name: cek kode dengan flake8
Expand Down
43 changes: 43 additions & 0 deletions OpenSeries/statistika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import numpy as np
from typing import Union
from OpenSeries.util import error as pesan_error


def entropy(label: list[int], base: int = None) -> Union[float, int, str]:
"""
fungsi menghitung entropy dari suatu fitur pada suatu dataset
parameter:
label (list (int)): label fitur yang akan di hitung entropynya
"""
if not isinstance(label, (list)):
return pesan_error.error_tipe_data(["list"])
if not label:
return pesan_error.error_format("label tidak boleh kosong")
if not all(isinstance(cek_nilai, int) for cek_nilai in label):
return pesan_error.error_tipe_data(["int"])
_, count = np.unique(label, return_counts=True)
probabilitas = count / len(label)
probabilitas[probabilitas == 0] = 1

log = np.log(probabilitas) if base is None else np.log(probabilitas) / np.log(base)
return np.sum(abs(probabilitas * log))


def standar_deviasi(vektor: np.ndarray) -> Union[float, str]:
"""
fungsi untuk mengukur penyebaran data terhadap nilai rata-ratanya
parameter:
vektor (np.array)
"""
if not isinstance(vektor, np.ndarray):
return pesan_error.error_tipe_data(["numpy array"])
if len(vektor) == 0:
return pesan_error.error_format("vektor tidak boleh kosong")

mean_value = np.mean(vektor)
squared_diff = np.square(vektor - mean_value)
variansi = np.mean(squared_diff)
std_deviasi = np.sqrt(variansi)
return std_deviasi
55 changes: 0 additions & 55 deletions example/example_fisika.py

This file was deleted.

59 changes: 0 additions & 59 deletions example/example_matematika.py

This file was deleted.

File renamed without changes.
11 changes: 11 additions & 0 deletions example/fisika/energi_kinetik.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import OpenSeries.fisika as fisika

# fungsi menghitung energi kinetik
massa_benda = 14
kecepatan_benda = 23.4
print(
f"menghitung energi kinetik pada benda {massa_benda} kg dengan kecepatan {kecepatan_benda} m/s"
)
print(
f"hasilnya adalah : {fisika.energi_kinetik(massa_benda, kecepatan_benda)} joule\n"
)
13 changes: 13 additions & 0 deletions example/fisika/energi_potensial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import OpenSeries.fisika as fisika

# menghitung energi potensial
massa_benda_potensial = 12
gravitasi_bumi = 9.78
ketinggian_benda = 400
print("menghitung energi potensial dari suatu benda")
print(f"dengan massa benda adalah: {massa_benda_potensial}")
print(f"gravitasi bumi {gravitasi_bumi}")
print(f"dan ketinggian benda adalah {ketinggian_benda}")
print(
f"hasilnya adalah: {fisika.energi_potensial(massa_benda_potensial, gravitasi_bumi, ketinggian_benda)}\n"
)
6 changes: 6 additions & 0 deletions example/fisika/glbb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import OpenSeries.fisika as fisika

# fungsi menghitung glbb
print("hitung glbb (gerak lurus beraturan)")
result_glbb = fisika.gerak_lurus_beraturan(10, 2, 3)
print(f"hasil dari glbb adalah: {result_glbb}\n")
9 changes: 9 additions & 0 deletions example/fisika/hukum_ohm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import OpenSeries.fisika as fisika

# menghitung hukum arus ohm
kuat_arus = 30
hambatan = 3
print("menghitung hukum ohm")
print(f"dengan kuat arus {kuat_arus}")
print(f"hambatan {hambatan}")
print(f"hasilnya adalah: {fisika.hukum_ohm(kuat_arus, hambatan)}\n")
5 changes: 5 additions & 0 deletions example/fisika/kecepatan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import OpenSeries.fisika as fisika

print("hitung kecepatan")
result_kecepatan = fisika.kecepatan(4, 2.3)
print(f"hasil dari kecepatan adalah: {result_kecepatan}\n")
9 changes: 9 additions & 0 deletions example/fisika/masa_jenis_benda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import OpenSeries.fisika as fisika

# menghitung masa jenis benda
massa_benda = 14
volume_benda = 8
print(
f"menghitung massa jenis benda dengan massa benda {massa_benda} gram, dan volume benda {volume_benda} cm3"
)
print(f"hasilnya adalah : {fisika.masa_jenis(massa_benda, volume_benda)}\n")
5 changes: 5 additions & 0 deletions example/fisika/percepatan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import OpenSeries.fisika as fisika

print("hitung percepatan")
result_percepatan = fisika.percepatan(4, 2.3)
print(f"hasil dari percepatan adalah: {result_percepatan}\n")
Empty file added example/matematika/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions example/matematika/deret_aritmatika.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import OpenSeries.matematika as matematika

nilai_suku = 5
suku_pertama = 3
selisih_suku = 2
print("menghitung jumlah deret aritmatika")
print(f"dengan nilai suku: {nilai_suku}")
print(f"suku pertama: {suku_pertama}")
print(f"selisih suku: {selisih_suku}")
print(
f"hasilnya adalah: {matematika.hitung_jumlah_deret(nilai_suku, suku_pertama, selisih_suku)}\n"
)
8 changes: 8 additions & 0 deletions example/matematika/fpb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import OpenSeries.matematika as matematika

nilai1 = 24
nilai2 = 40
print(
f"hitung nilai fpb (faktor persekutuan terbesar) dari nilai {nilai1} dan {nilai2}"
)
print(f"hasilnya adalah : {matematika.fpb(nilai1, nilai2)}\n")
5 changes: 5 additions & 0 deletions example/matematika/jari_lingkaran.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import OpenSeries.matematika as matematika

print("hitung jari jari lingkaran")
hasil_jari_lingkaran = matematika.luas_lingkaran(4.5)
print(f"hasil dari hitung jari jari lingkaran: {hasil_jari_lingkaran}\n")
5 changes: 5 additions & 0 deletions example/matematika/nilai_faktorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import OpenSeries.matematika as matematika

nilai_faktorial = 25
print(f"menghitung nilai faktorial dari angka {nilai_faktorial}")
print(f"hasilnya adalah : {matematika.faktorial(nilai_faktorial)}\n")
5 changes: 5 additions & 0 deletions example/matematika/nilai_kombinasi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import OpenSeries.matematika as matematika

nilai_kombinasi = 15
print(f"menghitung nilai kombinasi dari {nilai_kombinasi}")
print(f"hasilnya adalah : {matematika.kombinasi(nilai_kombinasi, 3)}\n")
4 changes: 4 additions & 0 deletions example/matematika/nilai_konstan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import OpenSeries.util.constant as konstanta

print("menampilkan nilai konstan")
print(f"nilai dari pi adalah: {str(konstanta.pi)}\n")
5 changes: 5 additions & 0 deletions example/matematika/nilai_permutasi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import OpenSeries.matematika as matematika

nilai_permutasi = 12
print(f"menghitung nilai permutasi dari {nilai_permutasi}")
print(f"hasilnya adalah : {matematika.permutasi(nilai_permutasi, 2)}\n")
5 changes: 5 additions & 0 deletions example/matematika/nilai_rata_rata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import OpenSeries.matematika as matematika

nilai_rata = [2, 3, 5, 5, 6, 1, 2, 3]
print(f"menghitung nilai rata-rata dari {nilai_rata}")
print(f"hasilnya adalah: {matematika.rata_rata(nilai_rata)}\n")
11 changes: 11 additions & 0 deletions example/matematika/probabilitias.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import OpenSeries.matematika as matematika

nilai_A = 5
nilai_S = 10

print("menghitung probabilitas suatu kejadian")
print(f"dengan jumlah hasil yang menguntungkan :{nilai_A}")
print(f"dan dengan ukuran ruang sampel {nilai_S}")
print(
f"probabilitas dari kejadiannya adalah: {matematika.peluang_kejadian(nilai_A, nilai_S)}\n"
)
6 changes: 6 additions & 0 deletions example/matematika/rentang_nilai_faktor_prima.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import OpenSeries.matematika as matematika

rentang_nilai = 300
print(
f"rentang nilai {rentang_nilai} faktor prima adalah {matematika.faktor_prima(rentang_nilai)}\n"
)
Empty file added example/statistika/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions example/statistika/entropy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from OpenSeries import statistika as statistika

# contoh dari entropy
label = [1, 1, 2, 2, 3, 3]
hasil_base_2 = statistika.entropy(label, base=2)
print(hasil_base_2)
7 changes: 7 additions & 0 deletions example/statistika/standar_deviasi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import numpy as np
from OpenSeries import statistika as statistika

# contoh dari standar deviasi
vektor = np.array([1, 2, 3, 4, 5])
hasil = statistika.standar_deviasi(vektor)
print(hasil)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numpy
14 changes: 14 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
from setuptools import setup, find_packages
from os import path
import io
import platform

# membuka file README
with open("README.md") as file_readme:
readme = file_readme.read()

# buat installasi requirement dari requirements agar auto install
info = path.abspath(path.dirname(__file__))
with io.open(path.join(info, "requirements.txt"), encoding="utf-8") as file:
core_require = file.read().split("\n")
if platform.system == "windows":
core_require.append("pywin32")

install_require = [x.strip() for x in core_require if "git+" not in x]

# setup nama project
setup(
# nama dari project
Expand Down Expand Up @@ -32,6 +44,8 @@
"Programming Language :: Python :: 3.12",
"Operating System :: OS Independent",
],
# fungsi untuk menginstall package tambahan dari requirements
install_requires=install_require,
# lisensi dari project
license="MIT License",
project_urls={
Expand Down
12 changes: 12 additions & 0 deletions testing/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
TestEnergiKinetik,
)

from testing.statistika_test import TestFungsiEntropy, TestFungiStandardDeviasi

if __name__ == "__main__":
testing_matematika: list = [
TestKelilingLingkaran,
Expand All @@ -42,6 +44,11 @@
TestEnergiKinetik,
]

testing_statistika: list = [
TestFungsiEntropy,
TestFungiStandardDeviasi,
]

all_tests = unittest.TestSuite()

for testing_math in testing_matematika:
Expand All @@ -50,4 +57,9 @@
for testing_physic in testing_fisika:
all_tests.addTest(unittest.TestLoader().loadTestsFromTestCase(testing_physic))

for testing_statistik in testing_statistika:
all_tests.addTest(
unittest.TestLoader().loadTestsFromTestCase(testing_statistik)
)

unittest.TextTestRunner(verbosity=2).run(all_tests)
Loading

0 comments on commit 3c1559d

Please sign in to comment.