Hướng dẫn how do you impute mode value in python? - làm thế nào để bạn áp dụng giá trị chế độ trong python?

Hướng dẫn how do you impute mode value in python? - làm thế nào để bạn áp dụng giá trị chế độ trong python?

Các giá trị bị thiếu là phổ biến trong việc xử lý các vấn đề trong thế giới thực khi dữ liệu được tổng hợp trong thời gian dài từ các nguồn khác nhau và mô hình học máy học đáng tin cậy yêu cầu xử lý cẩn thận dữ liệu bị thiếu. Một chiến lược là đưa ra các giá trị bị thiếu và nhiều thuật toán tồn tại kéo dài nội suy đơn giản (trung bình. Trung bình, chế độ), các phương pháp nhân tố ma trận như SVD, mô hình thống kê như bộ lọc Kalman và phương pháp học sâu. Thiếu giá trị cắt bỏ hoặc thay thế các kỹ thuật giúp các mô hình học máy học hỏi từ dữ liệu không đầy đủ. Có ba kỹ thuật cắt giảm giá trị chính - trung bình, trung bình và chế độ. Giá trị trung bình là trung bình của tất cả các giá trị trong một tập hợp, trung bình là số giữa trong một tập hợp các số được sắp xếp theo kích thước và chế độ là giá trị số phổ biến nhất cho hai hoặc nhiều bộ.

Trong bài đăng trên blog này, bạn sẽ tìm hiểu về cách áp đặt hoặc thay thế các giá trị bị thiếu & nbsp; với & nbsp; trung bình, trung bình & nbsp; và & nbsp; mode & nbsp; trong một hoặc nhiều cột tính năng số của gấu trúc & nbsp; . Bạn cũng sẽ tìm hiểu về cách quyết định sử dụng kỹ thuật nào để đưa ra các giá trị bị thiếu với các biện pháp xu hướng trung tâm của cột tính năng như trung bình, trung bình hoặc chế độ. Điều này rất quan trọng để hiểu kỹ thuật này cho các nhà khoa học dữ liệu khi xử lý các giá trị thiếu một trong những khía cạnh chính của tiền xử lý dữ liệu khi đào tạo các mô hình ML.how to impute or replace missing values  with mean, median and mode in one or more numeric feature columns of Pandas DataFrame while building machine learning (ML) models with Python programming. You will also learn about how to decide which technique to use for imputing missing values with central tendency measures of feature column such as mean, median or mode. This is important to understand this technique for data scientists as handling missing values one of the key aspects of data preprocessing when training ML models.

Bộ dữ liệu được sử dụng cho mục đích minh họa là tuyển dụng trường liên quan và được lấy từ trang Kaggle về tuyển dụng khuôn viên trường. & NBSP; Bước đầu tiên, tập dữ liệu được tải. Dưới đây là mã Python để tải bộ dữ liệu sau khi bạn tải xuống trên hệ thống của mình.

import pandas as pd
import numpy as np

df = pd.read_csv("/Users/ajitesh/Downloads/Placement_Data_Full_Class.csv")

df.head()

Đây là những gì dữ liệu trông như thế nào. Ghi lại giá trị NAN trong cột Mức lương.NaN value under the salary column.

Hướng dẫn how do you impute mode value in python? - làm thế nào để bạn áp dụng giá trị chế độ trong python?
Hình 1. Bộ dữ liệu vị trí để xử lý các giá trị bị thiếu bằng cách sử dụng trung bình, trung bình hoặc chế độ

Các giá trị bị thiếu được xử lý bằng các kỹ thuật nội suy khác nhau ước tính các giá trị bị thiếu từ các ví dụ đào tạo khác. Trong bộ dữ liệu trên, các giá trị bị thiếu được tìm thấy trong cột Mức lương. Lệnh như df.isnull (). Sum () in cột có giá trị bị thiếu. Các giá trị còn thiếu trong cột Mức lương trong ví dụ trên có thể được thay thế bằng các kỹ thuật sau:interpolation techniques which estimate the missing values from the other training examples. In the above dataset, the missing values are found in the salary column. The command such as df.isnull().sum() prints the column with missing value. The missing values in the salary column in the above example can be replaced using the following techniques:

  • Giá trị trung bình của các giá trị tiền lương khác
  • Giá trị trung bình của các giá trị tiền lương khác
  • Chế độ (thường xuyên nhất) giá trị của các giá trị tiền lương khác.
  • Giá trị hiện có

Trong bài đăng này, phương thức fillna () trên khung dữ liệu được sử dụng để buộc các giá trị bị thiếu với giá trị trung bình, trung bình, chế độ hoặc không đổi. Tuy nhiên, bạn cũng có thể muốn kiểm tra bài đăng có liên quan có tiêu đề Tắt dữ liệu bị thiếu bằng Sklearn Simplimimputer trong đó sklearn.impute.simpleimputer được sử dụng để thiếu giá trị sử dụng giá trị trung bình, trung bình, chế độ hoặc giá trị không đổi. Lớp & nbsp; ________ 3 & nbsp; cung cấp các chiến lược cơ bản để đưa ra các giá trị bị thiếu. Các giá trị bị thiếu có thể được buộc tội với giá trị không đổi được cung cấp hoặc sử dụng số liệu thống kê (trung bình, trung bình hoặc thường xuyên nhất) của mỗi cột trong đó các giá trị bị thiếu được đặt. Bạn cũng có thể muốn kiểm tra bài viết của Scikit-learn-buộc tội các giá trị bị thiếu.fillna() method on the data frame is used for imputing missing values with mean, median, mode or constant value. However, you may also want to check out the related post titled imputing missing data using Sklearn SimpleImputer wherein sklearn.impute.SimpleImputer is used for missing values imputation using mean, median, mode, or constant value. The SimpleImputer class provides basic strategies for imputing missing values. Missing values can be imputed with a provided constant value, or using the statistics (mean, median, or most frequent) of each column in which the missing values are located. You may also want to check out the Scikit-learn article – Imputation of missing values.

  • Làm thế nào để quyết định sử dụng kỹ thuật cắt bỏ nào?
  • Áp đặt / thay thế các giá trị bị thiếu bằng giá trị trung bình
  • Áp đặt / thay thế các giá trị bị thiếu bằng trung bình
  • Áp đặt / thay thế các giá trị bị thiếu bằng chế độ
  • Làm một bài kiểm tra
      • Điều nào sau đây không phải là một kỹ thuật được đề xuất để đưa ra các giá trị bị thiếu khi phân phối dữ liệu bị sai lệch?
      • Những lô nào sau đây có thể được sử dụng để xác định kỹ thuật thích hợp nhất để thiếu giá trị?
      • Đối với phân phối dữ liệu sai lệch, có thể sử dụng (các) kỹ thuật nào sau đây?
      • Đối với các tính năng phân loại, kỹ thuật nào sau đây có thể được sử dụng?
  • Sự kết luận

Làm thế nào để quyết định sử dụng kỹ thuật cắt bỏ nào?

Áp đặt / thay thế các giá trị bị thiếu bằng giá trị trung bìnhbetter measure of the central tendency of data and use that value for replacing missing values appropriately.

Áp đặt / thay thế các giá trị bị thiếu bằng trung bìnhbox plots and distribution plots come very handily in deciding which techniques to use. You can use the following code to print different plots such as box and distribution plots.

import seaborn as sns
#
# Box plot
#
sns.boxplot(df.salary)
#
# Distribution plot
#
sns.distplot(df.salary)

Áp đặt / thay thế các giá trị bị thiếu bằng chế độYou may note that the data is skewed. There are several or large numbers of data points that act as outliers. Outliers data points will have a significant impact on the mean and hence, in such cases, it is not recommended to use the mean for replacing the missing values. Using mean values for replacing missing values may not create a great model and hence gets ruled out. For symmetric data distribution, one can use the mean value for imputing missing values.

Thus, one may want to use either median or mode. Here is a great page on understanding boxplots.

Hướng dẫn how do you impute mode value in python? - làm thế nào để bạn áp dụng giá trị chế độ trong python?
Fig 1. Boxplot for deciding whether to use mean, mode or median for imputation

You can also observe a similar pattern from the plotting distribution plot. One can observe that there are several high-income individuals in the data points. The data looks to be right-skewed (long tail in the right). Here is how the plot looks like.

Hướng dẫn how do you impute mode value in python? - làm thế nào để bạn áp dụng giá trị chế độ trong python?
Fig 2. Distribution plot for deciding imputation technique

The most simple technique of all is to replace missing data with some constant value. The value can be any number that seemed appropriate.

Impute / Replace Missing Values with Mean

One of the techniques is mean imputation in which the missing values are replaced with the mean value of the entire feature column. In the case of fields like salary, the data may be skewed as shown in the previous section. In such cases, it may not be a good idea to use mean imputation for replacing the missing values. Note that imputing missing data with mean values can only be done with numerical data.

Impute / Replace Missing Values with Median

Another technique is median imputation in which the missing values are replaced with the median value of the entire feature column. When the data is skewed, it is good to consider using the median value for replacing the missing values. Note that imputing missing data with median value can only be done with numerical data.

Impute / Replace Missing Values with Mode

Yet another technique is mode imputation in which the missing values are replaced with the mode value or most frequent value of the entire feature column. When the data is skewed, it is good to consider using mode values for replacing the missing values. For data points such as the salary field, you may consider using mode for replacing the values. Note that imputing missing data with mode values can be done with numerical and categorical data.

Here is the python code sample where the mode of salary column is replaced in place of missing values in the column:

df['salary'] = df['salary'].fillna(df['salary'].mode()[0])

Here is how the data frame would look like (df.head())after replacing missing values of the salary column with the mode value. Note the value of 30000 in the fourth row under the salary column. 30000 is the mode of salary column which can be found by executing commands such as df.salary.mode()

Hướng dẫn how do you impute mode value in python? - làm thế nào để bạn áp dụng giá trị chế độ trong python?
Fig 4. Mode value 30000 replaced NaN in 4th row under salary column

You may want to check other two related posts on handling missing data:

  • Missing data imputation techniques in machine learning
  • Imputing missing data using Sklearn SimpleImputer

Take a Quiz

Take a quick quiz to check your understanding of concepts related with imputing missing values with mean, median or mode.

Which of the following is not a recommended technique for imputing missing values when data distribution is skewed?

Replacing missing values with mean

Replacing missing values with median

Replacing missing values with mode

Which of the following plots can be used to identify most appropriate technique for missing values imputation?

Box plot

Distribution plot

Both of the above

For skewed data distribution, which of the following technique (s) can be used?

Use median

Use mode

Use both median and mode

For categorical features, which of the following technique can be used?

Use mean

Use median

Use mode

Conclusion

In this post, you learned about some of the following:

  • You can use central tendency measures such as mean, median or mode of the numeric feature column to replace or impute missing values.
  • You can use mean value to replace the missing values in case the data distribution is symmetric.
  • Consider using median or mode with skewed data distribution.
  • Pandas Dataframe method in Python such as fillna can be used to replace the missing values.
  • Methods such as mean(), median() and mode() can be used on Dataframe for finding their values.

  • Author
  • Recent Posts

Hướng dẫn how do you impute mode value in python? - làm thế nào để bạn áp dụng giá trị chế độ trong python?

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin.

Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking

Hướng dẫn how do you impute mode value in python? - làm thế nào để bạn áp dụng giá trị chế độ trong python?

Ajitesh Kumar

Gần đây tôi đã làm việc trong lĩnh vực phân tích dữ liệu bao gồm khoa học dữ liệu và học máy / học sâu. Tôi cũng đam mê các công nghệ khác nhau bao gồm các ngôn ngữ lập trình như Java/JEE, JavaScript, Python, R, Julia, v.v. vv Để cập nhật và blog mới nhất, hãy theo dõi chúng tôi trên Twitter. Tôi rất thích kết nối với bạn trên LinkedIn. Kiểm tra cuốn sách mới nhất của tôi có tiêu đề là Nguyên tắc đầu tiên suy nghĩ: Xây dựng các sản phẩm chiến thắng bằng cách sử dụng suy nghĩ nguyên tắc đầu tiên

Làm thế nào để bạn áp đặt chế độ trong Python?

Bạn có thể sử dụng các biện pháp xu hướng trung tâm như trung bình, trung bình hoặc chế độ của cột tính năng số để thay thế hoặc áp đặt các giá trị bị thiếu. Bạn có thể sử dụng giá trị trung bình để thay thế các giá trị bị thiếu trong trường hợp phân phối dữ liệu là đối xứng. Xem xét sử dụng trung bình hoặc chế độ với phân phối dữ liệu sai lệch.use central tendency measures such as mean, median or mode of the numeric feature column to replace or impute missing values. You can use mean value to replace the missing values in case the data distribution is symmetric. Consider using median or mode with skewed data distribution.

Cắt bỏ chế độ là gì?

Định nghĩa: Việc cắt bỏ chế độ (hoặc thay thế chế độ) Thay thế các giá trị bị thiếu của biến phân loại bằng chế độ của các trường hợp không bỏ lỡ của biến đó.replaces missing values of a categorical variable by the mode of non-missing cases of that variable.

Làm thế nào để bạn áp đặt dữ liệu phân loại trong Python?

Một cách tiếp cận để tạo ra các tính năng phân loại là thay thế các giá trị bị thiếu bằng lớp phổ biến nhất.Bạn có thể làm bằng cách lấy chỉ mục của tính năng phổ biến nhất được đưa ra trong hàm giá trị của Pandas.replace missing values with the most common class. You can do with by taking the index of the most common feature given in Pandas' value_counts function.

Cách tốt nhất để áp đặt các giá trị bị thiếu là gì?

Bảy cách để tạo dữ liệu: Các phương pháp phổ biến để buộc dữ liệu bị thiếu..
Nghĩa là cắt bỏ.....
Thay thế.....
Cắt bỏ sàn nóng.....
Cắt bỏ boong lạnh.....
Hồi quy.....
Cắt bỏ hồi quy ngẫu nhiên.....
Nội suy và ngoại suy ..