I have a list(T) of 6500 images(arrays) that I am using for image classification, and I would like to see how increasing the data affects the accuracy.
So, starting from n=2000 images, I am thinking of having a loop that will add 500(n+=500) images at each iteration till it reaches 6500 and therefore compare the accuracy between 2000, 2500, 3000, ... 6500. I have simplified the problem below by having a list of 20 elements.
lst = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
My second list (slist) contains the first 9 elements of the first list (lst).
I am trying to add 2 values to slist at each iteration, starting from lst[9:]. I know rather than using append, extend should be used to add multiple values at once. However, I couldn't find a way to do it.
In the following code, one element is added to slist (from lst) at each loop.
lst = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
slist = lst[:9]
for i in lst[9:]:
slist.append(i)
How can I add 2 or 3 elements simultaneously at each loop? An example output would be:
[1,2,3,4,5,6,7,8,9,0,1]
[1,2,3,4,5,6,7,8,9,0,1,2,3]
[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5]
You could try using extend:
l = [1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]
slist = l[:9]
for i in l[9:][::2]:
if i == l[9]:
slist.extend(l[9+i: 9+i+1])
else:
slist.extend(l[9+i-1: 9+i+1])
print(slist)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0]
lst=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20]
iter = 9
while True:
print(lst[:iter])
iter+=2
if len(lst) <= iter:
print(lst[:iter])
break
This code does the job
lst=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,21]
slist= lst[:9]
s,f=0,2
while True:
slist.extend(lst[9:][s:f])
print(slist)
s+=2
f+=2
if len(slist) >= len(lst):
break
It prints out:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
Related
My inputs are like this, i tried to make starting and ending points to control the routing from a point a --> (special scenario of my case: routing is from location 'a' to point 'a')
I try to get a routing with capacity , distance and time windows constraints, at this level, if i execute the code, I visualise the error bellow:
''TypeError: list indices must be integers or slices, not list ''
data['time_matrix'] = [
[0, 6, 9, 8, 7, 3, 6, 2, 3, 2, 6, 6, 4, 4, 5, 9, 7],
[6, 0, 8, 3, 2, 6, 8, 4, 8, 8, 13, 7, 5, 8, 12, 10, 14],
[9, 8, 0, 11, 10, 6, 3, 9, 5, 8, 4, 15, 14, 13, 9, 18, 9],
[8, 3, 11, 0, 1, 7, 10, 6, 10, 10, 14, 6, 7, 9, 14, 6, 16],
[7, 2, 10, 1, 0, 6, 9, 4, 8, 9, 13, 4, 6, 8, 12, 8, 14],
[3, 6, 6, 7, 6, 0, 2, 3, 2, 2, 7, 9, 7, 7, 6, 12, 8],
[6, 8, 3, 10, 9, 2, 0, 6, 2, 5, 4, 12, 10, 10, 6, 15, 5],
[2, 4, 9, 6, 4, 3, 6, 0, 4, 4, 8, 5, 4, 3, 7, 8, 10],
[3, 8, 5, 10, 8, 2, 2, 4, 0, 3, 4, 9, 8, 7, 3, 13, 6],
[2, 8, 8, 10, 9, 2, 5, 4, 3, 0, 4, 6, 5, 4, 3, 9, 5],
[6, 13, 4, 14, 13, 7, 4, 8, 4, 4, 0, 10, 9, 8, 4, 13, 4],
[6, 7, 15, 6, 4, 9, 12, 5, 9, 6, 10, 0, 1, 3, 7, 3, 10],
[4, 5, 14, 7, 6, 7, 10, 4, 8, 5, 9, 1, 0, 2, 6, 4, 8],
[4, 8, 13, 9, 8, 7, 10, 3, 7, 4, 8, 3, 2, 0, 4, 5, 6],
[5, 12, 9, 14, 12, 6, 6, 7, 3, 3, 4, 7, 6, 4, 0, 9, 2],
[9, 10, 18, 6, 8, 12, 15, 8, 13, 9, 13, 3, 4, 5, 9, 0, 9],
[7, 14, 9, 16, 14, 8, 5, 10, 6, 5, 4, 10, 8, 6, 2, 9, 0],
]
data['time_windows'] = [
(0, 5), # depot
(7, 12), # 1
(10, 15), # 2
(16, 18), # 3
(10, 13), # 4
(0, 5), # 5
(5, 10), # 6
(0, 4), # 7
(5, 10), # 8
(0, 3), # 9
(10, 16), # 10
(10, 15), # 11
(0, 5), # 12
(5, 10), # 13
(7, 8), # 14
(10, 15), # 15
(11, 15), # 16
]
data['num_vehicles'] = 4
data['demands'] = [0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8]
data['vehicle_capacities'] = [15, 15, 15, 15]
data['depot'] = [ 0, 0, 0, 0]
data['ends']= [ 5, 5, 5, 5]
My code is :
depot_idx = data['depot']
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(
data['time_windows'][depot_idx][0],
data['time_windows'][depot_idx][1])
# Add time window constraints for each location except depot.
for location_idx, time_window in enumerate(data['time_windows']):
if location_idx == data['depot']:
continue
index = manager.NodeToIndex(location_idx)
time_dimension.CumulVar(index).SetRange(time_window[0], time_window[1])
And when i execute the code it gives me this :
<ipython-input-10-8bb55ac15980> in main()
47 index = routing.Start(vehicle_id)
48 time_dimension.CumulVar(index).SetRange(
---> 49 data['time_windows'][depot_idx][0],
50 data['time_windows'][depot_idx][1])
51
TypeError: list indices must be integers or slices, not list
Can anyone please tell me where and what it is the problem, because I tried to make "depot_idx" as arrays but in vain ?
You're trying to access a list item by giving another list (depot_idx is a list):
depot_idx = data['depot'] = [ 0, 0, 0, 0]
For accessing items in a list you need to use integers or slices that are representing the indexes you want to access.
In your case you need to pass an integer because your trying to access then the first element of the item (index 0):
data['time_windows'][YOUR_INTEGER][0]
depot_idx = data['depot']
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
time_dimension.CumulVar(index).SetRange(
data['time_windows'][depot_idx][0],
data['time_windows'][depot_idx][1])
Here, depot_idx is a list.
You mismatch index and node index
so this should work:
depot_idx = data['depot']
for vehicle_id in range(data['num_vehicles']):
start_index = routing.Start(vehicle_id) # solver index space
start_node = depot_idx[vehicle_id] # your index space or
# start_node = manager.IndexToNode(start_index)
time_dimension.CumulVar(start_index).SetRange(
data['time_windows'][start_node][0],
data['time_windows'][start_node][1])
side note: Here you have manager.IndexToNode(start_index) == start_node BUT the opposite is undefined aka you can't use manager.NodeToIndex(start_node) since the result is ambiguous (i.e. not a single integer) actually the result should be [routing.Start(v) for v in range(data['num_vehicles'])] but since API should return an integer NodeToIndex() is undefined for start/end nodes...
I need to simulate a certain scenario.
So I'm defining a variable which generates a loop of a random number of integers.
I get for example:
list = [2, 35, 4, 8, 56, 10]
Then, I'm generating this random list 50 times through another loop and I store the data into a dictionary to visualize a Pandas Dataframe.
data_dict = {'random_numers': list}
data_dict_pd = pd.DataFrame(data_dict)
So I get for example this:
[1, 16, 6, 6, 1, 10]
[3, 8, 4, 4, 1, 20, 7, 25, 12]
[14, 8, 16, 4, 11, 18, 5, 15, 24, 2, 15, 5]
[7, 24, 1, 14]
[5, 14, 19, 24, 1]
... 50 times.
Now, I need to create another column enumerating each number in each list of elements, to get the following, based on the previous results:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[1, 2, 3, 4]
[1, 2, 3, 4, 5]
...50 times.
Actually, came up with the following but it's wrong:
new_list = []
for index in enumerate(list)
new_list.append(index)
Any better idea?
I would strongly suggest changing the name of your list, as list is used for Python lists.
Assuming you change it to l, I would use:
l = [2, 35, 4, 8, 56, 10]
new_list = []
for i in range(1, len(l) + 1):
new_list.append(i)
print(new_list)
Output:
[1, 2, 3, 4, 5, 6]
If what you need is to iterate a list of lists, and incorporating #deceze suggestions (provided that you don't rename list):
lists = [
[1, 16, 6, 6, 1, 10],
[3, 8, 4, 4, 1, 20, 7, 25, 12],
[14, 8, 16, 4, 11, 18, 5, 15, 24, 2, 15, 5],
[7, 24, 1, 14],
[5, 14, 19, 24, 1]
]
new_lists = [list(range(1, len(lst) + 1)) for lst in lists]
print(new_lists)
Output:
[[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 2, 3, 4], [1, 2, 3, 4, 5]]
new_list = []
for index in range(1,len(list)+1)
new_list.append(index)
should be used in place of enumerator,as it will going to return the pair.
Thank you,
could you please help my with the following issue:
Sample File :
I try to aggregate the sum of 6 months, up from a specific start date per row.
The sum should be shown in a new column (Sum 6 months from startdate)
My first thought would be to get it with the following code:
df['sum_6_months'] = df.loc[:,'01.2018':'06.2018'].apply(sum, axis=1)
but this code is not individually and only for the timeframe (01.18-06.18) in all rows.
df = pd.DataFrame(np.array([[1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,1], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,2],[1,5,3, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4],
[1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,3], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,4],[1,5,5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4],
[1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,5], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,5],[1,5,2, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4],
[1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,6], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,2],[1,5,5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4],
[1, 5, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4,4], [1, 5, 3, 4, 5, 6, 7, 7,8,2,5,7,3,4,2],[1,5,1, 3, 4, 5, 6, 7, 7, 8,2,5,7,3,4]]),
columns=['01.2018', '02.2018', '03.2018', '04.2018', '05.2018','06.2018', '07.2018', '08.2018',
'09.2018','10.2018', '11.2018', '12.2018','01.2019', '02.2019', '03.2019'])
date = [01.2018, 03.2018,04.2018,05.2018,03.2018,01.2018, 03.2018,04.2018,05.2018,03.2018,01.2018, 03.2018,04.2018,05.2018,03.2018]
df['Startdate']= date
df['Startdate']=df['Startdate'].astype(str).str.rjust(7,'0')
df_columns = df.columns.tolist()
def get_sum_six(df_list):
start_date_index = df_columns.index(df_list[-1])
df_list = df_list[0:-1]
sum_of_six = sum(df_list[start_date_index: start_date_index + min(len(df_list)-start_date_index, 6)])
return (sum_of_six)
df['sum_last_six'] = df.apply(lambda x: get_sum_six(x.tolist()), axis=1)
First, calculate the number of columns to skip in each row:
df2['StartCol'] = 1 + df2.columns[1:].searchsorted(df2.Startdate)
The 1: skips the Startdate column. Then "roll" that many columns to the left, so they wrap around and end up at the end of each row, take the first 6, and sum:
np.roll(df2.iloc[:, 1:], -df2.StartCol)[:,:6].sum(1)
That gives you:
[27, 28, 27, 21, 26, 27, 29, 27, 21, 25, 27, 25, 25, 18, 23]
Which you can store in a new column if you like.
This is list of tile placements. Each integer stands for an id of a tile. Each time an integer is added to a new list it means that a new tile is placed. When a tile is removed, the last integer is removed from a new list. I want that every time a tile is placed the list to be unique. A list dont have to be unique, when a tile is removed. The code is placing these tiles in a for loop. So for this example, the last list of the lists is wrong, because it wasn't unique when a tile was placed. Is there a way to exclude numbers which will make the new list not unique. So for this example is there a way to exclude the id 18, before it adds to the list.
I know this is a very vague question, but I am new to python and can't make the code of this assignment easier. I hope someone could help me with this vague question
[[1, 2, 3, 13, 4, 5, 6, 7, 17],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10, 18],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 11],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 11, 18],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10],
[1, 2, 3, 13, 4, 5, 6, 7, 17, 8, 15, 9, 10, 18]]
The lists must be in this order. So for example i have had these lists:
[[1, 2, 3, 13, 4, 5, 6, 7, 17],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 8],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 19],
[1, 2, 3, 13, 4, 5, 6, 7]]
I want to exlude the ids 17,8,19
So for [1, 2, 3, 13, 4, 5, 6, 7] the output must look like this ( id ont care if the output is a list or integers)
[17,8,19]
But when i have this list [1, 2, 3, 13, 4, 5, 6] in lists
[[1, 2, 3, 13, 4, 5, 6, 7, 17],
[1, 2, 3, 13, 4, 5, 6],
[1, 2, 3, 13, 4, 5, 6, 7, 8],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 19],
[1, 2, 3, 13, 4, 5, 6, 7]]
The output is this:
[7]
I hope this will make it more clear.
I tried with itertools and collections- pass a list, a list element index and to be added value to the adder function if uniquness is kept the adder will add that passed value otherwise return intact list.compare_func return TRUE if list is unique using all.
import collections,itertools
compare_func = lambda x, y: collections.Counter(x) != collections.Counter(y)
lst = [[1, 2, 3],[1, 2, 3,4]]
def adder(mylist,indx,val):
mylist[indx].append(val)
if all([compare_func(*i) for i in list(itertools.combinations(lst,2))]):
print "Added item"
else:
print "Did not add item"
mylist[indx].pop()
return mylist
Now run print adder(lst,0,4)
Output-
Did not add item
[[1, 2, 3], [1, 2, 3, 4]]
But if run
print adder(lst,1,4)
Output-
Added item
[[1, 2, 3], [1, 2, 3, 4, 4]]
EDIT
After OP cleared question i added this portion-
Try using set as below-
import collections,itertools
data = [[1, 2, 3, 13, 4, 5, 6, 7, 17],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 8],
[1, 2, 3, 13, 4, 5, 6, 7],
[1, 2, 3, 13, 4, 5, 6, 7, 19],
[1, 2, 3, 13, 4, 5, 6, 7]]
interscntion = set.intersection(*map(set,data))
d = collections.Counter([i for j in data for i in j if i not in list(interscntion)])
if len(set(it[1] for it in d.most_common()))>1:
print [max(d.most_common(),key=lambda x:x[1])[0]]
else:
print [j[0] for j in d.most_common()]
Output-
[8, 17, 19]
Here you go:
def tiles(arrOfArrs):
final = []
for x in arrOfArrs:
final += x
return list(set(final))
Keep another list of lists and at each position add the tiles that appear at that index in any of the primary lists.
This is my first time handling multidimensional arrays and I'm having problems accessing elements. I'm trying to get the red pixels of a picture but just the first 8 elements within the array. Here's the code
import Image
import numpy as np
im = Image.open("C:\Users\Jones\Pictures\1.jpg")
pix = im.load()
r, g, b = np.array(im).T
print r[0:8]
Since you're dealing with images, r is a 2-D array. To get the first 8 pixels in the image, try
r.flatten()[:8]
This will wrap around automatically if the first row has less than 8 pixels.
do you want all rows too? Try this r[:,:8]
only want the first row? Try this r[0,:8]
You can do it like this:
r[0][:8]
Note, however, that this will not work if the first row has less than 8 pixels. To fix that, do this:
from itertools import chain
r = list(chain.from_iterable(r))
r[:8]
or (if you don't want to import an entire module):
r = [val for element in r for val in element]
r[:8]
I think it could be more simple. This example uses a random matrix (this will be your r matrix):
In [7]: from pylab import * # convention
In [8]: r = randint(0,10,(10,10)) # this is your image
In [9]: r
array([[7, 9, 5, 5, 6, 8, 1, 4, 3, 4],
[5, 4, 4, 4, 2, 6, 2, 6, 4, 2],
[1, 4, 9, 9, 2, 6, 1, 9, 0, 6],
[5, 9, 0, 7, 9, 9, 5, 2, 0, 7],
[8, 3, 3, 9, 0, 0, 5, 9, 2, 2],
[5, 3, 7, 8, 8, 1, 6, 3, 2, 0],
[0, 2, 5, 7, 0, 1, 0, 2, 1, 2],
[4, 0, 4, 5, 9, 9, 3, 8, 3, 7],
[4, 6, 9, 9, 5, 9, 3, 0, 5, 1],
[6, 9, 9, 0, 3, 4, 9, 7, 9, 6]])
Then, extract first 8 columns and do something
In [17]: r_8 = r[:,:8] # extract columns
In [18]: r_8
Out[18]:
array([[7, 9, 5, 5, 6, 8, 1, 4],
[5, 4, 4, 4, 2, 6, 2, 6],
[1, 4, 9, 9, 2, 6, 1, 9],
[5, 9, 0, 7, 9, 9, 5, 2],
[8, 3, 3, 9, 0, 0, 5, 9],
[5, 3, 7, 8, 8, 1, 6, 3],
[0, 2, 5, 7, 0, 1, 0, 2],
[4, 0, 4, 5, 9, 9, 3, 8],
[4, 6, 9, 9, 5, 9, 3, 0],
[6, 9, 9, 0, 3, 4, 9, 7]])
In [19]: r_8 = r_8 * 2 # do something
In [20]: r_8
Out[20]:
array([[14, 18, 10, 10, 12, 16, 2, 8],
[10, 8, 8, 8, 4, 12, 4, 12],
[ 2, 8, 18, 18, 4, 12, 2, 18],
[10, 18, 0, 14, 18, 18, 10, 4],
[16, 6, 6, 18, 0, 0, 10, 18],
[10, 6, 14, 16, 16, 2, 12, 6],
[ 0, 4, 10, 14, 0, 2, 0, 4],
[ 8, 0, 8, 10, 18, 18, 6, 16],
[ 8, 12, 18, 18, 10, 18, 6, 0],
[12, 18, 18, 0, 6, 8, 18, 14]])
Now, this is the trick. Replace the first 8 columns in r using hstack:
In [21]: r = hstack((r_8, r[:,8:])) # it replaces the FISRT 8 columns, note the indexing notation
In [22]: r
Out[22]:
array([[14, 18, 10, 10, 12, 16, 2, 8, 3, 4], # it does not touch the last 2 columns
[10, 8, 8, 8, 4, 12, 4, 12, 4, 2],
[ 2, 8, 18, 18, 4, 12, 2, 18, 0, 6],
[10, 18, 0, 14, 18, 18, 10, 4, 0, 7],
[16, 6, 6, 18, 0, 0, 10, 18, 2, 2],
[10, 6, 14, 16, 16, 2, 12, 6, 2, 0],
[ 0, 4, 10, 14, 0, 2, 0, 4, 1, 2],
[ 8, 0, 8, 10, 18, 18, 6, 16, 3, 7],
[ 8, 12, 18, 18, 10, 18, 6, 0, 5, 1],
[12, 18, 18, 0, 6, 8, 18, 14, 9, 6]])
EDIT: as to what DSM pointed out, OP is infact using a numpy array.
i retract my answer as nneonneo's correct