Dtreeviz NameError - python

I installed dtreebiz and wanted to make a three plot. I did exactly what example codes say as below, but keep getting "NameError: name 'dtreeviz' is not defined".
from dtreeviz.trees import *
viz = dtreeviz(lgbm,
x_data = df_X_train,
y_data = df_y_train,
target_name = TARGET,
feature_names = df_X_train.columns.tolist(),
tree_index = 0)
viz
I checked the version using pip show dtreeviz and confirmed version 2.1.3 is installed. I am using Spyder mostly but I also tried the same thing with Jupyter Notebook and got the same error.

You need to add
import dtreeviz
to your code (see the Quick Start in dtreeviz github page). Importing all the symbols from dtreeviz.trees, like you're doing with
from dtreeviz.trees import *
imports the symbols in the trees module only.

There was some compatibility issues from version 1 to 2.
I would suggest to start using the new 2.0 API :)
For the old API, you could try:
from dtreeviz import *
viz = dtreeviz(lgbm,
x_data = df_X_train,
y_data = df_y_train,
target_name = TARGET,
feature_names = df_X_train.columns.tolist(),
tree_index = 0)

Related

Shap value plotting error on Databricks but works locally

I want to do a simple shap analysis and plot a shap.force_plot. I noticed that it works without any issues locally in a .ipynb file, but fails on Databricks with the following error message:
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another user you must
also trust this notebook (File -> Trust notebook). If you are viewing this notebook on
github the Javascript has been stripped for security. If you are using JupyterLab this
error is because a JupyterLab extension has not yet been written.
Code:
import xgboost
import shap
shap.initjs()
X, y = shap.datasets.boston()
bst = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)
explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(X)
shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])
Is there any way to get the image plotting work on Databricks?
Let's try slightly different (matplotlib=True):
import xgboost
import shap
X, y = shap.datasets.boston()
bst = xgboost.train({"learning_rate": 0.01}, xgboost.DMatrix(X, label=y), 100)
explainer = shap.TreeExplainer(bst)
shap_values = explainer.shap_values(X)
shap.force_plot(
explainer.expected_value,
shap_values[0,:],
X.iloc[0,:],
matplotlib=True # <--
)

matplotlib-SciencePlots package cannot run

i install SciencePlots packsge by pip install SciencePlots, and i have the following codes:
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('science')
def model(x, p):
return x ** (2 * p + 1) / (1 + x ** (2 * p))
x = np.linspace(0.75, 1.25, 201)
fig, ax = plt.subplots()
for p in [10, 15, 20, 30, 50, 100]:
ax.plot(x, model(x, p), label=p)
ax.legend(title='Order')
ax.set(xlabel='Voltage (mV)')
ax.set(ylabel='Current ($\mu$A)')
ax.autoscale(tight=True)
fig.savefig('fig1.jpg', dpi=300)
but it results in an error,the error says:
OSError: 'science' not found in the style library and input is not a valid URL or path; see `style.available` for list of available styles
python version: 3.8.8
SciencePlots version: 1.0.1
matplotlib version: 3.3.4
numpy version: 1.19.2
i use Anaconda and Jupyter Notebook for python coding
Can anybody help to fix it? Thanks a lot!
The function expects an iterable.
plt.style.use(['science'])
This allows you to overlap multiple styles
plt.style.use(['science', 'notebook])
The style does not exist by default. Instead it has to be installed here: https://github.com/garrettj403/SciencePlots
Otherwise if you check:
import matplotlib.pyplot as plt
import numpy as np
#plt.style.use(['science', 'notebook'])
plt.style.available
no such style even thought the example did have them:
['Solarize_Light2',
'_classic_test_patch',
'_mpl-gallery',
...
There is no such style.
BTW the example did show you can use that science style if just one and if multiple then you have to use []
I had been facing the same trouble as you. I managed to work it out by installing scienceplots package from the Github repo SciencePlots by typing
pip install SciencePlots (For the latest release) or pip install git+https://github.com/garrettj403/SciencePlots (for the latest commit) into a Linux Terminal.
Since then I have been able to import 'science' style successfully.
Although in the Github repo it is recommended to import scienceplots into the notebook, but I actually tested my notebook without it and it worked just fine.
EDIT: Nonetheless, the command plt.style.available doesn't show science as an available style and I only get
['Solarize_Light2',
'_classic_test_patch',
'_mpl-gallery',
'_mpl-gallery-nogrid',
'bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn-v0_8',
'seaborn-v0_8-bright',
'seaborn-v0_8-colorblind',
'seaborn-v0_8-dark',
'seaborn-v0_8-dark-palette',
'seaborn-v0_8-darkgrid',
'seaborn-v0_8-deep',
'seaborn-v0_8-muted',
'seaborn-v0_8-notebook',
'seaborn-v0_8-paper',
'seaborn-v0_8-pastel',
'seaborn-v0_8-poster',
'seaborn-v0_8-talk',
'seaborn-v0_8-ticks',
'seaborn-v0_8-white',
'seaborn-v0_8-whitegrid',
'tableau-colorblind10']
as available styles.

'NearMiss' object has no attribute '_validate_data'

Detailed Image
This is the code below which shows the error.
from imblearn.under_sampling import NearMiss
nm = NearMiss()
X_res,y_res=nm.fit_sample(X,Y)
You are probably trying to under sample your imbalanced dataset. For this purpose, you can use RandomUnderSampler instead of NearMiss.
Try the following code:
from imblearn.under_sampling import RandomUnderSampler
under_sampler = RandomUnderSampler()
X_res, y_res = under_sampler.fit_resample(X, y)
Now, your dataset is balanced. You can verify it using y_res.value_counts().
Cheers!
Instead of "imblearn" package my conda installed a package named "imbalanced-learn" that's why it does not take the data. But it is strange that the jupyter notebook doesn't tell me that "imblearn" isn't installed.

Error ImportError: No module named 'np_plots'

I have the next code in python
import np_plots as npp
import matplotlib.pyplot as plt
import numpy as np
import math as m
import scipy
from scipy.integrate import odeint
def plotLimitCycle(bval):
rhs = lambda X, t: [-X[0]+X[1]*X[0]**2, bval - X[1]*X[0]**2]
xeq, yeq = bval, 1.0/bval
cyclerad = m.sqrt(1-bval)
nbh = min(cyclerad, 0.05)
IC = [xeq-nbh/5.0, yeq-nbh/5.0]
time_span = np.linspace(0,400,40000)
fig = plt.figure()
solution = odeint(rhs, IC, time_span)
X, Y = zip(*solution)
plt.plot(X, Y)
axes = plt.gca()
axXmin, axXmax = axes.get_xlim()
axYmin, axYmax = axes.get_ylim()
xmin = max(-15, axXmin)
xmax = min(15, axXmax)
ymin = max(-15, axYmin)
ymax = min(15, axYmax)
X,Y,U,V = npp.ezDomainQuiver2D([[xmin, xmax],[ymin, ymax]],[25,25],lambda X: rhs(X, 0),Normalize=True)
plt.quiver(X,Y,U,V)
plt.scatter([xeq],[yeq], color='red')
plt.xlim([xmin, xmax])
plt.ylim([ymin, ymax])
plt.axes().set_aspect('equal', 'datalim')
plt.show()
It work pretty well on my friend computer because he showed me the plots but I can't make it run in mine, I'm using Python 3.5.0cr1 Shell to run it out but it always came with te next error:
**Traceback (most recent call last):
File "C:\Users\PankePünke\Desktop\limites.py", line 1, in <module>
import np_plots as npp
ImportError: No module named 'np_plots'**
I'm totally new in Python programming and my friend made this program for me in order to make some advances in my thesis but I want to continue working with this program and get the plots from this. I do not know how to install or what kind of procceddure I should follow in order to get what I want (the plots and graphics that this program make) So... I'll be very thankful if somebody can help me in not a advance way, because how a wrote I'm totally new in Python, I just installed it and that is all.
You friend had a lib called np_plots on their computer, it is not part of the standard lib so you need to also install/get it on your comp or the code will not run. Most likely your friend actually wrote the code as I cannot see any mention of that lib anywhere so you will have to get it from them.
Apart from your friends lib, scipy and numpy are also not in the standard library, they do come with some distributions like Canopy but if you just installed a regular version of python you will need to install those also.
Might be worth checking out pip as it is the de-facto standard package manager for python.

using rpy2 with IPython notebooks?

Is it possible to use rpy2 (calling ggplot2) with IPython notebooks, and then save them (and share on NBViewer like other notebooks http://nbviewer.ipython.org/)? Is there any challenge in having the rpy2 ggplots appear in the notebook and/or interactively? It would be helpful if someone could provide an example session and its output of making a ggplot2 figure within a notebook using rpy2 in IPython.
This was written without looking the code in rmagic.
They have have a more clever way to do it (I have 11 lines of code).
import uuid
from rpy2.robjects.packages import importr
from IPython.core.display import Image
grdevices = importr('grDevices')
def ggplot_notebook(gg, width = 800, height = 600):
fn = '{uuid}.png'.format(uuid = uuid.uuid4())
grdevices.png(fn, width = width, height = height)
gg.plot()
grdevices.dev_off()
return Image(filename=fn)
To try it:
from rpy2.robjects.lib import ggplot2
from rpy2.robjects import Formula
datasets = importr('datasets')
mtcars = datasets.__rdata__.fetch('mtcars')['mtcars']
p = ggplot2.ggplot(mtcars) + \
ggplot2.aes_string(x='mpg', y='cyl') + \
ggplot2.geom_point() + \
ggplot2.geom_smooth() + \
ggplot2.facet_wrap(Formula('~ am'))
ggplot_notebook(p, height=300)
It's possible with the rmagic extension, which uses rpy2. You seem to need to print() the figure to show it, though. Here's an example session: http://nbviewer.ipython.org/5029692
If you prefer to use rpy2 directly, it must be possible. Have a look at the rpy2 documentation for ggplot2. To get it into the notebook, you can draw to a PNG/SVG device, then read it from the Python side (this is what rmagic does).

Categories