<jupyter_start><jupyter_text>Ordinal Encoding * Where you can order the feature labels in such a way that you can assign a rank to them.* For example: Low, Medium and High or Grading System (A-1, B-2, C-3, D-4, F-5)* Experience of a Batsman (A-10 years-1st Rank, B-5 years-2nd Rank, C-3 years-3rd Rank)* Weekdays and Weekends (Mon-7, Tues, Wed, Thur, Fri-3, Sat-2, Sun-1)<jupyter_code>import datetime
today_date = datetime.datetime.today()
today_date
today_date-datetime.timedelta(3)<jupyter_output><empty_output><jupyter_text>List Comprehension<jupyter_code>days = [today_date - datetime.timedelta(x) for x in range(0,15)]
days
import pandas as pd
data=pd.DataFrame(days)
data.columns=["Day"]
data.head()<jupyter_output><empty_output><jupyter_text>* Advised to take out the **hour, minute, second, month, year, day of the month** and create those features from the 'Day' Column and then drop the 'Day' column<jupyter_code>data['weekday']=data['Day'].dt.day_name()
data.head()<jupyter_output><empty_output><jupyter_text>Ranking the days of the week<jupyter_code>dictionary={'Monday':1,'Tuesday':2,'Wednesday':3,'Thursday':4,'Friday':5,'Saturday':6,'Sunday':7}
dictionary<jupyter_output><empty_output><jupyter_text>* .map() function maps the week day with the rank assigned to it.<jupyter_code>data['weekday_ordinal']=data['weekday'].map(dictionary)
data<jupyter_output><empty_output><jupyter_text>Count Or Frequency Encoding * It replace the feature label with the **frequency or the count** of that feature label.* Replace the **'Nan' or '?'** with **'Missing'/'Others'** label.  * If a new country is added after the deployment, it can be added to the **'Others'** column.<jupyter_code>train_set = pd.read_csv('http://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data',
                        header = None,index_col=None)
train_set.head()
columns=[1,3,5,6,7,8,9,13]
train_set=train_set[columns]
train_set.columns=['Employment','Degree','Status','Designation','family_job','Race','Sex','Country']
train_set.head()
for feature in train_set.columns[:]:
    print(feature,":",len(train_set[feature].unique()),'labels')
country_map=train_set['Country'].value_counts
country_map
country_map=train_set['Country'].value_counts().to_dict()
country_map
train_set['Country']=train_set['Country'].map(country_map)
train_set.head(20)<jupyter_output><empty_output><jupyter_text>1. **Advantages**    * Easy To Use    * Not increasing feature space         2. **Disadvantage**    * It will provide same weight if the frequencies of the features are same.  Target Guided Ordinal Encoding 1. Ordering the labels according to the target feature.2. Replace the labels by the joint probability of being 1 or 0.<jupyter_code>df=pd.read_csv('titanic.csv', usecols=['Cabin','Survived'])
df.head()
df['Cabin'].fillna('Missing',inplace=True)
df.head()
df['Cabin']=df['Cabin'].astype(str)
df.head()
df['Cabin']=df['Cabin'].astype(str).str[0]
df.head()
df.Cabin.unique()<jupyter_output><empty_output><jupyter_text>* We are grouping the rows by the **Cabin Index** and taking the **Percentage of People who survived** in that particular **Cabin Index**<jupyter_code>df.groupby(['Cabin'])['Survived'].mean()
df.groupby(['Cabin'])['Survived'].mean().sort_values().index
ordinal_labels=df.groupby(['Cabin'])['Survived'].mean().sort_values().index
ordinal_labels
enumerate(ordinal_labels, start=0)
ordinal_labels2={k:i for i,k in enumerate(ordinal_labels,0)}
ordinal_labels2<jupyter_output><empty_output><jupyter_text>* The **iterator variable(0 to 8)** will always be returned first and then the value of **ordinal label(T to D)** will be returned for **enumerate*** In this higher the **'Cabin_ordinal_labels'**, higher is the **Percentage of Survival Rate**<jupyter_code>df['Cabin_ordinal_labels']=df['Cabin'].map(ordinal_labels2)
df.head()<jupyter_output><empty_output><jupyter_text>Mean Encoding * Here instead of Ranking the **Percentage Survival Rate** from 0 to 8, we replace the **Cabin Feature** with the **Percentage Survival Rate Value**itself.<jupyter_code>mean_ordinal=df.groupby(['Cabin'])['Survived'].mean().to_dict()
mean_ordinal
df['Cabin_Mean_Encoding'] = df['Cabin'].map(mean_ordinal)
df.head(20)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15233821445441662, 'fraction_numerical': 0.013226263580538498, 'mean_word_length': 7.504016064257028, 'pattern_counts': {'":': 1, '<': 41, '<?xml version=': 0, '>': 41, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '441216', 'license': 'MIT', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1430, 'n_words': 342, 'path': 'Feature Engineering/Feature_Engineering_Part_4.ipynb', 'repo_name': 'aryamanak10/Data-Science-Learning', 'size': '4234'}