Python program to Normalize data for KNN Classifier
data = pd.read_csv('iris.data')
Y=data['status']
X=data[data.columns.difference(['status','name'])]
# split the data in testing and training
train_in,test_in,train_out,test_out=train_test_split(X,Y,train_size=0.8)
knn = KNeighborsClassifier()
use sklearn.preprocessing.scalar(), Stan- dardScaler(), MinMaxScaler(), MaxAbsScaler()) to normalize data
import pandas as pd
from sklearn.preprocessing import MinMaxScaler,StandardScaler,MaxAbsScaler
data = pd.read_csv('iris.data')
Y=data['status']
X=data[data.columns.difference(['status','name'])]
# split the data in testing and training
train_in,test_in,train_out,test_out=train_test_split(X,Y,train_size=0.8)
# Normalize feature data using MinMax scaler
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(train_in)
X_test_scaled = scaler.transform(test_in)
# Normalize feature data using StandardScaler
standarad_scaler = StandardScaler()
X_train_scaled = standarad_scaler.fit_transform(train_in)
X_test_scaled = standarad_scaler.transform(test_in)
# Normalize feature data using MaxAbsScaler
max_abs_scaler = MaxAbsScaler()
X_train_scaled = max_abs_scaler.fit_transform(train_in)
X_test_scaled = max_abs_scaler.transform(test_in)
knn = KNeighborsClassifier()
Get Answers For Free
Most questions answered within 1 hours.