代码:
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
df=DataFrame({
"GDP": [12,23,34,45,56],
"air_temperature": [23,25,26,27,30],
"year":['2005','2003','2004','2005','2006']
})
df
结果:
GDP air_temperature year
0 12 23 2005
1 23 25 2003
2 34 26 2004
3 45 27 2005
4 56 30 2006
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
df=DataFrame({
"GDP": [12,23,34,45,56],
"air_temperature": [23,25,26,27,30],
"year":['2005','2003','2004','2005','2006']
})
print(u"相关系数:\n",df.corr())
结果:
相关系数:
GDP air_temperature
GDP 1.000000 0.977356
air_temperature 0.977356 1.000000
代码:
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
df=DataFrame({
"GDP": [12,23,34,45,56],
"air_temperature": [23,25,26,27,30],
"year":['2005','2003','2004','2005','2006']
})
print(u"GDP相关系数:",df['GDP'].corr(df['air_temperature']))
结果:
GDP相关系数: 0.9773555548504418
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
df=DataFrame({
"GDP": [12,23,34,45,56],
"air_temperature": [23,25,26,27,30],
"year":['2005','2003','2004','2005','2006']
})
print(u"协方差:\n",df.cov())
结果:
协方差:
GDP air_temperature
GDP 302.5 44.0
air_temperature 44.0 6.7
代码:
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
df=DataFrame({
"GDP": [12,23,34,45,56],
"air_temperature": [23,25,26,27,30],
"year":['2005','2003','2004','2005','2006']
})
print(u"GDP协方差:",df['GDP'].cov(df['air_temperature']))
结果:
GDP协方差: 44.0