0% found this document useful (0 votes)
20 views8 pages

Pandas isnull() and Data Cleaning Guide

The document outlines a comprehensive data analysis process for fire incident data, including data cleaning, handling missing values, and preparing the data for modeling. It details various analyses such as trends in incidents over time, spatial clustering of incidents, and the financial impact of different incident types. The code also implements predictive modeling techniques to optimize resource utilization and improve incident response efficiency.

Uploaded by

tali66261
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

Pandas isnull() and Data Cleaning Guide

The document outlines a comprehensive data analysis process for fire incident data, including data cleaning, handling missing values, and preparing the data for modeling. It details various analyses such as trends in incidents over time, spatial clustering of incidents, and the financial impact of different incident types. The code also implements predictive modeling techniques to optimize resource utilization and improve incident response efficiency.

Uploaded by

tali66261
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Code Explanation:

Data Cleaning:

1. Importing Libraries: The code starts by importing necessary libraries such as


pandas for data manipulation, scikit>learn for machine learning tasks, and geopy
for geocoding functionalities.

2. Reading Data: It reads the data from a CSV file using pandas' `read_csv` function.

3. Checking Empty Cells: The code checks for empty cells in each column of the
DataFrame using the `isnull().sum()` method.

4. Filtering Special Service Data: It filters rows where the 'StopCodeDescription' is


'Special Service' and counts NaN values in the 'SpecialServiceType' column within
the filtered rows.

5. Handling NaN Values: NaN values in the 'SpecialServiceType' column are replaced
with 'Not applicable' using the `fillna()` method.

6. Filling Data Efficiently: Several functions are defined to efficiently fill missing data:
> `fill_postcode_from_district`: Fills 'Postcode_full' values based on
'Postcode_district'.
> `fill_lat_lon_from_postcode_efficiently`: Fills 'Latitude' and 'Longitude' values
based on 'Postcode_full'.
> `fill_postcode_from_district_efficiently`: Another approach to fill 'Postcode_full'
based on 'Postcode_district'.
> `fill_incgeo_wardcode_from_propercase`: Fills 'IncGeo_WardCode' based on
'ProperCase'.
> `fill_easting_from_uprn`: Fills 'Easting_m' based on 'UPRN'.

7. Filling Blank Cells: Additional columns like 'IncGeo_WardCode',


'IncGeo_WardName', etc., are filled with 'Unknown' or 0 where appropriate.
8. Preparing for Linear Regression: The data is prepared for a linear regression
model by selecting relevant columns and handling missing values in 'Easting_m' and
'Northing_m' using linear regression.

9. Fitting Linear Regression Models: Two linear regression models are fitted:
> One predicts missing 'Easting_m' values based on 'Longitude'.
> Another predicts missing 'Northing_m' values based on 'Longitude'.

10. Predicting and Filling Missing Values: Missing 'Easting_m' and 'Northing_m'
values are predicted using the fitted models and filled in the DataFrame.

11. Saving Cleaned Data: Finally, the cleaned DataFrame is saved to a new CSV file.

In your report, you can explain each step briefly, highlighting the data cleaning and
preprocessing techniques used, the strategies for handling missing values, and the
use of linear regression for imputation. You can also mention the efficiency
considerations in filling missing data and the overall goal of preparing the data for
further analysis or modeling.

Data Behavior:

1. Trend of Incidents Over Time:


> The code converts the 'DateOfCall' column to datetime format.
> It groups the data by month and calculates the number of incidents per month.
> The monthly trend of incidents is plotted using a line graph.

2. Trend of Incident Types:


> The code counts the frequency of different types of incidents ('IncidentGroup').
> It plots the frequency of incident types using a bar chart.

3. Trend of First Pump Arriving Attendance Time:


> Rows with 'Unknown' or missing values in 'FirstPumpArriving_AttendanceTime'
are filtered out.
> The average first pump arriving attendance time is calculated monthly.
> The trend of average attendance time over time is plotted using a line graph.

4. Incidents by Hour of Call:


> The code counts the number of incidents by hour of the day ('HourOfCall').
> It plots the incidents by hour using a bar chart.

5. Notional Cost of Incidents Over Time:


> The 'Notional Cost (£)' column is converted to numeric format, handling errors if
any.
> The total notional cost of incidents is calculated monthly.
> The trend of total notional cost over time is plotted using a line graph.

Each visualization provides valuable insights into different aspects of the incident
data, such as the overall trend of incidents over time, the frequency of incident
types, response time trends, hourly patterns of incidents, and the financial impact of
incidents. These visualizations can help in understanding patterns, identifying
trends, and making informed decisions based on the data.

Spatial Analysis for Fire Incident Hotspots:

1. Loading and Preprocessing Data:


> Reads the dataset from a CSV file, selecting specific columns.
> Filters out invalid latitude and longitude values.
> Fills missing values in the 'Postcode_district' column with 'Unknown'.
> Imputes missing values in 'Latitude' and 'Longitude' columns with their mean
values.

2. Applying K>means Clustering:


> Extracts latitude and longitude coordinates for clustering.
> Applies K>means clustering with a specified number of clusters (default is 5).
> Adds a new column 'Cluster' to the DataFrame indicating the cluster each data
point belongs to.

3. Analyzing Clusters:
> Prints detailed information about each cluster, including the number of
incidents, centroid coordinates, and bounding box of latitude and longitude values.

4. Plotting Clusters:
> Plots the clustered data points on a scatter plot, using seaborn for visualization.
> Includes centroids of the clusters as red stars for better understanding of
cluster centers.

5. Generating Heatmap:
> Creates a heatmap based on the density of incidents within each cluster.
> Utilizes seaborn's kdeplot to visualize the density distribution.

This analysis helps in understanding the spatial distribution and clustering patterns
of fire incidents, providing valuable insights for further investigation or
decision>making. You can customize parameters such as the number of clusters
and plot sizes based on your specific analysis needs.

Impact of Incident Types on Resource


Utilization:
This Python code performs several data analysis and modeling tasks on fire incident
data. Here's a summary of what each part of the code accomplishes:

1. Data Exploration and Visualization:


> Groups the data by 'IncidentGroup' and aggregates unique
'StopCodeDescription' values.
> Converts the series into a DataFrame for better readability.
> Computes frequency and distribution of stations and pumps attending incidents
by incident type.
> Plots bar charts and box plots to visualize the frequency and distribution of
stations and pumps attending incidents.

2. Feature Engineering and Modeling:


> Extracts the hour from the 'TimeOfCall' column and adds it as a new feature
'HourOfCall'.
> Selects features ('IncidentGroup', 'HourOfCall', 'PropertyType') and target
('NumPumpsAttending') for the regression model.
> Encodes categorical variables using OneHotEncoder.
> Splits the dataset into training and testing sets.
> Creates a pipeline with OneHotEncoder and DecisionTreeRegressor.
> Trains the model on the training data and evaluates it on the testing data using
mean absolute error (MAE).
> Plots actual vs. predicted values to visualize model performance.

3. Cost Analysis:
> Aggregates notional costs by incident type to assess the financial impact.
> Calculates the average cost per incident within each incident group.
> Plots bar charts to visualize total and average notional costs by incident type.

Overall, this code provides a comprehensive analysis of fire incident data, including
visualization of incident characteristics, modeling the number of pumps attending
incidents, and assessing the financial impact of different incident types.

Efficiency of Incident Call Processing:


1. Data Cleaning and Preprocessing
Numeric Conversion and Handling NaNs: Ensuring that time metrics are numeric
and handling missing values are crucial steps to maintain data integrity. Clean data
leads to more reliable models, which in turn supports sound decision>making.
2. Feature Engineering
Creating New Features: Features like CallToIncidentRatio provide new insights that
can help in understanding the factors affecting response times. Knowing which
variables influence response times the most can guide resource allocation and
process improvement.
3. Model Training and Evaluation
Predictive Modeling: Using RandomForest and XGBoost models to predict first pump
arriving attendance time based on various features can help in anticipating delays
and identifying areas for improvement.
Evaluation Metrics: Metrics such as Mean Squared Error (MSE), Mean Absolute Error
(MAE), and R>squared offer insights into model performance. A model with lower
MSE and MAE and higher R>squared is more reliable. Businesses can use these
models to simulate different scenarios and prepare more effectively for future
incidents.
4. Visualization of Actual vs Predicted Values
Understanding Model Accuracy: Visual comparisons between actual and predicted
response times illustrate the model's accuracy in real>world terms. This can help in
trust>building among stakeholders and in refining the models for better accuracy.
5. Analysis of Call Volume Patterns
Call Volume by Time of Day/Week: Visualizing call volumes can reveal patterns and
trends, such as peak times or days when incidents are more likely to occur. This
insight allows businesses to allocate resources more effectively, ensuring that
adequate personnel and equipment are available when needed most.
Business Benefits and Decision Support:
Resource Optimization: By understanding when and where incidents are more likely
to occur, businesses can optimize resource allocation, ensuring that response teams
are adequately staffed and equipped to handle peak times.
Process Improvement: Identifying factors that lead to delayed response times can
highlight areas for process improvement. For example, if certain times of day have
longer response times, it may indicate a need for process adjustments or additional
resources.
Strategic Planning: Predictive models can inform long>term strategic planning, such
as where to station new resources or how to design training programs for
responders based on the most impactful factors affecting response times.
Performance Monitoring: Continuously monitoring model predictions against actual
outcomes can help in setting and tracking performance benchmarks. It also
supports a culture of continuous improvement.
In summary, the script supports a data>driven approach to managing incident
response times, offering a foundation for making informed business decisions,
optimizing operations, and enhancing readiness for future incidents.

Incident Response Cost Analysis:


1. Data Cleaning and Preprocessing:
> Loads the dataset and inspects the 'Notional Cost (£)' column for data type and
missing values.
> Converts 'Notional Cost (£)' to a numeric format, handling potential
non>numeric characters like currency symbols and commas.
> Handles missing values in 'Notional Cost (£)' by filling them with the median.

2. Data Analysis and Visualization:


> Computes summary statistics and visualizes the average cost per incident type
using bar charts.

3. Feature Selection and Splitting:


> Defines relevant columns and separates features (X) and target variable (y).
> Splits the dataset into training and testing sets.

4. Pipeline Setup:
> Sets up preprocessing pipelines for numerical and categorical features using
SimpleImputer for missing values and StandardScaler for scaling numerical
features.
> Combines the preprocessing pipelines using ColumnTransformer.
> Creates a model pipeline with GradientBoostingRegressor as the estimator.

5. Model Training and Evaluation:


> Fits the model pipeline on the training data.
> Makes predictions on both training and testing sets.
> Evaluates the model using mean absolute error (MAE) and mean squared error
(MSE) for both training and testing data.

6. Visualization of Predictions:
> Plots a scatter plot of actual vs. predicted notional costs, along with a line of
best fit.
Overall, this code provides a comprehensive example of cleaning, preprocessing,
modeling, and evaluating a machine learning regression model for predicting the
notional cost of fire incidents.

Common questions

Powered by AI

The code employs several strategies to fill missing geographic data. It uses functions like `fill_postcode_from_district` and `fill_lat_lon_from_postcode_efficiently` to impute missing 'Postcode_full', 'Latitude', and 'Longitude' values based on related fields like 'Postcode_district'. Additionally, it uses the mean values of 'Latitude' and 'Longitude' to fill their respective missing values . These methods help maintain the dataset's completeness, enabling spatial analysis such as clustering. However, relying on generalized data may lead to inaccuracies, especially if local variations are significant, which might impact the resolution of spatial patterns.

Using linear regression to predict missing 'Easting_m' and 'Northing_m' values can be beneficial by providing a statistical method to estimate these values based on correlated features like 'Longitude'. This imputation helps maintain the dataset's completeness for spatial analysis, enabling more robust models . However, challenges include potential inaccuracies if there are non-linear relationships, which could misrepresent the spatial data structure. Ensuring that the assumptions of linear regression are met is crucial for accurate imputation.

Converting 'DateOfCall' to datetime format is crucial for analyzing incident trends over time as it allows for structured temporal analyses, such as grouping incidents by month or day to identify trends. This conversion facilitates accurate time series analyses, plots, and trend extractions, enabling the identification of patterns in incident occurrences, such as seasonal peaks or long-term trends . Without this step, temporal analyses would lack precision, potentially obscuring meaningful insights.

The Python script handles missing values in the 'SpecialServiceType' column by replacing NaN values with 'Not applicable' using the `fillna()` method . This approach ensures that all entries in the column are uniform and complete, which maintains data integrity when performing analyses that depend on the presence of this field. By imputing a placeholder value, the analysis avoids biases that could arise from dropping records with missing values, though it may obscure the underlying reasons for the missing data.

A heatmap of fire incidents provides visual insights into the density and distribution of incidents across a region. By highlighting areas with high incident frequencies, it identifies hotspots that could benefit from increased resource allocation. This spatial recognition allows for strategic placement of response units and informs decisions about where additional support may be needed, potentially improving response times and effectiveness . Such information is critical for policymakers and emergency services to deploy resources efficiently and mitigate response challenges.

The code ensures data integrity when handling the 'Notional Cost (£)' column by converting it to a numeric format and handling potential errors such as non-numeric characters . This process involves cleaning the data by removing currency symbols and commas, and filling missing values with the median. By preprocessing in this manner, the code maintains consistency and reliability in financial analyses, enabling accurate aggregation and assessment of incident costs. These steps ensure that subsequent analyses are based on precise cost data, facilitating better financial decision-making.

The code assesses the financial impact of different incident types by aggregating notional costs based on 'IncidentGroup', calculating total and average costs for each group . These financial impacts are visualized using bar charts that provide a clear representation of the cost distribution across incident types, facilitating easy comparison and identification of high-cost incidents. This visualization format enables stakeholders to quickly grasp financial burdens and may guide resource allocation decisions.

Feature engineering plays a crucial role in predicting the number of pumps attending incidents by creating new informative features such as 'HourOfCall' extracted from 'TimeOfCall' and encoding categorical features like 'IncidentGroup' and 'PropertyType' for machine learning models . These steps help capture temporal patterns and categorical influences, improving model accuracy. By tweaking these features, models can more effectively learn relevant patterns, ultimately enhancing prediction quality and facilitating better resource allocation strategies.

Plotting incident trends by the hour of call reveals patterns in daily activity, identifying peak incident times. These insights can inform operational decisions, such as optimizing staffing levels and resource availability during high-incident hours. For instance, if the analysis shows higher incidents in the evening, more personnel and equipment could be stationed to ensure prompt response, improving efficiency and potentially reducing damages . Such data-driven adjustments lead to enhanced preparedness and resource utilization.

The application of K-means clustering enhances the understanding of fire incident spatial distributions by segmenting the dataset into clusters based on location coordinates (latitude and longitude). This approach identifies regional patterns, with cluster centroids indicating areas of higher incident density, which could signify hotspots. Additionally, examining the number of incidents per cluster and their geographical bounds provides deeper insights into spatial concentrations and potential resource needs . However, cluster accuracy depends on appropriate parameter settings, such as the number of clusters.

You might also like