Practice Data Analysis 100 Knock 61

Asked 2 years ago, Updated 2 years ago, 31 views

Python Practical Data Analysis 100 Knock Knock 61 Questions

Running the code above will result in a total shipping cost of 1450, which is different from the 1298 in the book.

The running environment is jupiter notebook and both pulp and orthoolpy have been installed with conda prompt.

If there is a problem with the code, please let me know.

import numpy as np
import pandas aspd
from itertools import product
from pull import LpVariable, lpSum, value
from toolpy import model_min, addvars, addvals

df_tc=pd.read_csv("trans_cost.csv", index_col="Factory")
df_demand=pd.read_csv("demand.csv")
df_supply=pd.read_csv("supply.csv")

np.random.seed(1)
nw = len(df_tc.index)
nf=len(df_tc.columns)
pr=list(product(range(nw), range(nf)))))

m1 = model_min()
v1 = {(i,j): LpVariable("v%d_%d"%(i,j), lowBound=0)for i,jinpr}

m1+=lpSum(df_tc.iloc[i][j]*v1[i,j] for i, jinpr)
for i in range (nw):
    m1+=lpSum(v1[i,j]) for jin range(nf))<=df_supply.iloc[0][i]
for jin range (nf):
    m1+=lpSum(v1[i,j]) for i in range(nw))>=df_demand.iloc[0][i]
m1.solve()

df_tr_sol=df_tc.copy()
total_cost = 0
fork, x in v1.items():
    i,j = k[0], k[1]
    df_tr_sol.iloc[i][j] = value(x)
    total_cost+=df_tc.iloc[i][j]*value(x)

print(df_tr_sol)
print("Total shipping cost:" + str(total_cost))

python

2022-09-30 17:05

1 Answers

Compared to the corresponding answer in the distributed sample_100knocks.zip, the index of df_demand.iloc[0][i] at the end of line 23 appears to be incorrect.

The correct answer is j, not i, and df_demand.iloc[0][j].

By the way, Python 3.10.1, which is not an Anaconda environment or a jupyter notebook, has a slightly different total shipping cost of 1296, so there may be differences in the number of versions of other modules or CSV data.

@ Holly's comment and the following article says that the total shipping cost is 1296 and there is no comment, so 1296 seems to be correct, so it seems that 1298 of books is typo in the question.

"Python Practical Data Analysis 100 Knocks" Knock 61
Read Python Practice Data Analysis 100 Knocks


2022-09-30 17:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.