AttributeError: module 'community' has no attribute 'best_partition' - python

I try to run this code:
from cdlib import algorithms
import networkx as nx
G = nx.karate_club_graph()
coms = algorithms.louvain(G, resolution=1., randomize=False)
but the error remains the same.
I have tried all options given by
AttributeError: module 'networkx.algorithms.community' has no attribute 'best_partition'
but it doesn't work.
Also, I'm working in Google Colab and I have installed cdlib.

If still useful, this worked out for me :
pip uninstall community
pip install python-louvain
I could import community afterwards and use best_partition.

From this, it looks like there is a community python package that conflicts with the python-louvain package. Both packages happen to be pre-installed in google colab kernels. To avoid this conflict, I just uninstalled networkx, python-louvain and community and then reinstalled networkx and python-louvain. Finally I installed cdlib.
After that I ran your code and everything worked well. So overall the code is:
!pip uninstall networkx
!pip uninstall python-louvain
!pip uninstall community
!pip install python-louvain
!pip install networkx
!pip install cdlib
from cdlib import algorithms
import networkx as nx
G = nx.karate_club_graph()
coms = algorithms.louvain(G, resolution=1., randomize=False)
print(coms)
And the ouput gives:

Related

Networkx & Jupyter Notebook using pip AttributeError

Hi everyone, I'm trying to create a network visualization with python using the networkx package. Unfortunately I'm getting a "AttributeError: module 'scipy.sparse' has no attribute 'coo_array'" at the end of my code. I've tried to fix it but every help in the internet points towards upgrading pip or conda which I've done and still get the same error. I'm using jupyter notebooks - any help would be greatly appreciated!
#Prerequisites
import sys
!{sys.executable} -m pip install --user networkx
!{sys.executable} -m pip install --user numpy
!{sys.executable} -m pip install --user pandas
pip install notebook --upgrade
conda upgrade notebook
import networkx as nx
import numpy as np
import pandas as pd
from pathlib import Path
from pandas import DataFrame
#Read in Source File - NB this must match the schema requirements
df_InputData = pd.read_excel("/Users/paulkruse/Desktop/FR/Fr1.xlsx")
Src_Column = 'Source ID'
Tgt_Column = 'Target ID'
print(df_InputData);
#Nodes are positioned using the Fruchterman-Reingold force-directed algorithm.
Q = nx.Graph()
arr_SrcTgt= np.array(df_InputData[[Src_Column, Tgt_Column]])
print(arr_SrcTgt);
Q.add_edges_from(arr_SrcTgt)
dict_Coords = nx.spring_layout(Q)
It seems that your scipy version is out of date. The most recent version 1.8.1 certainly has coo_array function. One way to resolve this should be to upgrade scipy. If you're doing this from inside jupyter notebook, then use
!pip install --user scipy==1.8.1
Make sure to restart kernel after installation.

No module named pyLDAvis

I can't import pyLDAvis.
It is installed but for some reason, I can not import it.
I tried
conda update anaconda
pip install --upgrade pip
pip install --upgrade jupyter notebook
pip install pyLDAvis
Installing pyLDAvis returns the message 'requirement already satisfied'. So I tried uninstalling and reinstalled the package but still doesn't work. This never happened with any other packages.
How can I solve this problem?
The pyLDAvis gensim name changed. When I use gensim_models rather than gensim the interactive viz works.
The 'gensim_models' name is in the latest commit to bmabey's repo.
import pyLDAvis
import pyLDAvis.gensim_models as gensimvis
pyLDAvis.enable_notebook()
# feed the LDA model into the pyLDAvis instance
lda_viz = gensimvis.prepare(ldamodel, corpus, dictionary)
Following code worked for me and I'm using Google Colaboratory.
!pip install pyLDAvis
import pyLDAvis
import pyLDAvis.gensim_models
pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim_models.prepare(ldamodel, doc_term_matrix, dictionary)
vis
If you are working in jupyter notebook (python vs3.3.0)
"the No module named ‘pyLDAvis.gensim’"
error can be solved using:
import pyLDAvis.gensim_models
instead of:
import pyLDAvis.gensim
Try this
!pip install pyLDAvis
import pyLDAvis.gensim_models
This should work.
I faced the same issue and it worked for me
Please follow the below
import pyLDAvis.gensim_models as gensimvis
pyLDAvis.enable_notebook()
vis = gensimvis.prepare(lda_model, corpus, dictionary)
vis
The pip installation may not agree with Anaconda. It is better to use conda installation.
conda install -c conda-forge pyldavis
Then it should work fine with Anaconda Python.

import osmnx on google colab give me an error

I'm trying to import osmnx on google Colab and it did install successfully using !pip install osmnx but when I try to import it in Colab like this import osmnx give me this error
AttributeError: /usr/bin/python3: undefined symbol: Error_GetLastErrorNum
Does anyone know how to fix this error?
You need to install libspatialindex-dev first.
!apt install libspatialindex-dev
!pip install osmnx
Then you can import it
import osmnx

Module Not found when importing PyCaret in Jupyter

I'm trying to learn PyCaret but having a problem when trying to import it in Jupyter Lab.
I'm working in a virtualenv and installed pycaret via pip:
pip install pycaret
I can confirm its installed via pip list:
prompt-toolkit 3.0.7
protobuf 3.13.0
py 1.9.0
pycaret 2.1.2
pycparser 2.20
The very first line in the notebook is:
from pycaret.nlp import *
however this results in:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-3-7c206b4a9ead> in <module>
----> 1 from pycaret.nlp import *
2 import psycopg2
3 import sys, os
4 import numpy as np
5 import pandas as pd
ModuleNotFoundError: No module named 'pycaret'
I'm pulling my hair out trying to figure this out and can't find anyone else with something similar.
I've tried to import via the python shell as well and that works perfectly.
You should create a seperate environment for installing time series alpha module
after creating a new environment and switching into
pip install pycaret-ts-alpha
and then you will be able to access
https://towardsdatascience.com/announcing-pycarets-new-time-series-module-b6e724d4636c
I forgot that you had to install modules via Jupyter.
Following this guide: http://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/index.html
Installing like so:
# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m pip install numpy
Got it working
First Create a new environment conda documentation
Second Download the Pycaret with this instruction
Third check your sklearn version is greater thansklearn>=0.23.2.
Because if it's greater PyCaret is not compatible with that.
Nothing works for you? Download directly from github with this command
pip install git+https://github.com/pycaret/pycaret.git#egg=pycaret
I read on the tutorial page of pycaret that to install it through a Jupyter-notebook you should add an exclamation mark in front of the python command in the Jupyter-cell:
!pip install pycaret

\_tfq_simulate_ops.so not found while import tensorflow_quantum

Trying import of initial libraries related to tensorflow_quantum:
import tensorflow as tf
import tensorflow_quantum as tfq
import cirq
import sympy
import numpy as np
Getting error in 2nd line:
File "Path_to_anaconda_site_packages\tensorflow_core\python\framework\load_library.py", line 61, in load_op_library
lib_handle = py_tf.TF_LoadLibrary(library_filename)
NotFoundError: Path_To_Tensorflow_Quantum\core\ops\_tfq_simulate_ops.so not found
This is the solution provided by the Tensorflow team, it worked for me.
!pip install tensorflow-gpu==2.1.0
!pip install cirq==0.7.0 pathos==0.2.5 tensorflow-quantum==0.2.0
Do following steps:
!pip uninstall tensorflow
!pip install tensorflow
then restart runtime to import the packages.It works.
This might be something that would be worth raising as an issue on the TensorFlow Quantum Github here (https://github.com/tensorflow/quantum/issues). Without much information on the platform your are using or even what python version you have it might be hard to diagnose the problem here, but a quick fix you could try might be that you have an older version of TensorFlow installed. TensorFlow Quantum requires tf == 2.1.0.
pip install --upgrade pip
pip install --upgrade tensorflow
If that doesn't do it, then it might be worth opening an issue on github and giving a few more details on there :)

Categories

Resources