Hướng dẫn how to normalize categorical data in python - cách chuẩn hóa dữ liệu phân loại trong python
Mã hóa 0/1 của nam/nữ không tự nó đặt trọng lượng hơn cho nữ so với nam giới; Nó không thực sự khác biệt so với giá trị 0 so với 1 (hoặc 1 so với 2) trong một yếu tố dự đoán liên tục. Nó chỉ là một sự khác biệt của 1 đơn vị trong giá trị dự đoán. Như @tim đã chỉ ra một cách đúng đắn, đối với nhiều phương pháp học máy không cần thiết và không cần phải có vấn đề gì về trọng số. Show
Tuy nhiên, có một vấn đề tiềm năng khi phương pháp mô hình hóa của bạn yêu cầu tất cả các yếu tố dự đoán phải ở cùng một quy mô. Ví dụ là phân tích thành phần chính (PCA) và các phương pháp tuyến tính bị phạt như Lasso, Ridge hoặc mạng đàn hồi lai của chúng. Mặc định, trong một số triển khai ít nhất, là bình thường hóa tất cả các dự đoán bao gồm các yếu tố dự đoán phân loại. Đó là nơi bạn có thể gặp khó khăn. Nếu bạn không bình thường hóa các yếu tố dự đoán phân loại, thì chúng có giống như các quy mô với các dự đoán liên tục, như phương pháp xử phạt của bạn hoàn toàn giả định? Nếu bạn thực hiện bình thường hóa các yếu tố dự đoán 2 cấp như nam/nữ, các giá trị được chuẩn hóa có thể phụ thuộc rất nhiều vào tần số lớp trong mẫu của bạn. Vì vậy, bình thường hóa các yếu tố dự đoán có sự mất cân bằng lớp học có thể thay đổi trọng số. Đối với các yếu tố dự đoán phân loại đa cấp, vấn đề thậm chí còn gây khó chịu hơn vì sự lựa chọn mức độ tham chiếu có thể ảnh hưởng đến các giá trị được chuẩn hóa. Trang này đi vào chi tiết hơn. Không có giải pháp phù hợp với một kích cỡ nào cho vấn đề này trong PCA và bị phạt hồi quy; Ứng dụng thông minh của kiến thức đối tượng của bạn có thể là tốt nhất. Nếu bạn có nhiều hơn 2 giá trị phân loại, hãy chuyển đổi tốt hơn một mã hóa nóng. Các giá trị phân loại không nên có mối quan hệ toán học. Nếu bạn không thể giải thích một thứ tự toán học giữa dữ liệu phân loại của bạn (ví dụ: phù hợp> khỏe mạnh?), Bạn nên tạo một vectơ nóng và biểu diễn nó dưới dạng các tính năng:
Về cơ bản, bạn có 6 tính năng mới và mỗi tính năng thể hiện một danh mục của bạn. Lưu ý: Không cần phải áp dụng chuẩn hóa hoặc chuẩn hóa cho dữ liệu nhị phân vì nó đã có trong [0,1] Giới thiệuTrong nhiều hoạt động khoa học dữ liệu thực tế, tập dữ liệu sẽ chứa các biến phân loại. Các biến này thường được lưu trữ dưới dạng các giá trị văn bản đại diện cho các đặc điểm khác nhau. Một số ví dụ bao gồm màu sắc (màu đỏ, màu vàng, màu vàng, màu xanh da trời), kích thước (một cách nhỏ, vừa, trung bình, một cách khác) hoặc chỉ định địa lý (tiểu bang hoặc quốc gia). Bất kể giá trị được sử dụng cho những gì, thách thức là xác định cách sử dụng dữ liệu này trong phân tích. Nhiều thuật toán học máy có thể hỗ trợ các giá trị phân loại mà không cần thao tác thêm nhưng có nhiều thuật toán không có. Do đó, nhà phân tích phải đối mặt với thách thức tìm ra cách biến các thuộc tính văn bản này thành các giá trị số để xử lý thêm & NBSP; Cũng như nhiều khía cạnh khác của thế giới khoa học dữ liệu, không có câu trả lời nào về cách tiếp cận vấn đề này. Mỗi cách tiếp cận có sự đánh đổi và có tác động tiềm năng đến kết quả của phân tích. May mắn thay, các công cụ python của gấu trúc và scikit-learn cung cấp một số cách tiếp cận có thể được áp dụng để chuyển đổi dữ liệu phân loại thành các giá trị số phù hợp. Bài viết này sẽ là một cuộc khảo sát về một số phương pháp phổ biến (và một vài phức tạp hơn) với hy vọng rằng nó sẽ giúp những người khác áp dụng các kỹ thuật này vào thế giới thực & NBSP; Dữ liệu & NBSP; ĐặtĐối với bài viết này, tôi đã có thể tìm thấy một bộ dữ liệu tốt tại kho lưu trữ máy học UCI. Bộ dữ liệu ô tô đặc biệt này bao gồm sự kết hợp tốt của các giá trị phân loại cũng như các giá trị liên tục và đóng vai trò là một ví dụ hữu ích tương đối dễ hiểu. Vì sự hiểu biết miền là một khía cạnh quan trọng khi quyết định làm thế nào để mã hóa các giá trị phân loại khác nhau - tập dữ liệu này tạo ra một trường hợp tốt & NBSP; nghiên cứu.UCI Machine Learning Repository. This particular Automobile Data Set includes a good mix of categorical values as well as continuous values and serves as a useful example that is relatively easy to understand. Since domain understanding is an important aspect when deciding how to encode various categorical values - this data set makes a good case study. Trước khi chúng tôi bắt đầu mã hóa các giá trị khác nhau, chúng tôi cần quan trọng dữ liệu và thực hiện một số việc dọn dẹp nhỏ. May mắn thay, gấu trúc làm điều này & nbsp; đơn giản: import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()
hatchback symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object Audi obj_df = df.select_dtypes(include=['object']).copy() obj_df.head()
hatchback obj_df[obj_df.isnull().any(axis=1)]
RWD obj_df["num_doors"].value_counts() four 114 two 89 Name: num_doors, dtype: int64 obj_df = obj_df.fillna({"num_doors": "four"}) đổi diện …mpfi hatchback Audi obj_df["num_cylinders"].value_counts() four 159 six 24 five 11 eight 5 two 4 twelve 1 three 1 Name: num_cylinders, dtype: int64 Bốn Sedan import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()0 FWD import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()1
hatchback import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()2 Audi BốnMột cách tiếp cận khác để mã hóa các giá trị phân loại là sử dụng một kỹ thuật gọi là mã hóa nhãn. Mã hóa nhãn chỉ đơn giản là chuyển đổi từng giá trị trong một cột thành một số. Ví dụ: cột obj_df[obj_df.isnull().any(axis=1)]0 chứa 5 giá trị khác nhau. Chúng tôi có thể chọn mã hóa nó như & nbsp; này:
Quá trình này làm tôi nhớ đến Ralphie bằng cách sử dụng chiếc nhẫn giải mã bí mật của anh ấy trong một Giáng sinh & NBSP; Câu chuyện Một thủ thuật bạn có thể sử dụng trong gấu trúc là chuyển đổi một cột thành một danh mục, sau đó sử dụng các giá trị danh mục đó cho nhãn của bạn & NBSP; mã hóa: import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()3 import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()4 Sau đó, bạn có thể gán biến được mã hóa cho một cột mới bằng cách sử dụng trình truy cập obj_df[obj_df.isnull().any(axis=1)]1: import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()5
hatchback ohcvAudi Sedan FWD ohc import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()6
hatchback
ohcv import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()7
hatchback ohcv AudiSedan Trong tập dữ liệu cụ thể này, có một cột gọi là obj_df["num_doors"].value_counts()3 chứa một số giá trị khác nhau & nbsp; import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()8 import pandas as pd import numpy as np # Define the headers since the data does not have any headers = ["symboling", "normalized_losses", "make", "fuel_type", "aspiration", "num_doors", "body_style", "drive_wheels", "engine_location", "wheel_base", "length", "width", "height", "curb_weight", "engine_type", "num_cylinders", "engine_size", "fuel_system", "bore", "stroke", "compression_ratio", "horsepower", "peak_rpm", "city_mpg", "highway_mpg", "price"] # Read in the CSV file and convert "?" to NaN df = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/autos/imports-85.data", header=None, names=headers, na_values="?" ) df.head()9 Vì lợi ích của cuộc thảo luận, có thể tất cả những gì chúng ta quan tâm là liệu động cơ có phải là cam chi phí hay không hay không. Nói cách khác, các phiên bản khác nhau của OHC đều giống nhau cho phân tích này. Nếu đây là trường hợp, thì chúng ta có thể sử dụng obj_df["num_doors"].value_counts()4 accessor Plus obj_df["num_doors"].value_counts()5 để tạo một cột mới, cho biết liệu chiếc xe có công cụ OHC & NBSP;OHC) or not. In other words, the various versions of OHC are all the same for this analysis. If this is the case, then we could use the obj_df["num_doors"].value_counts()4 accessor plus obj_df["num_doors"].value_counts()5 to create a new column the indicates whether or not the car has an OHC engine. symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object0 Tôi thấy rằng đây là một chức năng tiện dụng mà tôi sử dụng khá nhiều nhưng đôi khi quên cú pháp vì vậy đây là một đồ họa hiển thị những gì chúng tôi đang làm & NBSP; DataFrame kết quả trông như thế này (chỉ hiển thị một tập hợp con của & nbsp; cột): symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object1
ohc Scikit-LearnCách tiếp cận này có thể thực sự hữu ích nếu có một tùy chọn để hợp nhất một giá trị Y/N đơn giản trong một cột. Điều này cũng nêu bật kiến thức miền quan trọng như thế nào đối với việc giải quyết vấn đề theo cách hiệu quả nhất & nbsp; có thể. Cập nhật Scikit-Learn Phiên bản trước của bài viết này được sử dụng obj_df["num_doors"].value_counts()6 và obj_df["num_doors"].value_counts()7 không phải là cách tiếp cận được đề xuất để mã hóa các giá trị phân loại. Các bộ mã hóa này chỉ nên được sử dụng để mã hóa các giá trị đích chứ không phải tính năng & nbsp; giá trị. Các ví dụ dưới đây sử dụng obj_df["num_doors"].value_counts()8 và obj_df["num_doors"].value_counts()9, đây là cách tiếp cận chính xác để sử dụng để mã hóa các giá trị Target & NBSP; Ngoài phương pháp Pandas, Scikit-Learn còn cung cấp chức năng tương tự. Cá nhân, tôi thấy sử dụng gấu trúc đơn giản hơn một chút để hiểu nhưng cách tiếp cận scikit là tối ưu khi bạn đang cố gắng xây dựng một mô hình dự đoán & nbsp; symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object2
ohc symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object3
Chẳng hạn, nếu chúng ta muốn thực hiện tương đương với mã hóa nhãn trên Make of the Car, chúng ta cần khởi tạo một đối tượng obj_df["num_doors"].value_counts()8 và four 114 two 89 Name: num_doors, dtype: int641 & nbsp; dữ liệu: dữ liệu: dữ liệu: symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object4 xe BMW Advanced ApproachesScikit-learn cũng hỗ trợ mã hóa nhị phân bằng cách sử dụng four 114 two 89 Name: num_doors, dtype: int642, chúng tôi sử dụng một quy trình tương tự như trên để chuyển đổi dữ liệu nhưng quá trình tạo DataFrame của Pandas thêm một vài bước bổ sung. chuyển đổi Hardtop symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object5
hatchback Sedan symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object6
toa xe scikit-learn pipelinesSử dụng đường ống Phần này đã được thêm vào tháng 11 năm 2020. Mục tiêu là chỉ ra cách tích hợp các chức năng mã hóa tính năng Scikit-LEARN vào một đường ống xây dựng mô hình đơn giản. Như đã đề cập ở trên, các bộ mã hóa phân loại Scikit-Learn, cho phép bạn kết hợp việc chuyển đổi vào các đường ống của bạn có thể đơn giản hóa quy trình xây dựng mô hình và tránh một số cạm bẫy. Tôi đề nghị video trường dữ liệu này như một phần giới thiệu tốt. Nó cũng đóng vai trò là cơ sở cho cách tiếp cận đã phác thảo & nbsp; bên dưới. Dưới đây là một ví dụ rất nhanh về cách kết hợp obj_df["num_doors"].value_counts()9 và obj_df["num_doors"].value_counts()8 vào đường ống và sử dụng four 114 two 89 Name: num_doors, dtype: int647 để phân tích & nbsp; kết quả: kết quả: symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object7 Bây giờ chúng tôi có dữ liệu của mình, hãy để xây dựng cột & NBSP; Transformer: symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object8 Ví dụ này cho thấy cách áp dụng các loại bộ mã hóa khác nhau cho các cột nhất định. Sử dụng đối số four 114 two 89 Name: num_doors, dtype: int648 để truyền tất cả các giá trị số thông qua đường ống mà không có bất kỳ & nbsp; thay đổi. Đối với mô hình, chúng tôi sử dụng hồi quy tuyến tính đơn giản và sau đó tạo & nbsp; đường ống: symboling int64 normalized_losses float64 make object fuel_type object aspiration object num_doors object body_style object drive_wheels object engine_location object wheel_base float64 length float64 width float64 height float64 curb_weight int64 engine_type object num_cylinders object engine_size int64 fuel_system object bore float64 stroke float64 compression_ratio float64 horsepower float64 peak_rpm float64 city_mpg int64 highway_mpg int64 price float64 dtype: object9 Chạy xác thực chéo 10 lần bằng cách sử dụng lỗi tuyệt đối trung bình âm làm chức năng tính điểm của chúng tôi. Cuối cùng, lấy trung bình của 10 giá trị để xem độ lớn của & nbsp; lỗi: obj_df = df.select_dtypes(include=['object']).copy() obj_df.head()0 Mang lại giá trị của four 114 two 89 Name: num_doors, dtype: int649 Rõ ràng có nhiều phân tích có thể được thực hiện ở đây nhưng điều này có nghĩa là để minh họa cách sử dụng các chức năng Scikit-learn trong một phân tích thực tế hơn & NBSP; đường ống. Sự kết luậnMã hóa các biến phân loại là một bước quan trọng trong quá trình khoa học dữ liệu. Bởi vì có nhiều cách tiếp cận để mã hóa các biến, điều quan trọng là phải hiểu các tùy chọn khác nhau và cách thực hiện chúng trên các bộ dữ liệu của riêng bạn. Hệ sinh thái khoa học dữ liệu Python có nhiều cách tiếp cận hữu ích để xử lý các vấn đề này. Tôi khuyến khích bạn ghi nhớ những ý tưởng này vào lần tới khi bạn thấy mình phân tích các biến phân loại. Để biết thêm chi tiết về mã trong bài viết này, vui lòng xem lại sổ ghi chép. Thay đổi
Tôi có thể bình thường hóa dữ liệu phân loại không?Không cần phải bình thường hóa các biến phân loại. Bạn không rõ ràng về loại phân tích bạn đang làm, nhưng thông thường bạn đang xử lý các biến phân loại như các biến giả trong phân tích thống kê.. You are not very explicit about the type of analysis you are doing, but typically you are dealing with the categorical variables as dummy variables in the statistical analysis.
Làm thế nào để bạn bình thường hóa dữ liệu trong Python?Sử dụng MinMaxScaler () để bình thường hóa dữ liệu trong Python Đây là một lựa chọn phổ biến hơn để bình thường hóa các bộ dữ liệu. Bạn có thể thấy rằng các giá trị trong đầu ra nằm giữa (0 và 1). MinMaxScaler cũng cung cấp cho bạn tùy chọn để chọn phạm vi tính năng. Theo mặc định, phạm vi được đặt thành (0,1). to Normalize Data in Python
This is a more popular choice for normalizing datasets. You can see that the values in the output are between (0 and 1). MinMaxScaler also gives you the option to select feature range. By default, the range is set to (0,1).
Chúng ta có thể sử dụng Smote cho dữ liệu phân loại không?Kỹ thuật lấy mẫu thiểu số tổng hợp (SMOTE) cho dự án này, tôi đã sử dụng kỹ thuật lấy mẫu thiểu số tổng hợp cho các tính năng danh nghĩa và liên tục (SMOTE-NC) từ thư viện mất cân bằng, tạo ra dữ liệu tổng hợp cho các tính năng phân loại cũng như định lượng trongbộ dữ liệu.creates synthetic data for categorical as well as quantitative features in the data set.
Làm thế nào để bạn chuyển đổi dữ liệu phân loại thành dữ liệu số trong Python?Chúng tôi sẽ sử dụng .labelencoder () từ Thư viện Sklearn để chuyển đổi dữ liệu phân loại sang dữ liệu số.Chúng tôi sẽ sử dụng hàm fit_transform () trong quy trình.using . LabelEncoder() from sklearn library to convert categorical data to numerical data. We will use function fit_transform() in the process. |