How to run a specific sklearn version? - scikit-learn

On my mac, I installed multiple versions of Sklearn as shown below:
Sklearn 0.19.1
~/anaconda2/pkgs/scikit-learn-0.19.1-py27h9788993_0/lib/python2.7/site-packages/sklearn
Sklearn 0.20.0
~/anaconda2/pkgs/scikit-learn-0.20.0-py27h4f467ca_1/lib/python2.7/site-packages/sklearn
When starting jupyter, it automatically runs the sklearn 0.20.0. I was wondering whether there is a way to run sklearn 0.19.1.
Thanks a lot,
Jeff

This should work, am not saying it is elegant but is what I would personally try first. sys.path is a list of all the places where it goes to import modules, so you first remove any occurrences of the one you don't want and then put in the one you do want.
In a cell before you import from sklearn:
import sys
syspath = sys.path
indexes = [i for i, s in enumerate(syspath) if 'scikit-learn-0.20.0-py27h4f467ca_1' in s]
for index in indexes:
syspath.pop(index)
sys.path.insert(0, '~/anaconda2/pkgs/scikit-learn-0.19.1-py27h9788993_0/lib/python2.7/site-packages/sklearn')
# now if you import from sklearn, should come from 19

Related

Unable to import 'MinMaxScaler' from sklearn.preprocessing

I tried all possible ways and checked everywhere, but I couldn't find the ideal solution.
Uninstalled sklearn, scikit-learn
Installed sklearn 0.23
Upgraded sklearn
but I still didn't succeed.
ImportError: cannot import name 'MinMaxScalar' from 'sklearn.preprocessing' (C:\Users\MY PC\anaconda3\lib\site-packages\sklearn\preprocessing\__init__.py)
You should change the letter A to an E, so MinMaxScalar changes to MinMaxScaler.
Change the last letter a to e in class name
from sklearn.preprocessing import MinMaxScaler

Cannot import bokeh built in themes from bokeh.themes

I am trying to set the theme for bokeh plots on jupyter lab.
To do so I am following the official docs, copying the exact code from https://docs.bokeh.org/en/latest/docs/reference/themes.html.
However, I get an error cannot import name 'built_in_themes'.
I assume the default themes location changed, but can't seem to find anything else besides the said page documenting bokeh.themes.
I assume the default themes location changed
No, it has only ever been in the same location since it was added in version 1.0. The explanation is almost certainly that the version you are using is older than that.
As bigreddot said, same location.
Check your Bokeh version:
$ python3.7
>>> import sys
>>> print(sys.version)
>>> import bokeh
>>> print(bokeh.__version__)
Or via jupyter notebook:
import numpy as np
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
Following the example here provided, the output should look like this:
Maybe you are just working in an environment with an older version.

How can I solve cannot import name 'fetch_openml' from 'sklearn.datasets'

I'm learning sklearn, but I can't use fetch_openml(). It says,
ImportError: cannot import name 'fetch_openml' from 'sklearn.datasets'
In the new version of sklearn, it's even easier to fetch open ML Datasets. For example, you can add import and fetch mnist dataset as:
from sklearn.datasets import fetch_openml
X, y = fetch_openml('mnist_784', version=1, return_X_y=True, as_frame=False)
print(X.shape, y.shape)
For more details check official example.
You can use this:
from sklearn.datasets import fetch_openml
Apparently, fetch_mldata has been deprecated in the newer sklearn. Use load_digits to achieve loading the MNIST data.
To solve this problem in jupyter follow these steps:
Download file mnist-original from " https://osf.io/jda6s/"
after download file copy it into C:\Users\YOURUSERNAME\scikit_learn_data\mldata
in notebook jupyter do:
from sklearn.datasets import fetch_mldata
mnist = fetch_mldata('mnist-original')

No graphviz output in console of Spyder

Working on a salary dataset. Everything works fine except when I use the python-graphviz module in Spyder 3.3.2 to show the decision tree graph it simply shows an image icon in console window. Well the same thing works on other systems. What I'm missing here?
The output image is here.Console Output
from sklearn.tree import DecisionTreeClassifier
dtf = DecisionTreeClassifier()
dtf.fit(X_train, y_train)
from sklearn.tree import export_graphviz
export_graphviz(dtf, out_file="tree.dot", class_names=["Less than 50k",
"More than 50k"])
import graphviz
with open("tree.dot") as f:
dot_graph = f.read()
graphviz.Source(dot_graph)
(spyder maintainer here) This seems a limitation of QtConsole, which is the package that powers our IPython consoles.
Please open an issue on the repo referenced above about this so we don't forget to fix it in the future.

Why the import "from tensorflow.train import Feature" doesn't work

That's probably totally noob question which has something to do with python module importing, but I can't understand why the following is valid:
> import tensorflow as tf
> f = tf.train.Feature()
> from tensorflow import train
> f = train.Feature()
But the following statement causes an error:
> from tensorflow.train import Feature
ModuleNotFoundError: No module named 'tensorflow.train'
Can please somebody explain me why it doesn't work this way? My goal is to use more short notation in the code like this:
> example = Example(
features=Features(feature={
'x1': Feature(float_list=FloatList(value=feature_x1.ravel())),
'x2': Feature(float_list=FloatList(value=feature_x2.ravel())),
'y': Feature(int64_list=Int64List(value=label))
})
)
tensorflow version is 1.7.0
Solution
Replace
from tensorflow.train import Feature
with
from tensorflow.core.example.feature_pb2 import Feature
Explanation
Remarks about TensorFlow's Aliases
In general, you have to remember that, for example:
from tensorflow import train
is actually an alias for
from tensorflow.python.training import training
You can easily check the real module name by printing the module. For the current example you will get:
from tensorflow import train
print (train)
<module 'tensorflow.python.training.training' from ....
Your Problem
In Tensorflow 1.7, you can't use from tensorflow.train import Feature, because the from clause needs an actual module name (and not an alias). Given train is an alias, you will get an ImportError.
By doing
from tensorflow import train
print (train.Feature)
<class 'tensorflow.core.example.feature_pb2.Feature'>
you'll get the complete path of train. Now, you can use the import path as shown above in the solution above.
Note
In TensorFlow 1.9.0, from tensorflow.train import Feature will work, because tensorflow.train is an actual package, which you can therefore import. (This is what I see in my installed Tensorflow 1.9.0, as well as in the documentation, but not in the Github repository. It must be generated somewhere.)
Info about the path of the modules
You can find the complete module path in the docs. Every module has a "Defined in" section. See image below (taken from Module: tf.train):
I would advise against importing Feature (or any other object) from the non-public API, which is inconvenient (you have to figure out where Feature is actually defined), verbose, and subject to change in future versions.
I would suggest as an alternative to simply define
import tensorflow as tf
Feature = tf.train.Feature

Resources