# Integrating Machine Learning Techniques for Enhanced Safety and Crime Analysis in Maryland
## 3 Methodology
### Data Analysis
#### 3.4.5 Clustering Analysis of Crime Trends Across Maryland Counties  
Understanding regional crime dynamics is a critical aspect of criminology and public policy. This study examines crime rate trends for robbery, murder, and rape across Maryland counties using the K-means clustering approach to uncover patterns over time and across geographic areas. The choice of the K-means technique was motivated by its effectiveness in identifying distinct groups within large datasets, facilitating the analysis of geographic and temporal patterns in crime data. **Data preprocessing**: Prior to clustering, the crime rate data underwent standardization using the StandardScaler 1.6.1. This crucial step ensured that each variable contributed equally to the distance measure used in the K-means algorithm, which is essential for accurate clustering. **Dimensionality reduction**: To manage the high-dimensional nature of crime data effectively, principal component analysis (PCA) was performed exclusively on the training data before clustering. This strategic application ensures that the test data remain untouched, preventing data leakage and preserving the integrity of the evaluation process. By reducing dimensionality while maintaining the significant variance, PCA enhances the performance of the K-means algorithm and ensures more accurate cluster formation. **Determination of optimal clusters (k):** The selection of the optimal number of clusters (k) was guided by both the elbow method and silhouette score analysis. The elbow method involves plotting the within-cluster sum of squares (distortion) against a range of k-values from 1 to 10. The plot in question indicated an elbow at k = 5, suggesting a significant reduction in variance with minimal gain beyond this point. Additionally, the silhouette score, which assesses cluster cohesion and separation, was calculated for the same range of k-values. Although the highest score was at k = 2, k = 5 provided a reasonable balance between well-defined clusters, as well as the granularity necessary for practical application to regional crime analysis. This informed our choice of setting k = 5, allowing for detailed yet meaningful groupings within the data. The K-means clustering analysis revealed five distinct groups of counties exhibiting similar crime trends, the summary is provided in Table 2. Time series plots visually display these trends for each crime type within the clusters, providing a comprehensive view that supports targeted public safety strategies. These findings are detailed through visualizations and are summarized in the corresponding tables. Figure 6a-o illustrates the time series analysis of robbery, murder, and rape rates across the clusters. Each figure provides detailed trends, aiding the interpretation of clustering outcomes. \begin{table}
\begin{tabular}{c c c} \hline \hline
**Cluster** & **Counties** & **Summary** \\ \hline \multirow{2}{*}{Cluster 0} & Anne Arundel, Baltimore, & \multirow{2}{*}{\begin{tabular}{c} Gradual declines in robbery rates, \\ with recent increases in murder \\ and rape rates. \\ \end{tabular} } \\  & Charles, Frederick, Montgomery & \\ \hline \multirow{2}{*}{Cluster 1} & Calvert, Carroll, Garrett, Howard, & \multirow{2}{*}{\begin{tabular}{c} High variability in robbery} \\ and murder rates; \\ unpredictable trends. \\ \end{tabular} \\  & Queen Anne’s, St. Mary’s, Talbot, & \multirow{2}{*}{\begin{tabular}{c} Persistently high crime rates with \\ slight reductions in \\ robbery after 2018. \\ \end{tabular} } \\  & Washington, Worcester & \\ \hline \multirow{2}{*}{Cluster 2} & \multirow{2}{*}{\begin{tabular}{c} Caroline, Cecil, Harford, Kent, \\ Prince George’s, Wicomico \\ \end{tabular} } & \multirow{2}{*}{\
\begin{tabular}{c} Steady declines in robbery, \\ murder, and rape rates. \\ \end{tabular} } \\  & & \\ \hline \multirow{2}{*}{Cluster 4} & Allegany, Dorchester, Somerset & \multirow{2}{*}{
\begin{tabular}{c} Sporadic spikes in crime rates, \\ driven by isolated incidents. \\ \end{tabular} } \\ \end{tabular}
\end{table}