How to install mpl_finance in python 3.6 [duplicate] - python-3.x

I am trying to import matplotlib.finance module in python so that I can make a Candlestick OCHL graph. My matplotlib.pyplot version is 2.00. I've tried to import it using the following commands:
import matplotlib.finance
from matplotlib.finance import candlestick_ohlc
I get this error:
warnings.warn(message, mplDeprecation, stacklevel=1)
MatplotlibDeprecationWarning: The finance module has been deprecated in mpl 2.0 and will be removed in mpl 2.2. Please use the module mpl_finance instead.
Then instead of using the above lines in python I tried using the following line:
import mpl_finance
I get this error:
ImportError: No module named 'mpl_finance'
What should I do to import candlestick from matplotlib.pyplot?

I've stopped using mpl_finance (and plotly) since they are too slow. Instead I've written an optimized finance plotting library, finplot, which I use to backtest up to 107 candles.
Here's a small example:
import yfinance as yf
import finplot as fplt
df = yf.download('SPY',start='2018-01-01', end = '2020-04-29')
fplt.candlestick_ochl(df[['Open','Close','High','Low']])
fplt.plot(df.Close.rolling(50).mean())
fplt.plot(df.Close.rolling(200).mean())
fplt.show()
Examples included show SMA, EMA, Bollinger bands, Accumulation/Distribution, Heikin Ashi, on balance volume, RSI, TD sequential, MACD, scatter plot indicators, heat maps, histograms, real-time updating charts and interactive measurements; all with sensible defaults ready for use.
I do dogfooding every day, drop me a note or a pull request if there is something you want. Hope you take it for a spin!

In 2020, one can now pip install mplfinance

What this warning tells you is that the finance module will be removed at some point.
At the moment you don't need to worry about this warning. It will only affect you when you update to a yet to be released version 2.2 of matplotlib, in which case you'll need to change your imports.
If you already want to be compatible with future versions now, you can download the mpl_finance module from
https://github.com/matplotlib/mpl_finance .
After having downloaded the files, you may install in the usual way,
python setup.py install
Alternatively you may try installing through pip,
pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
The reason for this is that the people at matplotlib want to keep their code clean and not maintain a specialized sidepackage like this in the main code. They probably also do not want to maintain the package and spend resources on it, which can be better used in the core development.

Since mpl_finace is not on pip now, you may also want to use following command to install mpl_finance by pip:
pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

mpl_finance is no longer part of matplotlib. Install the module directly from gitHub via pip
pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
and import it with
from mpl_finance import candlestick_ohlc
Then it works the same as before.

There is a new version of matplotlib finance, with documentation, here:
https://pypi.org/project/mplfinance/
https://github.com/matplotlib/mplfinance
Install with:   pip install --upgrade mplfinance
Or with:   conda install -c conda-forge mplfinance
NOTE: The package name no longer has the dash or underscore:
It is now mplfinance (not mpl-finance, nor mpl_finance)

I'm working on google colab , i got the same problem . then what i did -for python3.6
import mpl_finance
from mpl_finance import candlestick_ohlc

Plotly.py, a web-browser based, interactive plotting module has finance plotting functions https://plot.ly/python/candlestick-charts/. And it is maintained.

Simply use pip install mpl_finance for Windows or pip3 install mpl_finance for Linux/Unix for installation.
Then use from mpl_finance import candlestick_ohlc to call the library in the Jupyter notebook!

Replace from matplotlib.finance import candlestick_ohlc with from mplfinance.original_flavor import candlestick_ohlc , That should work.

Related

Pandas-profiling error AttributeError: 'DataFrame' object has no attribute 'profile_report'

I wanted to use pandas-profiling to do some eda on a dataset but I'm getting an error : AttributeError: 'DataFrame' object has no attribute 'profile_report'
I have created a python script on spyder with the following code :
import pandas as pd
import pandas_profiling
data_abc = pd.read_csv('abc.csv')
profile = data_abc.profile_report(title='Pandas Profiling Report')
profile.to_file(output_file="abc_pandas_profiling.html")
AttributeError: 'DataFrame' object has no attribute 'profile_report'
The df.profile_report() entry point is available from v2.0.0. soln from here
Did you install pandas-profiling via pip or conda?
use : pip install -U pandas-profiling to solve this and restart your kernel
The issue is that the team hasn't updated the pip or conda installations yet (described here). If you installed using one of these, try this for the time being.
profile = pandas_profiling.ProfileReport(df)
print(profile)
This should work for those who want to use the latest version:
Run pip uninstall pandas_profiling from anaconda prompt (given you're using Spyder, I'd guess this would be your case) / or command prompt
Run pip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip
If you're using something like a Jupyter Notebook/Jupyter Lab, be sure to restart your kernel and re-import your packages.
I hope this helps.
For the those using google colabs, the profiling library is outdated, hence use the command below and restart the runtime
! pip install https://github.com/pandas-profiling/pandas-profiling/archive/master.zip
The only workaround I found was that the python script I made is getting executed from the command prompt and giving the correct output but the code is still giving an error in Spyder.
Some of the version of the pandas-profiling does not work for me and I installed 2.8.0 version and it work for me.
!pip install pandas-profiling==2.8.0
import numpy as np
import pandas as pd
import pandas_profiling as pp
df = pd.read_csv('/content/sample_data/california_housing_train.csv')
profile = df.profile_report(title = "Data Profiling Report")
profile.to_file("ProfileReportTest.html")
If none of the above worked, can you check by setting the encoding to unicode_escape in read_csv? It may be due to one of your columns
encoding = 'unicode_escape'
My solution
For me installation via pip was giving errors, therefore I installed it via conda from here.
Code Example
And here is the code example to use profile report:
import pandas as pd
from pandas_profiling import ProfileReport
data_abc = pd.read_csv('abc.csv')
profile = ProfileReport(data_abc, minimal=True)
profile.to_file("abc_pandas_profiling.html")
To read the html file I used the following code
df = pd.read_html("abc_pandas_profiling.html")
print(df[0])
Try in conda environment
!pip install --user pandas-profiling
import pandas_profiling
data.profile_report()

Cannot import Sklearn from sklearn.externals.joblib

I am a beginner and I just started with machine learning. I am trying to import classes like imputer from sklearn but i am unable to do it.
from sklearn.preprocessing import Imputer,LabelEncoder,OneHotEncoder,StandardScaler
ImportError: cannot import name 'version' from
'sklearn.externals.joblib'
(C:\ProgramData\Anaconda3\lib\site-packages\sklearn\externals\joblib__init__.py)
I had the same problem.
I have replaced
from sklearn.externals import joblib
with
import joblib
and it works fine in Python 3.7.2
I believe there was an update on Scikit-learn that render that import unusable.
I had my local installation to be version 0.20.3 and this import is pefectly working. But on my server I have installation 0.23.1 and this error pop up. Something must be chaging in the new version.
For my case, use import joblib fix the problem. In your case it seems more complicated. This sounds very much likeky to be caused if you have more than one Scikit-learn version installed on your system. You need to uninstall all of them and do a clean install of sklearn.
The problem sometimes happens due to the version. This may help:
If you has written like this
from sklearn.externals import joblib
Modify it as this:
import joblib
Try
python -m pip install sklearn --upgrade
and
python -m pip install joblib --upgrade
and then, use this :
import joblib
Good luck.
import joblib
This works for me. Actually I was having that kinda challenge

AttributeError: module 'networkx.algorithms.community' has no attribute 'best_partition'

well i am trying to use community detection algorithms by networkx on famous facebook snap data set.
here are my codes :
import networkx as nx
import matplotlib.pyplot as plt
from networkx.algorithms import community
from networkx.algorithms.community.centrality import girvan_newman
G_fb = nx.read_edgelist("./facebook_combined.txt",create_using = nx.Graph(), nodetype=int)
parts = community.best_partition(G_fb)
values = [parts.get(node) for node in G_fb.nodes()]
but when i'm run the cell i face with the title error which is :
AttributeError: module 'networkx.algorithms.community' has no attribute 'best_partition'
any advice ?
I think you're confusing the community module in networkx proper with the community detection in the python-louvain module which uses networkx.
If you install python-louvain, the example in its docs works for me, and generates images like
Note that you'll be importing community, not networkx.algorithms.community. That is,
import community
[.. code ..]
partition = community.best_partition(G_fb)
I faced this in CS224W
AttributeError: module 'community' has no attribute 'best_partition'
Pls change this file karate.py
replace import to
import community.community_louvain as community_louvain
then it works for me.
I had the same problem. In my case, it was solved importing the module in a different manner:
import community.community_louvain
Source
I also faced this in CS224W
but changing the karate.py or other solutions didn't work.
For me (in colab) using the new PyG installation code worked.
this code, will install the last version:
!pip install -q torch-scatter -f https://pytorch-geometric.com/whl/torch-1.9.0+cu102.html
!pip install -q torch-sparse -f https://pytorch-geometric.com/whl/torch-1.9.0+cu102.html
!pip install -q git+https://github.com/rusty1s/pytorch_geometric.git
I had a similar issue.
In my case, it was because on the other machine the library networkx was obsolete.
With the following command, the issues was solved.
pip3 install --upgrade networkx
I naively thought that pip install community was the package I was looking for but rather I needed pip install python-louvain which is then imported as import community.
This has helped me to run the code without errors:
pip uninstall community
import community.community_louvain as cl
partition = cl.best_partition(G_fb)

Unable to import sklearn and statsmodels from Anaconda from windows 10 pro

I'm relatively new to python, so please excuse my ignorance on what could be a very easy fix. I am running python 3.6 through the Rodeo IDE, and it has been great, as it is similar to R-Studio (which I am very familiar with). As an aspiring data scientist, I am trying to learn how to fit regression and time series models to data, and all of the tutorials that I have found all say that I need various packages, all of which should be included in the Anaconda library. After downloading and re-downloading Python, Rodeo, and Anaconda, and trying various online fixes, I have been unable to successfully load the scikit-learn and the statsmodels modules.
#here is everything I have tried.
#using pip
! pip install 'statsmodels'
! pip install 'scikit-learn'
! pip install 'sklearn'
I don't get any errors here, and to be honest I'm kind of confused as to what this actually does, but I have seen many people online always suggest that this is a big problem when trying to import modules.
#using import
import sklearn
import statsmodels
from sklearn import datasets
import statsmodels.api as sm
all of the above give me the same error:
import statsmodels.api as sm
ImportError: No module named 'statsmodels'
ImportError: Traceback (most recent call last)
ipython-input-184-6030a6549dc0 in module()
----> 1 import statsmodels.api as sm
ImportError: No module named 'statsmodels'
I have tried to set my working directory to the Anaconda 3 file that has all of the packages and rerunning the above code with no success.
I'm thinking that the most likely problem has to do with my inexperience, and it is probably a simple fix. Is it possible that the IDE is bad or anaconda just doesn't like me?
So keeping all of the above in mind, the question is, how can I import these modules successfully so that I can access their functionality?
Option 1:
After installing packages with pip, try closing and reopening your IDE/Jupyter Notebook and try again.
This is a known bug that Jake VanderPlas outlined here
Option 2:
Don't put quotations around your pip messages.
!pip install -U statsmodels
!pip install scikit-learn
Option 3:
Also are you using Anaconda? If you are, you should already have scikit-learn. If you are trying inside Rodeo, I think you need to set your path inside Rodeo. Open Rodeo and set the Python Path to your fresh anaconda. See here

Since matplotlib.finance has been deprecated, how can I use the new mpl_finance module?

I am trying to import matplotlib.finance module in python so that I can make a Candlestick OCHL graph. My matplotlib.pyplot version is 2.00. I've tried to import it using the following commands:
import matplotlib.finance
from matplotlib.finance import candlestick_ohlc
I get this error:
warnings.warn(message, mplDeprecation, stacklevel=1)
MatplotlibDeprecationWarning: The finance module has been deprecated in mpl 2.0 and will be removed in mpl 2.2. Please use the module mpl_finance instead.
Then instead of using the above lines in python I tried using the following line:
import mpl_finance
I get this error:
ImportError: No module named 'mpl_finance'
What should I do to import candlestick from matplotlib.pyplot?
I've stopped using mpl_finance (and plotly) since they are too slow. Instead I've written an optimized finance plotting library, finplot, which I use to backtest up to 107 candles.
Here's a small example:
import yfinance as yf
import finplot as fplt
df = yf.download('SPY',start='2018-01-01', end = '2020-04-29')
fplt.candlestick_ochl(df[['Open','Close','High','Low']])
fplt.plot(df.Close.rolling(50).mean())
fplt.plot(df.Close.rolling(200).mean())
fplt.show()
Examples included show SMA, EMA, Bollinger bands, Accumulation/Distribution, Heikin Ashi, on balance volume, RSI, TD sequential, MACD, scatter plot indicators, heat maps, histograms, real-time updating charts and interactive measurements; all with sensible defaults ready for use.
I do dogfooding every day, drop me a note or a pull request if there is something you want. Hope you take it for a spin!
In 2020, one can now pip install mplfinance
What this warning tells you is that the finance module will be removed at some point.
At the moment you don't need to worry about this warning. It will only affect you when you update to a yet to be released version 2.2 of matplotlib, in which case you'll need to change your imports.
If you already want to be compatible with future versions now, you can download the mpl_finance module from
https://github.com/matplotlib/mpl_finance .
After having downloaded the files, you may install in the usual way,
python setup.py install
Alternatively you may try installing through pip,
pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
The reason for this is that the people at matplotlib want to keep their code clean and not maintain a specialized sidepackage like this in the main code. They probably also do not want to maintain the package and spend resources on it, which can be better used in the core development.
Since mpl_finace is not on pip now, you may also want to use following command to install mpl_finance by pip:
pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
mpl_finance is no longer part of matplotlib. Install the module directly from gitHub via pip
pip install https://github.com/matplotlib/mpl_finance/archive/master.zip
and import it with
from mpl_finance import candlestick_ohlc
Then it works the same as before.
There is a new version of matplotlib finance, with documentation, here:
https://pypi.org/project/mplfinance/
https://github.com/matplotlib/mplfinance
Install with:   pip install --upgrade mplfinance
Or with:   conda install -c conda-forge mplfinance
NOTE: The package name no longer has the dash or underscore:
It is now mplfinance (not mpl-finance, nor mpl_finance)
I'm working on google colab , i got the same problem . then what i did -for python3.6
import mpl_finance
from mpl_finance import candlestick_ohlc
Plotly.py, a web-browser based, interactive plotting module has finance plotting functions https://plot.ly/python/candlestick-charts/. And it is maintained.
Simply use pip install mpl_finance for Windows or pip3 install mpl_finance for Linux/Unix for installation.
Then use from mpl_finance import candlestick_ohlc to call the library in the Jupyter notebook!
Replace from matplotlib.finance import candlestick_ohlc with from mplfinance.original_flavor import candlestick_ohlc , That should work.

Resources