Skip to main content

Prediction

This is part 2 of my data parsing and prediction example and assumes you successfully gathered data and saved it locally, as explained in Part 1. Once we have all the data from our solve.out files (Nodes, Elements, DOF and time), we can use that data set and some python AI/ML (Artificial Intelligence/Machine Learning) tools to create a method to predict the time based on our own inputs. This means once we have a tool built to do the prediction, we can use our analysis preliminary data to predict the time to solve. First we need to get a few packages for our python virtual environment installation.

Step 0: Python venv activation

If you are restarting, you can just activate the virtual environment (bob) you created in Part 1.
There is a short list of required packages for the prediction part of the example. So either add or create a new requirements.txt file in the C:\bob file and with these lines:
These will be imported with the pip command:
It will take a few seconds to import the packages.

Step 1: Importing the data, cleaning and sorting

We will again use pandas to import the json data file we created in part 1. Note there is data in the file that did not appear to be needed in this iteration of the predictor tool, so we delete a few columns that are not needed. Memory and version were not used in this analysis.
Data preparation is a constant challenge for data analysis. In this case, we have some holes in our data. Some of the node and element count came in as zero. My solution was to determine which data sets have no data and remove them. Then I determined which data set has the most data (nodes, elements, dof) has the use that as a key feature, and remove the others.
My quick analysis showed that DOF are not consistent (missing data). So I delete it and remove zero data sets from the pandas dataframe.

Step 2: Setup the data for using scikit-learn

The scikit ML tools are set up to take a set of data and divide it into two categories, testing and training data. The training data should be the majority, commonly 70% is used. The testing data is then used to test the accuracy of the predictions from the training data. For more information about scikit-learn and what it does, and you will need more information to understand it: scikit-learn. The standard procedure is to break out the data you are hoping to predict to a y parameter and remove it from the data set. Then train_test_split is used to build the predictive model (but not launch it).
Elements to time plot

Step 3: Baseline Regression model fitting (Prediction)

The prediction model has hyperparameters that allow the users to tune the fitting to the data. The extensive list of scikit hyperparameters can be found at: hyperparameters. Here I use some baseline values as suggested in another online training. Later we can refine them. ensemble.GradientBoostingRegressor is the command that will run the data fitting.
The error comparison allows you to see the quality of the data fitting.
The goal is to have both a low error in both the training and test data. If the values are wildly off, the data is either over or underfit. Naturally there are ways to correct these issues unfit.

Step 4: Refine the Regression model fitting (parameter optimization)

However, being short of time, I decided to use the GridSearchCV capabilities of the prediction tool to allow a best fit to be determine from ranges of the hyperparameters. Computers are there to do these tasks for us. To set up some ranges I just used 1/2 and 2 times the original values. Note, this will take time depending on how much data you have. Check the documentation for the ranges of the hyperparameters. Using more CPUs will speed up the process. The parameter search is run only on the training data. Here a timer is inserted to see how long it takes. Note: once you launch this you might want to go get a coffee. It takes a bit of time.
We can save the best set to file if we import the joblib package.
We can later import just the ansys_data_best_parameters pkl file to do the prediction (coming up).

Step 5: Important! as in, what is?

Not all inputs or features have the same importance. We can use scikit to determine the importance of the different features. The higher the value, the more relevant that value is to the prediction, here, time. We could actually remove low value features with very little loss of prediction accuracy.
The refinement did not improve the prediction much. And we see, some of the features or inputs have very little effect on the prediction. If we want to further refine the prediction, some of the inputs can be dropped.

Step 6: Predict using your best values

Let’s use the best values from our grid search, that we saved, by reimporting the pkl.
To run a prediction, we need to input our values or features in the exact same order as we did when we set up the training data set. Note the prediction wants lists of lists, so package appropriately. Any Ansys run of respectable size should give you the required data you need to run a prediction. For comparison I use a local run of a box model with 1e6 elements and 8 cores. Ansys Mechanical results Current solve time results
The results are within reason:

Summation

And that is how easy it could be. The python modules (and more thorough trainings!) are there for users to learn and use. Use the links in the text to find out more about the respective packages. This example demonstrated that users can gather their own solve data, which is always the most representative, and use scikit learn to create a (relatively) accurate prediction tool for the time to solve. This could be set up on local Ansys installations as a Mechanical ACT button using the pkl to provide users a current model prediction! Special Thanx to JD who, in spite of his best efforts, is super helpful in all things python and bringing form and order to all things scripting. The author, Mark Capellaro, is an Ansys Application Engineer and data enthusiast and always looking for interesting ways to turn data into information. The full code can be viewed on GitHub here. If you missed the first part of this series you can read it here: Using Machine Learning to predict analysis solve times from local data: Part 1