Support Vector Machine Overview and Code
Support Vector Machine Overview and Code
Support Vector Machine (SVM) is a supervised machine learning algorithm primarily used for
classification tasks, although it can also be applied to regression. SVM's goal is to find a
hyperplane in an N-dimensional space (where N is the number of features) that distinctly classifies
the data points. Here’s a breakdown of how it works:
KEY CONCEPTS OF SVM:
1. Hyperplane: This is a decision boundary that separates different classes in the dataset. In
2D, this is a line, while in higher dimensions, it becomes a plane or a hyperplane. The SVM
algorithm tries to find the optimal hyperplane that best separates the data into different
classes.
2. Support Vectors: These are the data points that lie closest to the hyperplane. These points
are critical because they determine the position and orientation of the hyperplane. The SVM
algorithm aims to maximize the margin between the support vectors of different classes.
3. Margin: This is the distance between the hyperplane and the closest data points from each
class (support vectors). The goal of SVM is to maximize this margin, ensuring better
separation between the classes.
4. Linear and Non-Linear Classification:
o If the data is linearly separable, SVM finds a straight hyperplane.
o If the data is not linearly separable, SVM uses something called the kernel trick to
transform the data into a higher-dimensional space where it becomes separable.
Common kernels include:
Linear Kernel: For linearly separable data.
Polynomial Kernel: For data with polynomial relationships.
Radial Basis Function (RBF) Kernel: For more complex, non-linear
relationships.
STEPS OF THE SVM ALGORITHM:
1. Training: The algorithm is given a labelled dataset and tries to find the optimal hyperplane
that separates the classes.
2. Maximizing the Margin: SVM ensures that the margin between the support vectors of
different classes is maximized, making the model more generalizable.
3. Prediction: Once the model is trained, it can be used to classify new data points by
determining on which side of the hyperplane they lie.
ANALYSIS OF CODE SVM MODEL TRAINING AND TESTING:
The program is a comprehensive machine learning workflow that predicts a continuous target
variable (`TRUE_TEC`) using two different algorithms: Support Vector Regression (SVR) and
Random Forest Regressor. Here's an overview of its working without diving into specific code
details:
1. Data Loading
The program begins by loading a dataset from a CSV file. It performs a quick preview of the data
and strips any unnecessary whitespace from the column names to avoid issues later.
2. Date and Time Handling
If the dataset contains columns representing dates and times, they are converted into numeric
formats that can be used by machine learning models:
- Dates are converted to Unix timestamps (seconds since epoch).
- Times are converted to total seconds.
This step ensures that these columns are usable in numerical models.
3. Handling Missing Data
Any missing values in the dataset are filled using the median value of each column. Filling missing
data is important to prevent errors during model training and to ensure a robust learning process.
4. Feature Preparation
The program identifies the target variable (`TRUE_TEC`) and separates it from the other features
(input variables). If there are non-numeric columns (e.g., categorical features), they are converted
into numeric form using label encoding.
5. Train-Test Split
The data is split into two sets:
- Training set (99%): Used to train the machine learning models.
- Test set (1%): Used to evaluate the performance of the models on unseen data.
6. Feature Scaling
The feature values are scaled to a range between 0 and 1 using normalization. This is particularly
important for models like SVR, which are sensitive to the magnitude of feature values.
7. Hyperparameter Tuning for SVR
A Support Vector Regression (SVR) model is trained. The program uses a grid search technique
to tune various hyperparameters such as:
- `C` (regularization parameter)
- `kernel` (e.g., linear, polynomial, RBF)
- `degree` (for polynomial kernels)
- `gamma` (kernel coefficient)
This grid search uses cross-validation (splitting the training data into smaller sets) to find the best-
performing combination of these hyperparameters.
8. Model Training and Evaluation (SVR)
After selecting the best hyperparameters from the grid search, the SVR model is trained on the
training set and evaluated on the test set. The program calculates the Mean Squared Error (MSE)
and R-squared (R²) metrics to assess how well the model performs:
- MSE: Measures the average squared difference between actual and predicted values.
- R²: Measures the proportion of variance in the target variable explained by the model.
9. Cross-Validation for SVR
The program applies 10-fold cross-validation to the SVR model, which divides the dataset into 10
parts and trains/evaluates the model on different subsets. This provides a more robust estimate of
the model’s performance.
10. Training and Evaluation of Random Forest Regressor
For comparison, a Random Forest Regressor model is also trained and evaluated using the same
training and test sets. Similar to SVR, the program calculates the MSE and R² metrics to assess
the Random Forest’s performance.
11. Cross-Validation for Random Forest
The Random Forest model is evaluated using 10-fold cross-validation to ensure its performance is
stable across different parts of the dataset.
12. Visualization
The program generates scatter plots to visualize the predictions made by both models (SVR and
Random Forest) compared to the actual values. A "perfect prediction" line is drawn to help visually
assess how close the predicted values are to the actual values.
13. Conclusion
The program compares the performance of the two models (SVR and Random Forest) using both
the evaluation metrics and the visual plots. This allows you to determine which model performs
better for predicting the target variable (`TRUE_TEC`).
Key Components:
1. Data Preprocessing: Converts date/time and categorical data into numeric formats and fills
missing values.
2. Modeling: Trains two models, Support Vector Regression (SVR) and Random Forest, for
regression tasks.
3. Hyperparameter Tuning: Uses `GridSearchCV` to optimize SVR hyperparameters.
4. Evaluation: Assesses model performance using MSE and R² scores on a test set, as well
as cross-validation.
5. Visualization: Provides visual insights into the models' predictive performance.
This workflow is designed to give a robust comparison between two different algorithms (SVR and
Random Forest) on a regression task, with the ultimate goal of selecting the model that performs
best for predicting the target variable `TRUE_TEC`.
ANALYSIS OF PROGRAM FOR PLOTTING TEC VALUE AND PREDICTED VALUE ALSO TO
HIGHLIGHT SOLAR FLARE:
This code performs the following tasks:
1. Data Loading:
It reads data from an Excel file into a pandas DataFrame using the pd.read_excel()
function. The file path is specified, and the file is expected to contain columns for DATE and
TIME, as well as columns for TRUE_TEC and PREDICTED_TRUE_TEC.
2. Combining Date and Time:
The DATETIME column is created by combining the DATE and TIME columns. The
[Link]() function is applied row-wise to combine the date and time into a
single timestamp, enabling easier plotting.
3. Plot Initialization:
A matplotlib plot is initialized with a figure size of 12x8 inches, providing a larger canvas for
the plot.
4. Unique Date Extraction:
The unique dates in the dataset are extracted using df['DATE '].unique(). This will allow the
plot to be segmented by date.
5. Custom Colors:
Four custom colors are defined:
o true_tec_color: for plotting the TRUE_TEC values (blue).
o predicted_tec_color: for plotting the PREDICTED_TRUE_TEC values (orange).
o highlight_true_tec_color: for highlighting the TRUE_TEC values on December 15th
(red).
o highlight_predicted_tec_color: for highlighting the PREDICTED_TRUE_TEC values
on December 15th (green).
6. Highlighting a Specific Date:
The date 2023-12-15 is chosen as a special date (highlight_date). On this date, the
TRUE_TEC and PREDICTED_TRUE_TEC values will be plotted in red and green,
respectively, to highlight the data points for this day.
7. Plotting Loop:
For each unique date, the data for that date is filtered using df[df['DATE '] == date].
o If the date matches the highlight_date (i.e., December 15th), the TRUE_TEC and
PREDICTED_TRUE_TEC are plotted with highlight colors (red for TRUE_TEC,
green for PREDICTED_TRUE_TEC), thicker lines, and a legend label.
o For all other dates, the TRUE_TEC and PREDICTED_TRUE_TEC values are plotted
using the default colors (blue for TRUE_TEC, orange for PREDICTED_TRUE_TEC).
8. Plot Details:
X-axis and Y-axis labels are set to "Date and Time" and "TEC Units" respectively.
A title is added: "Spectrum Plot of TRUE_TEC and Predicted TEC (Highlighted 15th
December)".
The grid is enabled for easier reading of the plot.
Empty plot calls are used to create legend entries for TRUE_TEC and Predicted TEC colors
outside the loop. These dummy plot calls help set the labels without needing a specific plot
for them.
9. Final Adjustments:
The x-tick labels (timestamps) are rotated by 45 degrees to prevent overlap.
plt.tight_layout() is called to adjust the layout of the plot and avoid overlap of elements (like
labels and the plot).
Finally, [Link]() displays the plot.
Purpose:
The code creates a time series plot of two variables—TRUE_TEC and PREDICTED_TRUE_TEC
—across different dates. A specific date, December 15th, is highlighted to emphasize the
difference between the true and predicted values on that day, while the rest of the dates are
plotted with default colours. The plot allows for visual inspection of how closely the predicted
values match the true values over time, with a specific focus on the highlighted day.
Output: