I am a Python newbie using Jupyter Notebook, and I am coming across the following problem:
I create 2 very simple lists and assign them to their own respective variables, which works fine.
My code is below:
x = [-2,1,3]
y = [-1,1,2]
I then execute other pieces of simple code, which involves using Matplotlib to plot a graph with values from the lists and also multiplying each element in the lists.
However, I noticed when I try to print the original lists after initialising them, I only get the last element from the list as an integer, not in list form. I find this strange as I haven't made any changes to the lists.
The issue isn't prominent, because I'm still able to continue to use the lists in their original form e.g. to plot the graph, but I'm wondering why this is happening, hoping to strengthen my Python/programming knowledge...
I've added screenshots to display my issue more clearly, but please ask me to explain anything if I haven't made myself clear.
Thanks in advance everyone!!
I think there is an issue in your output of Jupyter, because you pass from the line 93 to 88 and between we don't see the instructions you used.
I might be wrong , but your problem is that you are giving the same name for both the iterator and the sequence in line 85. instead of doing this:
xy = [x*y for x,y in zip(x,y)]
try this:
xy = [i*j for i,j in zip(x,y)]
Related
I'm about to pull my hair out on this. I'm not sure why the index in my array is not being implemented in the second column.
I created this array - project_information :
project_information.append([proj_id,project_text])
When I print this out, I get the rows and columns. It contains about 40 rows.
When I iterate through it to print out the contents, everything comes out fine. I am using this:
for i in range(0,len(project_information)):
project_id = project_information[i][0]
project_text = project_information[i][1]
print(project_id)
print (project_text)
The project_text column contains text, while the project_id contains integers. It prints out perfectly, and the index, changes for both project_id and project_text.
However, I need to use the project_text in a different way, and I am really struggling with this. I need to slice the text to a shorter text for reuse. To do this, I tried:
for i in range(0,len(project_information)):
project_id = project_information[i][0]
project_text = project_information[i][1]
print(project_id)
print (project_text)
if len(project_text) > 5000:
trunc_proj_text = project_text[:1000]
else:
trunc_proj_text = project_text
print (project_id)
print(trunc_proj_text)
The problem I'm having here is that though the project_id column is being iterated through properly, the project_text is not. What I am getting is just the text in the first row for the project_text, sliced, and repeated for as many times as the length of the array.
I have tried different ways, and also a while loop, but it is still not working.
I've also looked at these answers for reference - Slicing,indexing and iterating over 2D Numpy arrays,Efficient iteration over slice in Python, iteration over list slices, and I can't seem to see how they can be applied to my problem.
I'm not well-versed in using Numpy, so is this something that it could help with? I'm well aware this might be simple and I'm missing it because I've been working on various aspects of this project for the past weeks, so I would appreciate a bit of consideration in this.
Thanks in advance.
The problem was with the input list here, so the slicing with this code does in fact work. The code to create the input array has now been fixed. The original code to create the input list was concatenating the strings for each entry, so the project_texts for each appeared different from the end, but all had the same beginning. But viewing this on a console, it was hard to see.
I'm trying to make a program that takes random parts of various images and then adds them back together, but when i then try to access a single array element it comes out as a sequence instead.
def select_from_image(img):
factor=rng.uniform(1/20,1/10)
width=int(np.floor(img.shape[1]*np.sqrt(factor)))
height=int(np.floor(img.shape[0]*np.sqrt(factor)))
x=rng.randint(0,img.shape[1]-1-width)
y=rng.randint(0,img.shape[0]-1-height)
return img[y:y+height-1:,x:x+width-1:]
imgs=[]
for i in range(len(paths)):
imgs.append(ig.imread(paths[i]))
selection=[]
for img in imgs:
selection.append(select_from_image(img))
I've done some testing and deduced that the problem is in "select_from_image(img)" but i just can't put my finger on it.
Here's an example output:
https://imgur.com/a/1Cs8i4J
Any help is welcome!
I found the problem and it wasn't(entirely) the code.
The images i was using weren't monochrome(which meant that each element in the array produced by imread had three values instead of one), so if anyone happens upon a problem like this and get's confused/frustrated read this:
https://brohrer.github.io/convert_rgb_to_grayscale.html
it helped me quite a bit!
I am fairly new to using tensorflow so it is possible there is a very obvious solution to my problem that I am missing. I currently have a 3-dimensional array filled with integer values. the specific values are not important so I have put in a smaller array with filler values for the sake of this question
`Array = tf.constant([[[0,0,1000,0],[3000,3000,3000,3000],[0,2500,0,0]],
[[100,200,300,400],[0,0,0,100],[300,300,400,300]]]).eval()`
So the array looks like this when printed I believe.
`[[[0,0,1000,0],
[3000,3000,3000,3000],
[0,2500,0,0]],
[[100,200,300,400],
[0,0,0,100],
[300,300,400,300]]]`
In reality this array has 23 2-D arrays stacked on top of each other. What I want to do is to create an array or 3 separate arrays that contain the range of values in each row of different levels of the 3-D array.
Something like
`Xrange = tf.constant([Array[0,0,:].range(),Array[1,0,:].range(),Array[2,0,:].range()...,Array[22,0,:].range()])`
Firstly, I am having trouble finding a functioning set of commands strung together using tensorflow that allows me to find the range of the row. I know how to do this easily in numpy but have yet to find any way to do this. Secondly, assuming there is a way to do the above, is there a way to consolidate the code without having to write it out 23 times within one line for each unique row. I know that could simply be done with a for loop, but I would also like to avoid using a solution that requires a loop. Is there a good way to do this, or is more information needed? Also please let me know if I'm screwing up my syntax since I'm still fairly new to both python and tensorflow.
So as I expected, my question has a reasonably simple answer. All that was necessary was to use the tf.reduce_max and tf.reduce_min commands
The code I finally ended with looks like:
Range = tf.subtract(tf.reduce_max(tf.constant(Array),axis=2,keep_dims=True),tf.reduce_min(tf.constant(Array),axis=2,keep_dims=True))
This produced:
[[[1000]
[ 0]
[2500]]
[[ 300]
[ 100]
[ 100]]]
I am running a function developed by Esri to get list of values in a integer column of a spatial table (however, the same behaviour is observed even when running the function on a non-spatial table). According to the help, I should get NumPy structured array. After running the function, I have a numpy array. I run print in this format:
in_table = r"C:\geodb101#server.sde\DataTable" #
data = arcpy.da.TableToNumPyArray(in_table, "Field3")
print data
Which gives me back this in IDE (copy/pasted from IDE interpreter):
[(20130825,) (20130827,) (20130102,)]
I am running:
allvalues = data.tolist()
and getting:
[(20130825,), (20130827,), (20130102,)]
Same result when running data.reshape(len(data)).tolist() as suggested in comments.
Running type() lets me know that in the first case it is <type 'numpy.ndarray'> and in the second case <type 'list'>. I am expecting to get my output list in another format [20130825, 20130827, 20130102]. What am I doing wrong or what else should I do to get the output list in the specified format?
I have a possible approach, but I'm not 100% sure it will work, as I can't figure out how you got tuples into an array (when I tried to create an array of tuples, it looks like the tuples got converted to arrays). In any case, give this a shot:
my_list = map(lambda x: x[0], my_np_array_with_tuples_in_it)
This assumes you're dealing specifically with the single element tuples you describe above. And like I said, when I tried to recreate your circumstances, numpy did some conversion moves that I don't fully understand (not really a numpy expert).
Hope that helps.
Update: Just saw the new edits. Not sure if my answer applies anymore.
Update 2: Glad that worked, here's a bit of elaboration.
Lambda is basically just an inline function, and is a construct common in a lot of languages. It's essentially a temporary, anonymous function. You could have just as easily done something like this:
def my_main_func():
def extract_tuple_value(tup):
return tup[0]
my_list = map(extract_tuple_value, my_np_array_with_tuples_in_it)
But as you can see, the lambda version is more concise. The "x" in my initial example is the equivalent of "tup" in the more verbose example.
Lambda expressions are generally limited to very simple operations, basically one line of logic, which is what is returned (there is no explicit return statement).
Update 3: After chatting with a buddy and doing some research, list comprehension is definitely the way to go (see Python List Comprehension Vs. Map).
From acushner's comment below, you can definitely go with this instead:
my_list = [tup[0] for tup in my_np_array_with_tuples_in_it]
So, I've got a quickhull implementation in python that I'm trying to use in python 3.2. Mostly it's fine, but there's a list indexing problem I'm having. The code does:
axis = sample[:,0]
This doesn't work, as python complains that list indices need to be integers, no tuples. I'm having trouble trying to understand what the line is trying to do. Anyone have any ideas?
Here's some surrounding code, if that helps:
if len(sample) > 2:
axis = sample[:,0]
base = numpy.take(sample, [numpy.argmin(axis), numpy.argmax(axis)], axis=0)
return link(dome(sample, base),
dome(sample, base[::-1]))
else:
return sample
(Additionally, I'm not sure what the base[::-1] means, but that at least works.)
axis is a Python list, but it should be a numpy array. The code is using numpy's special indexing rules for arrays to extract the first column.