【问题标题】:Understanting what the syntax for {:.2} means in python [duplicate]了解 {:.2} 的语法在 python 中的含义 [重复]
【发布时间】:2021-04-04 00:51:09
【问题描述】:

我正在为特定数据集创建线性回归模型,我正在按照我在你的电子管上找到的示例进行操作,有时我会计算峰度和偏度,如下所示:

# calculate the excess kurtosis using the fisher method. The alternative is Pearson which 
calculates regular kurtosis.
exxon_kurtosis = kurtosis(price_data['exxon_price'], fisher = True)
oil_kurtosis = kurtosis(price_data['oil_price'], fisher = True)

# calculate the skewness
exxon_skew = skew(price_data['exxon_price'])
oil_skew = skew(price_data['oil_price'])

display("Exxon Excess Kurtosis: {:.2}".format(exxon_kurtosis))  # this looks fine
display("Oil Excess Kurtosis: {:.2}".format(oil_kurtosis))      # this looks fine

display("Exxon Skew: {:.2}".format(exxon_skew))          # moderately skewed
display("Oil Skew: {:.2}".format(oil_skew))              # moderately skewed, it's a 
little high but we will accept it.

我是python新手,下面的代码让我很困惑{:.2},请有人解释一下这部分{:.2}

display("Exxon Excess Kurtosis: {:.2}".format(exxon_kurtosis))

【问题讨论】:

标签: python string format


【解决方案1】:

kurtosisskew 函数正在执行计算,而 display 函数可能只是该环境中 print() 的某种形式!

".. {:.2}".format(x) 是一个字符串格式化程序,它将浮点数四舍五入为 2 个有效数字

>>> "{:.2}".format(3.0)
'3.0'
>>> "{:.2}".format(0.1555)
'0.16'
>>> "{:.2}".format(3.1555)
'3.2'

字符串格式详尽无遗detailed here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多