AttributeError: module 'sklearn.metrics' has no attribute 'items' - python-3.x

Here is my code..
import imp
from sklearn.metrics import classification_report
from sklearn import metrics
from sklearn.metrics import accuracy_score
for title, metric in metrics.items():
print(title, metric(labels_categorical_dev.argmax(axis=1), y_pred.argmax(axis=1)))
print(classification_report(labels_categorical_dev.argmax(axis=1), y_pred.argmax(axis=1)))
y_pred = model.predict([message_first_message_test, message_second_message_test, message_third_message_test])
Iam getting below error..
Traceback (most recent call last):
File "getopt.py", line 6, in
for title, metric in metrics.items():
AttributeError: module 'sklearn.metrics' has no attribute 'items'
I have tried with versions from scikit-learn=0.20.0 to scikit-learn=0.24.2
But still getting this error. Please give me a solution for this.

Can you share more details about what is the purpose of your code? As you can see here, there isn't any attribute of sklearn.metrics named items().
.items() is used for dictionaries in order to get the values pertaining to different keys in that dictionary.
Also, you have defined y_pred after it has been referenced, so that will cause an error as well.

Related

Understand the difference between pyspark.ml.regression 'IsotonicRegression' vs 'IsotonicRegressionModel'

I am trying to calibrate the output of an pyspark GradientBoostingClassifier model to probabilities and want to try this option.
I have run an IsotonicRegression like this:
from pyspark.ml.regression import IsotonicRegression, IsotonicRegressionModel
model = IsotonicRegression().fit(train_data)
predictions_train=model.transform(test_data)
But I am unable to perform fit using IsotonicRegressionModel because when I try this:
irm = IsotonicRegressionModel()
model_irm =irm.fit(train_data)
I'm getting the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[70], line 4
1 # Trains an isotonic regression model.
2 irm = IsotonicRegressionModel()
----> 3 model_irm=irm.fit(train_data)
AttributeError: 'IsotonicRegressionModel' object has no attribute 'fit'
I would like to run second option to identify the difference between IsotonicRegression vs IsotonicRegressionModel.
Thanks in advance if anyone can help me understand this difference.
Im using spark.version 3.1.3

getting error 'tensorflow.python.ops.rnn_cell_impl' has no attribute '_linear'

I tried the below line of code, but it is giving me the below error
y = rnn_cell_impl._linear(slot_inputs, attn_size, True)
AttributeError: module 'tensorflow.python.ops.rnn_cell_impl' has no attribute '_linear'
I am currently using Tensorflow version 2.10, I tried with all possible solutions by using
#from tensorflow.contrib.rnn.python.ops import core_rnn_cell
or
#from tensorflow.keras.layers import RNN
still no solution.
Can someone help me with the same?

Not able to find out the version of a module

I have imported norm as:
from scipy.stats import norm
I want to find out the version using:
print(scipy.__version__)
but it is raising an error called:
NameError: name 'scipy' is not defined
if i am using this:
print(norm.__version__)
but it is raising another error called:
AttributeError: 'norm_gen' object has no attribute '__version__'
Please help me to solve this issue.
Thanks
The line from scipy.stats import norm doesn't make the name scipy available in your current namespace. To use scipy.__version__, you must first import scipy.
In [57]: import scipy
In [58]: print(scipy.__version__)
1.4.1

tf.contrib.metrics.f1_score can not be imported

I'm trying to calculate the F1 score using tf.contrib.metrics.f1_score, but it gives me an error. I know how to calculate it using precision and recall but i want to use this function.
I have tried it on ubuntu 16.04 LTS with tensorflow version 1.9.0 with gpu suport and no gpu suport
from tensorflow.contrib.metrics import f1_score as ms
i get this error:
ImportError: Traceback (most recent call last)
<ipython-input-6-627f14191ea2> in <module>()----> 1 from tensorflow.contrib.metrics import f1_score as ms
ImportError: cannot import name 'f1_score'
AND
from tensorflow.contrib import metrics as ms
ms.f1_score
I get this error:
AttributeError Traceback (most recent call last)
<ipython-input-8-c19f57465581> in <module>()
1 from tensorflow.contrib import metrics as ms
----> 2 ms.f1_score
AttributeError: module 'tensorflow.contrib.metrics' has no attribute 'f1_score'
I expect ms.f1_score would load
If you are sure that you have tf.contrib available and this doesn't work for you, maybe you will need to reinstall tensorflow use pip install -U tensorflow or use the -GPU if you are using that version.
If it fails, go to the place where tensorflow is installed and manually check if it is available or not, if it is available, make sure that you don't have a file in the same directory (Current working directory) named as tensorflow.py or tf.py
After that you should get
Update: As pointed by User #grwlf
Since TensorFlow 2.0, tf.contrib modules were moved to the Addons repo. See github.com/tensorflow/addons. There, F1 mesure is available as F1Score from tensorflow_addons.metrics import F1Score
You can find the documentation of f1_score here
Since it is a function, maybe you can try out:
from tensorflow.contrib import metrics as ms
ms.f1_score(labels,predictions)
Which will return a scalar tensor of the best f1 scores across different thresholds.
Example from tensorflow docs:
def model_fn(features, labels, mode):
predictions = make_predictions(features)
loss = make_loss(predictions, labels)
train_op = tf.contrib.training.create_train_op( total_loss=loss, optimizer='Adam')
eval_metric_ops = {'f1': f1_score(labels, predictions)}
return tf.estimator.EstimatorSpec( mode=mode, predictions=predictions, loss=loss, train_op=train_op, eval_metric_ops=eval_metric_ops, export_outputs=export_outputs)
estimator = tf.estimator.Estimator(model_fn=model_fn)
Hope this answers your question.

AttributeError in sklearn_crfsuite has no attribute CRF arror

I am getting this error when trying our rasa_nlu with spacy
AttributeError: 'sklearn_crfsuite' object has no attribute 'CRF'
rasa_nlu was importing this way
import sklearn_crfsuite
So I tried importing like below before calling rasa_nlu
from sklearn_crfsuite import CRF
But getting a different
error - cannot import name 'CRF'
Looking for some suggestions.
If you want to do sklearn_crfsuite.CRF, then do import sklearn_crfsuite to import it. If you're importing with from sklearn_crfsuite import CRF, then just use CRF by itself.

Resources