Cannot get the accuracy of a model through sklearn.metrics - python

from sklearn.metrics import accuracy_score
print("Accuracy of the model: {0}%".format(accuracy_score(y_test, y_pred)*100))
NameError Traceback (most recent call last)
Input In [1], in <cell line: 2>()
1 from sklearn.metrics import accuracy_score
----> 2 print("Accuracy of the model: {0}%".format(accuracy_score(y_test, y_pred)*100))
NameError: name 'y_test' is not defined

The error message suggests that you dont have a variable named y_test.
If you are using that code in a new file, make sure to import y_test. If you are using this use in a new cell, make sure the cell that defines y_test was executed before.

Related

NameError: name 'X_train' is not defined....tried in spyder too

have been trying to run this statement in colab but it's always showing the NameError
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.33,random_state = 42)
pl help to solve this problem

AttributeError: 'RandomOverSampler' object has no attribute 'fit_sample'

I am trying to use RandomOverSampler from imblearn but I'm getting error.
Looking at other posts, there seems to be a problem with older versions, but I checked my versions and I have:
sklearn.__version__
'0.24.1'
imblearn.__version__
'0.8.0'
This is the code I'm trying to run:
from imblearn.over_sampling import RandomOverSampler
OS = RandomOverSampler(sampling_strategy='auto', random_state=0)
osx, osy = OS.fit_sample(X, y)
And the error I get is:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-a080b92fc7bc> in <module>
2
3 OS = RandomOverSampler(sampling_strategy='auto', random_state=0)
----> 4 osx, osy = OS.fit_sample(X, y)
AttributeError: 'RandomOverSampler' object has no attribute 'fit_sample'
You want OS.fit_resample(X, y), not fit_sample.
You want OS.fit_resample(X, y), not fit_resample.

cannot import name 'TfidfVectorizer' from 'sklearn.feature_extraction'

I am trying to do a Topic modeling project, but when I use
from sklearn.feature_extraction import TfidfVectorizer
I will receive this error, my sckit-learn version installed is 0.24.1. I will be grateful if anyone could help me.
ImportError Traceback (most recent call last)
<ipython-input-2-5ae89ed22b7e> in <module>
----> 1 from sklearn.feature_extraction import TfidfVectorizer
ImportError: cannot import name 'TfidfVectorizer' from 'sklearn.feature_extraction' (C:\Users\mozha\Anaconda3\envs\spyder-env\lib\site-packages\sklearn\feature_extraction\__init__.py)
You have to import vectorizers like TfidfVectorizer from sklearn.feature_extraction.text and not sklearn.feature_extraction.

ImportError: cannot import name 'AutoModelWithLMHead' from 'transformers'

This is literally all the code that I am trying to run:
from transformers import AutoModelWithLMHead, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
model = AutoModelWithLMHead.from_pretrained("microsoft/DialoGPT-small")
I am getting this error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-14-aad2e7a08a74> in <module>
----> 1 from transformers import AutoModelWithLMHead, AutoTokenizer
2 import torch
3
4 tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
5 model = AutoModelWithLMHead.from_pretrained("microsoft/DialoGPT-small")
ImportError: cannot import name 'AutoModelWithLMHead' from 'transformers' (c:\python38\lib\site-packages\transformers\__init__.py)
What do I do about it?
I solved it! Apperantly AutoModelWithLMHead is removed on my version.
Now you need to use AutoModelForCausalLM for causal language models, AutoModelForMaskedLM for masked language models and AutoModelForSeq2SeqLM for encoder-decoder models.
So in my case code looks like this:
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")

ImportError: Numpy openblaspy flavour needed error displayed even when i have scipy, numpy and scikit modules installed

I am trying to run the following code:
# Gaussian Naive Bayes
from sklearn import datasets
from sklearn import metrics
from sklearn.naive_bayes import GaussianNB
# load the iris datasets
dataset = datasets.load_iris()
# fit a Naive Bayes model to the data
model = GaussianNB()
model.fit(dataset.data, dataset.target)
print(model)
# make predictions
expected = dataset.target
predicted = model.predict(dataset.data)
# summarize the fit of the model
print(metrics.classification_report(expected, predicted))
print(metrics.confusion_matrix(expected, predicted))
I have all the prerequisite modules installed like numpy, scipy, scikit-learn.
when i run the code the error displayed is:
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/sentiment-analysis/ldaex.py", line 4, in
from sklearn import datasets
File "C:\Python27\lib\site-packages\sklearn__init__.py", line 57, in
from .base import clone
File "C:\Python27\lib\site-packages\sklearn\base.py", line 9, in
from scipy import sparse
File "C:\Python27\lib\site-packages\scipy__init__.py", line 131, in
raise ImportError("numpy openblaspy flavour needed.")
ImportError: numpy openblaspy flavour needed.
Can anyone let me know the problem in my modules???
Also when i try to run this following program, the same set of errors are displayed:
from sklearn.datasets import fetch_20newsgroups
categories = ['alt.atheism', 'soc.religion.christian','comp.graphics',
'sci.med']
twenty_train = fetch_20newsgroups(subset='train',categories=categories,
shuffle=True, random_state=42)
print twenty_train.target_names

Categories