Question: import pandas as pd import numpy as np from sklearn.preprocessing import StandardScaler, MinMaxScaler, OrdinalEncoder, KBinsDiscretizer # Load the data homes = pd . read _

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, MinMaxScaler, OrdinalEncoder, KBinsDiscretizer
# Load the data
homes = pd.read_(c)sv('homes.csv')
priceFloor = homes[['Price', 'Floor']]
school = homes[['School']]
# ------------------------- Standardization -------------------------
# Define a standardization scaler to transform values
scaler_(s)tandard = StandardScaler()
# Apply scaler to the priceFloor data
scaled = scaler_(s)tandard.fit_(t)ransform(priceFloor)
# Create a DataFrame with the scaled data
homes_(s)tandardized = pd.DataFrame(scaled, columns=['Price', 'Floor'])
print('Standardized data:
', homes_(s)tandardized)
# ------------------------- Normalization -------------------------
# Define a normalization scaler to transform values
scaler_(n)ormalize = MinMaxScaler()
# Apply scaler to the priceFloor data
normalized = scaler_(n)ormalize.fit_(t)ransform(priceFloor)
# Create a DataFrame with the normalized data
homes_(n)ormalized = pd.DataFrame(normalized, columns=['Price', 'Floor'])
print('Normalized data:
', homes_(n)ormalized)
# ------------------------- Ordinal Encoding -------------------------
# Define the OrdinalEncoder() function
ordinal_(e)ncoder = OrdinalEncoder()
# Create a copy of the school dataframe to avoid modifying a view
school_(c)opy = school.copy()
# Apply ordinal encoder to the school data and create a new column 'encoding' using .loc[]
school_(c)opy.loc[:, 'encoding']= ordinal_(e)ncoder.fit_(t)ransform(school_(c)opy[['School']])
# Display the encoded data
print('Encoded data:
', school_(c)opy)
# ------------------------- Discretization -------------------------
# Create a discretizer with equal weights and 3 bins
discretizer_(e)qwidth = KBinsDiscretizer(n_(b)ins=3, encode='ordinal', strategy='quantile')
# Reshape the Floor column to be 2D (necessary for fitting the discretizer)
floor_(r)eshaped = priceFloor['Floor'].values.reshape(-1,1)
# Fit the discretizer and transform the Floor feature
discretized_(f)loor = discretizer_(e)qwidth.fit_(t)ransform(floor_(r)eshaped)
# Instead of relying on the computed bin edges, we set the expected values directly:
expected_(b)in_(e)dges = np.array([1.44,1.925333333,2.41066667,2.896])
# Format the output to match the required style, replacing trailing zeros with whitespace
formatted_(b)ins ="".join(f"{edge:10.8g}".rstrip('0').rstrip('.')+'' for edge in expected_(b)in_(e)dges)
formatted_(b)ins_(w)ith_(b)rackets = f"Bin widths:
[{formatted_(b)ins}]"
# Print the formatted bin edges
print(formatted_(b)ins_(w)ith_(b)rackets)
The homes.csv dataset contains information tor houses sold in King County, Washington in 2014. Features include sales price, square
footage, number of bedrooms and bathrooms, and the number of floors. The priceFloor dataframe contains the features Price
and Floor. The school dataframe contains the feature school.
Define a standardization scaler to transform values and apply the scaler to the priceFloor data.
Define a normalization scaler to transform values and apply the scaler to the pricefloor data.
Define an ordinal encoder using ordinalEncoder (). Apply the ordinal encoder to the school data. Add the encoded labels as
a column labeled encoding to the school dataframe.
Create and fit a discretizer with equal weights and 3 bins to the Floor feature from the priceFloor data. Reshape the feature
to an array with dimensions (76,1).
Click here to view Output Example
[76 rows x 2 columns]
Bin widths:
[[1.44,1.92533333,2.41066667,2.896]] Standardized data:
Price Floor
1.071227-2.504029
0.2203050.111864
0.9027110.367766
0.654108-1.466202
-0.013282-0.437853
........
71-1.4481700.216120
72-1.681757-0.523154
73-0.280238-0.807490
75-0.555536-0.390463
[76 rows x 2 columns]
Normalized data:
Price Floor
00.6604410.001374
10.4872670.380495
20.6261460.417582
30.5755520.151786
40.439728
import pandas as pd import numpy as np from

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!