Question

2. Simply keeping numerical data unchanged may not always yield optimal results. Algorithms like k-NN, which

rely on distance metrics, can be sensitive to the scale of the features. A variable that spans a large range can

unduly influence the distance computation. For instance, age and hours-per-week typically have much larger

ranges than other binary features, which could result in the two of them dominating the distance calculations.

To tackle this, we can perform rescaling on the numerical fields, ensuring they lie within a similar range, e.g.,

bounding each field's max Manhattan distance by 2 (see Part 1 Q4). MinMaxScaler from sklearn.preprocessing

is a tool designed for this task, scaling features to lie between a given range, often between zero and one.

Here's an improved version of the previous implementation:

Import the necessary libraries:

from sklearn.compose import Column Transformer

from sklearn.preprocessing import MinMaxScaler, OneHotEncoder

Instantiate the processing methods for numerical and categorical data:

num_processor = MinMaxScaler (feature_range=(0, 2))

cat_processor = OneHotEncoder (sparse=False, handle_unknown='ignore')

• Fit the processor to the dataset and transform:

preprocessor = ColumnTransformer ( [

])

preprocessor.fit (data)

processed_data = preprocessor.transform (data)

This new version will produce more reasonable features for numerical data:

[[1.48571429 1.

0.

0.

0.

[0.62857143 0.

[1.02857143 0.

[0.91428571 0.

[0.8

[o.

[1.6

('num', num_processor, ['age']),

('cat', cat_processor, ['sector'])

[0.8

[12

0.

0

0.

0.

0.

0.

1.

0.

0.

0.

instructions filo

intrunti

0.

1.

1.

1.

0.

1.

1.

1.

]

]

]

]

]

]

]

]

Question image 1