火警预测
火警 系统当检测到烟雾、火灾、一氧化碳或其他与火灾有关或一般通知的紧急情况时,向人们发出警告。
这些警报可以通过烟雾探测器和热探测器自动激活,也可以通过手动火警激活设备(例如手动呼叫点或拉
站)激活。警报可以是电动铃或壁挂式发声器或喇叭。它们也可以是扬声器闪光灯,发出警报,然后发出
语音疏散信息,警告建筑物内的人不要使用电梯。火警发声器可以设置为特定频率和不同的音调,包括低、
中和高,具体取决于设备的国家和制造商。欧洲的大多数火警系统听起来像频率交替的警报器。火警电子
设备在美国和加拿大被称为喇叭,可以是连续的,也可以设置为不同的代码。火警警报装置也可以设置为
不同的音量级别。
目录
不
内容
不
内容
不
内容
1 个
导入库
7
温度
13
空气压力
2 个
关于数据集
8 个
湿度
14
颗粒物
3 个
基本探索
9
总挥发性有机化合物
15
颗粒物浓度
4 个
数据集摘要
10
二氧化碳当量浓度
16
相关图
5 个
数据预处理
11
原氢存在
17
模型创建和性能评估
6 个
用于可视化的自定义调色板
12
原料乙醇存在
18
谢谢
导入库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# !pip install -q ptitprince
import ptitprince as pt
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import BernoulliNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
import warnings
warnings.filterwarnings('ignore')
关于数据集
� UTC:进行实验的时间
� 温度[C]:环境温度,单位为摄氏度
� 湿度[%]:实验过程中的空气湿度
� TVOC[ppb]:总挥发性有机化合物,以 ppb(十亿分之一)为单位测量
� eCO2[ppm]:二氧化碳当量浓度,单位为 ppm(百万分之一)
� Raw H2:原氢量 [Raw Molecular Hydrogen; 未补偿(偏差、温度等)] 存在于环境中
� 原乙醇:环境中存在的原乙醇量
� Pressure[hPa]:气压,以 hPa 为单位
� PM1.0:直径小于 1.0 微米的颗粒状物质
� PM2.5:直径小于 2.5 微米的颗粒状物质
� NC0.5:直径小于 0.5 微米的颗粒物浓度
� NC1.0:直径小于 1.0 微米的颗粒物浓度
� NC2.5:直径小于 2.5 微米的颗粒物浓度
� CNT:样本计数。火灾警报(现实)如果存在火灾则值为 1 否则为 0
� 火警: 1 表示肯定,0 表示不肯定
data = pd.read_csv("../input/smoke-detection-dataset/smoke_detection_iot.csv",index_col=0)
基本探索
让我们看一下数据集。
print(f"Shape Of The Dataset : {data.shape}")
print(f"\nGlimpse Of The Dataset :")
data.head().style.set_properties(**{"background-color": "#5f0d11","color":"#ddab46","border":
"1.5px #5f0d11"})
In [ ]:
print(f"Informations Of The Dataset :\n")
print(data.info())
数据集摘要
print(f"Summary Of The Dataset :")
data.describe().style.set_properties(**{"background-color": "#5f0d11","color":"#ddab46","border":
"1.5px #5f0d11"})
In [ ]:
dup = data[data.duplicated()].shape[0]
print(f"There is {dup} duplicate entry among {data.shape[0]} entries in this dataset.")
data.drop_duplicates(keep='first',inplace=True)
print(f"After removing duplicate entries there are {data.shape[0]} entries in this dataset.")
In [ ]:
print(f"Null values of the Dataset :")
data.isna().sum().to_frame().T.style.set_properties(**{"background-color":
"#5f0d11","color":"#ddab46","border": "1.5px #5f0d11"})
数据预处理
data["Fire Alarm"].replace({0:"No", 1:"Yes"}, inplace = True)
data.rename(columns={"Temperature[C]": "Temperature","Humidity[%]":
"Humidity","TVOC[ppb]":"TVOC","eCO2[ppm]":"eCO2","Pressure[hPa]":"Pressure"}, inplace =
True)
data = data[["Temperature","Humidity","TVOC","eCO2","Raw H2","Raw
Ethanol","Pressure","PM1.0","PM2.5","NC0.5","NC1.0","NC2.5","Fire Alarm"]]
print("After preprocessing, let's have a glimpse of the final dataset :")
data.head().style.set_properties(**{"background-color": "#5f0d11","color":"#ddab46","border":
"1.5px #5f0d11"})
In [ ]:
print(f"After preprocessing, let's have a look on the summary of the dataset :")
data.describe().T.style.set_properties(**{"background-color":
"#5f0d11","color":"#ddab46","border": "1.5px #5f0d11"})
用于可视化的自定义调色板
sns.set_style("white")
sns.set(rc={"axes.facecolor":"#e9bb93", "figure.facecolor":"#e9bb93",
"axes.grid":True, "grid.color":"white", "axes.edgecolor":"black",
"grid.linestyle": u"-", "axes.labelcolor": "black", "font.family": [u"DejaVu Sans"],
"text.color": "black", "xtick.color": "black", "ytick.color": "black",
"legend.facecolor":"#e9bb93", "legend.frameon": True, "legend.edgecolor":"black"})
sns.set_context("poster",font_scale = .7)
palette = ["#272716","#6c2411","#ae0d13","#cc3f18","#db6400"]
palette_cmap = ["#272716","#6c2411","#cc3f18","#db6400","#ae0d13"]
# sns.palplot(sns.color_palette(palette))
# sns.palplot(sns.color_palette(palette_cmap))
# plt.show()
温度
print(f"Let's have a look on the distribution of temperature :")
_, axs = plt.subplot_mosaic([["00", "00"],
["10", "11"]],figsize=(20,12), sharey=True)
plt.tight_layout(pad=5.0)
sns.histplot(data=data,x="Temperature",hue="Fire Alarm",hue_order=data["Fire
Alarm"].value_counts(ascending=True).index,multiple="stack",ax=axs["00"],palette=palette[0:3:2],
kde=True,bins=45,alpha=1,fill=True,edgecolor="black",linewidth=2)
axs["00"].lines[0].set_color("orange")
axs["00"].lines[1].set_color("orange")
axs["00"].set_title("\nOverall Temperature",fontsize=25)
axs["00"].set_ylabel("Count",fontsize=20)
axs["00"].set_xlabel("Temperature",fontsize=20)
axs["00"].set_yscale("linear")
sns.histplot(data=data[data["Fire
Alarm"]=="Yes"],x="Temperature",ax=axs["10"],color=palette[2],kde=True,bins=30,alpha=1,fill=
True,edgecolor="black",linewidth=2)
axs["10"].lines[0].set_color("orange")
axs["10"].set_title("\nFor Triggering Fire Alarm",fontsize=25)
axs["10"].set_ylabel("Count",fontsize=20)
axs["10"].set_xlabel("Temperature",fontsize=20)
axs["10"].set_yscale("linear")
sns.histplot(data=data[data["Fire
Alarm"]=="No"],x="Temperature",ax=axs["11"],color=palette[0],kde=True,bins=30,alpha=1,fill=T
rue,edgecolor="black",linewidth=2)
axs["11"].lines[0].set_color("orange")
axs["11"].set_title("\nFor Not Triggering Fire Alarm",fontsize=25)
axs["11"].set_ylabel("Count",fontsize=20)
axs["11"].set_xlabel("Temperature",fontsize=20)
axs["11"].set_yscale("linear")
plt.show()
In [ ]:
print("Let's have a look on the distribution of effectiveness-wise temperature analysis :")
plt.subplots(figsize=(20, 8))
p=pt.RainCloud(data=data,x=data["Fire Alarm"],y=data["Temperature"],order=data["Fire
Alarm"].value_counts(ascending=True).index,pointplot=True,linecolor="orange",point_size=2,pale
tte=palette[0:3:2],saturation=1,linewidth=3,edgecolor="black")
p.axes.set_title("\nEffectiveness Towards Fire Alarm\n",fontsize=25)
p.axes.set_xlabel("Effectiveness",fontsize=20)
p.axes.set_ylabel("Temperature",fontsize=20)
plt.show()
见解:
� 我们可以看到温度分布是偏斜的。大多数温度介于 12°C 和 28°C 之间。
� 大多数引起火灾警报的温度都在 18°C 到 28°C 之间。
� 大多数温度不会引起火灾警报,落在 12°C 到 28°C 之间。
� 不引起火警的平均温度为 19.69°C。
� 引起火警的平均温度为 14.48°C。
湿度
print(f"Let's have a look on the distribution of humidity :")
_, axs = plt.subplot_mosaic([["00", "00"],
["10", "11"]],figsize=(20,12), sharey=False)
plt.tight_layout(pad=5.0)
sns.histplot(data=data,x="Humidity",hue="Fire Alarm",hue_order=data["Fire
Alarm"].value_counts(ascending=True).index,multiple="stack",ax=axs["00"],palette=palette[0:3:2],
kde=True,bins=45,alpha=1,fill=True,edgecolor="black",linewidth=2)
axs["00"].lines[0].set_color("orange")
axs["00"].lines[1].set_color("orange")
axs["00"].set_title("\nOverall Humidity",fontsize=25)
axs["00"].set_ylabel("Count",fontsize=20)
axs["00"].set_xlabel("Humidity",fontsize=20)
axs["00"].set_yscale("linear")
sns.histplot(data=data[data["Fire
Alarm"]=="Yes"],x="Humidity",ax=axs["10"],color=palette[2],kde=True,bins=30,alpha=1,fill=Tru
e,edgecolor="black",linewidth=2)
axs["10"].lines[0].set_color("orange")
axs["10"].set_title("\nFor Triggering Fire Alarm",fontsize=25)
axs["10"].set_ylabel("Count",fontsize=20)
axs["10"].set_xlabel("Humidity",fontsize=20)
axs["10"].set_yscale("linear")
sns.histplot(data=data[data["Fire
Alarm"]=="No"],x="Humidity",ax=axs["11"],color=palette[0],kde=True,bins=30,alpha=1,fill=True
,edgecolor="black",linewidth=2)
axs["11"].lines[0].set_color("orange")
axs["11"].set_title("\nFor Not Triggering Fire Alarm",fontsize=25)
axs["11"].set_ylabel("Count",fontsize=20)
axs["11"].set_xlabel("Humidity",fontsize=20)
axs["11"].set_yscale("linear")