How to get the print of 3 values? - arrays

I want to get prints of mini, maxi, and a8_1, but, instead, I just got the last print.
a8_1 = [-0.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0]
a8_1 = np.array(a8_1)
a8_1
mini = a8_1.min()
mini
maxi = a8_1.max()
maxi

Assuming you are using interactive shell, and have already imported the numpy package (which I do not see in the code above), this above code should definitely print all 3.
>>> import numpy as np
>>> a8_1 = [-0.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2.0]
>>> a8_1 = np.array(a8_1)
>>> a8_1
array([-0.7, -1.5, -1.7, 0.3, 1.5, 1.8, 2. ])
>>> mini = a8_1.min()
>>>
>>> mini
-1.7
>>> maxi = a8_1.max()
>>> maxi
2.0
>>>

Related

What function in DolphinDB corresponds to choice in Python numpy?

For example, I want to generate a sample of 100 elements from the array a = [1, 2, 3, 4] with the probabilities p = [0.1, 0.1, 0.3, 0.5] associated with each element in a. In Python I can use np.random.choice(a=[1, 2, 3, 4], size=100, p=[0.1, 0.1, 0.3, 0.5]).
Does DolphinDB have a built-in function for this?
You can use a user-defined function:
def choice(v, n, p){
        cump = removeTail!([0.0].join(cumsum(p\p.sum())), 1)
        return v[cump.asof(rand(1.0, n))]
}
a=[1, 2, 3, 4]
n=100000
p=[0.1, 0.1, 0.3, 0.5]
r = choice(a, n, p)
Starting from version 1.30.19/2.00.7, you can use the built-in function randDiscrete directly:
randDiscrete(1 2 3 4, [0.1, 0.1, 0.3, 0.3], 100)
output:

Arranging a list based on the distance of its elements from a given value

Let's assume we have a list like the following
[2.3, 1.02, 1.99, 0.99, 0.089, 0, 1.1, -1.1, -2.1]
We want to arrange the elements of this list based on their distance from target value equal to 1 in the following manner:
[0.99, 1.02, 1.1, 0.089, 1.99, 0, 2.3, -1.1, -2.1]
How to do that in python in one or two lines?
python solution
Use sorted with the absolute distance to target as key:
L = [2.3, 1.02, 1.99, 0.99, 0.089, 0, 1.1, -1.1, -2.1]
target = 1
out = sorted(L, key=lambda x: abs(x-target))
output: [0.99, 1.02, 1.1, 0.089, 1.99, 0.0, 2.3, -1.1, -2.1]
numpy solution
Compute the absolute distance and use numpy.argsort:
L = [2.3, 1.02, 1.99, 0.99, 0.089, 0, 1.1, -1.1, -2.1]
target = 1
import numpy as np
a = np.array(L)
out = a[np.argsort(abs(a-target))].tolist()
output: [0.99, 1.02, 1.1, 0.089, 1.99, 0.0, 2.3, -1.1, -2.1]

Convert pandas dataframe column to numpy array with each separated based on value in otehr column

I have a pandas dataframe with two columns like:
data = {'first_column': [1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 0.1, 0.2, 0.3, 0.4, 11, 12, 13],
'second_column': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]
}
df = pd.DataFrame (data, columns = ['first_column','second_column'])
I want to get a numpy array like follow:
array([[[1.1], [2.1], [3.1], [4.1], [5.1], [6.1]], [[0.1], [0.2], [0.3], [0.4]], [[11], [12], [13]]])
I´m not able to accomplish getting this.
This should do the trick:
df.groupby(['second_column']).apply(lambda x: list(map(lambda el:[el], x['first_column'].to_list()))).values
I'm grouping by your second column and converting series within each group to lists.
list(map(lambda el:[el],...))
This part converts each element of a list to an individual list as mentioned by you in the question.
One way using aggregate:
l = df.groupby("second_column")["first_column"].agg(list).tolist()
print(l)
Output:
[[1.1, 2.1, 3.1, 4.1, 5.1, 6.1], [0.1, 0.2, 0.3, 0.4], [11.0, 12.0, 13.0]]

Shorthand to create an array with a floating-point step in F#

In F# there is a shorthand for creating an array of numbers. For example, the code
[1..10]
will create an array containing {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
Or
[-2..2]
will create {-2, -1, 0, 1, 2}.
Is there any related shorthand for creating an array in F# with a floating-point step? For example, an array like {-2.0, -1.5, -1.0, -0.5, 0, 0.5, 1.0, 1.5, 2} where the step is 0.5? Or is using a for or while loop the only way?
Yes there is.
[-2.0 .. 0.5 .. 2.0]
This creates
[-2.0; -1.5; -1.0; -0.5; 0.0; 0.5; 1.0; 1.5; 2.0]
Documentation: https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/loops-for-in-expression

How to extract just floats from list

I want to get arrays with floats from A,B,C list.
page = requests.get("http://www.arso.gov.si/potresi/obvestila%20o%20potresih/aip/")
soup = BeautifulSoup(page.content, 'html.parser')
all_tables=soup.find_all('table')
right_table=soup.find('table',class_='online')
A=[]
B=[]
C=[]
for row in right_table.findAll("tr"):
cells = row.findAll('td')
if len(cells)==6:
A.append(cells[1].find(text=True))
B.append(cells[2].find(text=True))
C.append(cells[3].find(text=True))
For now I have variables like this:
A=[u'45.50',u'46.00',...]
and I want just floats from list:
A=[45.50,46.00,...]
Just convert the element's text to float type:
...
if len(cells) == 6:
A.append(float(cells[1].text))
B.append(float(cells[2].text))
C.append(float(cells[3].text))
print(A)
print(B)
print(C)
The output:
[45.5, 46.0, 46.07, 45.89, 45.83, 46.1, 46.53, 45.88, 45.84, 45.9, 46.09, 46.39, 45.3, 45.34, 46.7, 45.25, 46.39, 45.5, 46.39]
[14.41, 14.76, 14.22, 14.59, 15.12, 14.42, 14.57, 15.19, 15.18, 14.57, 14.19, 13.39, 14.62, 14.59, 15.23, 14.58, 15.03, 14.4, 15.03]
[1.2, 1.2, 1.0, 0.8, 1.2, 1.0, 1.1, 1.3, 0.8, 0.9, 0.5, 1.0, 1.3, 2.3, 1.4, 1.9, 0.7, 0.8, 0.4]
You could use python2.7 map function to convert each list of strings to a list of floats:
A = map(float, A)
B = map(float, B)
C = map(float, C)
print A # [45.5, 46.0, 46.07, 45.89, 45.83, 46.1, 46.53, 45.88, 45.84, 45.9, 46.09, 46.39, 45.3, 45.34, 46.7, 45.25, 46.39, 45.5, 46.39]
print B # [14.41, 14.76, 14.22, 14.59, 15.12, 14.42, 14.57, 15.19, 15.18, 14.57, 14.19, 13.39, 14.62, 14.59, 15.23, 14.58, 15.03, 14.4, 15.03]
print C # [1.2, 1.2, 1.0, 0.8, 1.2, 1.0, 1.1, 1.3, 0.8, 0.9, 0.5, 1.0, 1.3, 2.3, 1.4, 1.9, 0.7, 0.8, 0.4]

Resources