I get the error
NameError: name 'girvan_newman' is not defined
when try to use this algorithm in networkx 2.0.
Could someone help to fix this problem?
davedwards is right , you need to provide the missing import statements:
import networkx as nx
from networkx.algorithms import community
communities_generator = community.girvan_newman(G)
Check https://networkx.github.io/documentation/latest/reference/algorithms/community.html
Related
I tried to generate random probs by using the following line code:
probs = [np.clip(random.normalvariate(0.1, 0.05), 0, 1) for x in range(1000)]
Unexpectedly I faced the following error message:
AttributeError: module 'numpy.random' has no attribute 'normalvariate'
Any idea how to solve this? I checked out the docs I find that this attribute exists in the numpy.random however it doesn't work when I used it in above code.
Any help to fix this issue will be appreciated.
It seems that you make confusion between random module whose documenttion is : https://docs.python.org/3.11/library/random.html
And random sub-module that belongs to numpy, its documentation can be found here https://numpy.org/doc/stable/reference/random/index.html
Error origin
It seems that you imported numpy.random and you tried to use normalvariate while the latter function belongs to random module.
Solution
So to solve the issue write the following import:
import random
probs = [np.clip(random.normalvariate(0.1, 0.05), 0, 1) for x in range(1000)]
Output:
[0.10399310517618868,
0.10416076922742254,
0.10683877729386676,
0.14789317007499886,
0.11551976284566698,
...
I was trying to replicate this code for stat forecasting in python, I came across the issue of not being able to load this model 'adida' form statsforecast library,
Here is the link for reference : https://towardsdatascience.com/time-series-forecasting-with-statistical-models-f08dcd1d24d1
import random
from itertools import product
from IPython.display import display, Markdown
from multiprocessing import cpu_count
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from nixtlats.data.datasets.m4 import M4, M4Info
from statsforecast import StatsForecast
from statsforecast.models import (
adida,
croston_classic,
croston_sba,
croston_optimized,
historic_average,
imapa,
naive,
random_walk_with_drift,
seasonal_exponential_smoothing,
seasonal_naive,
seasonal_window_average,
ses,
tsb,
window_average
)
Attached is the error message, Can you please have a look at this and let me know why is there an issue in importing this?
Given below is the error image:
I did some research and figured out the issue is probably with the version, try installing this specific version of statsforecast
pip install statsforecasts==0.6.0
Trying loading these models after that, hopefully this should work.
As of v1.0.0 of StatsForecast, the API changed to be more like sklearn, using classes instead of functions. You can find an example of the new syntax here: https://nixtla.github.io/statsforecast/examples/IntermittentData.html.
The new code would be
from statsforecast import StatsForecast
from statsforecast.models import ADIDA, IMAPA
model = StatsForecast(df=Y_train_df, # your data
models=[ADIDA(), IMAPA()],
freq=freq, # frequency of your data
n_jobs=-1)
If you want to use the old syntax, setting the version as suggested should work.
If you have updated the package ..use ADIDA it will work
see the model list name with new packages
ADIDA(),
IMAPA(),
(SimpleExponentialSmoothing(0.1)),
(TSB(0.3,0.2)),
(WindowAverage( 6))
I'm getting this error "name 'ImageDataBunch' is not defined" and am not able to find a solution on the Internet.
The full code can be found here: https://dzlab.github.io/jekyll/update/2018/11/13/audio-classification/.
np.random.seed(42)
data = ImageDataBunch.from_lists(path, fnames, labels, ds_tfms=None, size=224, bs=bs)
data.normalize(imagenet_stats)
data.show_batch(rows=5, figsize=(8,8))
I'm running this code on Google Colab and Python 3.6.
You just need to import the ImageDataBunch class from the fast.ai library. Here's the docs for it. Just add the following to the top of your code to import the entirety of the vision library classes:
from fastai.vision import *
Thanks at first for actually taking the time to help me :-)
I want to create a undirected graph from waterways for example from the canals of Venice or Amsterdam.
OpenStreetMaps has such a graph of a waterway network of these cities, but the OSMnx package does not have such a filter to filter the waterways (or maybe I don't know it yet ;-) ).
import osmnx as ox
G = ox.graph_from_bbox(52.36309012572587,4.890326718121118,52.36590601699889,4.898316757037793, network_type='all')
G_projected = ox.project_graph(G)
ox.plot_graph(G_projected)
I thought I was possible to just download the whole map, and then filter on just the waterway network. But I don't know how to go further about this. OSMnx would be the best as I then immediately have a graph which I can use for functions of networksx such as Dijkstra's shortest path.
Another way I was thinking of was overpass package:
import overpass
import networkx as nx
import matplotlib.pyplot as plt
api= overpass.API()
data = api.get('way(52.36309012572587,4.890326718121118,52.36590601699889,4.898316757037793);(._;>;)', verbosity='geom')
[f for f in data.features if f.geometry['type'] == "LineString"]
But this still doesn't work, because I haven't figured out how to filter the data and transform it to a graph for so that networkx can use it.
Hope you guys (and girls :-) ) can help me, because I have no clue how to go further.
Kind regards,
Jeroen
You can use OSMnx to get other types of infrastructure such as canals, railways, powerlines, etc, as described in this example, using the custom_filter parameter:
import osmnx as ox
ox.config(use_cache=True, log_console=True)
G = ox.graph_from_place('Amsterdam', retain_all=False, truncate_by_edge=False,
simplify=True, custom_filter='["waterway"~"canal"]')
fig, ax = ox.plot_graph(G)
I am starting to use this interface now, I have some experience with Python but nothing extensive. I am calculating the transitivity and community structure of a small graph:
import networkx as nx
G = nx.read_edgelist(data, delimiter='-', nodetype=str)
nx.transitivity(G)
#find modularity
part = best_partition(G)
modularity(part, G)
I get the transitivity just fine, however - there is the following error with calculating modularity.
NameError: name 'best_partition' is not defined
I just followed the documentation provided by the networkx site, is there something I am doing wrong?
As far as I can tell best_partition isn't part of networkx. It looks like you want to use https://sites.google.com/site/findcommunities/ which you can install from https://bitbucket.org/taynaud/python-louvain/src
Once you've installed community try this code:
import networkx as nx
import community
import matplotlib.pyplot as plt
G = nx.random_graphs.powerlaw_cluster_graph(300, 1, .4)
nx.transitivity(G)
#find modularity
part = community.best_partition(G)
mod = community.modularity(part,G)
#plot, color nodes using community structure
values = [part.get(node) for node in G.nodes()]
nx.draw_spring(G, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=False)
plt.show()
edit: How I installed the community detection library
ryan#palms ~/D/taynaud-python-louvain-147f09737714> pwd
/home/ryan/Downloads/taynaud-python-louvain-147f09737714
ryan#palms ~/D/taynaud-python-louvain-147f09737714> sudo python3 setup.py install
I just met the same error NameError: name 'best_partition' is not defined when using this example code.
This error occurs because I named my python file as networkx.py, then when we execute this program
import networkx as nx
This program may import the networkx we defined instead of the library. In the program, best_partition is not defined. So this error occur.
Having same name with library is not appropriate. Maybe you should check this!