<jupyter_start><jupyter_text>Numerical Variable Transform  Min-Max Scaling<jupyter_code>a = np.array([[1., -1., 2.],
              [2., 0., 0.],
              [0., 1, -1.]])

min_max_scaler = preprocessing.MinMaxScaler()
a_scaled = min_max_scaler.fit_transform(a)
a_scaled
min_max_scaler.min_
min_max_scaler.scale_
a_test = np.array([[-3., -1., 4.]])
a_test_scaled = min_max_scaler.transform(a_test)
a_test_scaled<jupyter_output><empty_output><jupyter_text>z-score<jupyter_code>zscore_scaler = preprocessing.StandardScaler()
a_scaled = zscore_scaler.fit_transform(a)
a_scaled
zscore_scaler.mean_
zscore_scaler.scale_
a_test_scaled = zscore_scaler.transform([[-1., 1., 0.]])
a_test_scaled<jupyter_output><empty_output><jupyter_text>Normalization<jupyter_code>normalizer = preprocessing.Normalizer().fit(a)

a_normalized = normalizer.transform(a)
normalizer.transform([[-1., 1., 0.]])<jupyter_output><empty_output><jupyter_text>Popular Transforms  Logarithm<jupyter_code>from sklearn.preprocessing import FunctionTransformer

a = np.array([[0, 1], [2, 3]])

transformer = FunctionTransformer(np.log1p)
transformer.transform(a)
transformer = FunctionTransformer(np.sqrt)
transformer.transform(a)
def cube_root(x):
    return np.power(x, 1.0 / 3 )
transformer = FunctionTransformer(cube_root)
transformer.transform(a)
transformer = FunctionTransformer(np.square)
transformer.transform(a)
def cube(x):
    return np.power(x, 3 )
transformer = FunctionTransformer(cube)
transformer.transform(a)<jupyter_output><empty_output><jupyter_text>One-Hot Encoder<jupyter_code># two important parameters of OneHotEncoder
#  handle_unknown: 'error', 'ignore'
#  n_values: n
enc = preprocessing.OneHotEncoder()
enc.fit([[0, 0, 3], [1, 1, 0], [0, 2, 1], [1, 0, 2]])
enc.transform([[0, 1, 3]]).toarray()<jupyter_output><empty_output><jupyter_text>Discretization and binning<jupyter_code>import pandas as pd
ages = [20, 22, 25, 27, 21, 23, 37, 31, 61, 45, 41, 32]
bins = [18, 25, 35, 60, 100]
cats = pd.cut(ages, bins)
cats
cats.codes
pd.value_counts(cats)
pd.cut(ages, [18, 26, 36, 61, 100], right=False)
group_names = ['Youth', 'YoungAdult', 'MiddleAged', 'Senior']
pd.cut(ages, bins, labels=group_names)
data = np.random.rand(20)
pd.cut(data, 4, precision = 2)
data = np.random.randn(1000) # Normally distributed
cats = pd.qcut(data, 4) # Cut into quartiles
cats
pd.value_counts(cats)
pd.qcut(data, [0, 0.1, 0.5, 0.9, 1.])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15406737880032867, 'fraction_numerical': 0.04313886606409203, 'mean_word_length': 6.356495468277946, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '644530', 'license': 'Apache-2.0', 'n_tokens_mistral': 1058, 'n_tokens_neox': 960, 'n_words': 178, 'path': 'ml-workshop/src/Feature-Engineering-Fundamental-Feature.ipynb', 'repo_name': 'yao-matrix/mProto', 'size': '2434'}