Related
Can I sample an array a = [1, 2, 3, 4] based on the specified probabilities p = [0.1, 0.1, 0.3, 0.5]?
For example, in python I can use np.random.choice(a=[1, 2, 3, 4], size=100, p=[0.1, 0.1, 0.3, 0.5])
For me I will form a new random choice data list by percentages/probabilities, for example do random choice on [1, 2, 3, 3, 3, 4, 4, 4, 4, 4] will be equivalent with your data and probabilities.
You can use take (https://www.dolphindb.com/help/FunctionsandCommands/FunctionReferences/t/take.html) function for helping the data forming:
v = take(1, 1) <- take(2, 1) <- take(3, 3) <- take(4, 5)
rand(v, 100)
I have a dictionary that contains lists as value, and I want to divide each element in those lists on constant, how can I do that using a def function?!
Assuming you're using python and that I got your question, simple way of doing that:
import numpy as np
def divide(input_list, dividend):
return list(np.array(input_list) / dividend)
You'd probably want to use something like:
CONSTANT_K = 2
dict = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }
for value in dict.values():
quotientResult = value / CONSTANT_K
# do whatever you want with the quotient result here
Where CONSTANT_K is your constant. Then, you iterate through your dictionary values in a for loop. The loop takes each value in the dictionary and divides it by the constant. You can handle the values inside of the for loop, or you can store them inside a new dictionary or array.
You can put this into a def function by doing:
CONSTANT_K = 2
dict = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }
def divideDict(k, dictA):
for value in dict.values():
quotientResult = value / CONSTANT_K
# do whatever you want with the quotient result here
divideDict(CONSTANT_K, dict)
Where divideDict() is your function. If you're looking for a dictionary containing lists, you'll have to loop through the lists as well:
CONSTANT_K = 2
dict = { 'a': [1, 2], 'b': [3, 4], 'c': [5, 6], 'd': [7, 8] }
def divideDict(k, dictA):
for value in dict.values():
for val in value:
quotientResult = val / CONSTANT_K
# do whatever you want with the quotient result here
divideDict(CONSTANT_K, dict)
Im stuck at what probably has a simple solution.
I have a string representation of a list, like this:
"[[1, 2, 3], [4, 5, 6]]"
In other words, a list containing 2 lists of 3 integers
How to convert the string to a list object of List<List> in Kotlin?
You can use kotlinx.serialization to deserialize JSON!
As a standalone Kotlin script:
#file:DependsOn("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0")
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
val s = "[[1, 2, 3], [4, 5, 6]]"
val j = Json.decodeFromString<List<List<Int>>>(s)
println(j) // [[1, 2, 3], [4, 5, 6]]
println(j[0][0]) // 1
In an Android app's build.gradle you would need these lines instead of #file:DependsOn:
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.0'
}
apply plugin: 'kotlinx-serialization'
I want to apply a function to the variable x and saved as y. But why the x is also changed? How to prevent it?
import torch
def minus_min(raw):
for col_i in range(len(raw[0])):
new=raw
new[:,col_i] = (raw[:,col_i] - raw[:,col_i].min())
return new
x=torch.tensor([[0,1,2,3,4],
[2,3,4,0,8],
[0,1,2,3,4]])
y=minus_min(x)
print(y)
print(x)
output:
tensor([[0, 0, 0, 3, 0],
[2, 2, 2, 0, 4],
[0, 0, 0, 3, 0]])
tensor([[0, 0, 0, 3, 0],
[2, 2, 2, 0, 4],
[0, 0, 0, 3, 0]])
Because this assignment:
new[:,col_i] = (raw[:,col_i] - raw[:,col_i].min())
is an in-place operation. Therefore, x and y will share the underlying .data.
The smallest change that would solve this issue would be to make a copy of x inside the function:
def minus_min(raw):
new = raw.clone() # <--- here
for col_i in range(len(raw[0])):
new[:,col_i] = raw[:,col_i] - raw[:,col_i].min()
return new
If you want, you can simplify your function (and remove the for loop):
y = x - x.min(dim=0).values
I am using the embed .html example given on the bokeh site: http://docs.bokeh.org/en/latest/docs/user_guide/embed.html. Note I am using bokeh 12.3. The plots are displaying fine but the text is rendering as the exact output from the script function - including '{' and '\n' characters.
scatter function:
from bokeh.plotting import figure
from bokeh.models import Range1d
from bokeh.embed import components
def scatter():
# create some data
x1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y1 = [0, 8, 2, 4, 6, 9, 5, 6, 25, 28, 4, 7]
x2 = [2, 5, 7, 15, 18, 19, 25, 28, 9, 10, 4]
y2 = [2, 4, 6, 9, 15, 18, 0, 8, 2, 25, 28]
x3 = [0, 1, 0, 8, 2, 4, 6, 9, 7, 8, 9]
y3 = [0, 8, 4, 6, 9, 15, 18, 19, 19, 25, 28]
# select the tools we want
TOOLS="pan,wheel_zoom,box_zoom,reset,save"
# the red and blue graphs will share this data range
xr1 = Range1d(start=0, end=30)
yr1 = Range1d(start=0, end=30)
# only the green will use this data range
xr2 = Range1d(start=0, end=30)
yr2 = Range1d(start=0, end=30)
# build our figures
p1 = figure(x_range=xr1, y_range=yr1, tools=TOOLS, plot_width=300, plot_height=300)
p1.scatter(x1, y1, size=12, color="red", alpha=0.5)
p2 = figure(x_range=xr1, y_range=yr1, tools=TOOLS, plot_width=300, plot_height=300)
p2.scatter(x2, y2, size=12, color="blue", alpha=0.5)
p3 = figure(x_range=xr2, y_range=yr2, tools=TOOLS, plot_width=300, plot_height=300)
p3.scatter(x3, y3, size=12, color="green", alpha=0.5)
# plots can be a single Bokeh Model, a list/tuple, or even a dictionary
plots = {'Red': p1, 'Blue': p2, 'Green': p3}
script, div = components(plots)
return script, div
My flask code is:
script, div = scatter()
return self.render_template('bokeh_example.html', script=script, div=div)
bokeh_example.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.3.min.css" type="text/css" />
<script type="text/javascript" src="http://cdn.bokeh*emphasized text*.org/bokeh/release/bokeh-0.12.3.min.js"></script>
{{ script | safe }}
</head>
<body>
<div class='bokeh'>
<h1>Scatter Example</h1>
{{ div | safe }}
</div>
</body>
</html>
The plots display fine but the div text renders as literals:
{'Red': '\n #this text displays instead of just the string 'Red'
\n #this displays on next line in smaller font
#plot displays fine here
\n #this text displays after the plot instead of creating a blank line.
Any clues?
You are passing a dictionary of plots to components:
plots = {'Red': p1, 'Blue': p2, 'Green': p3}
script, div = components(plots)
return script, div
This means (per the documentation) that the result is not a single script and a single div. Rather, it's a single script and a dictionary mapping your original names to multiple divs:
components({"Red": p1, "Blue": p2, "Green": p3})
#=> (script, {"Red": p1_div, "Blue": p2_div, "Green": p3_div})
Right now you are trying to template the dict itself into your HTML. Presumably Jinja just calls str on the dict to turn it into a string, and the browser doesn't know what to do with that. You need to template each one of the divs in the dict returned by components, individually.
For a suitably updated template, that might look like:
script, divs = scatter() # notice plural: divS
return self.render_template(
'bokeh_example.html',
script=script,
div_red=divs['Red'],
div_blue=divs['Blue'],
div_green=divs['Green'],
)
Or alternatively you might update the template to iterate over divs directly using some of Jinja2's capabilities for iterating over template arguments that are collections.