AttributeError: 'EfficientFrontier' object has no attribute 'efficient_frontier'
时间: 2023-06-22 12:25:57 浏览: 173
对不起,我的回答有误。最新版的PyPortfolioOpt中已经将`efficient_frontier()`方法重命名为`efficient_frontier()`,如果你使用的是较旧的版本,可能会出现此错误。以下是绘制有效前沿图的正确代码示例:
```
import numpy as np
import matplotlib.pyplot as plt
# 计算有效前沿
ef = EfficientFrontier(mu3, S3)
raw_frontier = ef.efficient_frontier()
cleaned_frontier = ef.clean_frontier()
# 绘制有效前沿图
fig, ax = plt.subplots()
ax.plot(cleaned_frontier[0], cleaned_frontier[1], label='Efficient Frontier')
ax.plot(np.sqrt(np.diagonal(S3)), mu3, 'o', markersize=5, label='Stocks')
ax.set_title('Efficient Frontier')
ax.set_xlabel('Volatility')
ax.set_ylabel('Expected Return')
ax.legend()
plt.show()
```
此代码将有效前沿和各个资产的预期收益率和波动率作为散点图进行可视化,并标注有效前沿的位置。可以通过修改mu3和S3变量中的值来适应不同的投资组合。
相关问题
AttributeError: Trainer object has no attribute loss_items
AttributeError: 'Trainer' object has no attribute 'loss_items'是由于Trainer对象中没有名为loss_items的属性而导致的错误。要解决这个问题,需要检你的代码,确保在Trainer类中定义了loss_items属性或者在使用该属性之前进行了正确的初始化。如果你已经定义了loss_items属性,但仍然出现该错误,可能是因为你没有正确地引用该属性。请检查你的代码,并确保正确地使用了loss_items属性。
AttributeError: 'Basemap' object has no attribute 'set_extent'AttributeError: 'Basemap' object has no attribute 'set_extent'
这个错误通常是由于使用了过时的Basemap库导致的。建议使用Cartopy库来代替Basemap库,因为Cartopy库已经成为了Basemap库的替代品,并且具有更好的性能和更多的功能。在Cartopy库中,可以使用set_extent()方法来设置地图的范围。
阅读全文
相关推荐















