How to get graph coordinates (layout) with networkx using graphviz? - python-3.x

I have a graph with 3000 nodes. I am trying to use the pydot layout engine to find a more pleasing layout than the default networkx layout layout = nx.fruchterman_reingold_layout(G)
The example from networkx doc
G_tst = nx.complete_graph(4)
pos = nx.nx_pydot.pydot_layout(G_tst )
pos = nx.nx_pydot.pydot_layout(G_tst , prog='dot')
works just fine. However when I use my own graph
pos = nx.nx_pydot.pydot_layout(G) I get a Type Error where it claims that G has the attibute name more than once.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-72-1326868cc786> in <module>()
1
----> 2 pos = nx.nx_pydot.pydot_layout(G)
3 nx.draw(G, pos=pos)
C:\Anaconda3\lib\site-packages\networkx\drawing\nx_pydot.py in pydot_layout(G, prog, root, **kwds)
261 """
262 import pydotplus
--> 263 P=to_pydot(G)
264 if root is not None :
265 P.set("root",make_str(root))
C:\Anaconda3\lib\site-packages\networkx\drawing\nx_pydot.py in to_pydot(N, strict)
200 for n,nodedata in N.nodes_iter(data=True):
201 str_nodedata=dict((k,make_str(v)) for k,v in nodedata.items())
--> 202 p=pydotplus.Node(make_str(n),**str_nodedata)
203 P.add_node(p)
204
TypeError: __init__() got multiple values for argument 'name'
Here are the node attributes I do have:
`G.add_node(G.number_of_nodes(),
name=endNode.endWord, # string
teaching_text=endNode.tt_corpus, # string
definition=endNode.domainDef, # string
search_string=endNode.searchKey_obj.search_key_str,
name_len = len(endNode.endWord))` #int

I got the same error yesterday. I'm not 100 percent sure but it seems some internal variables conflict with your attribute "name". In my case, I change it to "name_" then it works.

Related

Error while replicating Keras-LSTM example for SHAP-based interpretability

I am trying to replicate Keras-LSTM DeepExplainer example. I am getting the following error when trying to compute the shap values:
This warning: keras is no longer supported, please use tf.keras instead.
Your TensorFlow version is newer than 2.4.0 and so graph support has been removed in eager mode and some static graphs may not be supported. See PR #1483 for discussion.
And this error:
TypeError Traceback (most recent call last)
in
1 import shap
2 explainer = shap.DeepExplainer(model, x_train[:100])
----> 3 shap_values = explainer.shap_values(x_test[:10])
~/miniconda3/envs/mtq/lib/python3.8/site-packages/shap/explainers/_deep/init.py
in shap_values(self, X, ranked_outputs, output_rank_order,
check_additivity)
122 were chosen as "top".
123 """
--> 124 return self.explainer.shap_values(X, ranked_outputs, output_rank_order, check_additivity=check_additivity)
~/miniconda3/envs/mtq/lib/python3.8/site-packages/shap/explainers/_deep/deep_tf.py
in shap_values(self, X, ranked_outputs, output_rank_order,
check_additivity)
306 # run attribution computation graph
307 feature_ind = model_output_ranks[j,i]
--> 308 sample_phis = self.run(self.phi_symbolic(feature_ind), self.model_inputs,
joint_input) 309
310 # assign the attributions to the right part of the output arrays
~/miniconda3/envs/mtq/lib/python3.8/site-packages/shap/explainers/_deep/deep_tf.py
in run(self, out, model_inputs, X)
363
364 return final_out
--> 365 return self.execute_with_overridden_gradients(anon)
366
367 def custom_grad(self, op, *grads):
~/miniconda3/envs/mtq/lib/python3.8/site-packages/shap/explainers/_deep/deep_tf.py
in execute_with_overridden_gradients(self, f)
399 # define the computation graph for the attribution values using a custom gradient-like computation
400 try:
--> 401 out = f()
402 finally:
403 # reinstate the backpropagatable check
~/miniconda3/envs/mtq/lib/python3.8/site-packages/shap/explainers/_deep/deep_tf.py
in anon()
356 shape = list(self.model_inputs[i].shape)
357 shape[0] = -1
--> 358 data = X[i].reshape(shape)
359 v = tf.constant(data, dtype=self.model_inputs[i].dtype)
360 inputs.append(v)
TypeError: 'NoneType' object cannot be interpreted as an integer
I have checked out the PR#1483, but couldn't find a relevant fix there. Please suggest on what tensorflow, keras, and shap versions are needed to successfully replicate the example.

Python / Bokeh / Pandas AttributeError: unexpected attribute 'responsive' to Figure

I'm trying to use bokeh and pandas to create a graph. If ", responsive = True" is not included, the code works. If it is included, it doesn't work. Any suggestions for what I'm doing wrong? Thanks!
from bokeh.plotting import Figure, output_file, show
import pandas
file="adbe.csv"
df=pandas.read_csv(file, parse_dates=["Date"])
p=figure(width=500, height=500, x_axis_type="datetime", responsive = True)
p.line(df["Date"],df["Close"],color="Orange", alpha=0.5)
output_file("Time_Series.html")
show(p)
Here's the full error message:
AttributeError Traceback (most recent call last)
in
6 df=pandas.read_csv(file, parse_dates=["Date"])
7
----> 8 p=figure(width=500, height=500, x_axis_type="datetime", responsive = True)
9
10 p.line(df["Date"],df["Close"],color="Orange", alpha=0.5)
c:\users\jason\appdata\local\programs\python\python37\lib\site-packages\bokeh\plotting\figure.py in figure(**kwargs)
1530
1531 def figure(**kwargs):
-> 1532 return Figure(**kwargs)
1533 figure.__doc__ = Figure.__doc__
1534
c:\users\jason\appdata\local\programs\python\python37\lib\site-packages\bokeh\plotting\figure.py in __init__(self, *arg, **kw)
163 kw['title'] = Title(text=title)
164
--> 165 super().__init__(*arg, **kw)
166
167 self.x_range = get_range(opts.x_range)
c:\users\jason\appdata\local\programs\python\python37\lib\site-packages\bokeh\model.py in __init__(self, **kwargs)
232 kwargs.pop("id", None)
233
--> 234 super().__init__(**kwargs)
235 default_theme.apply_to_model(self)
236
c:\users\jason\appdata\local\programs\python\python37\lib\site-packages\bokeh\core\has_props.py in __init__(self, **properties)
245
246 for name, value in properties.items():
--> 247 setattr(self, name, value)
248
249 def __setattr__(self, name, value):
c:\users\jason\appdata\local\programs\python\python37\lib\site-packages\bokeh\core\has_props.py in __setattr__(self, name, value)
280
281 raise AttributeError("unexpected attribute '%s' to %s, %s attributes are %s" %
--> 282 (name, self.__class__.__name__, text, nice_join(matches)))
283
284 def __str__(self):
AttributeError: unexpected attribute 'responsive' to Figure, possible attributes are above, align, aspect_ratio, aspect_scale, background, background_fill_alpha, background_fill_color, below, border_fill_alpha, border_fill_color, center, css_classes, disabled, extra_x_ranges, extra_y_ranges, frame_height, frame_width, height, height_policy, hidpi, inner_height, inner_width, js_event_callbacks, js_property_callbacks, left, lod_factor, lod_interval, lod_threshold, lod_timeout, margin, match_aspect, max_height, max_width, min_border, min_border_bottom, min_border_left, min_border_right, min_border_top, min_height, min_width, name, outer_height, outer_width, outline_line_alpha, outline_line_cap, outline_line_color, outline_line_dash, outline_line_dash_offset, outline_line_join, outline_line_width, output_backend, plot_height, plot_width, renderers, reset_policy, right, sizing_mode, subscribed_events, tags, title, title_location, toolbar, toolbar_location, toolbar_sticky, visible, width, width_policy, x_range, x_scale, y_range or y_scale
The responsive parameter was removed several years ago. (https://docs.bokeh.org/en/dev-3.0/docs/releases.html)
Deprecations Removed:
The following functions, arguments, or features have were previously deprecated with warnings and instructions for new usage, and have now been permanently removed:
--host argument to bokeh serve
responsive argument to plots and layout functions
Plot.toolbar_sticky property
Tool.plot property
bokeh.sampledata.population.load_population function
bokeh.util.notebook module
In modern versions of Bokeh you can use sizing_mode instead, or you can try deleting it from your code. (https://docs.bokeh.org/en/latest/docs/user_guide/layout.html?highlight=sizing_mode#sizing-mode)
Like the error said, Figure has not attribute 'responsive', so, don't use it.
You can check the documentation
Are you doing Ardit Sulce's tutorial? Because I stumbled upoh the same issue. I looked that up and found more explanation on this at https://github.com/bokeh/bokeh/issues/4620
I managed to get the similar output to responsive = True by using sizing_mode = "scale_width"
I hope this helps others who stumbled upon the same issue while following the tutorial.

Loading existing MODFLOW-USG model w/ Voronoi Mesh in FloPy

I am trying to load an existing MODFLOW-USG model with FloPy (Windows environment). The model has a Voronoi mesh, and this seems to trip the "load" function:
m1=flopy.modflow.Modflow.load(model_name+".nam",model_ws=model_dir,verbose=True,check=False,exe_name="mfusg.exe",version='mfusg')
I get the following error, which appears to relate to the fact that FloPy is expecting a structured grid with rows and columns:
TypeError Traceback (most recent call last)
<ipython-input-33-62420c415719> in <module>
6 head_file = os.path.join(model_dir,model_name+'.hds')
7 print(head_file)
----> 8 m1=flopy.modflow.Modflow.load(model_name+".nam",model_ws=model_dir,verbose=True,check=False,exe_name="mfusg.exe",version='mfusg')
9 headobj = bf.HeadUFile(head_file,verbose=True,text='HEADU')
10 headobj.list_records()
~\Anaconda3\lib\site-packages\flopy\modflow\mf.py in load(f, version, exe_name, verbose, model_ws, load_only, forgive, check)
797 item.package.load(item.filehandle, ml,
798 ext_unit_dict=ext_unit_dict,
--> 799 check=False)
800 else:
801 item.package.load(item.filehandle, ml,
~\Anaconda3\lib\site-packages\flopy\modflow\mfrch.py in load(f, model, nper, ext_unit_dict, check)
408 print(txt)
409 t = Util2d.load(f, model, (nrow, ncol), np.float32, 'rech',
--> 410 ext_unit_dict)
411 else:
412 parm_dict = {}
~\Anaconda3\lib\site-packages\flopy\utils\util_array.py in load(f_handle, model, shape, dtype, name, ext_unit_dict, array_free_format, array_format)
2699
2700 elif cr_dict['type'] == 'internal':
-> 2701 data = Util2d.load_txt(shape, f_handle, dtype, cr_dict['fmtin'])
2702 u2d = Util2d(model, shape, dtype, data, name=name,
2703 iprn=cr_dict['iprn'], fmtin="(FREE)",
~\Anaconda3\lib\site-packages\flopy\utils\util_array.py in load_txt(shape, file_in, dtype, fmtin)
2376 elif len(shape) == 2:
2377 nrow, ncol = shape
-> 2378 num_items = nrow * ncol
2379 else:
2380 raise ValueError(
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'
I could not find any documentation or Jupyter notebooks with examples of loading an existing model with Voronoi mesh, only creating new triangular meshes or structured / local-grid-refined grids.
Try the code with forgive = True.
m1=flopy.modflow.Modflow.load(model_name+".nam",model_ws=model_dir,verbose=True,check=False,exe_name="mfusg.exe",version='mfusg', forgive = True)

How can I make a list of three sentences to a string?

I have a target word and the left and right context that I have to join together. I am using pandas and I try to join the sentences, and the target word, together into a list, which I can then turn into a string so that it would work with my vectorizer. Basically I am just trying to turn a list of three sentences to a string.
This is the error that I get:
AttributeError Traceback (most recent call last)
<ipython-input-195-ae09731d3572> in <module>()
3
4 vectorizer=CountVectorizer(max_features=100000,binary=True,ngram_range=(1,2))
----> 5 feature_matrix=vectorizer.fit_transform(trainTexts)
6 print("shape=",feature_matrix.shape)
3 frames
/usr/local/lib/python3.6/dist-packages/sklearn/feature_extraction/text.py in _preprocess(doc, accent_function, lower)
66 """
67 if lower:
---> 68 doc = doc.lower()
69 if accent_function is not None:
70 doc = accent_function(doc)
AttributeError: 'list' object has no attribute 'lower'
I have tried using .joinand .split but they are not working for me so I am doing something wrong.
import sys
import csv
import random
csv.field_size_limit(sys.maxsize)
trainLabels = []
trainTexts = []
with open ("myTsvFile.tsv") as train:
trainData = [row for row in csv.reader(train, delimiter='\t')]
random.shuffle(trainData)
for example in trainData:
trainLabels.append(example[1])
trainTexts.append(example[3:6])
The indexes example[3:6] means that the 3 is left context 4 is target word and 5 right context.
print('Text:', trainTexts[3])
print('Label:', trainLabels[1])
edited the few printed lines from the code:
['Visa electron käy aika monessa paikassa luottokortista . Mukaanlukien ', 'Paypal', ' , mikä avaa taas lisää ovia .']
['Nyt pistän pääni pölkyllä : ', 'WinForms', ' on ihan ok .']

Don't understand error message (basic sklearn command)

I'm new to Python and programming in general and I wanted to exercise a littlebit with linear regression in one variable.
Im currently following this tutorial in the link
https://www.youtube.com/watch?v=8jazNUpO3lQ&list=PLeo1K3hjS3uvCeTYTeyfe0-rN5r8zn9rw&index=2
and I am exactly doing what he is doing.
I did however encounter an error when compiling as shown in the code below
(for simplicity, I put '--' to places which is the output. I used Jupyter Notebook)
At the end I encounterd a long list of errors when trying to compile 'reg.predict(3300)'.
I don't understand what went wrong.
Can someone help me out?
Cheers!
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
df = pd.read_csv("homeprices.csv")
df
--area price
0 2600 550000
1 3000 565000
2 3200 610000
3 3600 680000
4 4000 725000
%matplotlib inline
plt.xlabel('area(sqr ft)')
plt.ylabel('price(US$)')
plt.scatter(df.area, df.price, color='red', marker = '+')
--<matplotlib.collections.PathCollection at 0x2e823ce66a0>
reg = linear_model.LinearRegression()
reg.fit(df[['area']],df.price)
--LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None,
normalize=False)
reg.predict(3300)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-16-ad5a8409ff75> in <module>
----> 1 reg.predict(3300)
~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in predict(self, X)
211 Returns predicted values.
212 """
--> 213 return self._decision_function(X)
214
215 _preprocess_data = staticmethod(_preprocess_data)
~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in _decision_function(self, X)
194 check_is_fitted(self, "coef_")
195
--> 196 X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
197 return safe_sparse_dot(X, self.coef_.T,
198 dense_output=True) + self.intercept_
~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
543 "Reshape your data either using array.reshape(-1, 1) if "
544 "your data has a single feature or array.reshape(1, -1) "
--> 545 "if it contains a single sample.".format(array))
546 # If input is 1D raise error
547 if array.ndim == 1:
ValueError: Expected 2D array, got scalar array instead:
array=3300.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
Try reg.predict([[3300]]). The api used to allow scalar value but now you need to give 2D array
reg.fit(df[['area']],df.price)
I think above we are using 2 variables, so using 2D array to fit [X]. we need to use 2D array in reg.predict for [X],too. Hence,
reg.predict([[3300]])
Expected 2D array,got scalar array instead: this is written in the error explained box so
kindly change it to :
just wrote it like this
reg.predict([[3300]])

Resources