FEATURE SELECTION ONE OF THE MOST TIME CONSUMING TOPIC IN DATA SCIENCE PROFESSION
By: Karan Choudhary
Tags: Machine Learning, Datascience Training, Feature Engineering

FEATURE SELECTION TECNIQUES

1.variance threshold

→constant

→quasi constant

→duplicate features

2.fisher score

3.chi-square test

4.mutual information

# In[1]:

#importig the libararies

import pandas as pd

import numpy as np

# In[2]:

bc_data=pd.read_csv(‘breast_cancer.csv’)

# In[8]:

bc_data.head()

# In[10]:

bc_data.shape

# In[5]:

bc_data.columns

# In[6]:

bc_data.info()

# In[7]:

bc_data.corr()

# In[11]:

X=bc_data.drop(labels=[‘diagnosis’],axis=1)

y=bc_data[‘diagnosis’]

# In[13]:

X.shape

# In[15]:

y.shape

# In[16]:

#splitting the data into graining ad testing

from sklearn.model_selection import train_test_split

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.4,random_state=0)

# In[22]:

#shape of raining and testing will be

print(‘Independent variable

Training ->’,X_train.shape,”Testing ->”,X_test.shape)

print(‘Dependent variable

Training ->’,y_train.shape,”Testing ->”,y_test.shape)

# In[24]:

#1.VARIANCE THRESHOLD METHOD

# In[25]:

#Removing The constant features

from sklearn.feature_selection import VarianceThreshold

# In[28]:

V_thr = VarianceThreshold(threshold=0)

V_thr.fit(X_train)

# In[29]:

V_thr.get_support()

# In[30]:

sum(V_thr.get_support())

# In[32]:

constant_columns=[col for col in X_train.columns

if col not in X_train.columns[V_thr.get_support()]]

# In[33]:

constant_columns

# In[ ]:

# #From this constant coloums variable we come to know about that our dataset doesnt contain any constant features

# 2. Removing Quasi-constant features

# In[115]:

# Quasi-Constant features

var_thresh=VarianceThreshold(threshold=0.15)

var_thresh.fit(X_train)

# In[116]:

var_thresh.get_support()

# In[117]:

sum(var_thresh.get_support())

# In[118]:

q_constant_cols=[column for column in X_train.columns

if column not in X_train.columns[var_thresh.get_support()]]

# In[119]:

q_constant_cols

# In[120]:

len(X_train.columns[var_thresh.get_support()])

# In[121]:

len(q_constant_cols)

# # 3. Remove Duplicated Features

# In[123]:

dup_data = X_train.T

dup_data.head()

# In[ ]:

dup_data.duplicated().sum()

#

# In[129]:

dup_data[dup_data.duplicated()]

# In[131]:

dup_data.duplicated().sum()

# In[130]:

# checking duplicates in this we find it to be zero

# # ChiAquare

# In[132]:

from sklearn.feature_selection import SelectKBest

from sklearn.feature_selection import chi2

# In[147]:

features=SelectKBest(score_func=chi2,k=15)

# In[148]:

features

# In[149]:

rank=ordered_features.fit(X_train,y_train)

# In[150]:

rank

# In[151]:

df_scores=pd.DataFrame(ordered_rank.scores_,columns=[‘Score’])

dfcols=pd.DataFrame(X_train.columns)

# In[152]:

dfcols

# In[153]:

features_rank=pd.concat([dfcols,df_scores],axis=1)

# In[155]:

features_rank.columns=[‘Features’,’Score’]

features_rank.nlargest(5,’Score’)

# In[163]:

ranked_features=pd.Series(ordered_rank.scores_,index=X_train.columns)

ranked_features.nlargest(5).plot(kind=’bar’,color=”green”,figsize=(10,10))

plt.show()

# In[181]:

#fisher score

# In[183]:

#importing dataset

dataset=pd.read_csv(‘breast_cancer_data.csv’)

from sklearn import preprocessing



# label_encoder object knows how to understand word labels.

label_encoder = preprocessing.LabelEncoder()



# Encode labels in column ‘species’.

dataset[‘diagnosis’]= label_encoder.fit_transform(dataset[‘diagnosis’])



dataset[‘diagnosis’].unique()

# In[184]:

# using fisher man algorithm

#we will doing an them

def fisher_man_score(attribute_name):

mean_fields=[]

variance=[]

for k in range(0,2): # 0,2 refers here about the class of diagonisis M or B

data=dataset[dataset[‘diagnosis’]== k]

mean_fields.append(df[attribute_name].mean())

variance.append(df[attribute_name].var())

fisher=((mean_fields[0]-mean_fields[1])**2)/(var[0]+var[1])

return fisher

# In[191]:

fisher_1=[]

for i in list (dataset.columns):

fisher_1[i]=fisher_man_score[i]

print(i,” — — →>”,fisher_1[i])

# # MUTUAL INFO

# In[166]:

from sklearn.feature_selection import mutual_info_classif

mutual_data=mutual_info_classif(X,y)

# In[167]:

mutual_info=pd.Series(mutual_data,index=X.columns)

# In[173]:

mutual_info[:10]

# In[180]:

mutual_info.nlargest(5)