চিত্র থেকে Pexels
One-hot encoding is a data preprocessing step to convert categorical values into compatible numerical representations.
categorical_column | bool_col | col_1 | col_2 | লেবেল |
value_A | সত্য | 9 | 4 | 0 |
value_B | মিথ্যা | 7 | 2 | 0 |
value_D | সত্য | 9 | 5 | 0 |
value_D | মিথ্যা | 8 | 3 | 1 |
value_D | মিথ্যা | 9 | 0 | 1 |
value_D | মিথ্যা | 5 | 4 | 1 |
value_B | সত্য | 8 | 1 | 1 |
value_D | সত্য | 6 | 6 | 1 |
value_C | সত্য | 0 | 5 | 0 |
For example for this dummy dataset, the categorical column has multiple string values. Many machine learning algorithms require the input data to be in numerical form. Therefore, we need some way to convert this data attribute to a form compatible with such algorithms. Thus, we break down the categorical column into multiple binary-valued columns.
Firstly, read the .csv file or any other associated file into a Pandas data frame.
df = pd.read_csv("data.csv")
To check unique values and better understand our data, we can use the following Panda functions.
df['categorical_column'].nunique()
df['categorical_column'].unique()
For this dummy data, the functions return the following output:
>>> 4
>>> array(['value_A', 'value_C', 'value_D', 'value_B'], dtype=object)
For the categorical column, we can break it down into multiple columns. For this, we use pandas.get_dummies() method. It takes the following arguments:
যুক্তি | |
তথ্য: array-like, Series, or DataFrame | The original panda’s data frame object |
কলাম: list-like, default None | List of categorical columns to hot-encode |
drop_first: bool, default False | Removes the first level of categorical labels |
To better understand the function, let us work on one-hot encoding the dummy dataset.
Hot-Encoding the Categorical Columns
We use the get_dummies method and pass the original data frame as উপাত্ত ইনপুট. ভিতরে কলাম, we pass a list containing only the categorical_column হেডার।
df_encoded = pd.get_dummies(df, columns=['categorical_column', ])
The following commands drops the categorical_column and creates a new column for each unique value. Therefore, the single categorical column is converted into 4 new columns where only one of the 4 columns will have a 1 value, and all of the other 3 are encoded 0. This is why it is called One-Hot Encoding.
categorical_column_value_A | categorical_column_value_B | categorical_column_value_C | categorical_column_value_D |
1 | 0 | 0 | 0 |
0 | 1 | 0 | 0 |
0 | 0 | 0 | 1 |
0 | 0 | 0 | 1 |
0 | 0 | 0 | 1 |
0 | 0 | 0 | 1 |
0 | 1 | 0 | 0 |
0 | 0 | 0 | 1 |
0 | 0 | 1 | 0 |
0 | 0 | 0 | 1 |
The problem occurs when we want to one-hot encode the boolean column. It creates two new columns as well.
Hot Encoding Binary Columns
df_encoded = pd.get_dummies(df, columns=[bool_col, ])
bool_col_False | bool_col_True |
0 | 1 |
1 | 0 |
0 | 1 |
1 | 0 |
We unnecessarily increase a column when we can have only one column where True is encoded to 1 and False is encoded to 0. To solve this, we use the drop_first যুক্তি.
df_encoded = pd.get_dummies(df, columns=['bool_col'], drop_first=True)
bool_col_True |
1 |
0 |
1 |
0 |
The dummy dataset is one-hot encoded where the final result looks like
col_1 | col_2 | bool, | A | B | C | D | লেবেল |
9 | 4 | 1 | 1 | 0 | 0 | 0 | 0 |
7 | 2 | 0 | 0 | 1 | 0 | 0 | 0 |
9 | 5 | 1 | 0 | 0 | 0 | 1 | 0 |
8 | 3 | 0 | 0 | 0 | 0 | 1 | 1 |
9 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
5 | 4 | 0 | 0 | 0 | 0 | 1 | 1 |
8 | 1 | 1 | 0 | 1 | 0 | 0 | 1 |
6 | 6 | 1 | 0 | 0 | 0 | 1 | 1 |
0 | 5 | 1 | 0 | 0 | 1 | 0 | 0 |
1 | 8 | 1 | 0 | 0 | 0 | 1 | 0 |
The categorical values and boolean values have been converted to numerical values that can be used as input to machine learning algorithms.
মুহাম্মদ আরহাম কম্পিউটার ভিশন এবং ন্যাচারাল ল্যাঙ্গুয়েজ প্রসেসিং-এ কর্মরত একজন ডিপ লার্নিং ইঞ্জিনিয়ার। তিনি বেশ কয়েকটি জেনারেটিভ এআই অ্যাপ্লিকেশনের স্থাপনা এবং অপ্টিমাইজেশান নিয়ে কাজ করেছেন যা Vyro.AI-তে বিশ্বব্যাপী শীর্ষ তালিকায় পৌঁছেছে। তিনি বুদ্ধিমান সিস্টেমের জন্য মেশিন লার্নিং মডেল তৈরি এবং অপ্টিমাইজ করতে আগ্রহী এবং ক্রমাগত উন্নতিতে বিশ্বাস করেন।
- এসইও চালিত বিষয়বস্তু এবং পিআর বিতরণ। আজই পরিবর্ধিত পান।
- PlatoData.Network উল্লম্ব জেনারেটিভ Ai. নিজেকে ক্ষমতায়িত করুন। এখানে প্রবেশ করুন.
- প্লেটোএআইস্ট্রিম। Web3 ইন্টেলিজেন্স। জ্ঞান প্রসারিত. এখানে প্রবেশ করুন.
- প্লেটোইএসজি। মোটরগাড়ি / ইভি, কার্বন, ক্লিনটেক, শক্তি, পরিবেশ সৌর, বর্জ্য ব্যবস্থাপনা. এখানে প্রবেশ করুন.
- ব্লকঅফসেট। পরিবেশগত অফসেট মালিকানার আধুনিকীকরণ। এখানে প্রবেশ করুন.
- উত্স: https://www.kdnuggets.com/2023/07/pandas-onehot-encode-data.html?utm_source=rss&utm_medium=rss&utm_campaign=pandas-how-to-one-hot-encode-data
- : আছে
- : হয়
- :কোথায়
- 1
- 11
- 7
- 8
- 9
- a
- AI
- আলগোরিদিম
- সব
- এবং
- কোন
- অ্যাপ্লিকেশন
- রয়েছি
- আর্গুমেন্ট
- AS
- যুক্ত
- At
- BE
- হয়েছে
- বিশ্বাস
- উত্তম
- বিরতি
- ভবন
- নামক
- CAN
- চার্ট
- চেক
- স্তম্ভ
- কলাম
- উপযুক্ত
- কম্পিউটার
- কম্পিউটার ভিশন
- রূপান্তর
- ধর্মান্তরিত
- সৃষ্টি
- উপাত্ত
- গভীর
- গভীর জ্ঞানার্জন
- ডিফল্ট
- বিস্তৃতি
- নিচে
- ড্রপ
- প্রতি
- প্রকৌশলী
- উদাহরণ
- মিথ্যা
- ফাইল
- চূড়ান্ত
- প্রথম
- অনুসরণ
- জন্য
- ফর্ম
- ফ্রেম
- থেকে
- ক্রিয়া
- ক্রিয়াকলাপ
- সৃজক
- জেনারেটিভ এআই
- বিশ্বব্যাপী
- আছে
- he
- কিভাবে
- কিভাবে
- এইচটিএমএল
- HTTPS দ্বারা
- উন্নতি
- in
- বৃদ্ধি
- ইনপুট
- বুদ্ধিমান
- আগ্রহী
- মধ্যে
- IT
- JPG
- কেডনুগেটস
- ভাষা
- শিক্ষা
- দিন
- উচ্চতা
- লিঙ্কডইন
- তালিকা
- সৌন্দর্য
- মেশিন
- মেশিন লার্নিং
- অনেক
- পদ্ধতি
- মডেল
- বহু
- প্রাকৃতিক
- স্বভাবিক ভাষা
- স্বাভাবিক ভাষা প্রক্রিয়াকরণ
- প্রয়োজন
- নতুন
- of
- on
- ONE
- কেবল
- সর্বোচ্চকরন
- or
- মূল
- অন্যান্য
- আমাদের
- আউটপুট
- পান্ডাস
- পাস
- Plato
- প্লেটো ডেটা ইন্টেলিজেন্স
- প্লেটোডাটা
- সমস্যা
- প্রক্রিয়াজাতকরণ
- পৌঁছেছে
- পড়া
- প্রয়োজন
- ফল
- প্রত্যাবর্তন
- s
- ক্রম
- বিভিন্ন
- একক
- সমাধান
- কিছু
- ধাপ
- স্ট্রিং
- এমন
- সিস্টেম
- লাগে
- যে
- সার্জারির
- অতএব
- এই
- এইভাবে
- থেকে
- শীর্ষ
- সত্য
- দুই
- বোঝা
- অনন্য
- অকারণে
- us
- ব্যবহার
- ব্যবহৃত
- মূল্য
- মানগুলি
- দৃষ্টি
- প্রয়োজন
- উপায়..
- we
- আমরা একটি
- কখন
- কেন
- ইচ্ছা
- সঙ্গে
- হয়া যাই ?
- কাজ করছে
- কাজ
- zephyrnet