To standardize and normalize the input features for more accurate results in python.
Features from Iris flower data set.
Sepal-Length
Sepal-Width
Petal-Length
Petal-Width
Standardized features.
Normalized features between 0 to 1.
Import the libraries.
Load the data set. (Sample)
Initialize the standard scalar and normalizer from sklearn.
Fit the data into the constructor, which features you want to standardize or normalize.
Print the results.
#Import libraries
import pandas as pd
#load data
data = pd.read_csv(‘/home/soft50/soft50/Sathish/practice/iris.csv’)
#make it as dataframe
df = pd.DataFrame(data)
#Feature extraction
X = df.iloc[:,0:4]
print(“Before standardization\n\n”,X)
print(“\n”)
#Scaling the independent variable
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
stand_X = sc.transform(X)
print(“After standardization\n\n”,stand_X)
print(“\n”)
#variable normalization
from sklearn.preprocessing import Normalizer
scaler = Normalizer().fit(X)
normalized_X = scaler.transform(X)
print(“After Normalization\n\n”,normalized_X)