Unable to append data into a nested list python - list

'''
distList = [[0.0, 0.0, 0.0, 0.0, 0.0], [-1.76333, -4.22504, -2.42978, 0.81744, 0.1], [-0.91351, -3.76965, -3.34161, -1.66984, 0.1], [-2.5362, 0.22098, -3.49264, -2.34982, 0.1]]
sumList = []
for i in range(len(distList)):
for j in range(len(distList[i])):
sumofNum =distList[i][j]*distList[i][j]
sumList[i].append(sumofNum)
'''
' getting error IndexError: list index out of range at last line '

sumList[i].append(sumofNum)
to
sumList.append(sumofNum)

Related

Question on Pyomo Transport/Shipping issue

I am learning pyomo and practicing an exercise about shipping problem with conditions below:
The shipment can either directly deliver from source cities to the destination or through a depot (source city -> depot -> destination)
The way of shipping will be selected based on least cost consumed
Every Depot has its own capacity limitation
I tried to use pyomo to solve this question, however, no solution can be figured out.
Please give me some advice and direction why the codes didnt work. Thank you!
import pyomo.environ as pyomo
import numpy as np
supply = dict({'Liverpool': 150000,
'Brighton': 200000})
through = dict({'Newcastle': 70000,
'Birmingham': 50000,
'London': 100000,
'Exeter': 40000,
'Liverpool': 150000,
'Brighton': 200000
})
demand = dict({'C1': 50000,
'C2': 10000,
'C3': 40000,
'C4': 35000,
'C5': 60000,
'C6': 20000})
cost = dict({
('Liverpool', 'Newcastle'): 0.5,
('Liverpool', 'Birmingham'): 0.5,
('Liverpool', 'London'): 1.0,
('Liverpool', 'Exeter'): 0.2,
('Liverpool', 'C1'): 1.0,
('Liverpool', 'C3'): 1.5,
('Liverpool', 'C4'): 2.0,
('Liverpool', 'C6'): 1.0,
('Brighton', 'Birmingham'): 0.3,
('Brighton', 'London'): 0.5,
('Brighton', 'Exeter'): 0.2,
('Brighton', 'C1'): 2.0,
('Newcastle', 'C2'): 1.5,
('Newcastle', 'C3'): 0.5,
('Newcastle', 'C5'): 1.5,
('Newcastle', 'C6'): 1.0,
('Birmingham', 'C1'): 1.0,
('Birmingham', 'C2'): 0.5,
('Birmingham', 'C3'): 0.5,
('Birmingham', 'C4'): 1.0,
('Birmingham', 'C5'): 0.5,
('London', 'C2'): 1.5,
('London', 'C3'): 2.0,
('London', 'C5'): 0.5,
('London', 'C6'): 1.5,
('Exeter', 'C3'): 0.2,
('Exeter', 'C4'): 1.5,
('Exeter', 'C5'): 0.5,
('Exeter', 'C6'): 1.5
})
cost_total = {}
for city_from in supply.keys():
for city_through in through.keys():
for city_to in demand.keys():
if city_from == city_through:
cost_total[(city_from , city_through , city_to)] = 0 + cost.get((city_through , city_to) , 9999)
else:
cost_total[(city_from , city_through , city_to)] = cost.get((city_from , city_through) , 9999) + cost.get((city_through , city_to) , 9999)
supplier = supply.keys()
througher = through.keys()
demander = demand.keys()
model = pyomo.ConcreteModel()
model.i = pyomo.Set(initialize = supplier , doc = 'City From')
model.j = pyomo.Set(initialize = througher , doc = 'City Through')
model.k = pyomo.Set(initialize = demander , doc = 'City To')
model.s = pyomo.Param(model.i , initialize=supply, doc='Supply by City')
model.t = pyomo.Param(model.j , initialize = through , doc = 'through / warehouse')
model.d = pyomo.Param(model.k , initialize=demand , doc='Demand by City')
model.cost = pyomo.Param(model.i , model.j , model.k , initialize=cost_total , doc = 'total cost')
model.x = pyomo.Var(model.i , model.j , model.k , bounds = (0 , None))
def supply_rule(model, i):
return sum(model.x[i,j,k] for j in model.j for k in model.k) <= model.s[i]
model.supply = pyomo.Constraint(model.i, rule=supply_rule, doc='Observe supply limit at plant i')
def demand_rule(model, k):
return sum(model.x[i,j,k] for i in model.i for j in model.j) == model.d[k]
model.demand = pyomo.Constraint(model.k, rule=demand_rule, doc='Satisfy demand at market j')
def depot_cont(model , j):
return sum(model.x[i , j , k] for i in model.i for k in model.k) <= model.t[j]
model.through_cont2 = pyomo.Constraint(model.j , rule = depot_cont)
def objective_rule(model):
return sum(model.cost[i,j,k]*model.x[i,j,k] for i in model.i for j in model.j for k in model.k)
model.objective = pyomo.Objective(rule=objective_rule, sense=pyomo.minimize, doc='Define objective function')
Your code (unmodified) appears to be working for me.
Realize there are a couple ways to set this up, which is a larger discussion... In your case you enumerated all possible routes, and snuck in a zero-cost option for the direct routes and added high cost for infeasible routes, which is fine. On a larger problem, or more complicated network, the standard way to approach this is as a network flow problem with flow-balance constraints at all the nodes (cities).
Anyhow, your question isn't too clear on what "isn't working". I added this to the end of your code:
solver = pyomo.SolverFactory('glpk')
result = solver.solve(model)
print(result)
for ijk in model.x.index_set():
if model.x[ijk]:
print(f'ship : {ijk} qty: {model.x[ijk].value}')
and it produced this result, which passes 1st level sanity check, but I didn't look at it too closely...
Problem:
- Name: unknown
Lower bound: 198500.0
Upper bound: 198500.0
Number of objectives: 1
Number of constraints: 15
Number of variables: 73
Number of nonzeros: 217
Sense: minimize
Solver:
- Status: ok
Termination condition: optimal
Statistics:
Branch and bound:
Number of bounded subproblems: 0
Number of created subproblems: 0
Error rc: 0
Time: 0.007380008697509766
Solution:
- number of solutions: 0
number of solutions displayed: 0
ship : ('Liverpool', 'Liverpool', 'C1') qty: 50000.0
ship : ('Liverpool', 'Liverpool', 'C6') qty: 20000.0
ship : ('Brighton', 'Birmingham', 'C2') qty: 10000.0
ship : ('Brighton', 'Birmingham', 'C4') qty: 35000.0
ship : ('Brighton', 'Birmingham', 'C5') qty: 5000.0
ship : ('Brighton', 'London', 'C5') qty: 55000.0
ship : ('Brighton', 'Exeter', 'C3') qty: 40000.0
[Finished in 579ms]

Build custom Metric for Loss Function with Keras, with errors

I'm trying to write custom metric function to set in the compile step wrote in this way:
self.model.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=[self.dice_similarity_coefficient_metric,self.positive_predictive_value_metric,self.sensitivity_metric])
I wrote Dice Similarity Coefficient, Positive Predictive Value and Similarity in this way:
FP = false positive
TP = true positive
FN = false negative
def dice_similarity_coefficient_metric(self, y_true, y_pred):
y_true = np.array(K.eval(y_true))
y_pred = np.array(K.eval(y_pred))
FP = np.sum(y_pred & np.logical_not(y_true)).astype(float)
TP = np.sum(y_true & y_pred).astype(float)
FN = np.sum(np.logical_not(y_pred) &
np.logical_not(y_true)).astype(float)
return K.variable(np.array((2 * TP) / (FP + (2 * TP) + FN +
K.epsilon())))
def positive_predictive_value_metric(self, y_true, y_pred):
y_true = np.array(K.eval(y_true))
y_pred = np.array(K.eval(y_pred))
FP = np.sum(y_pred & np.logical_not(y_true)).astype(float)
TP = np.sum(y_true & y_pred).astype(float)
return K.variable(np.array(TP / (FP + TP + K.epsilon())))
def sensitivity_metric(self, y_true, y_pred):
y_true = np.array(K.eval(y_true))
y_pred = np.array(K.eval(y_pred))
TP = np.sum(y_true & y_pred).astype(float)
FN = np.sum(np.logical_not(y_pred) &
np.logical_not(y_true)).astype(float)
return K.variable(np.array(TP / (TP + FN + K.epsilon())))
when i run the code i have the following error:
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'dense_3_target' with dtype float
[[Node: dense_3_target = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]]
Can someone care to explain where is the problem?
Where i'm wrong?
Thank you
Probably, it's better to define metrics using backend functions. For example:
def false_negatives(Y_true, Y_pred):
return K.sum(K.round(K.clip(Y_true - Y_pred, 0, 1)))
It can be checked on an example data with 5 FN:
y_true = np.array([[1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 0.0, 1.0]], dtype=np.float32)
y_pred = np.array([[0.3, 0.99, 0.99, 0.1], [0.6, 0.99, 0.99, 0.1], [0.1, 0.99, 0.99, 0.1]], dtype=np.float32)
n_fn = np.sum((y_true - y_pred) > 0.5)
Y_true = K.placeholder((None, 4), dtype=K.floatx())
Y_pred = K.placeholder((None, 4), dtype=K.floatx())
n_fn = false_negatives(Y_true, Y_pred).eval(inputs_to_values={Y_true: y_true, Y_pred: y_pred})
HTH

DICT() and MATPLOTLIB?

I created a dictionary to match the feature importance of a Decision Tree in sklearn with the corresponding name of the feature in my df. Here the code below:
importances = clf.feature_importances_
feature_names = ['age','BP','chol','maxh',
'oldpeak','slope','vessels',
'sex_0.0','sex_1.0',
'pain_1.0','pain_2.0','pain_3.0','pain_4.0',
'bs_0.0','bs_1.0',
'ecg_0.0','ecg_1.0','ecg_2.0',
'ang_0.0','ang_1.0',
'thal_3.0','thal_6.0','thal_7.0']
CLF_sorted = dict(zip(feature_names, importances))
in output I obtained this:
{'BP': 0.053673644739136502,
'age': 0.014904980747733202,
'ang_0.0': 0.0,
'ang_1.0': 0.0,
'bs_0.0': 0.0,
'bs_1.0': 0.0,
'chol': 0.11125922817930389, ...}
as I expected. I have two question for you:
how could I create a bar plot where the x-axis represents the feature_names and the y-axis the corresponding importances?
if it is possible, how could I sort the bar plot in a descending way?
try this:
import pandas as pd
df = pd.DataFrame({'feature': feature_names , 'importance': importances})
df.sort_values('importance', ascending=False).set_index('feature').plot.bar(rot=0)
demo:
d ={'BP': 0.053673644739136502,
'age': 0.014904980747733202,
'ang_0.0': 0.0,
'ang_1.0': 0.0,
'bs_0.0': 0.0,
'bs_1.0': 0.0,
'chol': 0.11125922817930389}
df = pd.DataFrame({'feature': [x for x in d.keys()], 'importance': [x for x in d.values()]})
In [63]: import matplotlib as mpl
In [64]: mpl.style.use('ggplot')
In [65]: df.sort_values('importance', ascending=False).set_index('feature').plot.bar(rot=0)
Out[65]: <matplotlib.axes._subplots.AxesSubplot at 0x8c83748>

for each loop with undefined number of inside loops

Assume foo is a list or some other iterator. I want some thing so that I can (pseudo-code):
for i in foo
for j in foo - [i]
for k in foo - [i, j]
...
for some_var in foo - [i, j, k, ...]//only one value left in foo
do_something(some_args)
Is there some way to do this in python? Can I do this in a loop, would I have to use recursion, or would I have to make (only if no other way) a code object?
You're question has to do with combinatorics. Specifically Cartesian products.
Without recursion you need to know how many nestings of loops you are going to run. However you don't need to know this information ahead of time. As long as you can get it dynamically it is ok.
Consider this code taken from one of my repos: https://github.com/Erotemic/utool/blob/next/utool/util_dict.py
from itertools import product
import six
varied_dict = {
'logdist_weight': [0.0, 1.0],
'pipeline_root': ['vsmany'],
'sv_on': [True, False, None]
}
def all_dict_combinations(varied_dict):
tups_list = [[(key, val) for val in val_list]
for (key, val_list) in six.iteritems(varied_dict)]
dict_list = [dict(tups) for tups in product(*tups_list)]
return dict_list
dict_list = all_dict_combinations(varied_dict)
running this code will result in dict_list being
[
{'pipeline_root': 'vsmany', 'sv_on': True, 'logdist_weight': 0.0},
{'pipeline_root': 'vsmany', 'sv_on': True, 'logdist_weight': 1.0},
{'pipeline_root': 'vsmany', 'sv_on': False, 'logdist_weight': 0.0},
{'pipeline_root': 'vsmany', 'sv_on': False, 'logdist_weight': 1.0},
{'pipeline_root': 'vsmany', 'sv_on': None, 'logdist_weight': 0.0},
{'pipeline_root': 'vsmany', 'sv_on': None, 'logdist_weight': 1.0},
]
and then you could write code like
for some_vars in dict_list:
do_something(some_vars)
To relate it back to your example if you were to list each let of values foo can take in each nested level in what I call varied_dict then you can get a solution to your question. Also note that varied_dict can be built dynamically, and it doesn't really have to be a dict. If you modified my code you could easilly specify the values using a list of some other structure.
The magic in the above code comes down to the use of the itertools.product function. I suggest you take a look at that. https://docs.python.org/2/library/itertools.html#itertools.product

print all the values of a group of objects without iterating through it

I want to print the values of the group of objects that return from the database.
I have tried like the following,
Products = productBll.listProduct(params)
print Products.__dict__
it will display like the following,
{'_result_cache': [Product: Product object, Product: Product object]}
But when i am doing like this ,
for prd in Products:
print prd.__dict__
it showing all the contents in the Products objects
{'product_price': 0.0, 'right_side_min_depth': 0.0, 'short_description': u'', 'left_side_min_depth': 0.0, 'max_depth': 0.0, 'height_scale': 2.0, 'left_side_max_depth': 0.0, 'is_hinges': u'No', 'max_height': 1.04}
{'product_price': 0.0, 'right_side_min_depth': 0.0, 'short_description': u'', 'left_side_min_depth': 0.0, 'max_depth': 1000.0, 'height_scale': 1000.0, 'left_side_max_depth': 0.0, 'is_hinges': u'No', 'max_height': 1000.0}
But i want the above result without using the for loop.
Is there any way to do it by one line of code?
If all you're looking for is a one-liner, here it is:
Products = productBll.listProduct(params)
print [prd.__dict__ for prd in Products]
You can try using values(). Assuming your model is Products you can do
Product.objects.filter(your_filter_criteria).values()
this will give you list of dict per item selected.