kds library gives AttributeError: module 'kds' has no attribute 'metrics' - python

Hi I am trying to use pypi kds package.
I have installed it with: pip install kds
I didn't have any installation problem. But when I ran the following example script:
# REPRODUCABLE EXAMPLE
# Load Dataset and train-test split
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn import tree
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33,random_state=3)
clf = tree.DecisionTreeClassifier(max_depth=1,random_state=3)
clf = clf.fit(X_train, y_train)
y_prob = clf.predict_proba(X_test)
# The magic happens here
import kds
kds.metrics.report(y_test, y_prob)
It gives an error:
AttributeError Traceback (most recent call last)
<ipython-input-4-fa00bcb248e7> in <module>
13 # The magic happens here
14 import kds
---> 15 kds.metrics.report(y_test, y_prob)
AttributeError: module 'kds' has no attribute 'metrics'

Issue is resolved in the latest version. Please update the package using pip install kds
Also don't forget the y_prob[:,1] in the last line. (output of scikit learn has 2 columns, so select 1 column)
# The magic happens here
import kds
kds.metrics.plot_lift(y_test, y_prob[:,1])

Related

Data frames, importing csv files in jupyter notebook

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.

name 'StandardScaler' is not defined

I have installed scikit-learn 0.23.2 via pip3, however, I get this error from my code
Traceback (most recent call last):
File "pca_iris.py", line 12, in <module>
X = StandardScaler().fit_transform(X)
NameError: name 'StandardScaler' is not defined
I searched the web and saw similar topics, however the version is correct and I don't know what to do further. The line import sklearn is in the top of the script.
Any thought?
StandardScaler is a method under sklearn.preprocessing. You need to import the StandardScaler like this:
from sklearn.preprocessing import StandardScaler
X = StandardScaler().fit_transform(X)
Or
import sklearn
X = sklearn.preprocessing.StandardScaler().fit_transform(X)

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)

Import Label Encorder sklearn

I am getting below error when running the code :-
from sklearn.preprocessing import LabelEncorder
LabelEncoder_X = LabelEncorder()
mod_set[:,0] = labelencorder_X.fit_transform(mod_set[:,0])
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-12-5d65523a64f6> in <module>
----> 1 from sklearn.preprocessing import LabelEncorder
2 LabelEncoder_X = LabelEncorder()
3 mod_set[:,0] = labelencorder_X.fit_transform(mod_set[:,0])
ImportError: cannot import name 'LabelEncorder' from 'sklearn.preprocessing' (/Users/kenilpatel/opt/anaconda3/lib/python3.7/site-packages/sklearn/preprocessing/__init__.py)
I tried reinstalling scikit as well as updating conda , but nothing seems to work.The problem is with Encorder only , Imputer and other things work fine .
You got a spelling mistake in your module import: LabelEncorder => LabelEncoder
# from sklearn.preprocessing import LabelEncorder
from sklearn.preprocessing import LabelEncoder

AttributeError: 'RandomForestClassifier' object has no attribute 'fit_transform'

I am getting an error
AttributeError: 'RandomForestClassifier' object has no attribute 'fit_transform'
However, there is a method named fit_transform(X,y) in sklearn.ensemble.RandomForestClassifier. This can be seen here
I don't understand why I am getting this error and how do I resolve it.
Here is the code snippet-
from sklearn.ensemble import RandomForestClassifier
import pickle
import sys
import numpy as np
X1=np.array(pickle.load(open('X2g_train.p','rb')))
X2=np.array(pickle.load(open('X3g_train.p','rb')))
X3=np.array(pickle.load(open('X4g_train.p','rb')))
X4=np.array(pickle.load(open('Xhead_train.p','rb')))
X=np.hstack((X2,X1,X3,X4))
y = np.array(pickle.load(open('y.p','rb')))
rf=RandomForestClassifier(n_estimators=200)
Xr=rf.fit_transform(X,y)
There's no such method in the scikit-learn API documentation
To train your model and get predictions, you need to do like this
rf = RandomForestClassifier()
# train the model
rf.fit(X_train, y_train)
# get predictions
predictions = rf.predict(X_test)

Categories

Resources