First explain what is called data correlation:
Calculates the average of two arrays, or true if two values in two arrays are greater than or are less than the average. If a value greater than the average is less than the average, then false is obtained. The number of true and false is finally calculated.
For example, two arrays [1, 2, 3, 4] and [4, 5, 6, 7], get (4, 0). Since both 1 and 4 are below the average of their array, 2 and 5 are also lower than the average of their array, 3 and 6 are higher than the average of their array, and 4 and 7 are higher than the average of their array. So there are 4, false, 0, which evaluates to true.
If the array is [1, 2, 3, 4] and [7, 6, 5, 4], the result is (0, 4). Because 1 is lower than the average of the array he is in, and 7 is higher than the average of the array in which they are located, several others are similar ...
A chestnut:
For example, the correlation between the average life expectancy of 20 cities and the GDP of cities, the calculation of life and GDP, is that cities with higher life expectancy, higher GDP, lower life expectancy and lower GDP ... That is, GDP and longevity, whether related ... If the calculated true is more, that is the expression is related, if the resulting false more, it means irrelevant.
Countries = [' Albania ', ' Algeria ', ' Andorra ', ' Angola ', ' Antigua and Barbuda ', ' Argentina ', ' Armenia ', ' Australia ', ' Austria ', ' Azerbaijan ', ' Bahamas ', ' Bahrain ', ' Bangladesh ', ' Barbados ', ' Belarus ', ' Belgium ', ' Belize ', ' Benin ', ' Bhutan ', ' Bolivia ']life_expectancy_values = [74.7, ---83.4, 57.6, 74.6, 75.4, 72.3, 81.5, 80.2, 70.3, 72.1, 76.4, 68.1, 75.2, 69.8 , 79.4, 70.8, 62.7, 67.3, 70.6]gdp_values = [1681.61390973, 2155.48523109, 21495.80508273, 562.98768478, 13495.1274663, 9388.68852258, 1424.19056199, 24765.54890176, 27036.48733192, 1945.63754911, 21721.61840978, 13373.21993972, 483.97086804, 9783.98417323, 2253.46411147, 25034.66692293, 3680.91642923, 366.04496652, 1175.92638695, 1132.21387981]
Life_expectancy = PD. Series (life_expectancy_values)
GDP = PD. Series (gdp_values)
# Functions for calculating correlations
defvariable_correlation (Pd_1, pd_2): Pd_1_mean=Pd_1.mean () Pd_2_mean=Pd_2.mean ()
# are above average or are below average, get true, a high one low, get false
Result_series= ((Pd_1 > Pd_1_mean) &(Pd_2> Pd_2_mean)) |((Pd_1< Pd_1_mean) &(Pd_2<Pd_2_mean))
# Count the number of true num_same_direction=result_series.sum () # calculates the number of false
Num_different_direction= Len (result_series)-num_same_directionreturn(num_same_direction,num_different_direction)Print(Variable_correlation (LIFE_EXPECTANCY,GDP))
# Results
(17,3)
The description of life and GDP is correlated.
python-the correlation of calculated data