How python List and Numpy array works? - arrays

How array works differently from a python list?
a=[0,1,2,3,4,5,6,7,8,9]
b=2
a==b
gives
False
But
a=np.array([0,1,2,3,4,5,6,7,8,9])
b=2
a==b
gives
array([False, False, True, False, False, False, False, False, False, False])

This happens because the __eq__ method is defined differently on numpy arrays comparing to default python lists.
Numpy was designed for various proposes, mainly for data science usage, which makes this method definition a very useful (and very fast) choice.
In other words, np.array and lists are different animals. Using each depends on what you're trying to achieve, although for some proposes it doesn't vary much, as they share some similarities.

First, you don't need to add any ; in python at the end of your line.
Then,
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = 2
a == b
will check if a is equal to b, meaning that it's exactly the same (same type and same content for a list here).
You can use:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = 2
b in a # True if yes, false if not
for example. There is several methods in python (which you can read here )
With numpy array it's a bit diffrent because the == for a np.array and a int/float will check if the number you want is a value of the array and will give you the result for each element of it. As mentionned by Kevin (in comment) it's called broadcasting.
This will perform this calculation :
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = 2
a == b
result_list = []
for value in a:
result_list.append(b == value)
print(result_list)
which can be more interessant in some case. Don't forget that numpy, because it's written in Cython, is faster than what I wrote here (especially for large arrays/lists)

Numpy returns the equality element-wise, see here.
Python checks the equality element-wise: if it finds that two elements are different (or the lengths are different) it returns false, otherwise true. See here paragraph 5.8.

Related

Two if statements in a for loop?

class Solution:
def transformArray(self, arr: List[int]) -> List[int]:
x=arr
while True:
f=True
for i in range(1,len(arr)-1):
if arr[i-1]<arr[i] and arr[i]>arr[i+1]:
f=False
x[i]=x[i]-1
print(x[i])
if arr[i-1]>arr[i] and arr[i]<arr[i+1]:
f=False
x[i]=x[i]+1
print(x[i])
#print(x)
x=arr
if f==True:
break
return x
In the above code both the if statements don't execute , only the second one does. I have tried using elif but it still doesn't work. What am i missing here?
For your code, I considered two types of examples as input for the array list
For example 1, when the arr = [1, 2, 1, 4], the 2nd element is bigger than 1st and 3rd
The first if statement (if arr[i-1]<arr[i] and arr[i]>arr[i+1]:) is working, because both the conditions are met and it gives the output x = [1, 1, 1, 4]
In example 2, when the arr = [3, 2, 3, 4], the 2nd element is smaller than the 1st and 3rd
The second if statement (if arr[i-1]>arr[i] and arr[i]<arr[i+1]:) is working, because both the conditions are met and it gives the output x = [3, 3, 3, 4]
So, the working of if statements largely depends on the elements in the array. Both the if statements' purpose is totally opposite. If one satisfies the condition the other will not.
Hope my answer provides some clarification.

How to map a numpy array to a differently sized array based on boolean values with the rest as zeros

This is a horribly worded question but not sure how to describe it without examples. I feel this should be really simple but have tried lots of numpy functions and not got the desired result.
Say I had two arrays:
a = np.array([7, 3])
b = np.array([True, False, True, False])
How could I combine these using numpy methods to get the resultant array c:
c = np.array([7, 0, 3, 0])
Obviously this is an overly simplified example but, in theory, my arrays could be very large.
In [283]: a = np.array([7, 3])
...: b = np.array([True, False, True, False])
What the comments were trying to get you to try and or show is indexing operations like this:
In [284]: c = np.zeros(b.shape, int)
In [285]: c[b] = a
In [286]: c
Out[286]: array([7, 0, 3, 0])
The boolean mask applied to c produces array of the selected values:
In [287]: c[b]
Out[287]: array([7, 3])

Does ruby support an enumerable map_cons method or its equivalent?

Ruby has a handy function for enumerables called each_cons. Which "Iterates the given block for each array of consecutive elements." This is really nice. Except that this is definitely an each method, which returns nil upon completion and not an array of the values you've looped over like map would.
However, if I have a situation where I need to iterate over an enumerable type, take an element and its cons, then perform some operation on them and return them back into an array what can I do? Normally, I'd use map for this sort of behavior. But map_cons doesn't exist.
An example:
Given a list of integers, I need to see which ones of those integers repeat and return a list of just those integers
[1, 1, 4, 5, 6, 2, 2] ## I need some function that will get me [1, 2]
I can say:
[1, 1, 4, 5, 6, 2, 2].each_cons(2) {|e| e[0] if e[0] == e[1]}
But, since it eachs over the array, it will complete successfully and return nil at the end. I need it to behave like map and not like each.
Is this behavior something that ruby supports? Am I coming at it from the wrong direction entirely?
The documentation of each_cons ends with this innocent phrase: "If no block is given, returns an enumerator." Most methods of Enumerable do this. What can you do with an Enumerator? Nothing truly impressive . But Enumerators include Enumerable, which does provide a large amount of powerful methods, map being one of them. So, as Stefan Pochmann does:
[1, 1, 4, 5, 6, 2, 2].each_cons(2).map { |e| e[0] if e[0] == e[1] }
each_consis called without a block, so it returns an Enumerator. mapis simply one of its methods.
Just add map?
[1, 1, 4, 5, 6, 2, 2].each_cons(2).map { |e| e[0] if e[0] == e[1] }
=> [1, nil, nil, nil, nil, 2]
Ruby 2.7 added the method filter_map which makes this even easier:
p [1, 1, 4, 5, 6, 2, 2].each_cons(2).filter_map{|a,b| a if a == b} # => [1, 2]

How to handle large files in python?

I am new in python. I have asked another question How to arrange three lists in such a way that the sum of corresponding elements if greater then appear first? Now the problem is following:
I am working with a large text file, in which there are 419040 rows and 6 columns containing floats. Among them I am taking first 3 columns to generate those three lists. So the lists I am actually working with has 419040 entries in each. While I was running the python code to extract the three columns into three lists the python shell was not responding, I suspected the large number of entries for this, I used this code:
file=open("file_location","r")
a=[]
b=[]
c=[]
for lines in file:
x=lines.split(" ")
a.append(float(x[0]))
b.append(float(x[1]))
c.append(float(x[2]))
Note: for small file this code was running perfectly.
To avoid this problem I am using the following code:
import numpy as np
a = []
b = []
c = []
a,b,c = np.genfromtxt('file_location',usecols = [0,1,2], unpack=True)
So when I am running the code given in answers to my previous question the same problem is happening. So what will be the corresponding code using numpy? Or, any other solutions?
If you're going to use numpy, then I suggest using ndarrays, rather than lists. You can use loadtxt since you don't have to handle missing data. I assume it'll be faster.
a = np.loadtxt('file.txt', usecols=(0, 1, 2))
a is now a two-dimensional array, stored as an np.ndarray datatype. It should look like:
>>> a
array([[ 1, 20, 400],
[ 5, 30, 500],
[ 3, 50, 100],
[ 2, 40, 300],
[ 4, 10, 200]])
However, you now need to re-do what you did in the previous question, but using numpy arrays rather than lists. This can be easily achieved like so:
>>> b = a.sum(axis=1)
>>> b
Out[21]: array([535, 421, 342, 214, 153])
>>> i = np.argsort(b)[::-1]
>>> i
Out[26]: array([0, 1, 2, 3, 4])
>>> a[i, :]
Out[27]:
array([[ 5, 30, 500],
[ 1, 20, 400],
[ 2, 40, 300],
[ 4, 10, 200],
[ 3, 50, 100]])
The steps involved are described in a little greater detail here.

Sort array in Scala partially

How can I sort a region in an array in Scala?
I have an array, say var DivA = new Array[Int](100005).
I filled the elements in DivA up to some position which is less than 100005.
For example my array looks like Diva = {1,2,10,5,15,20}.
In C++ we have sort(DivA, DivA + N), but how can we sort arrays in Scala up to a certain index?
I tried Sorting.quickSort(DivA), but I don't know how to define the index up to which I want to sort the array, so that the statement above sorts the array, and the array looks like DivA={0,0,0,0,0}?
var Diva=new Array[Int](10000);
for(j<-15 to 0 by -1){
DivA(posA)=j;
posA=posA+1;
}
Sorting.quickSort(DivA);
for(j<-0 to posA-1)
{
print(DivA(j));
print(" ");
}
If you want to sort a region in an array, you can use the sort method in java.util.Arrays:
java.util.Arrays.sort(DivA, 0, 15)
this method offers fromIndex and toIndex. Note that this method sorts in place. That's probably OK in your case, as the style you apply seems to be "imperative".
If performance matters, arrays will probably be faster than other collections. Other reasons for using arrays might be memory consumption or interoperability.
Otherwise you can probably design your data structures/algorithm in a different (more "functional") way by using idiomatic Scala collections.
You can use sorted or sortBy, depending on your needs. In case of simple Array[Int], sorted should be enough:
val arr = Array.iterate(15, 15)(_ - 1)
arr: Array[Int] = Array(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
val sortedArray = arr.sorted
sortedArray: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
You can provide your own Ordering typeclass if you want some other kind of order.
You can find it in the documentation.
Although, as was noted in other answer, you should prefer immutable Vector unless you have good reason to switch to Array.
sorted() method is common for all collections, and Arrays in Scala are implicitly converted to a collection.
So you can call sorted on Array[Int], eg:
scala> val p = (10 to 1 by -1).toArray
p: Array[Int] = Array(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
scala> p.sorted
res5: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
First, if you are programming in Scala, you should think about Vector or List because those are immutable which is prefferd way of programming in Scala...
And for Vector and List there is sorted() function which sorts an array

Resources