<jupyter_start><jupyter_text>**Connect With Me in Linkedin** :- https://www.linkedin.com/in/dheerajkumar1997/  Feature Selection: Quasi Constant Features**Quasi Constant Features** are those that show the same value for a great majority of observations in the dataset.<jupyter_code># Import Dependencies
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import VarianceThreshold
# Load Data
df = pd.read_csv('./dataset/Santander-Customer-Satisfaction-data/train.csv', nrows=50000)
df.head()
# Check for presence of Null features
[col for col in df.columns if df[col].isnull().sum() > 0]
X = df.drop(['TARGET'], axis=1)
X.head()
y = df['TARGET']
y.head()
# Train Test Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)
X_train.shape, y_train.shape, X_test.shape, y_test.shape
# Remove Constant Features
# Find all constant Features
# For each feature in training data, if it's standard deviation == 0, it is a constant variable.
const_features = [feat for feat in X_train.columns if X_train[feat].std() == 0]
len(const_features)
# Remove constant features
X_train.drop(labels=const_features, axis=1, inplace=True)
X_test.drop(labels=const_features, axis=1, inplace=True)
# Feature Selector
# 0.01: Approx. 99% of observations
feature_selector = VarianceThreshold(threshold=0.01)
feature_selector.fit(X_train)
# Num. features to be retained
sum(feature_selector.get_support())
# Features that are Quasi -constant
[x for x in X_train.columns if x not in X_train.columns[feature_selector.get_support()]]
# Percentage of observations showing all different values
X_train['num_var34_0'].value_counts() / np.float(len(X_train))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10853589598643301, 'fraction_numerical': 0.017524024872809497, 'mean_word_length': 6.224489795918367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '566755', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 588, 'n_tokens_neox': 559, 'n_words': 171, 'path': 'Feature-Selection-for-Machine-Learning-master/Filter Methods/Quasi-Constant-Features.ipynb', 'repo_name': 'viknesh1968/Feature-Selection-Techniques-Python--ML-master', 'size': '1769'}