wget https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
Code
# DataFlair Iris Flower Classification
# Import Packages
import numpy as np
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
# Step 1: Load data
columns = ['Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Class_labels']
# Load the data
df = pd.read_csv('./datasets/iris.data', names=columns)
print(df)
# Step 2: Perform One-Hot Encoding
# creating instance of one-hot-encoder
encoder = OneHotEncoder(handle_unknown='ignore')
# perform one-hot encoding on 'team' column
encoder_df = pd.DataFrame(encoder.fit_transform(df[['Class_labels']]).toarray())
print(encoder_df)
final_df = df.join(encoder_df)
print(final_df)
# Step 3: Drop the Original Categorical Variable
final_df.drop('Class_labels', axis=1, inplace=True)
print(final_df)
final_df.to_csv("", index=0, header=0)
- How to Perform One-Hot Encoding in Python
- [pandas read and write]https://blog.csdn.net/hao5335156/article/details/81093929
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)