Grey relational analysis in Python
Learn how grey relational analysis works in Python, from data normalization to the grey relational grade, with pandas and NumPy steps you can follow on your own data.
Contents
- What grey relational analysis does
- When the method fits your decision
- Building the decision table in pandas
- Normalizing criteria with NumPy
- Choosing the reference series
- Computing the grey relational coefficient
- Averaging coefficients into the grade
- Reading the ranking with care
- Grey relational analysis in Python questions
- Can I run grey relational analysis in Python without special libraries?
- What resolution coefficient should I use in grey relational analysis?
- Does grey relational analysis need normalized data first?
- How do I add weights to the grey relational grade?
- How is grey relational analysis different from a weighted sum ranking?
- What happens when one criterion has the same value for all alternatives?
Grey relational analysis is a ranking method that compares options by how closely each one's normalized data resembles a reference series. In Python you do the work in a few steps: build a table, normalize the columns, compute the grey relational coefficient, then average the coefficients into a grey relational grade. The grades let you sort alternatives from closest to furthest from the reference.
- Normalize each criterion to a shared scale before comparing anything, because criteria expressed in different units cannot be compared directly.
- Decide the direction of every criterion first: larger values may be better, smaller values may be better, or a target value may be ideal.
- The grey relational coefficient measures how close one alternative sits to the reference series at each criterion.
- Averaging the coefficients across criteria gives the grey relational grade, and the grades produce the final ranking.
- Pandas handles the table layout and NumPy handles the arithmetic, which keeps the code short and readable.
What grey relational analysis does
Grey relational analysis ranks alternatives by the similarity of their data to a reference series. The method belongs to grey system theory, which deals with samples that are small or partly uncertain.
Each alternative becomes a series of values across the criteria. The reference series holds the ideal value on each criterion, and the method scores how close each alternative's series comes to that ideal.
The output is a grey relational grade per alternative, a single number you can sort. A higher grade means the alternative stays nearer the reference across the criteria you included.
When the method fits your decision
Grey relational analysis suits decisions with a modest set of alternatives and criteria where the data is known but not rich. It needs no probability distribution and no large sample, which separates it from statistical ranking tools.
The method works well when criteria carry different units and directions. Normalization brings them onto one scale, so a criterion measured in dollars can sit beside one measured in hours.
Grey relational analysis does not weigh criteria by itself. If some criteria matter more, you supply weights and apply them when you average the coefficients.
- Small samples with several criteria
- Mixed units and mixed directions
- Ranking rather than forecasting
- A need for a transparent calculation you can inspect
Building the decision table in pandas
Start with a pandas DataFrame where each row is an alternative and each column is a criterion. Keep the alternative names in the index so the ranking stays readable.
Check the table for missing values before any arithmetic. A missing cell breaks the normalization step, so fill it with a justified value or drop the alternative and say why.
Store the direction of each criterion in a small dictionary or a list beside the table. The direction tells the normalization step whether larger values are better or smaller values are better.
| Criterion | Direction | Example unit |
|---|---|---|
| Cost | Smaller is better | currency |
| Throughput | Larger is better | items per hour |
| Error rate | Smaller is better | ratio |
| Coverage | Larger is better | score |
Normalizing criteria with NumPy
Data normalization puts every criterion on a comparable scale, and the choice of formula follows the direction of the criterion. For a larger-is-better criterion, subtract the column minimum and divide by the column range.
For a smaller-is-better criterion, subtract the column value from the column maximum and divide by the range. A target-is-best criterion needs a different formula that measures distance from the target value.
NumPy arrays make these operations short: take column minimums and maximums, then apply the formula across the array. Guard against a zero range, which happens when every alternative scores the same on a criterion.
- Confirm each criterion's direction before choosing a formula
- Compute column minimum and maximum with NumPy
- Apply the larger-is-better or smaller-is-better formula
- Check for a zero range and handle that column separately
Choosing the reference series
The reference series holds the ideal normalized value for each criterion, which is the top of the scale after normalization. For larger-is-better criteria the reference value sits at the maximum, and for smaller-is-better criteria it sits at the minimum.
After normalization both cases often land on the same ideal value, which simplifies the code. Build the reference as an array with one entry per criterion and reuse it for every alternative.
An alternative that matches the reference on a criterion earns a perfect coefficient there. The reference series is a construction, so state in your write-up how you set it.
Computing the grey relational coefficient
The grey relational coefficient measures the distance between an alternative and the reference series at one criterion. The formula uses the absolute difference, a resolution coefficient, and the smallest and largest differences across the whole table.
The resolution coefficient, often written as rho, controls how sharply small differences are separated. A value near zero sharpens the distinction, while a value near one flattens it, so report the value you use.
NumPy computes the absolute difference matrix in one line, then the minimum and maximum of that matrix. Apply the coefficient formula element by element to get a matrix with one coefficient per alternative and criterion.
- Take the absolute difference between each value and the reference
- Find the global minimum and maximum of the difference matrix
- Choose and state the resolution coefficient
- Apply the coefficient formula across the matrix
Averaging coefficients into the grade
The grey relational grade is the average of an alternative's coefficients across the criteria. Compute it along the criterion axis so each alternative receives one grade.
When criteria carry different importance, use a weighted average instead of a plain one. The weights sum to one, and each coefficient is multiplied by its criterion's weight before the sum.
Sort the grades in descending order to produce the ranking. Keep the coefficient matrix beside the grade so a reader can see which criteria drove an alternative's position.
Reading the ranking with care
Grey relational analysis ranks alternatives relative to each other, so the grades describe positions within your table. Adding or removing an alternative can shift the grades and the order.
Test how sensitive the ranking is to the resolution coefficient and to the weights. A stable order across reasonable settings supports the result, while a shifting order tells you the decision is close.
The method scores similarity to a reference built from your own data. It does not prove that an alternative performs well in absolute terms, so treat the ranking as one input to the decision.
Grey relational analysis in Python questions
Can I run grey relational analysis in Python without special libraries?
Yes, grey relational analysis runs in plain Python with NumPy and pandas, which handle arrays and tables. The arithmetic is a small set of normalizations, differences and averages, so no dedicated grey-system package is required.
What resolution coefficient should I use in grey relational analysis?
The resolution coefficient in grey relational analysis is a setting you choose, often placed around the middle of its range. A smaller value sharpens the separation between close alternatives, and reporting your choice keeps the result reproducible.
Does grey relational analysis need normalized data first?
Yes, grey relational analysis requires data normalization because the criteria carry different units and directions. Min-max normalization based on each criterion's direction brings every column onto one scale before the coefficients are computed.
How do I add weights to the grey relational grade?
You add weights to the grey relational grade by replacing the simple average with a weighted average of the coefficients. Each coefficient is multiplied by its criterion's weight, and the weights sum to one.
How is grey relational analysis different from a weighted sum ranking?
Grey relational analysis scores each alternative against a reference series built from the ideal values, while a weighted sum adds raw or normalized scores directly. The grey relational coefficient adds a comparison of relative differences across the whole table.
What happens when one criterion has the same value for all alternatives?
A criterion with the same value for all alternatives normalizes to a zero range in grey relational analysis. That column adds no information for ranking, so you can drop it or handle it with a separate rule and explain your choice.