How to convert list of array having two elements? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have input like this:
y=[[array([ 0.12984648, 0.02116148, 0.08041889, ..., -0.11139846,
-0.0893152 , -0.05336994]), 1], [array([-0.11865588, -0.16726171, -0.06753636, ..., 0.00991138,
-0.11180532, -0.01146698]), 0] ]
I want to convert it into:
y=[
[[ 0.12984648, 0.02116148, 0.08041889, ..., -0.11139846,
-0.0893152 , -0.05336994], 1], [[-0.11865588, -0.16726171, -0.06753636, ..., 0.00991138,
-0.11180532, -0.01146698], 0]
]

You cannot have non-rectangular arrays. So your only option is to use lists and here is how you convert it to lists:
y = [[i[0].tolist(),i[1]] for i in y]
output:
[[[0.12984648, 0.02116148, 0.08041889, ..., -0.11139846, -0.0893152, -0.05336994], 1],
[[-0.11865588, -0.16726171, -0.06753636, ..., 0.00991138, -0.11180532, -0.01146698], 0]]

Related

Multiplying only naturally adjacent integers in a list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a list:
l = [1,2,3,4,6,7,9,10]
I want to multiply only adjacent numbers whose difference is 1 to get a final list.
The process in this example would be:
[1*2*3*4, 6*7, 9*10]
[24, 42, 90]
Convert to an array then split after taking the np.diff then use np.prod:
l = [1,2,3,4,6,7,9,10]
a = np.array(l)
outlist = [*map(np.prod,np.split(a,np.where(np.diff(a)!=1)[0]+1))]
print(outlist)
#[24, 42, 90]

intersection of numpy multidimensional array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
a=np.array([[[[0,0],[0,1]],[[1,1],[1,1]]],
[[[1,0],[1,1]],[[0,1],[1,1]]]])
how can I get the intersection of this array?
This is the expected output:
array([[[0, 0],
[0, 1]],
[[0, 1],
[1, 1]]]
For case provided you can use
a[0] & a[1]
or, alternatively:
np.logical_and(a[0], a[1]).astype(int)
In general, if length of a is not defined, you can use:
np.logical_and.reduce(a).astype(int)

What is the use of numpy.c_ in this code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
what does np.c means in this code. Learning it from Udemy
df_cancer = pd.DataFrame(np.c_[cancer['data'], cancer['target'], columns=np.append(cancer ['feature_names'],['target;]))
According to official NumPy documentation,
numpy.c_ translates slice objects to concatenation along the second axis.
Example 1:
>>> np.c_[np.array([1,2,3]), np.array([4,5,6])]
array([[1, 4],
[2, 5],
[3, 6]])
Example 2:
>>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
array([[1, 2, 3, 0, 0, 4, 5, 6]])

How to obtain the index of the certain data type in a list? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a list containing string, int, and float data.
For example:
a = ['a', 'b', 1, 2, 3.5, 4.6]
I want to have the float data index such as [4,5] from the example above.
How can I do that?
Keep it simple:
[i for i, x in enumerate(a) if isinstance(x, float)]
You could find them using a list comprehension:
[i for i in range(len(a)) if isinstance(a[i], float)]
Result:
[4, 5]
Use list comprehensions to solve this -
index_float = [i for i in range(len(a)) if type(a[i])==float]
print(index_float)
[4, 5]
Depending upon the index of which datatype you want, you can equate it to type()

How to create an array from two others that will contain common pairs (sequences) of numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
a = [1,2,3,4,5,6,8]
b = [6,8,9,4,5,3,2,1]
final result should be
c = [6,8,4,5]
This array contains the same pair of numbers in both arrays - how to write this kind of code in python?
I only known how to create an array with duplicated values
a = [1,2,3,4,5,6,8]
b = [6,8,9,4,5,3,2,1]
c = [x for x in a if x in b]
print (c)
>>> [e for t in [t for t in zip(b,b[1:]) if t in zip(a,a[1:])] for e in t]
[6, 8, 4, 5]

Categories

Resources