How to print the full NumPy array without wrapping (in Jupyter Notebook) - python

This question is different from this one: How to print the full NumPy array, without truncation?
In that question, the user wanted to know how to print the full array without truncation. I can print the array without truncation just fine. My problem is that only a small portion of the screen width is used. When trying to inspect large adjacency matrices, it's impossible to inspect them when the rows unnecessarily wrap.
I'm asking this question here because it always takes me hours to find the solution, and I want to disambiguate it from that answer post above.
For example:
import networkx as nx
import numpy as np
np.set_printoptions(threshold=np.inf)
graph = nx.gnm_random_graph(20, 20, 1)
nx.to_numpy_matrix(graph)
This output displays as:

Just gonna post NaN's comment as the answer:
Use np.set_printoptions(linewidth=n) where n has to do with the number of characters (not array elements) per line. So in your case n=100 should do the trick.

Related

Python: Convert Long Numpy Array Into a Short Sequence of Characters?

I have an image that I converted to a numpy array using OpenCV. I want to copy the numpy array's printed output and assign it to a different variable. The issue is the resulting printed out numpy array appears to be thousands of lines long arranged vertically. Below is just a tiny snippet screen shot:
My question is: is there a way I can print out the numpy array so that it prints horizontally instead? Or is there a way to convert my numpy array into a short unique identifier like using bitwise or something.

Converting MATLAB random function to python

My task is to convert one big MATLAB file into python.
There is a line in MATLAB
weightsEI_slow = random('binom',1,0.2,[EneuronNum_slow,IneuronNum_slow]);
I am trying to convert this into python code, I am not quite finding the right documentation. I looked for numpy library too. Does any one have any suggestions?
It looks like you generate a random number that follows the Binomial distribution with probability p=0.2 and sample size n=1. In turn, you can leverage numpy
import numpy as np
np.random.binomial(n=1, p=0.2)
>0
If you require replicability, add np.random.seed(3408) before the number is sampled. Otherwise, the output might be 0 or 1 depending on the execution. Of course, you can switch in another integer value as the seed instead of 3408.

(Python3.x)Splitting arrays and saving them into new arrays

I'm writing a Python script intended to split a big array of numbers into equal sub-arrays. For that purpose, I use Numpy's split method as follows:
test=numpy.array_split(raw,nslices)
where raw is the complete array containing all the values, which are float64-type by the way.
nslices is the number of sub-arrays I want to create from the raw array.
In the script, nslices may vary depending of the size of the raw array, so I would like to "automatically" save each created sub-arrays in a particular array as : resultsarray(i)in a similar way that it can be made in MATLAB/Octave.
I tried to use afor in range loop in Python but I am only able to save the last sub-array in a variable.
What is the correct way to save the sub-array for each each incrementation from 1 to nslices?
Here, the complete code as is it now (I am a Python beginner, please bother the low-level of the script).
import numpy as np
file = open("results.txt", "r")
raw = np.loadtxt(fname=file, delimiter="/n", dtype='float64')
nslices = 3
rawslice = np.array_split(raw,nslices)
for i in range(0,len(rawslice)):
resultsarray=(rawslice[i])
print(rawslice[i])
Thank you very much for your help solving this problem!
First - you screwed up delimiter :)
It should be backslash+n \n instead of /n.
Second - as Serge already mentioned in comment you can just access to split parts by index (resultarray[0] to [2]). But if you really wanted to assign each part to a separate variable you can do this in fommowing way:
result_1_of_3, result_2_of_3, result_3_of_3 = rawslice
print(result_1_of_3, result_2_of_3, result_3_of_3)
But probably it isn't the way you should go.

Python 2.7: Adding all elements of multiple arrays together

I'm reading image files from a list with a variable number of objects.
How can I add the arrays from each file to each other?
Here's an example adding only two arrays:
imageArray= [sum(x,y) for x,y in zip(io.imread(list[1]),io.imread(list[2]))]
I want to extend this to be able to add a variable number of arrays.
I have tried the following, without avail:
for x in filelist:
imageArray = [sum(y) for y in itertools.izip(io.imread(x))]
Which yields the error:
TypeError: Invalid dimensions for image data
Any help would be much appreciated!
Edit
I have been able to read all the images into array using part of unutbu's answer below:
im = map(SNIO.imread,filelist)
From here, I wish to add all the resulting arrays together (element wise). The correct solution would reproduce the result from the following code:
imageArray = [x+y+z for x,y,z in zip(im[0],im[1],im[2])]
which works fine, but, as stated in the original question, I wish to do this with any number of arrays rather than specifying them as in the previous example.
Is io.imread the same as scipy.ndimage.io.imread?
If so, then to add the arrays element-wise, use np.add.reduce:
import numpy as np
import scipy.ndimage.io as SNIO
files = ...
arrs = map(SNIO.imread, files)
result = np.add.reduce(arrs)
This will be far faster than calling Python's sum function for each location in the arrays.

Python SciPy FFT function - Input?

I am currently writing some code which is supposed to perform FFT on a set of data. I have a python list of points and I can easily create a time list. When I run fft(datalist), I get the 'TypeError: 'numpy.ndarray' object is not callable' error. I think (but please correct me) the issue is that the list is one dimension and they have no attachment to time at all by using that one line of code above. My question is, do I have to input a two dimensional array with time and data points? or am I completely wrong and have to rethink?
Thanks, Mike
Edit - forgot to add some code. The t=time. Could it be because the number of entries in the array isnt equal to 2^n where N is an integer?
sample_rate=10.00
t=r_[0:191.6:1/sample_rate]
S = fft([mylist])
print S
The Numpy and SciPy fft functions are looking to have numpy arrays as input, not native python lists. Also they work just fine with lengths that are not powers of two. You probably just need to cast your list as an array before passing it to the fft.
From your example code above try:
from numpy.fftpack import fft
from numpy import array
""" However you generate your list goes here """
S = fft(array([mylist]))

Categories