To encode the target variable and do padding for independent variables using keras.
Iris data set.
Padded sequence of input data points.
Encoded target variables.
Load the data set.
Take features and target variables as an array.
Because in deep learning with keras everything should be in array.
Initialize sequence padding and one-hot encoder constructor from keras library.
Fit the data into the constructor.
Transform the independent and target variables.
#import necessary libraries
import pandas as pd
import numpy as np
import keras
from keras.preprocessing import sequence
from keras.utils import np_utils
from sklearn.model_selection import train_test_split
#load the sample data from csv file
data = pd.read_csv(‘/home/soft50/soft50/Sathish/practice/iris.csv’)
#Make it as a data frame
df = pd.DataFrame(data)
#feature selection
X = np.array(df.iloc[:,0:4])
y = np.array(df.iloc[:,5])
print(“Before padding\n\n”,X)
#Split the data into train and testing
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.1, random_state=0)
#Sequence padding
X_train = sequence.pad_sequences(X_train,maxlen=40)
X_test = sequence.pad_sequences(X_test,maxlen=40)
print(“After padding\n\n”,X_train,”\n\n”,X_test)
#One-hot encoding
Y_train = np_utils.to_categorical(Y_train,num_classes=3)
Y_test = np_utils.to_categorical(Y_test,num_classes=3)
print(“After Encoding\n\n”,Y_train,”\n\n”,Y_test)