improve your python code(10)

1. 深拷贝与浅拷贝

  • Python中对象的赋值都是进行对象引用(内存地址)传递
  • 使用copy.copy(),可以进行对象的浅拷贝,它复制了对象,但对于对象中的元素,依然使用原始的引用.
  • 如果需要复制一个容器对象,以及它里面的所有元素(包含元素的子元素),可以使用copy.deepcopy()进行深拷贝
  • 对于非容器类型(如数字、字符串、和其他’原子’类型的对象)没有被拷贝一说
  • 如果元祖变量只包含原子类型对象,则不能深拷贝。

2.使用Counter进行计数统计

#!/usr/bin/env python
# encoding: utf-8


"""
@python version: python3.6.1
@author: XiangguoSun
@contact: sunxiangguodut@qq.com
@site: http://blog.csdn.net/github_36326955
@software: PyCharm
@file: counter.py
@time: 5/7/2017 7:32 PM
"""
from collections import Counter

data = ['a', '2', 2, 4, 5, '2', 'b', 4, 7, 'a', 5, 'd', 'a', 'z']
print(Counter(data))
print(list(Counter(data).elements())) """Counter(data).elements()获取Counter中的key值,是一个chain的迭代器"""
print(Counter(data).most_common(2)) 
"""找出前n个出现频率最高的元素以及他们对应的次数"""
print(Counter(data)['y'])   
"""访问不存在的元素时,返回0,而不是KeyError异常"""
"""
output:
Counter({'a': 3, '2': 2, 4: 2, 5: 2, 2: 1, 'b': 1, 7: 1, 'd': 1, 'z': 1})
['a', 'a', 'a', '2', '2', 2, 4, 4, 5, 5, 'b', 7, 'd', 'z']
[('a', 3), ('2', 2)]
0
"""



"""
update()用于被统计对象元素的更新,原有Count计数器对象与新增元素的统计计数值相加。
subtract()用于实现计数器对象中统计值相减,允许有负值。
"""
c = Counter("success")
print(c)
c.update("successfully")
print(c)
c.subtract("successzz")
print(c)
"""
output:
Counter({'s': 3, 'c': 2, 'u': 1, 'e': 1})
Counter({'s': 6, 'c': 4, 'u': 3, 'e': 2, 'l': 2, 'f': 1, 'y': 1})
Counter({'s': 3, 'u': 2, 'c': 2, 'l': 2, 'e': 1, 'f': 1, 'y': 1, 'z': -2})
"""

a = Counter(a=1,b=2,c=3,d=1)
print(a)        # output Counter({'c': 3, 'b': 2, 'a': 1, 'd': 1})

3.

ARIMA (Autoregressive Integrated Moving Average) model is a popular time series forecasting model that combines autoregressive (AR), differencing (I), and moving average (MA) components. The AIC (Akaike Information Criterion) and BIC (Bayesian Information Criterion) are two common criteria used for model selection in ARIMA. AIC and BIC are both measures of the goodness-of-fit of a statistical model. They take into account both the model's performance and the complexity of the model. The lower the value of AIC or BIC, the better the model is considered to be. In ARIMA modeling, AIC and BIC can be used to compare different ARIMA models and select the one that provides the best fit to the data. These criteria help in avoiding overfitting by penalizing complex models that may not improve the forecasting performance significantly. To calculate AIC and BIC for an ARIMA model, you can use the following formulas: AIC = -2 * log-likelihood + 2 * p BIC = -2 * log-likelihood + log(n) * p where log-likelihood is the log-likelihood function value of the model, p is the number of parameters in the model, and n is the number of observations in the time series data. In Python, you can obtain AIC and BIC values for an ARIMA model using the `statsmodels` library. After fitting an ARIMA model to your data, you can use the `aic` and `bic` attributes of the fitted model to access these values. Here's an example code snippet: ```python import statsmodels.api as sm # Fit ARIMA model to your time series data model = sm.tsa.ARIMA(data, order=(p, d, q)) results = model.fit() # Get AIC and BIC values aic = results.aic bic = results.bic ``` Replace `data` with your actual time series data, and `p`, `d`, `q` with the order parameters of your ARIMA model. Remember, AIC and BIC are just two of many possible criteria for model selection. It's always recommended to consider other factors such as forecasting accuracy, interpretability, and practical applicability of the model in addition to AIC and BIC.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值