Numpy reapeat array attached to array?! what is the syntax here? - python

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

Related

Sympy pretty print matrix

I am using sympy to do symbolic matrix multiplication of 13 2x2 matrices (for optics). The resulting matrix is of course a 2x2 matrix but is huge.
I am using pprint() in order to display stuff in a nice manner.
Problem is that pprint is basically "splitting" the matrix over many rows making it basically unreadable. To put things into perspective, below is the first element of the matrix as it is pretty printed, so imagine how the whole thing is going to look like.
Any tips, tricks to pretty print the matrix in a continuous way?
Many thanks,
P.S; I am using jupyter notebook
This is probably a little late. After over an hour searching for this tiny problem, I finally found a fix: As stated in their internal documentation for pretty_print (pprint is essentially a wrapper for this):
num_columns : int or None, optional (default=None)
Number of columns before line breaking (default to None which reads
the terminal width), useful when using SymPy without terminal.
I would recommend setting the limit to something you will never exceed, e.g. 10,000 or even 100,000. This at least worked for me:
pprint(expression, num_columns=10_000)

Python - graph a basic function

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.

About the list of all nodes in graph(python)

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!

Using linalg.block_diag for variable number of blocks

So I have a code that generates various matrices. These matrices need to be stored in a block diagonal matrix. This should be fairly simply as I can use scipy's:
scipy.linalg.block_diag(*arrs)
However the problem I have is I don't know how many matrices will need to be stored like this. I want to keep things as simply as possible (naturally). I thought of doing something like:
scipy.linalg.block_diag( matrix_list[ii] for ii in range(len(matrix_list)) )
But this doesn't work. I can think of a few other ways to do it... but they all become quite convoluted for something I feel should be much simpler.
Does anyone have an idea (or know) a simple way of carrying this out?
Thanks in advance!
When you do:
scipy.linalg.block_diag( matrix_list[ii] for ii in range(len(matrix_list)) )
you're passing a generator expression to block_diag, which is not the way to use it.
Instead, use the * opertor, for expanding the argument list in the function call, like:
scipy.linalg.block_diag(*matrix_list)

Matlab function equivalent for Python (Flood Fill)

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.

Categories

Resources