Python Forum
NameError: name 'cross_validation' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'cross_validation' is not defined
#1
Hi there, please I am having an issue with the following code. I checked online including stack-overflow but no good response to this case.
Imports:

import math
import csv
import random
import numpy as np
import pandas as pd
import sklearn
from sklearn.ensemble import RandomForestClassifier
#from sklearn import cross_validation, linear_model
from sklearn.model_selection import cross_validate
from sklearn import linear_model
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
print("Fitting on %d samples." % len(X))

model = sklearn.linear_model.LogisticRegression()
# model = RandomForestClassifier(max_depth = 2)

# Check precision.
print("Performing cross-validation.")
print(cross_validation.cross_val_score(model, np.array(X), np.array(y), cv=10, scoring='accuracy').mean())

model.fit(X, y)
out: Fitting on 13305 samples.
Doing cross-validation.

error encountered:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-23-a98f90161d32> in <module>
      6 # Check accuracy.
      7 print("Doing cross-validation.")
----> 8 print(cross_validation.cross_val_score(model, np.array(X), np.array(y), cv=10, scoring='accuracy').mean())
      9 
     10 model.fit(X, y)

NameError: name 'cross_validation' is not defined

Please kindly help.
Reply
#2
I am curious why you have line 8, the import of the definition commented out? This will throw the error if it is not defined.
import math
import csv
import random
import numpy as np
import pandas as pd
import sklearn
from sklearn.ensemble import RandomForestClassifier
#from sklearn import cross_validation, linear_model
from sklearn.model_selection import cross_validate
from sklearn import linear_model
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
"Often stumped... But never defeated."
Reply
#3
(Apr-21-2020, 06:40 PM)DT2000 Wrote: I am curious why you have line 8, the import of the definition commented out? This will throw the error if it is not defined.
import math
import csv
import random
import numpy as np
import pandas as pd
import sklearn
from sklearn.ensemble import RandomForestClassifier
#from sklearn import cross_validation, linear_model
from sklearn.model_selection import cross_validate
from sklearn import linear_model
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split

The reason is that it throws up this specific error:
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-f28d8d77736c> in <module>
      6 import sklearn
      7 from sklearn.ensemble import RandomForestClassifier
----> 8 from sklearn import cross_validation, linear_model
      9 from sklearn.model_selection import cross_validate
     10 from sklearn import linear_model

ImportError: cannot import name 'cross_validation' from 'sklearn' (/anaconda3/lib/python3.7/site-packages/sklearn/__init__.py)

Sometimes, the best way to get answers to questions is by asking yourself or rather asking others that may not have the answer.
Here's the solution:

I simply modified the following line:
from:
print(cross_validation.cross_val_score(model, np.array(X), np.array(y), cv=10, scoring='accuracy').mean())
TO:

print(sklearn.model_selection.cross_val_score(model, np.array(X), np.array(y), cv=10, scoring='accuracy').mean())
Specifically, the object cross_validation is deprecated or obsolete(I think) and sklearn.model_selection works now like charm.
Thanks to you all.
Reply
#4
He was rubber ducking us. I feel used.
Reply
#5
(Apr-21-2020, 07:29 PM)deanhystad Wrote: He was rubber ducking us. I feel used.
dean, the problem was real, no one was used nor being rubber docked. I surfed up Stack0verflow and the solution wasn't just there. So I used some keywords from the online sklearn doc and I got the new object and method, plugged it in and wholah!!! it worked like charm,
Python has so many changes going on even as we speak, to me I feel its the most dynamic yet powerful language of all time.

Perhaps this thread could be useful in the future.
I just joined today and would gladly contribute more to this forum.
Cheers men, all the best.
Reply
#6
tmhsa... As per my reply, it was posted in a question format, and not having it imported will also give you a "is not defined" error. :)
"Often stumped... But never defeated."
Reply
#7
Hi all,

First post here. I found this forum and the thread while searching from the same thing as @tmhsa. The difference in the solution that worked for me was that I had to use

print(cross_val_score(model, np.array(X), np.array(y), cv=10, scoring='accuracy').mean())
So, without sklearn.model_selection.

Just wanted to say thanks for pointing me in the right direction, so while it might seem as rubber ducking to some this was actually very helpful to others, and I wanted to let the author know. Hopefully to also help someone else in the future who might have a similar issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 282 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,105 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,890 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,324 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,514 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,918 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,970 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,386 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,887 Jul-26-2021, 04:36 PM
Last Post: snippsat
  I am getting a NameError that is not defined and not sure why it happen rick0922 5 4,093 Jun-14-2021, 03:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020