Fakultas Ilmu Komputer UI

Commit 650574ba authored by Ahmad Dzikrul Fikri's avatar Ahmad Dzikrul Fikri
Browse files

TP 12 Tahap 3 - Data Analysis Result

parent eaa2fb89
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -4,6 +4,7 @@ import matplotlib.pyplot as plt
import io
import requests
import operator
from datetime import datetime as dtm
def cek_input(masukan):
case1 = masukan[0] == None or masukan[0] == ''
......@@ -154,14 +155,15 @@ def histogram(data,title, labels=['Rentang', 'Frekuensi'], jumlah_bins = 8, warn
except TypeError:
print('Data tidak valid')
def piechart(data, labels, title,bottom=0.1,top=0.9,right=0.9,left=0.13):
def piechart(data, labels, title,bottom=0.1,top=0.9,right=0.9,left=0.13,figsize=(12,5)):
try:
if title == None or title == '':
title = input('Masukan title pie chart: ')
plt.figure(figsize = figsize)
plt.pie(data,labels=labels,autopct='%1.1f%%')
plt.subplots_adjust(bottom=bottom,top=top,right=right,left=left)
plt.title(title)
plt.savefig(pengecekan[2]+'.png')
plt.savefig(title+'.png')
plt.show()
plt.clf()
except TypeError:
......@@ -183,11 +185,13 @@ def lineplot(sumbu_x,sumbu_y,title = None,labels=[None,None],bottom=0.1,top=0.9,
except TypeError:
print('Data tidak valid')
def scatter(sumbu_x,sumbu_y,title = None,labels=[None,None],bottom=0.1,top=0.9,right=0.9,left=0.13,rotate_x = 0,figsize=(12,5),color='black',alpha=1):
def scatter(sumbu_x,sumbu_y,title = None,labels=[None,None],bottom=0.1,top=0.9,right=0.9,left=0.13,rotate_x = 0,figsize=(12,5),color='black',alpha=1,ukuran = None):
try:
pengecekan = cek_input([labels[0],labels[1],title])
plt.figure(figsize = figsize)
plt.scatter(sumbu_x,sumbu_y,c = color,alpha=alpha)
plt.scatter(sumbu_x,sumbu_y,c = color,alpha=alpha,s=ukuran)
if str(type(ukuran)) != '<class \'NoneType\'>':
plt.colorbar()
plt.subplots_adjust(bottom=bottom,top=top,right=right,left=left)
plt.xlabel(pengecekan[0])
plt.xticks(rotation=rotate_x)
......@@ -244,7 +248,7 @@ def lineplot_mult(sumbu_x,sumbu_y,axloc,title=None,labels=[None,None],rotate_x=0
except TypeError:
print('Data tidak valid')
def pengelompokan(pandas_df,kolom_key,kolom_value,kelipatan=1,opcode='count'):
def pengelompokan(pandas_df,kolom_key,kolom_value,kelipatan=1,opcode='count',day=False):
data1 = pandas_df[[kolom_value,kolom_key]]
if opcode.lower() == 'count':
data = data1.groupby([kolom_key]).count()
......@@ -259,26 +263,40 @@ def pengelompokan(pandas_df,kolom_key,kolom_value,kelipatan=1,opcode='count'):
data_key = list(data.index)
for i in range(len(data_key)):
data_key[i] = str(data_key[i])
value_baru = 0
list_key = []
list_value =[]
for i in range(len(data_value)):
value_baru += data_value[i]
if kelipatan == 1:
list_key = data_key
list_value = data_value
elif i%kelipatan == 0:
awal = str(data_key[i])
elif i%kelipatan == kelipatan-1:
akhir = str(data_key[i])
list_key.append(awal+'-'+akhir)
list_value.append(value_baru)
value_baru = 0
elif i==len(data_value)-1:
akhir = str(data_key[i])
list_key.append(awal+'-'+akhir)
list_value.append(value_baru)
return [list_key,list_value]
if day == True:
jumlah_weekday = 0
jumlah_weekend = 0
weekday = ['Monday','Tuesday','Wednesday','Thursday','Friday']
for i in range(len(data_key)):
data_tanggal = dtm.strptime(data_key[i],'%Y-%m-%d')
hari = dtm.strftime(data_tanggal,'%A')
tes = hari in weekday
if tes == True:
jumlah_weekday += data_value[i]
else:
jumlah_weekend += data_value[i]
return [jumlah_weekday/5,jumlah_weekend/2]
else:
value_baru = 0
list_key = []
list_value =[]
for i in range(len(data_value)):
value_baru += data_value[i]
if kelipatan == 1:
list_key = data_key
list_value = data_value
elif i%kelipatan == 0:
awal = str(data_key[i])
elif i%kelipatan == kelipatan-1:
akhir = str(data_key[i])
list_key.append(awal+'-'+akhir)
list_value.append(value_baru)
value_baru = 0
elif i==len(data_value)-1:
akhir = str(data_key[i])
list_key.append(awal+'-'+akhir)
list_value.append(value_baru)
return [list_key,list_value]
def slice_item(pandas_df,kolom,split_by,title,take_index=None):
try:
......@@ -286,7 +304,10 @@ def slice_item(pandas_df,kolom,split_by,title,take_index=None):
data = split_data.to_list()
if take_index != None:
for i in range(len(data)):
data[i] = data[i][take_index]
if data[i][take_index] == '':
data[i] = None
else:
data[i] = data[i][take_index]
df = pd.DataFrame(data, columns=title)
return df
except ValueError :
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment