目录
1.导包
import torch
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
2.加载数据
data = pd.read_csv('./dataset/HR.csv')
data.head() #查看数据的前5条
data.shape #共计14999个数据,10个特征
(14999, 10)
data.info() #查看数据信息
#data原数据没有缺失数据, 若有缺失数据,可以使用机器学习中的特征工程进行缺失值的处理
<class 'pandas.core.frame.DataFrame'> RangeIndex: 14999 entries, 0 to 14998 Data columns (total 10 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 satisfaction_level 14999 non-null float64 1 last_evaluation 14999 non-null float64 2 number_project 14999 non-null int64 3 average_montly_hours 14999 non-null int64 4 time_spend_company 14999 non-null int64 5 Work_accident 14999 non-null int64 6 left 14999 non-null int64 7 promotion_last_5years 14999 non-null int64 8 part 14999 non-null object 9 salary 14999 non-null object dtypes: float64(2), int64(6), object(2) memory usage: 1.1+ MB
data.part.unique() #查看数据part列数据中的去重之后的数据
array(['sales', 'accounting', 'hr', 'technical', 'support', 'management', 'IT', 'product_mng', 'marketing', 'RandD'], dtype=object)
3.数据的特征工程
#深度学习是机器学习的一部分,一个分支,不分家
#分类数据:即离散数据, 取值很有限
# 对于离散的字符串, 有两种处理方式, 1. 字典映射:转化成数字. 2. 进行one-hot编码.
#这里进行one-hot编码.的链式写法(同时编码多列数据)
#join() 是 在dataframe数据结构中 横向添加数据(即新增列数据)
#两次连续使用join(), 属于链式调用API方法
#不能多次运行
# data = data.join(pd.get_dummies(data.part)).join(pd.get_dummies(data.salary)) #使用pd.get_dummies()时,独热编码默认转化为bool值(True 或 False)
data = data.join(pd.get_dummies(data.part, dtype=int)).join(pd.get_dummies(data.salary, dtype=int)) # dtype=int 规定 转化为 1 或 0
# 把part和salary删掉.
data.drop(columns=['part', 'salary'], inplace = True)
data.left.value_counts() #left列数据是分类目标标记
0 11428 1 3571 Name: left, dtype: int64
#深度学习 受 数据分布不均衡的影响 没有机器学习 大
#对于不均衡的数据 , 在机器学习中需要使用 SMOTE算法 进行平衡性处理
11428 / (11428 + 3571) #数据标记分布不均衡,还在可接受的范围
0.7619174611640777
Y_data = data.left.values.reshape(-1, 1)
Y_data
array([[1], [1], [1], ..., [1], [1], [1]], dtype=int64)
Y = torch.from_numpy(Y_data).type(torch.FloatTensor)
Y
tensor([[1.], [1.], [1.], ..., [1.], [1.], [1.]])
#条件判断花式索引 获取X数据
data.columns != 'left'
array([ True, True, True, True, True, True, False, True, True, True, True, True, True, True, True, True, True, True, True, True, True])
#条件判断花式索引 获取X数据
[c for c in data.columns if c != 'left']