I have graphed many things before, but any particular reason this basic two blocks won't plot my function? I can change the function to say, "f = t**2" and it will plot this, but for my specific function that I need, it won't work..
I don't know why it is giving me an error.
Jupyter screenshot:-
I believe it is because the function is expecting a single value, and you passed an array. Maybe try a for loop where each iteration you perform the calculation on the next value in t_span and save the results to an array to be returned.
when posting code, just copy and paste the code in your question and format it, that way it's easier for people to see it and help you.
Related
It's hard to search because I can't even figure out how to refer it.
I used R before, so let me give an example.
Say you did a linear regression.
As you can watch above, lm1 only shows the partial results but using names() function, you can list all the byproducts of the result.
So you can explicitly call it with lm1$coefficients, lm1$residuals, ...
What is the corresponding function in python?
I used inspect.getmembers so far, but maybe expecting some other functions.
You can edit this context in relevant wording, if there exists.
So I am implementing a pathfinding algorithm in python and I want to look how each iteration looks like. So I made it a generator that will yield every intermediate result up until the final result which will end with a return statement.
I made a quick pygame (because I am not aware of python libraries yet, so it was the simplest for me to make a grid and color the cells) program to visualize everything. Every frame makes an iteration on the algorithm and updates a variable result = next(alg). The problem is when the algorithm ends, It still tries to go next. I was wondering if there is a way around this other than catching the stopiteration error. The best for me would be something like if not alg.over() : result = next(alg), but I found nothing on the internet. Is there something like that that I can use? Thank you !
I meet this question when I read about the python script of SCI2(Science of Science)software. Maybe what I want to ask is a simple python question.
I will show the SCI2 script first in the picture below. After that,I will write some codes. My question is why the codes in the picture can get list of all nodes by slicing method [:] but I can't.
codes from SCI2 python script Maybe the picture can not be shown, I write the important codes here. (the language is python)
# make a copy of the list of all nodes
nodesbynumworks = g.nodes[:]
What I wirte is:
import networkx as nx
g =nx.Graph()
g.add_node(1,size=11)
g.add_node(2,size=12)
a = g.nodes[:]
And this is my result:
my result
I just want to get the list of all nodes so that I can take some nodes from this list. And maybe I can change some attributes of some nodes.But I can't do that now. What I can think about is the reason that python2 and python3 are different in some place. I know this a a very pale explanation.
Hope someone can help me.
If you want the list of keys, then try:
g.nodes().keys()
If you want the list of values, then try:
g.nodes().values()
For SCI2, it appears to me that they have made a choice about how to represent graphs. For them graph.nodes is a list (or perhaps it's a numpy array or something similar). Thus graph.nodes[:] is a perfectly well-defined command.
You're using networkx for your graphs. For networkx graph.nodes is something different. In earlier versions of networkx, it is a function that returns a list of nodes, so graph.nodes()[:] will do what you want, but graph.nodes[:] doesn't make since because you're asking for a slice of the function, rather than a slice of the list it returns. In later versions it is a NodeView. I think your code might work in this version. If not, then I'm fairly confident that graph.nodes()[:] still works.
[I don't have the newer version on the computer I'm using right now, so I'm not 100% sure.]
Thank you for everyone.I have used a method to know something.
I used print type(g)and print type(g.nodes)and found something like the picture.
the type of g and g.nodes
So this is created by Jython which combine python and Java and it is not an object of networkx.
Okay,now I can convince myself.
Thanks for everyone again!
So, I currently need to understand the following code properly:
J = p['M'].repeat(p['N'],1).T
p is a dictionary in which the entry under key M is simply an array, the T transposes, that much is clear.
But, the only version I can find for the repeat function is syntax in the form of
numpy.repeat(array , repeats [,axis])
This leaves me wondering what the meaning of a syntax of type array.repeat(something) actually means and I can neither find an answer in my head or the internet for now. This is numpy though, isnt it? It is imported, without being tagged with an 'as' clause.
So currently am on a machine without a python/numpy shell installed to simply try it, so I thought I give this a shot: What is repeated how many times?
My first interpretation would be p['M'] is repeated p['N'] times along the first axis, then transposed, but every example specifying an axis I find uses something like axis=1.
Thanks a lot =)
There is another version of repeat in numpy: numpy.ndarray.repeat
Please see the documentation here
Hope this helps
Quick question, I'm looking for a python function that performs the equivalent job that matlab's imfill.m does. I realize that python has openCV but I have been unable to get that to work properly and am trying to find a substitute for it. The part of imfill that I'm trying to replicate is the 'holes' part of it.
I have a mask that I've generated but I'm trying to fill in all regions that are surrounded by 'land' and leave only the water regions unfilled in.
If this isn't clear enough please let me know and I can try and be more specific. Thank you for your time.
I was able to find a function within scipy that performed similar to what imfill does. It's called binary_fill_holes and it can be found here for anyone that is having the same problem as myself.
Although I can't take full/any real credit for finding it since it was answered here to one of my other questions PIL Plus/imToolkit replacements by unutbu.