thesis-prediction-ml/doc/chapters/chapter4.tex
shoney.arickathil 86399459ed doc
2023-07-16 00:27:23 +02:00

140 lines
No EOL
6.3 KiB
TeX

\chapter{Impute missing text data}
Machine leaning algorithms requires that their inputs have no missing values.
Missing values encoded as NaNs or blanks are incompatible with estimators which represents as the numerical values and assumes all values have and hold meaning. Discarding the entire row and/or columns of a dataset containing the missing values could lead to losing valuable data. The process to fill (impute) missing values are referred to as imputation.
\section{Feature imputation / Regression imputation / Predictive Imputation}
Machine learning algorithm to predict values in a categorical variable based on other available features. Table \ref{table:feature_imputation} states the categorical features from the all the features mentioned in \ref{table:feature_decription}.
\begin{table}[h]
\centering
\caption{Feature imputation}
\label{table:feature_imputation}
\begin{tabular}{ lll }
\toprule
\textbf{No}& \textbf{Feature} & \textbf{Categorical}\\
\midrule
1&Product name & No\\
3&Description & No\\
4&Short description & No\\
5&Supplier & Yes\\
6&Manufacturer & Yes\\
7&Price & No \\
8&Dimension & Yes\\
\bottomrule
\end{tabular}
\end{table}
The \textit{dimension} column is also considered as a categorical value as it has few unique values.
If \textit{supplier} is the missing value from the set of row. The missing \textit{supplier} data must be predicted only from the available list of suppliers. A supervised machine learning with labeled dataset to train the model to classify the available features data to predict \textit{supplier}. A tensor size of the number of unique \textit{supplier} will be the output of the model.
In an iterated round-robin fashion, at every step a missing feature column \textit{y} and other feature columns treated as input \textit{x} predicts the missing values of \textit{y}. Regression imputation is more accurate than mode imputation on categorical value.
\section{Mode imputation}
Replacing the most frequent categorical value in a column is known as mode imputation. Pandas.DataFrame.mode \parencite{mckinney-proc-scipy-2010} function returns most often value. Code snippet will fill the missing values for each column using its own most frequent value.
\begin{lstlisting}[language=Python]
df = df.fillna(df.mode().iloc[0])
\end{lstlisting}
Mode imputation on categorical data are prone to fill incorrect data if the missing data is not the most frequent one.
\section{\textit{k}-Nearest Neighbors imputation}
k-Nearest Neighbor is a supervised learning algorithm is used to search dataset with the most similar elements to a given query element, with similarity defined by a distance function.
The most common distance functions are:-
\begin{enumerate}
\item Eulidean distance.
\item Manhattan distance.
\item Hamming distance.
\end{enumerate}
sklearn.neighbors.KNeighborsClassifier \parencite{scikit-learn} does an instance-based learning, it does not construct a model, but stores instance of training data. Classification is computed from majority vote of nearest neighbors of each point.
The \textit{k}-neighbors classification implements learning based on \textit{k} nearest neighbors of each query point, where \textit{K} is integer value specified by user.
The optimal \textit{k} value can be evaluated by iterating the classifier with different \textit{n\textunderscore neighbors} parameter and finding the minimum value of error rate.
\begin{lstlisting}[language=Python]
error_rate = []
for i in range(1,40):
knn = KNeighborsClassifier(n_neighbors=i)
knn.fit(X_train,y_train)
pred_i = knn.predict(X_test)
error_rate.append(np.mean(pred_i != y_test))
print("Minimum error:-",min(error_rate),"at K =",error_rate.index(min(error_rate)))
\end{lstlisting}
As per \parencite{scikit-learn}, larger \textit{k} suppresses the effects of noise, but makes the classification boundaries less distinct.
\subsection{KNN model implementation}
In table \ref{table:KNN_implementation} there are nine columns. Target column is 'catL3' which is the category at level three.
\begin{table}[h]
\centering
\caption{Data frame head}
\label{table:KNN_implementation}
\begin{tabular}{ lll }
\toprule
\textbf{No}& \textbf{Feature} & \textbf{Categorical}\\
\midrule
1&Product name & No\\
3&Description & No\\
4&Short description & No\\
5&Supplier & Yes\\
6&Manufacturer & Yes\\
7&Price & No \\
8&Dimension & Yes\\
\bottomrule
\end{tabular}
\end{table}
\begin{lstlisting}[language=Python]
X = df.drop(['catL3'], axis = 1)
y = df['catL3']
from sklearn import preprocessing
X = preprocessing.StandardScaler().fit(X).transform(X.astype(float))
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=4)
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
#Train Model and Predict
k = 3
neigh = KNeighborsClassifier(n_neighbors = k).fit(X_train,y_train)
Pred_y = neigh.predict(X_test)
print("Accuracy of model at K=3 is",metrics.accuracy_score(y_test, Pred_y))
\end{lstlisting}
\begin{itemize}
\item Collect independent data features into the X data frame and target field into a y data frame.
\item Split data into training and testing.
\item Import the classifier model from sklearn library and fit the model with \textit{k} equal to the optimal value.
\end{itemize}
\section{Mean/median imputation}
Pandas.DataFrame.median \parencite{mckinney-proc-scipy-2010} returns the median value. These can be applied to the features represented in numerical value. In reference to table \ref{table:feature_imputation}, \textit{price} could be a column on which median value can be filled. However, using median to fill missing value could underestimate or overestimate the value.
\begin{enumerate}
\item Median - The mid point value
\item Mean - The average value
\end{enumerate}
\begin{lstlisting}[language=Python]
df = df.fillna(df.median().iloc[0])
\end{lstlisting}