python——将类别数据转化为数值数据

文章目录

      • LabelEncoder
      • pd.Catrgorical(series).codes
      • 字典映射

LabelEncoder

# 数据预处理-将类别数据转化为数值数据
import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder


# 加载数据
def loaddata():
    columns = ['sepal_length', 'speal_width', 'petal_length', 'petal_width', 'type']
    data = pd.read_csv('data/iris.data', header=None, names=columns)
    data = data.values
    X = data[:, :-1]
    y = data[:, -1]
    return X, y


if __name__ == '__main__':
    # 加载数据
    X, y = loaddata()
    # 获取标签
    label = np.unique(y)

    le = LabelEncoder()
    le.fit(label)
    y = le.transform(y)
    print(y)

pd.Catrgorical(series).codes

# 数据预处理-将类别数据转化为数值数据
import numpy as np
import pandas as pd


# 加载数据
def loaddata():
    columns = ['sepal_length', 'speal_width', 'petal_length', 'petal_width', 'type']
    data = pd.read_csv('data/iris.data', header=None, names=columns)
    # 将类别信息转化为数值信息
    data['type'] = pd.Categorical(data['type']).codes
    data = data.values
    X = data[:, :-1]
    y = data[:, -1]
    return X, y


if __name__ == '__main__':
    # 加载数据
    X, y = loaddata()
    print(y)

字典映射

# 数据预处理-将类别数据转化为数值数据
import numpy as np
import pandas as pd


# 加载数据
def loaddata():
    columns = ['sepal_length', 'speal_width', 'petal_length', 'petal_width', 'type']
    data = pd.read_csv('iris.data', header=None, names=columns)
    # 将类别信息转化为数值信息
    data['type'] = data['type'].map({'Iris-setosa':0,'Iris-versicolor':1,'Iris-virginica':2}).astype(int)
    data = data.values
    X = data[:, :-1]
    y = data[:, -1]
    return X, y


if __name__ == '__main__':
    # 加载数据
    X, y = loaddata()
    print(y)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
 1. 1. 1. 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
 2. 2. 2. 2. 2. 2.]

你可能感兴趣的:(#,基础进阶,类别数据转化为数值数据)