Cara menggunakan cumsum vs sum python

numpy.cumsum() function is used when we want to compute the cumulative sum of array elements over a given axis.

Syntax : numpy.cumsum(arr, axis=None, dtype=None, out=None)

Parameters :
arr : [array_like] Array containing numbers whose cumulative sum is desired. If arr is not an array, a conversion is attempted.
axis : Axis along which the cumulative sum is computed. The default is to compute the sum of the flattened array.
dtype : Type of the returned array, as well as of the accumulator in which the elements are multiplied. If dtype is not specified, it defaults to the dtype of arr, unless arr has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used instead.
out : [ndarray, optional] A location into which the result is stored.
  -> If provided, it must have a shape that the inputs broadcast to.
  -> If not provided or None, a freshly-allocated array is returned.

Return : A new array holding the result is returned unless out is specified, in which case it is returned.

Code #1 : Working




# Python program explaining

# numpy.cumsum() function

import numpy as geek

  

in_numInput array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 0 Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 1

  

Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 3 Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 4Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 5Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 6

Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 7 

Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 8Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 0 Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements taking axis 1: [[ 2 6 12] [ 1 4 9]] 0

Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 3 Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 4Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements taking axis 1: [[ 2 6 12] [ 1 4 9]] 3Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements taking axis 1: [[ 2 6 12] [ 1 4 9]] 4

Output :

Input number : 10 cumulative sum of input number : [10]

 
Code #2 :




# Python program explaining

# numpy.cumsum() function

  

import numpy as geek

  

numpy.cumsum()1Input array : [[2 4 6] [1 3 5]] cumulative sum of array elements: [ 2 6 12 13 16 21] 0 numpy.cumsum()3numpy.cumsum()4numpy.cumsum()5numpy.cumsum()6numpy.cumsum()5numpy.cumsum()8numpy.cumsum()9# Python program explaining0numpy.cumsum()5# Python program explaining2numpy.cumsum()5# Python program explaining4# Python program explaining5

Post berikut menggunakan dataset dari kaggle, gambaran data yang digunakan pada dataframe dapat dilihat pada Gambar 1. Semua nama kolom pada kode yang dicantumkan pada post ini sesuai dengan dataframe yang digunakan.

Gambar 1: Dataframe berisi data course

Summary statistics

Menampilkan data statistik seperti mean, median, min, max., sum, dan count.

Penggunaan fungsi-fungsi ini sangat straightforward pada dataframe, masih dengan data yang sama, dataframe paid_course adalah subset dari dataframe utama, isinya course yang berbayar. Dengan fungsi-fungsi diatas kita bisa mendapatkan beberapa info yang kita butuhkan, misalnya rata-rata harga course yang berbayar, berapa harga course termahal, dll.

Contoh kode:

paid_course['price'].mean() paid_course['price'].median() paid_course['price'].min() paid_course['price'].max()

Fungsi sum dan count

Untuk dataset ini data yang cocok dipakai untuk fungsi sum adalah total pembelian course. Untuk data pembelian masing-masing course bisa didapat dengan mengalikan jumlah subscriber dengan harga course.

Contoh kode:

paid_course['paid_amount'] = paid_course['price'] * paid_course['num_subscribers'] #sum (harga total yang didapat) paid_course['paid_amount'].sum()

Fungsi count biasanya digunakan untuk data bertipe kategori, untuk dataset ini kita bisa menghitung jumlah course pada masing-masing level dan subject. Perhatikan perbedaan count() dan value_counts().

Contoh kode:

paid_course['level'].count() # menghitung jumlah data pada kolom level # output : 2728 paid_course['level'].value_counts() # menghitung jumlah data pada masing-masing kategori # output : # Business Finance 1095 # Web Development 1066 # Graphic Design 567 # Name: subject, dtype: int64

Fungsi cumsum()

Fungsi ini adalah fungsi cumulative sum yaitu nilai kumulatif dari data, nilai ini menampilkan jumlah dari satu value dengan selanjutnya secara kumulatif. Dengan cumsum() kita bisa melihat perkembangan jumlah subscriber dari waktu ke waktu (asumsi data diurutkan berdasarkan tanggal). Gambar 2 menampilkan cumulative sum dari jumlah subscriber course yang termasuk subject ‘Web Development’.

paid_business['cum_sum'] = paid_business['num_subscribers'].cumsum()
Gambar 2: Cumulative sum dari jumlah subsribers

Contoh penggunaan cumsum()

Berikut adalah visualisasi perkembangan jumlah subscriber pada masing-masing tipe subject yang berbeda. Cara untuk menampilkan visualisasi data akan dibahas pada post yang berbeda, namun secara singkat kode yang digunakan adalah:

import matplotlib.pyplot as plt plt.figure(figsize=(9, 5)) plt.plot( paid_business['published_datetime'], paid_business['cum_sum'], label='Business Finance') plt.plot(paid_webdev['published_datetime'], paid_webdev['cum_sum'], label='Web Development') plt.ylabel('Jumlah subscriber') plt.xlabel('Tahun') plt.legend() plt.show()
Gambar 3: Perbandingan perkembangan jumlah subscribers

Grouped summary statistics

Untuk mendapatkan mean, max, sum, dll dari data bertipe kategori dapat digunakan groupby(). Misalnya dapat digunakan untuk mendapatkan jumlah subscriber (num_subscribers) untuk setiap level yang ada.

Postingan terbaru

LIHAT SEMUA