1 场景分析
1.1 项目背景
描述开发项目模型的一系列情境和因素,包括问题、需求、机会、市场环境、竞争情况等
1.2. 解决问题
传统机器学习在解决实际问题中主要分为两类:
- 有监督学习:已知输入、输出之间的关系而进行的学习,从而产生一个能够对已知输入给出合适输出的模型。这些算法在图像分类、语音识别、自然语言处理、推荐系统等领域有着广泛的应用
- 无监督学习:已知输入,无输出结果而进行的学习,发现数据中的潜在特征和规律而训练的模型。这些算法在数据挖掘、图像处理、自然语言处理等领域有着广泛的应用
传统机器学习达到的目的主要分为两类
- 分析影响结果的主要因素
- 充分必要条件下预测结果
传统机器学习算法在实际开发中主要分两类
- 基于树的算法
- 非基于树的算法
2 数据整体情况
2.1 数据加载
数据分析3剑客:numpy pandas matplotlib
# 导入相关包
import os
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
pd.set_option('display.max_rows', None)
import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
import seaborn as sns
import plotly.express as px
from sklearn import preprocessing
from sklearn.preprocessing import LabelEncoder
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')
1、 pandas读取数据: pd.read_csv(),训练数据一般从csv文件加载。读取数据返回DataFrame,df.head() 查看前5条件数据分布
# 读取数据
df = pd.read_csv('./xxx.csv')
df.head()
2、查看数据总体信息
df.info()
3、 查看数据描述
# 数据总数、平均值、标准差、最大最小值,25% 50% 75% 分位值
df.describe().T
4、统计数据空值
df.isnull().sum()
5、 查看数据形状
df.shape
6、查看数据类型
df.dtypes
2.2 样本是否均衡
如果正、负样本不均衡怎么做?
- 大样本变少——下采样
- 小样本变多——上采样
- 实际应用中,上采样较多,将真实的数据做重复冗余
2.3 数据分析
以下为案例:
2.3.1单因分析
- 绘制直方图
fig = px.histogram(df, x='列名', hover_data=df.columns, title='XXX分布', barmode='group')
fig.show()
fig = px.histogram(df, x='TPC_LIP', color='TPC_LIP', hover_data=df.columns, title='罐盖分布', barmode='group')
fig.show()
- 绘制分布图
hv.Distribution(np.round(df['列名'])).opts(title='标题', color='green', xlabel='x轴标签名', ylabel='y轴标签名')\
.opts(opts.Distribution(width=1000, height=600, tools=['hover'], show_grid=True))
hv.Distribution(df['BF_IRON_DUR']).opts(title='XXX时长', color='red', xlabel='时长(秒)', ylabel='Destiny')\
.opts(opts.Distribution(width=1000, height=600, tools=['hover'], show_grid=True))
2.3.2 多因分析
- 绘制直方图
temp_agg = df.groupby('OUTER_TEMPERATURE').agg({
'TEMPERATURE': ['min', 'max']})
temp_maxmin = pd.merge(temp_agg['TEMPERATURE']['max'],temp_agg['TEMPERATURE']['min'],right_index=True,left_index=True)
temp_maxmin = pd.melt(temp_maxmin.reset_index(), ['OUTER_TEMPERATURE']).rename(columns={
'OUTER_TEMPERATURE':'OUTER_TEMPERATURE', 'variable':'Max/Min'})
hv.Bars(temp_maxmin, ['OUTER_TEMPERATURE', 'Max/Min'], 'value').opts(title="Temperature by OUTER_TEMPERATURE Max/Min", ylabel="TEMPERATURE")\
.opts(opts.Bars(width=1000, height=700,tools=['hover'],show_grid=True))
- 寻找特征偏态(skewness)和核密度估计(Kernel density estimate KDE)
plt.figure(figsize=(15,10))
for i,col in enumerate(df.columns, 1):
plt.subplot(5,3,i)
plt.title(f"Distribution of {
col} Data"<