Deleting elements in a row - python

I want to get an answer for c where c is defined as:
c= x+y
I have the following entry for x and y:
x y
-2 5
-3 10
2 15
4 20
6 25
to get the data, I loaded it from a csv file:
data=np.loadtxt('data.csv',delimiter=',',skiprows=1)
x_value= data[:,0]
y_value= data[:,1]
The code works fine until I decided to remove the negative entry for x. I have:
x_value1= [x for x in x_value if float(x)>0]
It now returns an error. I understand that for it to work, I also have to remove 5 and 10 in y since the entries for the equation are not the same. I don't have any idea how should I do it.

You're probably going to want to ditch the list comprehension for this, since it's a longer operation and it will involve multiple lists. You could just use indices, and copy the non-negative data into new lists:
new_x = []
new_y = []
for i in range(len(x)):
if x[i] >= 0:
new_x.append(x[i])
new_y.append(y[i])
Alternate strategy using zip:
new_x = []
new_y = []
for x_elem, y_elem in zip(x,y):
if x_elem >= 0:
new_x.append(x_elem)
new_y.append(y_elem)
I'm not sure if there's a more condensed way of doing this, but I prefer longer, more readable code anyway.

Use your logic on data instead, checking for negative on the first entry of each 2-tuple:
>>> data
array([[-2, 5],
[-3, 10],
[ 2, 15],
[ 4, 20],
[ 6, 25]])
>>> data2 = n.array([x for x in data if float(x[0])>0]) # x here is each row
>>> data2
array([[ 2, 15],
[ 4, 20],
[ 6, 25]])

Probably you can try something like:
data[data[:,0]>0]
as hinted in the similar answer:filtering lines in a numpy array according to values in a range

Related

getting indices of factors of an np outer product given a value condition

I would like to get the the indices of factors for outer products that has passedd a critera.
beginner here, tried googling and searching SO and tabbing the available np functions for something but did not find anything
python 3.6
x= [2,3]
y= [4,5]
z = np.multiply.outer(x, y)
array([[ 8, 10],
[12, 15]])
for example:
for i in z:
for j in i:
if j >= 10:
Then looking for something that will give (x0y1,x1y0,x1y1) in some form
I think that you look for numpy.nonzero
then you can write:
x_cords, y_cords = numpy.nonzero(z > 10)

Change the first column of the matrix from another specified matrix

I have 2 matrices
x = [[1,2,3],
[4,5,6],
[7,8,9]]
y = [0,2,4]
and i want to change each first element from each row of matrix x using each element from matrix y so the end result would be
x = [[0,2,3],
[2,5,6],
[4,8,9]]
i have tried this code
x = [[1,2,3],[4,5,6],[7,8,9]]
y = [0,2,4]
for i in range (len(x)):
x[i][0] = y[0][i]
print (x)
but it only returns "TypeError: 'int' object is not subscriptable"
are there any ways to fix this and how do you expand this so that it's appliable to any n*n matrix?
Change x[i][0] = y[0][i] to x[i][0] = y[i].
Another way to do this with fewer indices:
x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
y = [0, 2, 4]
for x_row, y_int in zip(x, y):
x_row[0] = y_int
print(x)
You do not have matrices. x is a list of lists and y is a list. They can represent matrices/vectors/etc., but those are ultimately mathematical abstractions, which can be implemented in code in different ways.
The first way to do it, maintaining the structure of your code, requires taking note of the above fact: as y is a list containing ints, y[0][i] will clearly not work, since y[0] will always be an int, and you cannot further apply the subscript operator to ints.
Accordingly, this will work:
for i in range (len(x)):
x[i][0] = y[i]
That said, that is not the only way to do it. If you desired a more functional approach, you could do something like this list comprehension:
[[y_value, *x_value[1:]] for x_value, y_value in zip(x, y)]
This gives the same result, but approaches the problem in a more abstract way: the new list will itself contain lists where the first element comes from y and the rest from x. Understanding this, we can instead compose an inner list following this pattern.
zip creates an iterator of pairs of values from x and y. Using this iterator, each value from y can be positioned before each value from x in a list. Lastly, since x_value is a list, it must be unpacked so we get, for example, [0, 2, 4] instead of [0, [2, 4]].
With Python, you would typically avoid using indexes when possible. Since a "matrix" is actually a list of lists, going through rows returns lists which are object references that you can manipulate directly:
for row,value in zip(x,y): row[0] = value

counting up smaller numbers in python

i am trying to write a code as part of a simulator that will read a list of numbers and compare them to a secondary list of numbers and tell you how many numbers there are that are less then the first number. for example
X=[5,20,14,1,7]
Y=[2,12,9,5,4,6]
the code will take the first X value 5 and see how many of the Y values are less then 5. so the output Z would look something like
Z=[2,6,6,0,4]
i am not very familiar with these concepts at all, i am wondering how i would go about making a function for this type of work. how would i make a for loop that would go through and compare the numbers like that? also is it possible to combine and sort the lists from smallest to largest and then just search that list for the X value and print its position in the list?
Something like:
[len(list(filter(lambda k: k<m, Y))) for m in X]
You can do it using map and list comprehension in one line:
first = [5, 20, 14, 1, 7]
second = [2, 12, 9, 5, 4, 6]
z = map(lambda x: len([y for y in second if x > y]), first)
or without lambda (as #RobertB wrote):
z = [sum([x > y for y in second]) for x in first]
Result is:
[2, 6, 6, 0, 4]
There are many ways to go about the above question. I will explain the easiest method, although it is not the most efficient method
Concept: Nested For Loop
for x in range (0, a1_len-1):
for y in range (0, a2_len -1):
if a[y] < a[x]:
new_array.append(a[y])
print (new_array)
Hope this helps
Another answer using broadcasting with numpy:
import numpy as np
np.apply_along_axis(np.sum,0,np.array(Y).reshape((len(Y),1))<X)

What is this array and can I change it to a list?

So I was testing a code from python which generated an array
x = [[[3,5,10,7]],[[5,4,4,20]],[[9,100,56,7]]]
I do not know what kind of an array this is but for the purpose of my code I need that array to just be a list such as
x = [[3,5,10,7],[5,4,4,20],[9,100,56,7]]
Is there a way to change this? The actually array I have is much bigger having much more indice. I'm just want to see if its possible for the simpler array shown above
#my_actual_array [[[10.65568733, 13.84652805, 14.86591625, ..., 50.24303818, 51.95967865, 46.33126068]], [[12.68396473, 10.31947327, 7.9565444 , ..., 29.54590416, 29.96769142, 29.79670525]] [[28.92797089, 24.25578117, 22.36346626, ..., 55.95978546,56.59334183, 57.22693253]]]
What you're getting is just an list inside a list inside a third list.
You can simplify it to just a list in a list by grabbing the first (and only) element:
>>> x = [[[3,5,10,7]],[[5,4,4,20]],[[9,100,56,7]]]
>>> x2 = [a[0] for a in x]
>>> x2
[[3, 5, 10, 7], [5, 4, 4, 20], [9, 100, 56, 7]]

python numpy set elements of list by condition

I want to set a specific element of my list to specific value with low overhead.
for example if I have this : a = numpy.array([1,2,3,0,4,0]) I want to change every 0 value to 10; in the end I want to have [1, 2, 3, 10, 4, 10]
in Matlab you can do this easily like a(a==0) = 10, is there any equivalent in numpy?
Remarkably similar to Matlab:
>>> a[a == 0] = 10
>>> a
array([ 1, 2, 3, 10, 4, 10])
There's a really nice "NumPy for Matlab Users" guide at the SciPy website.
I should note, this doesn't work on regular Python lists. NumPy arrays are a different datatype that work a lot more like a Matlab matrix than a Python list in terms of access and math operators.
A little more pythonic way would be like this, I guess:
import numpy
a = numpy.array([1,2,3,0,4,0])
for k,v in enumerate(a):
if v == 0:
a[k] = 10
print a
Even more pythonic way (provided by #mtrw)
[10 if k == 0 else k for k in a]

Categories

Resources