本学习笔记为阿里云天池龙珠计划数据挖掘训练营的学习内容,学习链接为:-天池实验室-实时在线的数据分析协作工具,享受免费计算资源 (aliyun.com)https://tianchi.aliyun.com/notebook-ai/detail?spm=5176.20850271.J_3678908510.8.25243daangG2Dh&postId=170927
一、学习知识点概要
- 了解常用的机器学习模型,并掌握机器学习模型的建模与调参流程
线性回归模型
决策树模型
GBDT模型
XGBoost模型
LightGBM模型
推荐教材:
- 《机器学习》 机器学习 (豆瓣)
- 《统计学习方法》 统计学习方法 (豆瓣)
- 《Python大战机器学习》 Python大战机器学习 (豆瓣)
- 《面向机器学习的特征工程》 Feature Engineering for Machine Learning Models (豆瓣)
- 《数据科学家访谈录》 数据科学家访谈录 (豆瓣)
二、学习内容
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
reduce_mem_usage 函数通过调整数据类型,帮助我们减少数据在内存中占用的空间
def reduce_mem_usage(df):
""" iterate through all the columns of a dataframe and modify the data type
to reduce memory usage.
"""
start_mem = df.memory_usage().sum()
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in df.columns:
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
df[col] = df[col].astype(np.int8)
elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
df[col] = df[col].astype(np.int16)
elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
df[col] = df[col].astype(np.int32)
elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
df[col] = df[col].astype(np.int64)
else:
if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
df[col] = df[col].astype(np.float16)
elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
df[col] = df[col].astype(np.float32)
else:
df[col] = df[col].astype(np.float64)
else:
df[col] = df[col].astype('category')
end_mem = df.memory_usage().sum()
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))
return df
sample_feature = reduce_mem_usage(pd.read_csv('data_for_tree.csv'))
Memory usage of dataframe is 62099672.00 MB Memory usage after optimization is: 16520303.00 MB Decreased by 73.4%
continuous_feature_names = [x for x in sample_feature.columns if x not in ['price','brand','model'