python 3.8 error with cross validation Implementation - scikit-learn

When I run the following code from a tutorial, I keep getting the following error at the end in almost every video I attempt.
Source: https://pythonprogramming.net/k-means-from-scratch-2-machine-learning-tutorial/?completed=/k-means-from-scratch-machine-learning-tutorial/
I get the following error:
from sklearn import preprocessing, cross_validation
ImportError: cannot import name 'cross_validation' from 'sklearn
I did pip installs, changing the way cross_validation is stated based on other suggestions but I still can't solve it.

I could not find cross_validation as a library in sklearn.
You need to use from sklearn.model_selection import cross_validate as per the documentation. This has already been answered here.
I also suggest going through the documentation of the functions you use to gain a better understanding of what you are doing.

Related

Not able to import Mean Absolute percentage error from sklearn.metrics

I am getting error while importing mean absolute percentage error from sklearn.metrics:
Cannot import name 'mean_absolute_percentage_error' from sklearn.metrics
I am using sklearn version 0.23.1
mean_absolute_percentage_error is new in version 0.24. So you need to update your sklearn version either you can implement it on your own.
Here is the source if you want to implement it yourself.

Python 3.x - AttributeError: 'function' object has no attribute 'Kfold'

I'm following along an older tutorial with an SVM optimized with a genetic algorithm. I originally thought the issue was just with versions of python and/or scikit, but I'm now unsure of what the issue could be as it continues displaying the same error. I'm currently using python-scikit-learn 0.20.3-1 on Antergos and found a link here that unfortunately didn't seem to help.
So far I've found a few links and examples that have had me alter different aspects of the code, which overall just jumbled everything up. This GitHub page was useful in at least understanding the version difference, as was the first link. This blog post was also neat, but again didn't really help me narrow down the exact issue as to why it's reading out the error. I even tried looking at the sklearn documentation but I still couldn't get it.
These are what I've imported:
import numpy as np
import pandas as pd
import random as rd
from sklearn.model_selection import cross_validate
from sklearn import preprocessing
from sklearn import svm
I had "kfold" defined earlier in the program as such:
kfold = 3
As well, this is the exact line it seem to be having issues with:
kf = cross_validate.KFold(Cnt1,n_splits=kfold)
What it should be doing is simply applying cross validation. However, the error reads:
AttributeError: 'function' object has no attribue 'KFold'
I can't tell if the issue is that I'm not understanding what I should be altering via the links I've given, or if it's a different error born of ignorance. Is there something I'm missing in order to get this to work?
The KFold function is in the sklearn.model_selection module not in sklearn.model_selection.cross_validate
So you sould import
from sklearn import model_selection
and then using like
model_selection.KFold(...)
or you can import the function
from sklearn.model_selection import KFold
just like in the KFold Doc example.

module 'sklearn.metrics' has no attribute 'davies_bouldin_score'

I am trying to evaluate a clustering kmeans model using sklearn.metrics.davies_bouldin_score. I am using google colab with runtime Python 3 and GPU accelerator.
I got this error:
module 'sklearn.metrics' has no attribute 'davies_bouldin_score'.
I have tried to import metrics package in different ways as it was suggested by some people as from sklearn import metrics and import sklearn.metrics. It made no difference.
I have also updated sklearn package !pip install --upgrade sklearn and it did not solve the problem.
Is it google-colaboratory? How can I solve it?
You need to pip install scikit-learn, not sklearn, though the import sklearn.metrics is correct. It looks like it's also a recently added feature, so it may not be available in earlier versions of scikit-learn.
It is in version 0.20. Make sure you are using the right version of Sklearn.
"conda update sklearn"

name 'classification_model' is not defined

Im trying to model in Python 3.5 and am following an example that can be found at here.
I have imported all the required libraries from sklearn.
However I'm getting the following error.
Code:
from sklearn.linear_model import LogisticRegression
from sklearn.cross_validation import KFold #For K-fold cross validation
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn import metrics
outcome_var = 'Loan_Status'
model = LogisticRegression()
predictor_var = ['Credit_History']
classification_model(model, loan,predictor_var,outcome_var)
When I run the above code I get the following error:
NameError: name 'classification_model' is not defined
I'm not sure how to resolve this as I tried importing sklearn and all the sub libraries.
P.S. I'm new to Python, hence I'm trying to figure out basic steps
Depending on the exact details this may not be what you want but I have never had a problem with
import sklearn.linear_model as sk
logreg = sk.LogisticRegressionCV()
logreg.fit(predictor_var,outcome_var)
This means you have to explicitly separate your training and test set, but having fit to a training set (the process in the final line of my code), you can then use the methods detailed in the documentation [1].
For example figuring out what scores (how many did I get correct) you get on unseen data with the .score method
[1] http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegressionCV.html
It appears this code came from this tutorial.
The issue is exactly as the error describes. classification_model is currently undefined. You need to create this function yourself before you can call it. Check out this part of that tutorial so you can see how it's defined. Good luck!
from sklearn.metrics import classification_report

Cannot resolve import "MultiLabelBinarizer"

I am new to the "scikit-Learn" API and wish to implement a multilabel classification problem. After importing the following packages:
import numpy as np
from sklearn.multiclass import OneVsRestClassifier
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.svm import LinearSVC
from sklearn.metrics import classification_report
I get an error which says 'Unresolved import: MultiLabelBinarizer'. But other related packages imported seem to work fine. I wonder why the 'MultiLabelBinarizer' cannot be imported, given the fact that the 'sklearn ' package was properly installed. Any help will be appreciated.
I found out the reason, in case someone comes across the same problem. The error was due to fact that I was running the above code on 'sklearn' version 0.14 (which was installed by default on Ubuntu 14.04 LTS) instead of 0.16. I also think the MultiLabelBinarizer Class is only available on 'sklearn' version 0.16 (I have not tried the 0.15 - in case there is any).

Resources