linspace() generates a float values , but int is required - python-2.7

I am generating x-axis values which is time form 1 to 23 and v-values which is a number of clients. I want to join these 2 lists as dictionary which is done but I am getting float values of x as I generate using linspace().
time_values = np.linspace(1,23,23) # print from 1,2...23
number_of_clients = [] # create empty list that will hold number of clients
for i in range(1,24,1):
rand_value = random.randint(1,20) # generate number of clients
number_of_clients.append(rand_value)
data = dict(zip(time_values,number_of_clients))
print data
output is
{1.0: 12, 2.0: 11, 3.0: 3, 4.0: 19, 5.0: 12, 6.0: 12, 7.0: 5, 8.0: 13, 9.0: 15, 10.0: 3, 11.0: 15, 12.0: 20, 13.0: 5, 14.0: 3, 15.0: 18, 16.0: 12, 17.0: 5, 18.0: 6, 19.0: 8, 20.0: 16, 21.0: 19, 22.0: 1, 23.0: 16}
how to convert 1.0 to 1 and so on.I have tried int(time_vlaues), but it did not worked

try astype method to convert numpy float array to int array:
time_values = np.linspace(1,23,23) # print from 1,2...23
number_of_clients = [] # create empty list that will hold number of clients
for i in range(1,24,1):
rand_value = random.randint(1,20) # generate number of clients
number_of_clients.append(rand_value)
data = dict(zip(time_values.astype(int),number_of_clients))
print(data)
or
time_values = np.linspace(1,23,23,dtype='int') # print from 1,2...23
number_of_clients = [] # create empty list that will hold number of clients
for i in range(1,24,1):
rand_value = random.randint(1,20) # generate number of clients
number_of_clients.append(rand_value)
data = dict(zip(time_values,number_of_clients))
print(data)
output:
{1: 17, 2: 6, 3: 8, 4: 3, 5: 12, 6: 11, 7: 18, 8: 1, 9: 8, 10: 1, 11: 17, 12: 2, 13: 5, 14: 6, 15: 1, 16: 8, 17: 19, 18: 2, 19: 13, 20: 15, 21: 16, 22: 17, 23: 14}

Related

How to select rows by a column value in D with mir.ndslice?

I am browsing through mir.ndslice docs trying to figure out how to do a simple row selection by column.
In numpy I would do:
a = np.random.randint(0, 20, [4, 6])
# array([[ 8, 5, 4, 18, 1, 4],
# [ 2, 18, 15, 7, 18, 19],
# [16, 5, 4, 6, 11, 11],
# [15, 1, 14, 6, 1, 4]])
a[a[:,2] > 10] # select rows where the second column value is > 10
# array([[ 2, 18, 15, 7, 18, 19],
# [15, 1, 14, 6, 1, 4]])
Using mir library I naively tried:
import std.range;
import std.random;
import mir.ndslice;
auto a = generate!(() => uniform(0, 20)).take(24).array.sliced(4,6);
// [[12, 19, 3, 10, 19, 11],
// [19, 0, 0, 13, 9, 1],
// [ 0, 0, 4, 13, 1, 2],
// [ 6, 19, 14, 18, 14, 18]]
a[a[0..$,2] > 10];
But got
Error: incompatible types for `((ulong __dollar = a.length();) , a.opIndex(a.opSlice(0LU, __dollar), 2)) > (10)`: `Slice!(int*, 1LU, cast(mir_slice_kind)0)` and `int`
dmd failed with exit code 1.
So, I went through the docs and couldn't find anything that would look like np.where or similar. Is it even possible in mir?

How to split a List into "n" number of sublists in Java? User will input the value of "n"

Say I have the below-mentioned list:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Now, if user wants 4 sub-lists (n=4), then the sub-lists will be
[0,1,2,3,4,5]
[6,7,8,9,10,11]
[12,13,14,15,16,17]
[18,19,20]
Similarly, if user wants 6 sub-lists (n=6), then the sub-lists will be
[0,1,2,3]
[4,5,6,7]
[8,9,10,11]
[12,13,14,15]
[16,17,18,19]
[20]
Please let me know how can I achieve this.
list1 =[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
n=6
k = int(len(list1)/float(n))+1
i=0
for x in range(n-1):
i=(x+1)*k
print list1[i-k:i]
print list1[i:]
Try this python code. It answers your problem perfectly.

Using Gurobi in Python and adding variables

I am trying to write my first Gurobi optimization code and this is where I am stuck with:
I have the following dictionary for my first subscript:
input for k in range(1,11):
i[k] = int(k)
print i
output {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10}
And, I have the following dictionaries for my second subscript:
c_il = {1: 2, 2: 1, 3: 1, 4: 4, 5: 3, 6: 4, 7: 3, 8: 2, 9: 1, 10: 4}
c_iu = {1: 3, 2: 2, 3: 2, 4: 5, 5: 4, 6: 5, 7: 4, 8: 3, 9: 2, 10: 5}
I am trying to create variables as following:
x = m.addVars(i, c_il, vtype=GRB.BINARY, name="x")
x = m.addVars(i, c_iu, vtype=GRB.BINARY, name="x")
Apparently, it is not giving what I am looking for. What I am looking for is x_(i),(c_il) and x_(i),(c_iu); ignore parenthesis.
More clearly, the following is what I am trying to obtain by using dicts i, c_il, and c_iu:
{1: <gurobi.Var x[1,2]>,
2: <gurobi.Var x[2,1]>,
3: <gurobi.Var x[3,1]>,
4: <gurobi.Var x[4,5]>,
5: <gurobi.Var x[5,3]>,
6: <gurobi.Var x[6,4]>,
7: <gurobi.Var x[7,3]>,
8: <gurobi.Var x[8,2]>,
9: <gurobi.Var x[9,1]>,
10: <gurobi.Var x[10,4]>,
11: <gurobi.Var x[1,3]>,
12: <gurobi.Var x[2,2]>,
13: <gurobi.Var x[3,2]>,
14: <gurobi.Var x[4,5]>,
15: <gurobi.Var x[5,4]>,
16: <gurobi.Var x[6,5]>,
17: <gurobi.Var x[7,4]>,
18: <gurobi.Var x[8,3]>,
19: <gurobi.Var x[9,2]>,
20: <gurobi.Var x[10,5]>}
Since I am using dictionaries everywhere, I want to keep it consistent by continuing to use dictionaries so that I can do multiplications and additions with my parameters which are all in dictionaries. Is there any way to create these variables with m.addVars or m.addVar?
Thanks!
Edit: Modified to make it more clear.
It looks like you want to create 10 variables that are indexed by something. The best way to do this is to create the two indexes as lists. If you want x[12], x[21], then write:
from gurobipy import *
m = Model()
il = [ 12, 21, 31, 44, 53, 64, 73, 82, 91, 104 ]
x = m.addVars(il, vtype=GRB.BINARY, name="x")
And if you want to write x[1,2], x[2,1], then write:
from gurobipy import *
m = Model()
il = [ (1,2), (2,1), (3,1), (4,4), (5,3), (6,4), (7,3), (8,2), (9,1), (10,4) ]
x = m.addVars(il, vtype=GRB.BINARY, name="x")
After a few years of experience, I can easily write the below as an answer. Since the past myself was concerned with keeping the dictionaries as is (I highly criticize and question...), a quick solution is as follows.
x = {}
for (i,j) in c_il.items():
x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in c_iu.items():
x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
Alternatively,
x = {(i,j): m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in c_il.items()}
for (i,j) in c_iu.items():
x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
One liner alternative:
x = {(i,j): m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in [(k,l) for (k,l) in c_il.items()] + [(k,l) for (k,l) in c_iu.items()]}

Getting a list as the result of a function in pandas

I have data frame in pandas and I have written a function to use the information in each row to generate a new column. I want the result to be in a list format:
A B C
3 4 1
4 2 5
def Computation(row):
if row['B'] >= 3:
return [s for s in range(row['C'],50)]
else:
return [s for s in range(row['C']+2,50)]
df['D'] = df.apply(Computation, axis = 1)
However, I am getting the following error:
"could not broadcast input array from shape (308) into shape (9)"
Could you please tell me how to solve this problem?
Say you start with
In [25]: df = pd.DataFrame({'A': [3, 4], 'B': [4, 2], 'C': [1, 5]})
Then there are at least two ways to do it.
You can apply twice on the C column, but switch on the B column:
In [26]: np.where(df.B >= 3, df.C.apply(lambda c: [s for s in range(c, 50)]), df.C.apply(lambda c: [s for s in range(c + 2, 50)]))
Out[26]:
array([ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]], dtype=object)
Or you can apply on the entire row and switch on the B value per row:
In [27]: df.apply(lambda r: [s for s in range(r.C, 50)] if r.B >= 3 else [s for s in range(r.C + 2, 50)], axis=1)
Out[27]:
0 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14...
1 [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, ...
Note that the return types are different, but, in each case, you can still write
df['foo'] = <each one of the above options>

Adding 'n' values in list using for-loop and step-loop for that 'n' values in python

I have recently started to work with python 2.7 .
i have some data which i am passing to product API of amazon, to make it a batch call i want to pass 10 values per call as that is the max IDs or keywords per batch call.
Here is a question that, how to pass only 10 values to the function. I have some 76 values(it may increase) in total which is a list and 6 at the end.I can read values from list using *args but to get only 10 values how can i process it using for-loop statement or any loop.
I want to do something like this
data = rows_db
count = 76
for id in data[range start , count ]:
ids = id #copy 10 values or less
foo(ids)
start = start + 10
def foo(*ids):
#process and retrieve values
I guess you want to do something like this:
data_copy = list(data) # you can replace any appearance of data_copy with data if you don't care if it is changed
while data_copy: # this is equivalent to: while len(data_copy) != 0:
to = min(10, len(data_copy)) # If there are less then 10 entries left, the length will be smaller than ten, so that it is either 10 or the (smaller) length. This is the amount of data that's processed
f(data_copy[:to]) # make the function call with any value up to 'to'
del data_copy[:to] # delete the data, because we already processed it
This:
def f(x): print(x)
data = list(range(53)) # list from 0 (included) to 52 (included)
# here is the top part
yields the expected output of
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
[50, 51, 52]