is the mean is equal with moving average? Fungsi Z-score def zscore(s, window, thresh=1,…
- User
is the mean is equal with moving average?
Fungsi Z-score
def zscore(s, window, thresh=1, return_all=False): # Menghitung nilai rata-rata bergerak (rolling mean) roll = s.rolling(window=window, min_periods=1, center=True) avg = roll.mean() # Menghitung nilai standar deviasi bergerak (rolling std) std = roll.std(ddof=0) # Menghitung nilai Z-score untuk setiap elemen z = s.sub(avg).div(std) # Menentukan elemen mana yang berada di dalam batas threshold (thresh) m = z.between(-thresh, thresh)
# Jika return_all adalah True, kembalikan semua variabel if return_all: return z, avg, std, m # Jika tidak, hanya kembalikan data yang berada dalam batas dengan nilai rata-rata return s.where(m, avg)Menerapkan Deteksi Z-score
z, avg, std, m = zscore(df['REFGPS'], window=10, return_all=True)
Membuat plot deret waktu dengan outlier
plt.figure(figsize=(10, 6))
Menampilkan data asli dengan transparansi
df['REFGPS'].plot(label='data', alpha=0.6)
Menampilkan rata-rata bergerak (rolling mean)
avg.plot(label='mean', color='orange')
Menampilkan outlier dengan penanda khusus
df.loc[~m, 'REFGPS'].plot(label='outliers', marker='o', ls='', color='green')
Menampilkan penggantian nilai untuk outlier
avg[~m].plot(label='replacement', marker='o', ls='', color='red')
Menambahkan legenda untuk membedakan elemen-elemen yang ditampilkan
plt.legend()
Menambahkan judul pada grafik
plt.title("REFGPS Data with Outliers and Rolling Mean")
Menampilkan grafik
plt.show()
- Assistant
The mean and the moving average are related but not the same. Here’s the distinction:
-
Mean (Average): It is a statistical measure that gives you the central tendency of a data set by summing up all the numbers and dividing by the count of numbers. When people refer to the "mean," they are typically talking about the arithmetic mean of the entire data set.
-
Moving Average: This is a type of mean where the average is calculated over a sliding window of specified length as you move through the data. It smoothens the data by focusing on local averages rather than the mean of the entire series. In your code, the moving average is calculated over a window defined by the
windowparameter (in this case, 10).
In your function,
avgrepresents the moving average of the data over a specified window (ten data points in this case). Each value in theavgseries is the mean of a subset of the data, which means it will change as you slide through your dataset. The rolling mean helps in identifying trends and fluctuations in a time series by reducing noise and showing underlying patterns.The
zscorefunction in your code calculates moving averages to help identify outliers by computing Z-scores for each point and comparing them against the threshold. Here, the moving average acts as a baseline to find deviations from the norm.In summary, while the moving average does involve computing mean values, it is applied locally across subsets of the data rather than once over the entire data set.
-