<jupyter_start><jupyter_text>Select top 10 feature by Unitrate Method<jupyter_code>import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
#apply SelectKBest class to extract top 10 best features
bestfeatures = SelectKBest(score_func=chi2, k=10)
fit = bestfeatures.fit(X,y)
fit.scores_
dfscores = pd.DataFrame(fit.scores_)
dfscores
dfcolumns = pd.DataFrame(X.columns)
dfcolumns
#concat two dataframes for better visualization 
featureScores = pd.concat([dfcolumns,dfscores],axis=1)
featureScores
featureScores.columns = ['Specs','Score']  #naming the dataframe columns
featureScores
print(featureScores.nlargest(10,'Score'))
new=featureScores.nlargest(10,'Score')
new<jupyter_output><empty_output><jupyter_text>Feature Importance<jupyter_code>from sklearn.ensemble import ExtraTreesClassifier
import matplotlib.pyplot as plt
model = ExtraTreesClassifier()
model.fit(X,y)
print(model.feature_importances_) #use inbuilt class feature_importances of tree based classifiers
#plot graph of feature importances for better visualization
feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(10).plot(kind='barh')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08832807570977919, 'fraction_numerical': 0.011829652996845425, 'mean_word_length': 8.992125984251969, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '615852', 'license': 'MIT', 'n_tokens_mistral': 423, 'n_tokens_neox': 399, 'n_words': 95, 'path': 'Feature Selection/Features Selection.ipynb', 'repo_name': 'Hamza-Jamil121/Feature-Engineering-Feature-selection-with-Python-Programming', 'size': '1268'}