how to solve error module sklearn.cluster? - python

i want to make recommendation location but i have problem with my sklearn. i have been update my library but it is not work. i use python 2.7 with anaconda
please help me :D
it is my library code.
from sklearn.cluster import KMeans
import numpy as np
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score, recall_score, precision_score
from sklearn import svm
from numpy import algorithms, environment
import plotly
import plotly.plotly as py
import plotly.graph_objs as go

Windows
Open command prompt(as admin)
enter 'pip install -U scikit-learn'
Unix
Open terminal
Enter 'sudo pip install -U scikit-learn'

The default Anaconda distribution should have all of these packages, so it's likely your interpreter is looking for packages in a different spot. This is controlled by the PYTHONPATH system variable, which tell it where to look for package imports.
Anaconda can set this correctly for you during (re)installation if you choose to update the variable. You can also edit it yourself--how you do so depends on your OS.
To view the variable in python for troubleshooting:
How do I find out my python path using python?
This should point to a directory on you computer containing the package files.

Related

Why can't I import some of the modules in scikit-learn? (PyCharm)

I'm trying to run the following code:
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.feature_selection import SelectFromModel
from sklearn.model_selection import train_test_split
from sklearn import cross_validation
ExtraTreesClassifiers runs, and so does SelectFrom Model. But the latter two lines do not run. I get an error that says "ImportError: cannot import name 'cross_validation' from 'sklearn'."
I've checked in PyCharm to see if my interpreter has scikit-learn installed, and it's installed. I've uninstalled and reinstalled it from my terminal using pip3. I've tried running the file from my terminal, and it says "No such file or directory." I'm not sure what else to try.
Does anyone have any suggestions for what I could do next?
Thank you for your time.
cross_validation was used to exist as a scipy package but is now deprecated and so it isn't advisable to use it.
You can use sklearn.model_selection.train_test_split instead
:
from sklearn.model_selection import train_test_split
You can also try downgrading it by installing an older version of sklearn to continue using the cross_validation package.
References: ImportError: cannot import name cross_validation

Cannot import from sklearn.feature_extraction.text import CountVectorizer

I'm trying to import CountVectorizer from sklearn with the following line:
from sklearn.feature_extraction.text import CountVectorizer
sklearn: 0.0
scikit-learn: 0.23.2
numpy: 1.19.2
scipy: 1.5.2
threadpoolctl: 2.1.0
joblib: 0.17.0
Every time I try to run the code I receive the following error:
No name 'feature_extraction' in module 'sklearn' pylint(no-name-in-module)
Unable to import 'sklearn.feature_extraction.text' pylint(import-error)
If it matters I am running this in vscode on a Linux system inside of a VM. Also, I was able to run it earlier on the VM and it just stopped working for no apparent reason.
I found out the reason why for some reason my vscode was saving my file as .pyc and it wouldn't recognize the library with pyc. If anyone else experiences this problem note my file still said py but auto-generated a pycache folder.
because sklearn is deprecated
try this :
pip install scikit-learn

Can import python package in r while the package exists for other python interpreters

With the r package reticulate, I attempted to import the python package scipy to do my data analysis. However, it says
ModuleNotFoundError: No module named 'scipy'.
But when I use jupyter notebook, clearly I can import scipy so my computer has the package. Somehow RStudio is not importing it.
This is what I did:
library(reticulate) #import reticulate to enable python usage in r
use_python("/usr/local/bin/python3", required = TRUE) #set the path for where my python is.
from scipy.optimize import minimize #try to import the package in a python block
Then I get the error package not found. I tried so many things this afternoon but failed
First, I tried to install again using r studio with py_install("scipy"). It installs, but when I try to import again, it still reports not module found.
Second, I tried to create virtual environments by conda_create('r-reticulate', packages = "python=3") and then py_install("scipy"). Doesn't help.
Third, I upgraded the python to python 3.7. Now not only the scipy package, but all python packages like numpy, pandas also could not be imported. Of course they can still be imported in Jupiter notebook so it's not the case that my computer does not have them.
I tried the previous two things again but no help.
library(reticulate)
use_python("/usr/local/bin/python3.7", required = TRUE)
from scipy.optimize import minimize
import matplotlib.pyplot as plt # for plotting
import numpy as np # for numeric calculations
import pandas as pd # for python data frame
import time
I expect to successfully import all the python packages to RStudio. And figure out a way to install new python packages and import them to r.

Sklearn package

Recently I have upgraded my sklearn package in Python 3.7 and after that I could not find some important packages like gridsearch() , cross_validation() , GaussianNb () etc .
I am a beginner in Machine learning and I want to continue working with Python 3 instead of using Python 2. Can anyone please help me with this problem ? BTW, I use Anaconda 3 and Spyder 3.
I was able to create python 3.7.2 environment, then I could import gridsearch() , cross_validation() , GaussianNb () methods that you have described in the question.
Note: There are multiple ways in which you can install sklearn. One of the popular way to do it is using conda package manager.
The following is working on Windows-10 OS. I am creating python 3.7 as conda virtual environment.
I am pretty sure, this must work on other OS(Linux, redhat). But I havent tested.
My steps.
Created the virtual environment.
>>> conda create --name Py37Test python=3.7 pandas scikit-learn
>>> import sklearn
>>> from sklearn.model_selection import GridSearchCV
>>> from sklearn.model_selection import cross_validate
>>> from sklearn.naive_bayes import GaussianNB

Why won't Jupyter Notebook load a module?

I'm using Windows 10, anaconda navigator, and jupyter notebook 5.7.4 throws an error when I try to import 'basemap' using "from mpl_toolkits.basemap import basemap"
I'm very new to python. Following a tutorial to learn GIS mapping with python, I've installed all the required packages: pandas, numpy, geopandas, basemap, matplotlib, pillow and a few others.
The tutorial code says to write "from mpl_toolkits.basemap import basemap"
However, mpl_toolkits does not appear to be an available package when I search for it in the anaconda navigator or in the anaconda prompt.
Having searched the anaconda user guide and the matplotlib web page, I am inferring that mpl_toolkits is included within matploblib?
The error that results is this: ImportError: cannot import name 'basemap' from 'mpl_toolkits.basemap' (C:\Users\Kevin\AppData\Local\conda\conda\envs\mapping\lib\site-packages\mpl_toolkits\basemap__init__.py)
# command to display matplotlib plots inline within the ipython notebook
%matplotlib inline
# import necessary modules
import numpy as np, matplotlib.pyplot as plt, pandas as pd, geopandas as
gpd
from mpl_toolkits.basemap import basemap
from shapely.geometry import Point
from geopy.distance import great_circle
I expect the jupyter notebook to load all of the modules but instead I get the above error.
Any help is much appreciated, this is my first go at something like this so I am probably making a simple neophyte mistake.
Seems this is very peculiar to Anaconda. If you correctly installed all the dependencies you should check this Python basemap module impossible to import

Categories