Data frames, importing csv files in jupyter notebook - python

import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.externals import joblib
music_data = pd.read_csv('music.csv')
X = music_data.drop(columns=['genre'])
y = music_data['genre']
model = DecisionTreeClassifier()
model.fit(X, y)
joblib.dump(model, 'music-recommender.joblib' )
This is the output:
Traceback (most recent call last)
<ipython-input-28-802088a85507> in <module>
1 import pandas as pd
2 from sklearn.tree import DecisionTreeClassifier
----> 3 from sklearn.externals import joblib
4
5 music_data = pd.read_csv('music.csv')
ImportError: cannot import name 'joblib' from 'sklearn.externals' (C:\Users\christian\anaconda3\lib\site-packages\sklearn\externals\__init__.py)*****
I would appreciate if someone could help out, is there another syntax that I should use to import joblib from sklearn?

I had the same problem, I never knew that joblib was already install on my machine. just replace this line of code
from sklearn.externals import joblib
with
import joblib

The issue is here that you are trying to import joblib from scikit learn not reading csv file if you're wondering. Install joblib using pip install joblib and import it like this
import joblib
joblib.dump(model, 'music-recommender.joblib')
Remember to remove the import line from sklearn.externals. You can find more details here from the official documentation of scikit-learn.

Related

How to resolve the ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' in python?

Hello there,
I am new to python and I was trying out a project on jupyter notebook when I encountered an error which I couldn't resolve. I'd really appreciate some help.
This is my code:
import pandas as pd
from sklearn.tree import DesicionTreeClassifier
music_data = pd.read_csv(r'C:\python\python382\music.csv')
X=music_data.drop(columns=['genre'])
y=music_data['genre']
model=DesicionTreeClassifier()
model.fit(X,y)
music_data
And i got the output as :
ImportError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2540/2462038274.py in <module>
1 import pandas as pd
----> 2 from sklearn.tree import DesicionTreeClassifier #using desicion tree algo here to make model[we import DesicionTree module from tree module which is imported from sklearn library]
3 music_data = pd.read_csv(r'C:\python\python382\music.csv')
4
5 ##Cleaning and segregating data
ImportError: cannot import name 'DesicionTreeClassifier' from 'sklearn.tree' (C:\python\python382\lib\site-packages\sklearn\tree\__init__.py)
Thank you.
You have missspelled the fumction name DesicionTreeClassifier is in reality DecisionTreeClassifier

ModuleNotFoundError: No module named 'sklearn.tree.tree'

I'm trying to learn how to create a machine learning API with Flask, however, following this tutorial, the following error appears when I type the command python app.py:
Traceback (most recent call last):
File "C:\Users\Breno\Desktop\flask-api\app.py", line 24, in <module>
model = p.load(open(modelfile, 'rb'))
ModuleNotFoundError: No module named 'sklearn.tree.tree'
My code:
from flask import Flask, request, redirect, url_for, flash, jsonify
import numpy as np
import pickle as p
import pandas as pd
import json
#from sklearn.tree import DecisionTreeClassifier
app = Flask(__name__)
#app.route('/api/', methods=['POST'])
def makecalc():
j_data = request.get_json()
prediction = np.array2string(model.predict(j_data))
return jsonify(prediction)
if __name__ == '__main__':
modelfile = 'models/final_prediction.pickle'
model = p.load(open(modelfile, 'rb'))
app.run(debug=True,host='0.0.0.0')
Could someone help me please?
Pickles are not necessarily compatible across scikit-learn versions so this behavior is expected (and the use case is not supported). For more details, see https://scikit-learn.org/dev/modules/model_persistence.html#model-persistence. Replace pickle by joblib.
by example :
>>> from sklearn import svm
>>> from sklearn import datasets
>>> clf = svm.SVC()
>>> X, y= datasets.load_iris(return_X_y=True)
>>> clf.fit(X, y)
SVC()
>>> from joblib import dump, load
>>> dump(clf, open('filename.joblib','wb'))
>>> clf2 = load(open('filename.joblib','rb'))
>>> clf2.predict(X[0:1])
array([0])
>>> y[0]
0
For anyone coming across this issue (perhaps dealing with code written long ago), sklearn.tree.tree is now under sklearn.tree (as from v0.24). This can be see from the import error warning:
from sklearn.tree.tree import BaseDecisionTree
/usr/local/lib/python3.7/dist-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.tree.tree module is deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.tree. Anything that cannot be imported from sklearn.tree is now part of the private API.
warnings.warn(message, FutureWarning)
Instead, use:
from sklearn.tree import BaseDecisionTree
The problem is with the version of sklearn. Module sklearn.tree.tree is removed since version 0.24. Most probably, your model has been generated with the older version. Try installing an older version of sklearn:
pip uninstall scikit-learn
pip install scikit-learn==0.20.4

How can I solve "cannot import name 'plot_2d_separator' from 'figures' " error?

I was trying to draw a linear decision function which separates the two regions of data points in 2d input space using plot_2d_seperator function from figures module, even though figures and other modules have been installed in my device. But it is showing an error attached below.
from sklearn.datasets import make_blobs
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from figures import plot_2d_separator
It throws the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-69-4a72dfe173de> in <module>
2 from sklearn.model_selection import train_test_split
3 from sklearn.linear_model import LogisticRegression
----> 4 from figures import plot_2d_separator
5
6 X, y = make_blobs(centers=2, random_state=0)
ImportError: cannot import name 'plot_2d_separator' from 'figures' (C:\ProgramData\Anaconda3\lib\site-packages\figures\__init__.py)

Can't Import ColumnTransfomer (Scikit-Learn) - Import Error:Cannot import name 'Parallel'

I use the ColumnTransfomer from Scikit-Learn and it usually works fine. Some days its decides to give me this error?
I have updated scikitlearn and it is on version 0.20. I am using Azure Notebooks (Jupyter Notebooks).
Here is the error that printed when I run:
from sklearn.compose import ColumnTransformer
ImportError Traceback (most recent call last)
<ipython-input-71-95a87d70dcfe> in <module>()
----> 1 from sklearn.compose import ColumnTransformer
~/anaconda3_501/lib/python3.6/site-packages/sklearn/compose/__init__.py in <module>()
6 """
7
----> 8 from ._column_transformer import ColumnTransformer, make_column_transformer
9 from ._target import TransformedTargetRegressor
10
~/anaconda3_501/lib/python3.6/site-packages/sklearn/compose/_column_transformer.py in <module>()
15
16 from ..base import clone, TransformerMixin
---> 17 from ..utils import Parallel, delayed
18 from ..externals import six
19 from ..pipeline import _fit_transform_one, _transform_one, _name_estimators
ImportError: cannot import name 'Parallel'
Thanks!
Vrage
You can import ColumnTransfomer when you are importing numpy in the start of the code. By importing with compose package, it will get solved. I did the same and it worked. Make sure you have installed all the packages properly.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder

Not able to create shortcut for sklearn

I entered the following code:
import sklearn
import sklearn as sk
import sklearn.preprocessing as skl
from sklearn.preprocessing import Imputer
from sk.preprocessing import Imputer
from skl import Imputer
The part which reads; from sklearn.preprocessing import Imputer gets executed normally.
However, when I run from sk.preprocessing import Imputer, I get the following error:
from sk.preprocessing import Imputer
Traceback (most recent call last):`
File "<ipython-input-84-fc12144914d1>", line 1, in <module>`
from sk.preprocessing import Imputer`
ModuleNotFoundError: No module named 'sk'`
And from skl import Imputer yields the following:
from skl import Imputer`
Traceback (most recent call last):`
File "<ipython-input-85-1e925587d122>", line 1, in <module>`
from skl import Imputer`
ModuleNotFoundError: No module named 'skl'`
Why am I not able to create a shortcut for the Library?
Because it is wrong to do so. The right way do do it is as you have written already.
from sklearn.preprocessing import Imputer
the __init__.py in the preprocessing directory of sklearn defines the possible imports from that level.
The below is a valid aliasing and i think is what you are looking for.
from sklearn.preprocessing import Imputer as imp

Categories

Resources