<jupyter_start><jupyter_text>Constant*   Show the same value for all the observations of the dataset*   Provide no information*   Identifying and remove them*   Enhance interpretability<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import VarianceThreshold
from google.colab import drive
drive.mount("/content/gdrive")
data = pd.read_csv('/content/gdrive/My Drive/Colab Notebooks/FeatureSelection/train_santander.csv', nrows = 5000)
data.keys()
y = data.TARGET
X = data.drop(columns=['TARGET'])

X.head()
y.head()<jupyter_output><empty_output><jupyter_text>Analyze<jupyter_code>objs = []
nums = []
for i in X.columns:
  if X[i].dtype == 'O':
    objs.append(i)
  else:
    nums.append(i)
print(nums)
print("===")
print(objs)
X.isnull().sum().sum()
[col for col in data.columns if data[col].isnull().sum() > 0]<jupyter_output><empty_output><jupyter_text>ImportantIn all feature selection procedures, it is good practice to select the features by examining only the training set. And this is to avoid overfitting.<jupyter_code># TRAIN TEST SPLIT
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)

X_train.shape, X_test.shape<jupyter_output><empty_output><jupyter_text>Variance threshold By default, it removes all zero-variance features, i.e., features that have the same value in all samples.<jupyter_code>obj = VarianceThreshold(threshold=0)
obj.fit(X_train)  # find features with zero variance
# get_support is a boolean vector 
# indicates which features are retained
# summation gives the features that ain't constant 
sum(obj.get_support())
# Slice notation for columns
len(X_train.columns[obj.get_support()])
# Print the constants 
[x for x in X_train.columns if x not in X_train.columns[obj.get_support()]]
len([x for x in X_train.columns if x not in X_train.columns[obj.get_support()]])
# Proof of constants 
X_train['saldo_medio_var29_hace2'].unique()
X_train = obj.transform(X_train)
X_test = obj.transform(X_test)

X_train.shape, X_test.shape<jupyter_output><empty_output><jupyter_text>Try it yourself Code from scratch<jupyter_code>data = pd.read_csv('/content/gdrive/My Drive/Colab Notebooks/FeatureSelection/train_santander.csv', nrows = 5000)
y = data.TARGET
X = data.drop(columns=['TARGET'])
X.shape
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)

X_train.shape, X_test.shape
constant_features = [
    col for col in X_train.columns if X_train[col].std() == 0
]
len(constant_features)
# Drop them columns 
X_train.drop(labels=constant_features, axis=1, inplace=True)
X_test.drop(labels=constant_features, axis=1, inplace=True)

X_train.shape, X_test.shape<jupyter_output>/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py:4174: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  errors=errors,<jupyter_text>Based on the number of values<jupyter_code>data = pd.read_csv('/content/gdrive/My Drive/Colab Notebooks/FeatureSelection/train_santander.csv', nrows = 5000)
y = data.TARGET
X = data.drop(columns=['TARGET'])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)

X_train.shape, X_test.shape
# Find columns with only one value
constant_features = [
    col for col in X_train.columns if len(X_train[col].unique()) == 1
]
len(constant_features)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10976267529665588, 'fraction_numerical': 0.00889967637540453, 'mean_word_length': 6.0379506641366225, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '607768', 'license': 'MIT', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1280, 'n_words': 322, 'path': 'FeatureSelection_DataScience/Demo220_Filter_Nature_Constant.ipynb', 'repo_name': 'mahnooranjum/Programming_DataScience', 'size': '3708'}