<jupyter_start><jupyter_text>Data cleaning for outliers using robust sigma-clipping operation:<jupyter_code>##Suppose you have a set of data. Compute its median m and its standard deviation sigma. 
##Keep only the data that falls in the range (m-a*sigma,m+a*sigma) for some value of a, and discard everything else. 
##This is one iteration of sigma clipping. 
##Continue to iterate a predetermined number of times, and/or stop when the relative reduction in the value of sigma is small.
##Sigma clipping is geared toward removing outliers, to allow for a more robust (i.e. resistant to outliers) estimation of, say, the mean of the distribution. So it's applicable to data where you expect to find outliers.
import numpy as np
quartiles = np.percentile(births['births'], [25, 50, 75])
mu = quartiles[1]
sig = 0.74 * (quartiles[2] - quartiles[0])
births = births.query('(births > @mu - 5 * @sig) & (births < @mu + 5 * @sig)')
births['day'] = births['day'].astype(int)
births.index = pd.to_datetime(10000 * births.year +
                              100 * births.month +
                              births.day, format='%Y%m%d')

births['dayofweek'] = births.index.dayofweek
##births by weekday for several decades:
births.pivot_table('births', index='dayofweek',
                    columns='decade', aggfunc='mean').plot()
plt.gca().set_xticklabels(['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'])
plt.ylabel('mean births by day');
plt.show()
## number of births by the day of the year
births_month = births.pivot_table('births', [births.index.month, births.index.day])
print(births_month.head())

births_month.index = [pd.datetime(2012, month, day)
                      for (month, day) in births_month.index]
print(births_month.head())
fig, ax = plt.subplots(figsize=(12, 4))
births_month.plot(ax=ax)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13344138303619665, 'fraction_numerical': 0.015667206915180983, 'mean_word_length': 4.032608695652174, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '582616', 'license': 'MIT', 'n_tokens_mistral': 666, 'n_tokens_neox': 581, 'n_words': 193, 'path': 'Birth rate analysis.ipynb', 'repo_name': 'rjvmhn007/Birth-rate-analysis', 'size': '1851'}