Learning by Examples With Anaconda - 52904 - Foulabook - Com
Learning by Examples With Anaconda - 52904 - Foulabook - Com
Anaconda3/2019 – 7
Booklet
PhoTo:www.unsplash.com
Anaconda 3 / 2019 –
July
Booklet
Anaconda 3 / 2019 –
July
Booklet
www.havalpress.com
1
Learning by examples with Anaconda 3 / 2019 - July
Acknowledgment
I am grateful to my sister Nowf - Senior Architect, for her kind help in the
general setting of this booklet.
Ahmad A. M. Allawi
September / 2019
2
Learning by examples with Anaconda 3 / 2019 - July
3
Learning by examples with Anaconda 3 / 2019 - July
4
Learning by examples with Anaconda 3 / 2019 - July
The articles listed below that I chose in this booklet, though few, were
selected among the unlimited number of subjects covered in the various
textbooks, tutorials and research papers and these are:
6
Learning by examples with Anaconda 3 / 2019 - July
Articles
1- Pint for unit conversion………………………………………...…… Page-8
Units Conversion Program
2- SchemDraw……………………………………………………………….. Page-9
Schem Draw Program
3- Linear regression and machine learning…………………..… Page-10
Multiple Linear Regression Program
(Tkinter Window for MLR – Figure)
Machine Learning Program
Random Forest Program
4- Symbolic Math (SymPy) and ODE………………………..…..… Page-11
Algebra and Symbolic Math with SymP Program
5- Linear optimization using Pulp………………………..…………. Page-12
Simple Example Program
Company X Problem Program
Power Company
Power Company Program
Optimization of Boiler-Turbo Generators……………..……..Page-15
Boiler Turbo Generators Program
6- Linear / Non-Linear optimization using SciPy……………... Page-20
Scipy (Scientific Python)
Linear / Non-Linear Example with SciPy Program
Appendix………………………………………………………….…...……Page-23
References……………………………………………………..………..…Page-24
7
Learning by examples with Anaconda 3 / 2019 - July
8
9/3/2019 Unit Conversion
Units Conversion
# Pint For Units Conversion
Pint library is used for unit conversion.
With the internet connected, type after the prompt sign > pip install pint
Among these, pint is a relatively new library that builds on experience with earlier
attempts.
The core concept in pint is to work with a unit registry (ur), which is created as shown
below:
In [1]:
# For example, here's how to compute the molarity of a sodium chloride solution
# in 58.44 grams of NaCl (mw = 58.44)
ur = UnitRegistry()
# problem data
V = 3.0 * ur.liters
m = 58.44 * ur.grams
mw = 58.44 * ur.grams/ur.mol
# compute molarity
C = m/(mw*V)
print(C)
In [3]:
# Example-2
# In Unit Conversion Each variable with units has to() and ito() methods
# for converting the quantity to a
# desired set of units.
x = 0.5 * ur.kilograms/ur.gallon
y = x.to(ur.grams/ur.liter)
print(x)
print(y)
In [4]:
x = 0.5 * ur.kilograms/ur.gallon
x.ito(ur.grams/ur.liter)
print(x)
In [5]:
# Example-3
# problem data
V = 3.0 * ur.gallons
m = 0.5 * ur.lbs
mw = 58.44 * ur.grams/ur.mol
# compute concentration
C = m/(mw*V)
print(C)
C = C.to(ur.mol/ur.liter)
print(C)
C.ito(ur.mol/ur.gallon)
print(C)
In [8]:
# Unit Check
print(speed)
24.0 meter
8.0 second
<Quantity(8.0, 'second')>
In [11]:
# Example-5
193414489032258.03 hertz
193.41448903225802 terahertz
In [16]:
print(home.to('degF'))
77.72000039999993 degF
Solution
In [17]:
cost = price*kwh_per_joule*Q_btu/btu_per_joule
If the natural gas is stored at 1000 psia and 15 ∘C, how large a storage tank is required to store natural gas for
the winter?
Solution
The volume of natural gas required is determined by the heating requirement.
In [18]:
# compute lb moles
n_lbmol = 14.696*V_ft3/(R*T_degR)
In [19]:
V_ft3 = n_lbmol*R*T_degR/1000.0
m3_per_ft3 = 0.028317
V_m3 = V_ft3*m3_per_ft3
print("Storage volume of natural gas at 1000 psia and 15 deg C =", round(V_m3,1), "cubic me
Storage volume of natural gas at 1000 psia and 15 deg C = 20.8 cubic meters
Solution
In [20]:
btu_per_joule = 9.486e-4
Q_joule = Q_btu/btu_per_joule
print("Heat requirement =", round(Q_joule/1e6,1), "Megajoules")
m_kg = Q_joule/46.3e6
print("Mass of propane required = {0:.2f} kg".format(m_kg))
V_m3 = m_kg/0.493/1000.0
In [ ]:
2 – SchemDraw
9
9/3/2019 SchemDraw
Schem Draw
Drawing quality electrical schematics is one of those tasks that always takes too long.
Most software for the job
focuses on fancy circuit simulations and doesn't care about appearance. Constantly facing
the dilemma of how to
draw simple schematic diagrams.
SchemDraw is electrical engineering package and is not part of Anaconda library and
should be downloaded and
installed using Anaconda prompt:
In [24]:
# Example-1
d = schem.Drawing()
V1 = d.add(e.SOURCE_V, label='10V')
d.add(e.LINE, to=V1.start)
# Add a Ground
d.add(e.GND)
d.draw(showplot=False)
d.save('testschematic.eps')
In [2]:
# Example-2
In [3]:
# Example-3
d = schem.Drawing()
V1 = d.add(e.SOURCE_V, label='5V')
d.add(e.LINE, d='right', l=d.unit*.75)
S1 = d.add(e.SWITCH_SPDT2_CLOSE, d='up', anchor='b', rgtlabel='$t=0$')
d.add(e.LINE, d='right', xy=S1.c, l=d.unit*.75)
d.add(e.RES, d='down', label='$100\Omega$', botlabel=['+','$v_o$','-'])
d.add(e.LINE, to=V1.start)
d.add(e.CAP, xy=S1.a, d='down', toy=V1.start, label='1$\mu$F')
d.add(e.DOT)
d.draw(showplot=False)
d.save('cap_charge.svg')
In [4]:
d = schem.Drawing(inches_per_unit=.5, unit=3)
D1 = d.add(e.DIODE, theta=-45)
d.add(e.DOT)
D2 = d.add(e.DIODE, theta=225, reverse=True)
d.add(e.DOT)
D3 = d.add(e.DIODE, theta=135, reverse=True)
d.add(e.DOT)
D4 = d.add(e.DIODE, theta=45)
d.add(e.DOT)
d.draw(showplot=False)
d.save('powersupply.svg')
In [5]:
# Example-5
In [6]:
# Example-6: An OP Amp
plt.xkcd()
d = schem.Drawing(inches_per_unit=.5)
op = d.add(e.OPAMP)
d.add(e.LINE, d='left', xy=op.in2, l=d.unit/4)
d.add(e.LINE, d='down', l=d.unit/5)
d.add(e.GND)
d.add(e.LINE, d='left', xy=op.in1, l=d.unit/6)
d.add(e.DOT)
d.push()
Rin = d.add(e.RES,d='left',xy=op.in1-[d.unit/5,0],botlabel='$R_{in}$',lftlabel='$v_{in}$')
d.pop()
d.add(e.LINE, d='up', l=d.unit/2)
Rf = d.add(e.RES, d='right', l=d.unit*1, label='$R_f$')
d.add(e.LINE, d='down', toy=op.out)
d.add(e.DOT)
d.add(e.LINE, d='left', tox=op.out)
d.add(e.LINE, d='right', l=d.unit/4, rgtlabel='$v_{o}$')
d.draw()
d.save('ex_xkcd.svg')
In [8]:
d = schem.Drawing(unit=2.5)
R7 = d.add(e.RES, d='right', botlabel='$R_7$')
R6 = d.add(e.RES, d='right', botlabel='$R_6$')
d.add(e.LINE, d='right', l=2)
d.add(e.LINE, d='right', l=2)
R5 = d.add(e.RES, d='up' , botlabel='$R_5$')
R4 = d.add(e.RES, d='up', botlabel='$R_4$')
d.add(e.LINE, d='left', l=2)
d.push()
R3 = d.add(e.RES, d='down', toy=R6.end, botlabel='$R_3$')
d.pop()
d.add(e.LINE, d='left', l=2)
d.push()
R2 = d.add(e.RES, d='down', toy=R6.end, botlabel='$R_2$')
d.pop()
R1 = d.add(e.RES, d='left', tox=R7.start, label='$R_1$')
Vt = d.add(e.BATTERY, d='up', xy=R7.start, toy=R1.end, label='$V_t$', lblofst=0.3)
d.labelI(Vt, arrowlen=1.5, arrowofst=0.5)
d.draw()
d.save('7_resistors_3_loops.png')
#d.save('7_resistors_3_loops.pdf')
# Find:
In [9]:
# Circuit Values
Vt = 5.2
R1 = 0.0132
R2 = 0.021
R3 = 0.00360
R4 = 0.0152
R5 = 0.0119
R6 = 0.0022
R7 = 0.00740
In [10]:
In [11]:
d = schem.Drawing(unit=2.5)
R67 = d.add(e.RES, d='right', botlabel='$R_{67}$')
d.add(e.LINE, d='right', l=2)
d.add(e.LINE, d='right', l=2)
R45 = d.add(e.RES, d='up', botlabel='$R_{45}$')
d.add(e.LINE, d='left', l=2)
d.push()
R3 = d.add(e.RES, d='down', toy=R67.end, botlabel='$R_3$')
d.pop()
d.add(e.LINE, d='left', l=2)
d.push()
R2 = d.add(e.RES, d='down', toy=R67.end, botlabel='$R_2$')
d.pop()
R1 = d.add(e.RES, d='left', tox=R67.start, label='$R_1$')
Vt = d.add(e.BATTERY, d='up', xy=R67.start, toy=R1.end, label='$V_t$', lblofst=0.3)
d.labelI(Vt, arrowlen=1.5, arrowofst=0.5)
d.draw()
d.save('5_resistors_3_loops.png')
#d.save('5_resistors_3_loops.pdf')
In [12]:
Vt = 5.2
R1 = 0.0132
R2 = 0.021
R3 = 0.00360
R4 = 0.0152
R5 = 0.0119
R6 = 0.0022
R7 = 0.00740
R45 = R4 + R5
R67 = R6 + R7
R2345 = ((1/R2)+(1/R3)+(1/R45))**(-1)
print(f'R2345 = {round(R2345,7)} Ohm')
In [13]:
d = schem.Drawing(unit=2.5)
R67 = d.add(e.RES, d='right', botlabel='$R_{67}$')
R345 = d.add(e.RES, d='up' , botlabel='$R_{2345}$')
R1 = d.add(e.RES, d='left', tox=R67.start, label='$R_1$')
Vt = d.add(e.BATTERY, d='up', xy=R67.start, toy=R1.end, label='$V_t$', lblofst=0.3)
d.labelI(Vt, arrowlen=1.5, arrowofst=0.5)
d.draw()
d.save('3_resistors_1_loop.png')
#d.save('3_resistors_1_loop.pdf')
In [14]:
Vt = 5.2
R1 = 0.0132
R2 = 0.021
R3 = 0.00360
R4 = 0.0152
R5 = 0.0119
R6 = 0.0022
R7 = 0.00740
R45 = R4 + R5
R67 = R6 + R7
R2345 = ((1/R2)+(1/R3)+(1/R45))**(-1)
Rt = R1 + R2345 + R67
print(f'Rt = {round(Rt,7)} Ohm')
Rt = 0.0255602 Ohm
In [15]:
d = schem.Drawing(unit=2.5)
L2 = d.add(e.LINE, d='right')
Rt = d.add(e.RES, d='up' , botlabel='$R_{t}$')
L1 = d.add(e.LINE, d='left', tox=L2.start)
Vt = d.add(e.BATTERY, d='up', xy=L2.start, toy=L1.end, label='$V_t$', lblofst=0.3)
d.labelI(Vt, arrowlen=1.5, arrowofst=0.5)
d.draw()
d.save('1_resistor_no_loops.png')
#d.save('1_resistor_no_loops.pdf')
In [16]:
Vt = 5.2
R1 = 0.0132
R2 = 0.021
R3 = 0.00360
R4 = 0.0152
R5 = 0.0119
R6 = 0.0022
R7 = 0.00740
R45 = R4 + R5
R67 = R6 + R7
R2345 = ((1/R2)+(1/R3)+(1/R45))**(-1)
Rt = R1 + R2345 + R67
It = Vt/Rt
print(f'It = {round(It,2)} A')
It = 203.44 A
In [17]:
I6 = It
I7 = It
V6 = I6 * R6
V7 = I7 * R7
print(f'V6 = {round(V6,5)} V, V7 = {round(V7,5)} V')
V6 = 0.44757 V, V7 = 1.50547 V
In [18]:
I2345 = It
V2345 = I2345 * R2345
print(f'V2345 = {round(V2345,5)} V')
V2345 = 0.56153 V
In [19]:
# I3 and I6
V3 = V2345
I3 = V3 / R3
I6 = It
print(f'I3 = {round(I3,2)} A, I6 = {round(I6,2)} A')
I3 = 155.98 A, I6 = 203.44 A
In [20]:
# Power in resistor R7
I7 = It
P7 = R7 * I7**2
print(f'P7 = {round(P7,2)} W')
P7 = 306.27 W
In [21]:
# Current in R45
V45 = V2345
I45 = V45/R45
print(f'I45 = {round(I45,3)} A')
I45 = 20.721 A
In [22]:
# Power in R4
I4 = I45
P4 = R4 * I4**2
print(f'P4 = {round(P4,4)} W')
P4 = 6.5261 W
In [23]:
V6 = 0.448 V
V7 = 1.51 V
I3 = 156.0 A
I6 = 203.0 A
P4 = 6.53 W
P7 = 306.0 W
Conclusion
SchemDraw is a great package for making circuit diagrams in Python. Python is also useful for doing
calculations that involve lots of different values. Although none of the calculations in this problem were
particularly difficult, keeping track of all the values as variables in Python can cut down on errors when there
multiple calculations and many parameters to keep track of.
In [ ]:
3 - Linear Regression
and
Machine Learning
10
9/3/2019 Multiple Linear Regression
By using 2 independent variables/inputs, namely Input_1 and Input_2 and one System Output.
It is assumed that inputs to the System are read evry one hour and the output is recorded.
We will use Tkinter which is a Graphical User Interface (GUI). This will facilitate the inputs of the data by the
users.
So it makes sense to create for them a simple interface, via widgets, where they can manage the data in a
simplified
manner, and this is why we will use Tkinter objects for this purpose to enter the values and to display the
predicted results.
Also displayed the Ordinary Least-Squares (OLS) statistics and our equation constant and coefficients.
Ordinary least-squares (OLS) regression is a generalized linear modelling technique that may be used to
model a single response variable which has been recorded on at least an interval scale. The technique may
be applied to single or multiple explanatory variables and also categorical explanatory variables that have
been appropriately coded. Please refer to the many available books for statistics for further explanation of OLS.
Real data can be used by reading "csv" or "Excel" files.The data here can be easily modified to suit your
application,
for example reading the power output every hour from a power plant for a long period of time, say 24 Hrs.
In the example below, we have two inputs (Input_1 and Input_2) and one output.The time is 24Hrs period.
localhost:8889/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-1 Multiple Linear Regression/Multiple Linear Regression.ipynb 1/7
9/3/2019 Multiple Linear Regression
In [2]:
df = DataFrame(System,columns=['Hrs','Input_1','Input_2','Output'])
print(df)
localhost:8889/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-1 Multiple Linear Regression/Multiple Linear Regression.ipynb 2/7
9/3/2019 Multiple Linear Regression
In [3]:
X = df[['Input_1','Input_2']].astype(float)
# If you want to use one variable for simple linear regression, then use:
Y = df['Output'].astype(float)
# Using sklearn
regr = linear_model.LinearRegression()
regr.fit(X, Y)
Intercept:
1798.4039776258546
Coefficients:
[ 345.54008701 -250.14657137]
localhost:8889/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-1 Multiple Linear Regression/Multiple Linear Regression.ipynb 3/7
9/3/2019 Multiple Linear Regression
In [4]:
import statsmodels.api as sm
X = sm.add_constant(X)
model = sm.OLS(Y, X).fit()
predictions = model.predict(X)
print_model = model.summary()
print(print_model)
C:\Users\ORANGE\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py:2389:
FutureWarning: Method .ptp is deprecated and will be removed in a future ver
sion. Use numpy.ptp instead.
return ptp(axis=axis, out=out, **kwargs)
localhost:8889/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-1 Multiple Linear Regression/Multiple Linear Regression.ipynb 4/7
9/3/2019 Multiple Linear Regression
Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is corre
ctly specified.
From OLS table above we get the constant value as well as the intercept and the coefficients for our equation.
localhost:8889/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-1 Multiple Linear Regression/Multiple Linear Regression.ipynb 5/7
9/3/2019 Multiple Linear Regression
In [ ]:
# Using Tkinter Object(s) or widgets for our Graphical User Interface (GUI)
root= tk.Tk()
def values():
global New_Input_1 # our 1st input variable
New_Input_1 = float(entry1.get())
root.mainloop()
### Run the program and the Tkinter window shown below is displayed.
To check, we have entered the values 2.75 for Input_1 and 5.3 for Input_2.
The output or the predicted output is shown to be 1422.862 which is very close to the
output value in the above data.
In [ ]:
localhost:8889/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-1 Multiple Linear Regression/Multiple Linear Regression.ipynb 7/7
Tkinter window for the program
9/4/2019 Machine Learning
Machine Learning
What is Machine Learning? A definition.
Machine learning is an application of artificial intelligence (AI) that provides systems the ability to automatically
learn and improve from experience without being explicitly programmed. Machine learning focuses on the
development of computer programs that can access data and use it learn for themselves.
The process of learning begins with observations or data, such as examples, direct experience, or instruction,in
order to look for patterns in data and make better decisions in the future based on the examples that we
provide.
The primary aim is to allow the computers to learn automatically without human intervention or assistance and
adjust actions accordingly.
Supervised machine learning algorithms can apply what has been learned in the past to new data using labeled
examples to predict future events.
Starting from the analysis of a known training dataset, the learning algorithm produces an inferred function to
make predictions about the output values. The system is able to provide targets for any new input after
sufficient training.
The learning algorithm can also compare its output with the correct, intended output and find errors in order to
modify the model accordingly.
In contrast, unsupervised machine learning algorithms are used when the information used to train is neither
classified nor labeled. Unsupervised learning studies how systems can infer a function to describe a hidden
structure from unlabeled data.
The system doesn’t figure out the right output, but it explores the data and can draw inferences from datasets to
describe hidden structures from unlabeled data.
Semi-supervised machine learning algorithms fall somewhere in between supervised and unsupervised
learning, since they use both labeled and unlabeled data for training – typically a small amount of labeled data
and a large amount of unlabeled data. The systems that use this method are able to considerably improve
learning accuracy.
Usually, semi-supervised learning is chosen when the acquired labeled data requires skilled and relevant
resources in order to train it / learn from it. Otherwise, acquiringunlabeled data generally doesn’t require
additional resources.
Reinforcement machine learning algorithms is a learning method that interacts with its environment by
producing actions and discovers errors or rewards. Trial and error search and delayed reward are the most
relevant characteristics of reinforcement learning. This method allows machines and software agents to
automatically determine the ideal behavior within a specific context in order to maximize its performance.
Simple reward feedback is required for the agent to learn which action is best; this is known as the
reinforcement signal. Machine learning enables analysis of massive quantities of data. While it generally
delivers faster, more accurate results in order to identify profitable opportunities or dangerous risks, it may also
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Machine Learning.ipynb 1/5
9/4/2019 Machine Learning
require additional time and resources to train it properly. Combining machine learning with AI and cognitive
technologies can make it even more effective in processing large volumes of information.
Here, I we will use Decision Tree and Random Forest algorithm as an examples of machine learning.
Also we need to download 'pydotplus' using the same procedure above, type, conda install pydotplus.
work with Error message displayed " cannot find graphviz executable file".
From the Control Panel go to System and Security then System then Advanced Setting then Environment
Variables, and:
Go to the end of the line and put ; (is a must), then write after ; the following:
C:\Users\ORANGE\Anaconda3\Library\bin\graphviz
NOTE: IN MY COMPUTER I HAVE THE main folder titled 'ORANGE' BUT YOURS IS DIFFERENT.
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Machine Learning.ipynb 2/5
9/4/2019 Machine Learning
In [4]:
# Creat Data
data = [[8,8,'High'],[50,40,'High'],[8,9,'Low'],[15,12,'High'],[9,9.8,'Low']]
# defining predictors, here we need to define what are the predictors i.e the inputs
X = df[['Pressure','Height']]
# definig the target variable and mapping it to 1 for High and 0 for Low
y = df['Label'].replace({'High':1, 'Low':0})
tree = DecisionTreeClassifier()
model = tree.fit(X,y)
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Machine Learning.ipynb 3/5
9/4/2019 Machine Learning
In [2]:
dot_data = StringIO()
export_graphviz(
model,
out_file = dot_data,
filled=True, rounded=True, proportion=False,
special_characters=True,
feature_names=X.columns,
class_names=["High", "Low"])
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
#graph = pydotplus.graph_from_dot_data(dot_data)
nodes = graph.get_node_list()
edges = graph.get_edge_list()
# ///////////////////////////////////////////////////////////////////////////////////
graph.write_png('original_tree.png')
graph.set_size('"5,5!"')
graph.write_png('resized_tree.png')
# Show Tree
Image(graph.create_png())
Out[2]:
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Machine Learning.ipynb 4/5
9/4/2019 Machine Learning
In [5]:
# Prediction
a=np.array
prediction = model.predict([[5,9]])
if prediction == 1:
print("High")
else:
print("Low")
Low
In [ ]:
In [ ]:
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Machine Learning.ipynb 5/5
9/4/2019 Random Forests Classifier
# Random Forest
is flexible, easy to use machine learning algorithm that produces great results most of
the time.
It is also one of the most used algorithms because it’s simplicity and the fact that it
can be used for both classification and regression tasks which form the majority of
current machine learning systems.
We will discuss random forest in classification, since classification is sometimes
considered the building block of machine learning.
In the example, we are going to use Iris dataset already available, within the many
datasets in sklearn.The Iris flower dataset is rather small (consisting of only 150
evenly distributed samples), and is well behaved which makes it ideal for this study.
In [2]:
iris = datasets.load_iris()
In [3]:
# print the labels species, they are setosa, versicolor and virginica.
# The labels are in the dataset
print(iris.target_names)
print(iris.feature_names)
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Random Forests Classifier.ipynb 1/8
9/4/2019 Random Forests Classifier
In [4]:
# Print the iris data (only the first 5 records), just to make sure that
# We have loaded the dataset correctly
# These are petal and sepal length and width all in cm
print(iris.data[0:5])
In [5]:
import pandas as pd
data=pd.DataFrame({
'sepal length':iris.data[:,0],
'sepal width':iris.data[:,1],
'petal length':iris.data[:,2],
'petal width':iris.data[:,3],
'species':iris.target
})
data.head()
Out[5]:
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Random Forests Classifier.ipynb 2/8
9/4/2019 Random Forests Classifier
In [6]:
In [7]:
clf=RandomForestClassifier(n_estimators=100)
y_pred=clf.predict(X_test)
In [8]:
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
Accuracy: 1.0
In [9]:
# Check prediction
class_code = clf.predict([[3,5,4,2]])
print(class_code)
[2]
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Random Forests Classifier.ipynb 3/8
9/4/2019 Random Forests Classifier
In [10]:
import numpy as np
from sklearn.preprocessing import LabelEncoder
labels = np.asarray(iris.feature_names)
le = LabelEncoder()
le.fit(labels)
labels = le.transform(labels)
print(labels)
[2 3 0 1]
In [11]:
decoded_class = le.inverse_transform(class_code)
print (decoded_class)
In [12]:
import pandas as pd
feature_imp=pd.Series(clf.feature_importances_,
index=iris.feature_names).sort_values(ascending=False)
feature_imp
Out[12]:
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Random Forests Classifier.ipynb 4/8
9/4/2019 Random Forests Classifier
In [13]:
In [14]:
# From the above bar chart, it can be seen that the Feature sepal width has the
# lower importance among the others.
# So, we can remove and select the remaining 3-features.
In [15]:
# We will generate a model on the selected remaining features and train the model
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Random Forests Classifier.ipynb 5/8
9/4/2019 Random Forests Classifier
In [16]:
# n_estimators represents the number of trees in the forest. Usually the higher the number
# trees the better to learn
# the data. However, adding a lot of trees can slow down the training process considerably.
# N_estimator is between 50 and 200.
clf=RandomForestClassifier(n_estimators=100)
clf.fit(X_train,y_train)
y_pred=clf.predict(X_test)
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
Accuracy: 0.9523809523809523
In [17]:
# From the above accuracy, it is seen that after removing the least important feature
# (sepal length), the accuracy of our model is increased.
# This is because we removed a noise data.
y=iris.target
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Random Forests Classifier.ipynb 6/8
9/4/2019 Random Forests Classifier
In [23]:
Out[23]:
In [24]:
Out[24]:
localhost:8888/notebooks/THE BOOK/3-Linear Regression and Machine Learning/3-2 Machine Learning/Random Forests Classifier.ipynb 7/8
Learning by examples with Anaconda 3 / 2019 - July
4 - Symbolic Math
(SymPy)
and
ODE
11
9/4/2019 SymPy
Just think of all the x's and y's in a typical algebra problem. We refer to this type of
math as symbolic math. You know the factorize of x^3+ 3x^2+ 3x + 1 problems in math
class. We’ll use SymPy—a Python library that lets you write expressions containing
symbols and perform operations on them.
http://sympy.org
and provides the full (and up-to-date) documentation for this library.
In [1]:
import sympy
sympy.init_printing()
In [4]:
Out[4]:
𝑥
In [5]:
y = Symbol('y')
x + y + x + 10*y
Out[5]:
2𝑥 + 11𝑦
In [6]:
# We can abbreviate the creation of multiple symbolic variables using the symbols function.
# For example, to create the symbolic variables x, y and z, we can use:
# Note:
x, y, z = sympy.symbols('x,y,z')
# And evaluate the following expression:
x + 2*y + 3*z - x
Out[6]:
2𝑦 + 3𝑧
In [7]:
(x + 2*y).subs(x, 10)
Out[7]:
2𝑦 + 10
In [8]:
# Find:
(x + 2*y).subs(x, 10).subs(y, 3)
Out[8]:
16
In [9]:
# We can also substitute a symbolic variable for another one such as in this
# example where y is replaced
# with x before we substitute x with the number 2.
Out[9]:
3𝑥 + 𝑦2
In [10]:
myterm.subs(x, y).subs(y, 2)
Out[10]:
10
In [11]:
# Numeric types:
Out[11]:
1
10
In [12]:
b = Rational(45, 67)
b
Out[12]:
45
67
In [13]:
a-b
Out[13]:
− 383
670
In [17]:
# Note that the Rational class works with rational expressions exactly.
# This is in contrast to Python’s standard float data type which
# uses floating point representation to
# approximate (rational) numbers.
# We can convert the sympy.Rational type into a Python floating point variable
# using float or the evalf method of the Rational object.
# The evalf method can take an argument that specifies how many digits
# should be computed for the floating point approximation (not all of those may be used by
# floating point type of course).
c = Rational(2, 3)
c
Out[17]:
2
3
In [18]:
float(c)
Out[18]:
0.6666666666666666
In [19]:
c.evalf(20) # To 20 digits
Out[19]:
0.66666666666666666667
In [17]:
Out[17]:
cos(𝑥)
In [18]:
Out[18]:
9𝑥8 + 20𝑥 + 3
In [19]:
Out[19]:
4
In [20]:
Out[20]:
32
In [21]:
Out[21]:
263.66015625
localhost:8888/notebooks/THE BOOK/4-SymPy And ODE/SymPy.ipynb 4/30
9/4/2019 SymPy
In [22]:
diff(diff(3*x**4*y**7, x, x), y, y)
Out[22]:
1512𝑥2 𝑦5
In [23]:
r = sqrt(x**2 + y**2)
sigma = Symbol('σ')
def phi(x,y,sigma):
return sqrt(x**2 + y**2 + sigma**2)
In [24]:
Out[24]:
𝑥3
3
In [25]:
Out[25]:
8
3
In [26]:
Out[26]:
2.6666666666666665
Ordinary differential equations:
SymPy has inbuilt support for solving several kinds of ordinary differential equation via its dsolve command. We
need to set up the ODE and pass it as the first argument, eq. The second argument is the function f(x) to solve
for. An optional third argument is hint, influences the method that dsolve uses. some methods are better-suited
to certain classes of ODEs, or will express the solution more simply, than others.
To set up the ODE solver, we need a way to refer to the unknown function for which we are solving, as well as
its derivatives. The Function and Derivative classes facilitate this.
localhost:8888/notebooks/THE BOOK/4-SymPy And ODE/SymPy.ipynb 5/30
9/4/2019 SymPy
In [27]:
Out[27]:
𝑦(𝑥) = 𝐶1 𝑒−5𝑥
In [29]:
Out[29]:
𝑦(𝑥) = 𝐶1 𝑒5 + 125
−5𝑥
In [ ]:
In [4]:
# With SymPy, you can just tell SymPy the equation of the line you want to plot,
# and the graph will be created for you. Let’s plot a line whose equation is given by
# y = 2x + 3:
Out[4]:
<sympy.plotting.plot.Plot at 0x8c95cf0>
In [5]:
# Now, let’s say that you wanted to limit the values of 'x' in the preceding graph to lie
# in the range −5 to 5.
# (instead of −10 to 10). You’d do that as follows:
Out[5]:
<sympy.plotting.plot.Plot at 0x8c95190>
In [7]:
# You can use other keyword arguments in the plot() function, such as title to enter
# a title or xlabel and ylabel
# to label the x-axis and the y-axis, respectively.
# The following plot() function specifies the preceding three keyword arguments.
Out[7]:
<sympy.plotting.plot.Plot at 0x8d67990>
In [8]:
In [ ]:
# You will see that no graph is shown. The label p refers to the plot that is
# created, so you can now call p.show() to display the graph. You can also save
# the graph as an image file using the save() method, as follows:
p.save('line.png')
# This will save the plot to a file line.png in the current directory.
In [11]:
Out[11]:
[-2*x/3 + 2]
In the above example, we use the sympify() function to convert the input expression to a SymPy object and we
create a Symbol object to represent 'y' so that we can tell SymPy which variable we want to solve the equation
for. Then we solvethe expression to find y in terms of x by specifying y as the second argument to the solve()
function. Solution, is returned in terms of x, which is what we need for plotting.
Notice that this final expression is stored in a list, so before we can use it, we’ll have to extract it from the list as
below:
In [12]:
Out[12]:
-2*x/3 + 2
In [21]:
Out[21]:
<sympy.plotting.plot.Plot at 0x8e8b910>
This example brings out another difference between plotting in matplotlib and in SymPy. Here, using SymPy,
both lines are the same color, whereas matplotlib would have automatically made the lines different colors. To
set different colors for each line with SymPy, we’ll need to perform some extra steps, as shown in the following
code, which also adds a legend to the graph:
In [22]:
In [5]:
integrate(exp(x)*sin(x) + exp(x)*cos(x), x)
Out[5]:
𝑒𝑥 sin(𝑥)
In [6]:
Out[6]:
√⎯⎯2√⎯⎯𝜋
2
In [7]:
Out[7]:
1
In [8]:
# Solve x^2 - 2 = 0
solve(x**2 - 2, x)
Out[8]:
[−√⎯⎯2, √⎯⎯2]
In [10]:
Out[10]:
Out[11]:
3 + √⎯17⎯⎯⎯ : 1, − √⎯17⎯⎯⎯ + 3 : 1
{2 2 2 2 }
localhost:8888/notebooks/THE BOOK/4-SymPy And ODE/SymPy.ipynb 11/30
9/4/2019 SymPy
In [12]:
# Rewrite the Bessel function Jν(z) in terms of the spherical Bessel function jν(z)
besselj(nu, z).rewrite(jn)
Out[12]:
√⎯⎯2√⎯⎯𝑧𝑗𝜈− 12 (𝑧)
√⎯⎯𝜋
In [30]:
import sympy
sympy.factorial(40)
Out[30]:
815915283247897734345611269596115894272000000000
In [22]:
Out[22]:
'0.2999999999999999888977698'
In [23]:
sympy.Float(0.3, 25)
Out[23]:
0.2999999999999999888977698
In [24]:
sympy.Float('0.3', 25)
Out[24]:
0.3
Simplification
In [66]:
expr = 2 * (x**2 - x) - x * (x + 1)
In [67]:
expr
Out[67]:
2𝑥2 − 𝑥(𝑥 + 1) − 2𝑥
In [68]:
sympy.simplify(expr)
Out[68]:
𝑥(𝑥 − 3)
In [69]:
expr.simplify()
Out[69]:
𝑥(𝑥 − 3)
In [70]:
expr
Out[70]:
2𝑥2 − 𝑥(𝑥 + 1) − 2𝑥
In [71]:
In [72]:
expr
Out[72]:
2sin(𝑥)cos(𝑥)
In [73]:
sympy.trigsimp(expr)
Out[73]:
sin(2𝑥)
In [74]:
In [75]:
expr
Out[75]:
𝑒𝑥 𝑒𝑦
In [76]:
sympy.powsimp(expr)
Out[76]:
𝑒𝑥+𝑦
Expand
In [77]:
expr = (x + 1) * (x + 2)
In [78]:
sympy.expand(expr)
Out[78]:
𝑥2 + 3𝑥 + 2
In [79]:
sympy.sin(x + y).expand(trig=True)
Out[79]:
sin(𝑥)cos(𝑦) + sin(𝑦)cos(𝑥)
In [80]:
In [81]:
sympy.log(a * b).expand(log=True)
Out[81]:
log(𝑎) + log(𝑏)
In [82]:
sympy.exp(I*a + b).expand(complex=True)
Out[82]:
Out[83]:
𝑎𝑥 𝑏𝑥
In [84]:
sympy.exp(I*(a-b)*x).expand(power_exp=True)
Out[84]:
𝑒𝑖𝑎𝑥 𝑒−𝑖𝑏𝑥
Factor
In [85]:
sympy.factor(x**2 - 1)
Out[85]:
(𝑥 − 1)(𝑥 + 1)
In [86]:
Out[86]:
𝑥(sin(𝑧) + cos(𝑦))
In [87]:
sympy.logcombine(sympy.log(a) - sympy.log(b))
Out[87]:
log ( 𝑎𝑏 )
In [88]:
expr = x + y + x * y * z
In [89]:
expr.factor()
Out[89]:
𝑥𝑦𝑧 + 𝑥 + 𝑦
In [90]:
expr.collect(x)
Out[90]:
𝑥(𝑦𝑧 + 1) + 𝑦
In [91]:
expr.collect(y)
Out[91]:
𝑥 + 𝑦(𝑥𝑧 + 1)
In [92]:
In [93]:
expr.expand(trig=True).collect([sympy.cos(x),sympy.sin(x)]).collect(sympy.cos(y)-sympy.sin(
Out[93]:
In [94]:
Out[94]:
− 𝑥 +1 2 + 𝑥 +1 1
In [95]:
sympy.together(1 / (y * x + y) + 1 / (1+x))
Out[95]:
𝑦+1
𝑦(𝑥 + 1)
In [96]:
sympy.cancel(y / (y * x + y))
Out[96]:
1
𝑥+1
Numerical evaluation
In [103]:
sympy.N(1 + pi)
Out[103]:
4.14159265358979
localhost:8888/notebooks/THE BOOK/4-SymPy And ODE/SymPy.ipynb 16/30
9/4/2019 SymPy
In [104]:
sympy.N(pi, 50)
Out[104]:
3.1415926535897932384626433832795028841971693993751
In [105]:
(x + 1/pi).evalf(7)
Out[105]:
𝑥 + 0.3183099
In [106]:
In [107]:
Out[107]:
[0, 0.774, 0.642, 0.722, 0.944, 0.205, 0.974, 0.977, −0.87, −0.695
In [108]:
In [109]:
expr_func(1.0)
Out[109]:
0.773942685266709
In [110]:
In [111]:
import numpy as np
In [112]:
In [113]:
expr_func(xvalues)
Out[113]:
Calculus
In [114]:
f = sympy.Function('f')(x)
In [115]:
sympy.diff(f, x)
Out[115]:
𝑑 𝑓(𝑥)
𝑑𝑥
In [116]:
sympy.diff(f, x, x)
Out[116]:
𝑑2 𝑓(𝑥)
𝑑𝑥2
In [117]:
sympy.diff(f, x, 3)
Out[117]:
𝑑3 𝑓(𝑥)
𝑑𝑥3
In [118]:
g = sympy.Function('g')(x, y)
In [119]:
g.diff(x, y)
Out[119]:
∂2 𝑔(𝑥,𝑦)
∂𝑥∂𝑦
In [120]:
Out[120]:
∂5 𝑔(𝑥,𝑦)
∂𝑥3 ∂𝑦2
In [121]:
In [122]:
expr.diff(x)
Out[122]:
4𝑥3 + 3𝑥2 + 2𝑥 + 1
In [123]:
expr.diff(x, x)
Out[123]:
2 (6𝑥2 + 3𝑥 + 1)
In [124]:
expr = (x + 1)**3 * y ** 2 * (z - 1)
In [125]:
expr.diff(x, y, z)
Out[125]:
6𝑦(𝑥 + 1)2
In [126]:
In [127]:
expr.diff(x)
Out[127]:
expr = sympy.special.polynomials.hermite(x, 0)
In [129]:
expr.diff(x).doit()
Out[129]:
d = sympy.Derivative(sympy.exp(sympy.cos(x)), x)
In [131]:
Out[131]:
𝑑 𝑒cos (𝑥)
𝑑𝑥
In [132]:
d.doit()
Out[132]:
a, b = sympy.symbols("a, b")
x, y = sympy.symbols('x, y')
f = sympy.Function('f')(x)
In [134]:
sympy.integrate(f)
Out[134]:
∫ 𝑓(𝑥)𝑑𝑥
In [135]:
Out[135]:
𝑏
∫𝑎 𝑓(𝑥)𝑑𝑥
In [136]:
sympy.integrate(sympy.sin(x))
Out[136]:
−cos(𝑥)
In [137]:
Out[137]:
cos(𝑎) − cos(𝑏)
localhost:8888/notebooks/THE BOOK/4-SymPy And ODE/SymPy.ipynb 20/30
9/4/2019 SymPy
In [138]:
Out[138]:
√⎯⎯𝜋
2
In [139]:
In [140]:
Out[140]:
√⎯⎯𝜋𝑎𝑐
In [141]:
sympy.integrate(sympy.sin(x * sympy.cos(x)))
Out[141]:
∫ sin(𝑥cos(𝑥))𝑑𝑥
In [142]:
expr = sympy.sin(x*sympy.exp(y))
In [143]:
sympy.integrate(expr, x)
Out[143]:
−𝑒−𝑦 cos(𝑥𝑒𝑦 )
In [144]:
expr = (x + y)**2
In [145]:
sympy.integrate(expr, x)
Out[145]:
𝑥3 + 𝑥2 𝑦 + 𝑥𝑦2
3
In [146]:
sympy.integrate(expr, x, y)
Out[146]:
𝑥3 𝑦 + 𝑥2 𝑦2 + 𝑥𝑦3
3 2 3
In [147]:
Out[147]:
7
6
Series
In [148]:
x = sympy.Symbol("x")
In [149]:
f = sympy.Function("f")(x)
In [150]:
sympy.series(f, x)
Out[150]:
𝑓(0) + 𝑥 𝑑𝑥𝑑 𝑓(𝑥)|||| 𝑥=0 + 𝑥2 𝑑𝑥𝑑 2 𝑓(𝑥)||| + 𝑥6 𝑑𝑥𝑑 3 𝑓(𝑥)||| + 𝑥24 𝑑𝑥𝑑 4 𝑓(𝑥)||| + 120
𝑥5
2 2 3 3 4 4
| 𝑥=0 | 𝑥=0 | 𝑥=0
In [151]:
x0 = sympy.Symbol("{x_0}")
In [152]:
Out[152]:
In [153]:
Out[153]:
In [154]:
sympy.cos(x).series()
Out[154]:
1 − 𝑥2 + 𝑥24 + (𝑥6 )
2 4
In [155]:
sympy.sin(x).series()
Out[155]:
𝑥 − 𝑥6 + 120
3 𝑥5
+ (𝑥6 )
In [156]:
sympy.exp(x).series()
Out[156]:
1 + 𝑥 + 𝑥2 + 𝑥6 + 𝑥24 + 120
2 3 4 𝑥5
+ (𝑥6 )
In [157]:
(1/(1+x)).series()
Out[157]:
1 − 𝑥 + 𝑥2 − 𝑥3 + 𝑥4 − 𝑥5 + (𝑥6 )
In [158]:
In [159]:
expr.series(x, n=4)
Out[159]:
In [160]:
expr.series(y, n=4)
Out[160]:
expr.series(y).removeO().series(x).removeO().expand()
Out[161]:
− 61𝑥
5 5 5𝑥5 3 𝑥5 𝑦 2𝑥4 4 𝑥4 𝑦2 𝑥4 5𝑥3 3 𝑥3 𝑦 2 2 𝑥2
120 𝑦 + 12 𝑦 − 24 + 3 𝑦 − 2 + 24 − 6 𝑦 + 2 + 𝑥 𝑦 − 2 −
Limits
In [162]:
sympy.limit(sympy.sin(x) / x, x, 0)
Out[162]:
1
In [163]:
f = sympy.Function('f')
x, h = sympy.symbols("x, h")
In [164]:
In [165]:
sympy.limit(diff_limit.subs(f, sympy.cos), h, 0)
Out[165]:
−sin(𝑥)
In [166]:
sympy.limit(diff_limit.subs(f, sympy.sin), h, 0)
Out[166]:
cos(𝑥)
In [167]:
In [168]:
p = sympy.limit(expr/x, x, oo)
In [169]:
In [170]:
p, q
Out[170]:
1 , −1
(2 )
Sums and products
In [171]:
n = sympy.symbols("n", integer=True)
In [172]:
In [173]:
Out[173]:
∞ 1
∑
𝑛=1 𝑛2
In [174]:
x.doit()
Out[174]:
𝜋2
6
In [175]:
In [176]:
Out[176]:
7
∏ 𝑛
𝑛=1
In [177]:
x.doit()
Out[177]:
5040
In [178]:
x = sympy.Symbol("x")
In [179]:
Out[179]:
𝑒𝑥 − 1
Equations
In [180]:
x = sympy.symbols("x")
In [181]:
sympy.solve(x**2 + 2*x - 3)
Out[181]:
[−3, 1]
In [182]:
a, b, c = sympy.symbols("a, b, c")
In [183]:
sympy.solve(a * x**2 + b * x + c, x)
Out[183]:
1 −𝑏 + √⎯−4𝑎𝑐
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
+ 𝑏 2⎯ , − 1 𝑏 + √⎯−4𝑎𝑐
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
+ 𝑏 2⎯
[ 2𝑎 ( ) 2𝑎 ( )]
In [184]:
sympy.solve(sympy.sin(x) - sympy.cos(x), x)
Out[184]:
− 3𝜋 , 𝜋
[ 4 4]
In [185]:
sympy.solve(sympy.exp(x) + 2 * x, x)
Out[185]:
−LambertW 1
[ ( 2 )]
In [186]:
sympy.solve(x**5 - x**2 + 1, x)
Out[186]:
[RootOf (𝑥5 − 𝑥2 + 1,0), RootOf (𝑥5 − 𝑥2 + 1,1), RootOf (𝑥5 − 𝑥2 + 1,2), Roo
In [187]:
1 #s.solve(s.tan(x) - x, x)
Out[187]:
1
In [188]:
eq1 = x + 2 * y - 1
eq2 = x - y + 1
In [189]:
Out[189]:
𝑥 : − 1, 𝑦 : 2
[{ 3 3 }]
In [190]:
eq1 = x**2 - y
eq2 = y**2 - x
In [191]:
In [192]:
sols
Out[192]:
1 √ ⎯⎯3𝑖 1 √ ⎯⎯3𝑖
[{𝑥 : 0, 𝑦 : 0}, {𝑥 : 1, 𝑦 : 1}, {𝑥 : − 2 + 2 , 𝑦 : − 2 − 2 } , {
In [193]:
Out[193]:
Linear algebra
In [194]:
sympy.Matrix([1,2])
Out[194]:
1
[2]
In [195]:
sympy.Matrix([[1,2]])
Out[195]:
[1 2]
In [196]:
Out[196]:
1 2
[3 4]
In [197]:
Out[197]:
0 1 2 3
10 11 12 13
20 21 22 23
In [198]:
a, b, c, d = sympy.symbols("a, b, c, d")
In [199]:
In [200]:
Out[200]:
𝑎 𝑏
[ 𝑐 𝑑]
In [201]:
M * M
Out[201]:
𝑎2 + 𝑏𝑐 𝑎𝑏 + 𝑏𝑑
[ 𝑎𝑐 + 𝑐𝑑 𝑏𝑐 + 𝑑2 ]
In [202]:
x = sympy.Matrix(sympy.symbols("x_1, x_2"))
In [203]:
M * x
Out[203]:
𝑎𝑥1 + 𝑏𝑥2
[ 𝑐𝑥1 + 𝑑𝑥2 ]
In [204]:
p, q = sympy.symbols("p, q")
In [205]:
In [206]:
Out[206]:
1 𝑝
[𝑞 1]
In [207]:
b = sympy.Matrix(sympy.symbols("b_1, b_2"))
In [208]:
Out[208]:
𝑏1
[ 𝑏2 ]
In [209]:
x = M.solve(b)
x
Out[209]:
𝑏1 ( −𝑝𝑞+1
𝑝𝑞 + 1 − 𝑏2 𝑝
− 𝑏1𝑞 +) 𝑏2 −𝑝𝑞+1
−𝑝𝑞+1 −𝑝𝑞+1
In [210]:
x = M.LUsolve(b)
In [211]:
Out[211]:
𝑏1 − 𝑝(−−𝑝𝑞+1
𝑏1 𝑞+𝑏2 )
−𝑏1𝑞+𝑏2
−𝑝𝑞+1
In [212]:
x = M.inv() * b
In [213]:
Out[213]:
𝑏1 ( −𝑝𝑞+1
𝑝𝑞 + 1 − 𝑏2 𝑝
− 𝑏1𝑞 +) 𝑏2 −𝑝𝑞+1
−𝑝𝑞+1 −𝑝𝑞+1
In [ ]:
5 - Linear Optimization
Using
PulP
12
9/5/2019 First Simple Example
Simple Example
Suppose that we have the equation given below and we want to maximize the variable z i.e the objective
function.
Objective function
𝑧(𝑚𝑎𝑥) = 5𝑥1 + 4𝑥2
Constraints:
𝐶1 = 𝑥1 + 𝑥2 ≤ 5
𝐶2 = 10 ∗ 𝑥1 + 6 ∗ 𝑥2 ≤ 45
𝑥1 , 𝑥2 ≥ 0
We can solve the problem graphically but when the number of variables increases the problem becomes very
complicated. Here PulP comes to simplify our optimization problem.
In [8]:
import pulp
In [9]:
# Constraints
x1 = pulp.LpVariable('x1', lowBound=0)
x2 = pulp.LpVariable('x2', lowBound=0)
In [10]:
# Objective function, we use the += combined to add the objective and constraints
# to our problem.
z += 5*x1 + 4*x2
z += x1 + x2 <= 5
z += 10*x1 + 6*x2 <= 45
localhost:8888/notebooks/THE BOOK/5- Optimization Using Pulp/5-1 First Simple Example/First Simple Example.ipynb 1/2
9/5/2019 First Simple Example
In [11]:
Out[11]:
Problem:
MAXIMIZE
5*x1 + 4*x2 + 0
SUBJECT TO
_C1: x1 + x2 <= 5
_C2: 10 x1 + 6 x2 <= 45
VARIABLES
x1 Continuous
x2 Continuous
In [15]:
z.solve()
Out[15]:
In [16]:
# Now we can view our objective value (z) and the values of our variables, x1 and x2.
x1 3.75
x2 1.25
In [17]:
print(pulp.value(z.objective))
23.75
In [ ]:
localhost:8888/notebooks/THE BOOK/5- Optimization Using Pulp/5-1 First Simple Example/First Simple Example.ipynb 2/2
7/19/2019 CompanyX-Problem
Company X Problem
The Company X wants to know, How many products the company should make monthly. This company makes,
tables, sofas and chairs.
The Company needs to pay $75000 monthly, this includes, 1540 hours of work ($48.70 per hour).
Prices of each product:
Tables: $400per unit.
Sofas: $750per unit.
Chairs: $240per unit.
Variables
𝑋1 = Table
𝑋2 = Sofa
𝑋3 = Chair
Objective function
𝑧(𝑚𝑎𝑥) = (400 − 100)𝑋1 + (750 − 75 − 175)𝑋2 + (240 − 40)𝑋3 − 75000
This equation is simplified to:
In [1]:
import pulp
In [2]:
In [3]:
# Objective function
In [4]:
# Constraints
# Demand
In [5]:
Out[5]:
Company X:
MAXIMIZE
300*x1 + 500*x2 + 200*x3 + -75000
SUBJECT TO
_C1: 10 x1 + 7.5 x2 + 4 x3 <= 4350
VARIABLES
x1 Continuous
x2 Continuous
x3 Continuous
In [7]:
pulp.LpStatus[z.solve()]
Out[7]:
'Optimal'
In [8]:
Out[8]:
In [ ]:
For example, Ford Motor Company is a system whose goal consists of maximizing the profit
that can be earned by producing quality vehicles.
The term operations research was coined during World War II when British military leaders
asked scientists and engineers to analyze several military problems such as the deployment
of radar and the management of convoy, bombing, antisubmarine, and mining operations.
The scientific approach to decision making usually involves the use of one or more
mathematical models. A mathematical model is a mathematical representation of an actual
situation that may be used to make better decisions or simply to understand the actual
situation better.
The following example should clarify many of the key terms used to describe mathematical
models.
The example below is taken from the book Operations Research APPLICATIONS AND
ALGORITHMS by:
The example was solved by using Excel and Lingo/Lindo softwares, here I will be using PulP.
Powerco has three electric power plants that supply the needs of four cities. Each power
plant can supply the following numbers of kilowatt-hours (kwh) of electricity:
Plant_1 = 35 million
Plant_2 = 50 million
Plant_3 = 40 million
The peak power demands in these cities, which occur at the same time (2 P.M.), are as
follows (in kwh):
City1 = 45 million
City2 = 20 million
City3 = 30 million
City4 = 30 million
The costs of sending 1 million kwh of electricity from plant to city depend on the distance
the electricity must travel.
Solution:
To formulate Powerco’s problem as an LP, we begin by defining a variable for each decision
that Powerco must make.
Because Powerco must determine how much power is sent from each plant to each city, we
define i = 1, 2, 3, for plants and j = 1, 2, 3, 4, for cities.
Note: Each city will receive power from all plants. The table below shows the requirements.
TABLE 1:
Plant 1 $8 $6 $10 $9 35
In terms of these variables, the total cost of supplying the peak power demands to cities 1–4
may be written as:
First:
The total power supplied by each plant cannot exceed the plant’s capacity.
For example, the total amount of power sent from plant1 to the four cities cannot exceed 35
million kwh.
Each variable with first subscript 1 represents a shipment of power from plant 1, so we may
express this restriction by the LP constraint:
Because power is supplied by the power plants, each is a supply point. Analogously, a
constraint that ensures that the total quantity shipped from a plant does not exceed plant
capacity is a supply constraint.
The LP formulation of Powerco’s problem contains the following three supply constraints:
Second:
We need constraints that ensure that each city will receive sufficient power to meet its peak
demand. Each city demands power, so each is a demand point. For example,city 1 must
receive at least 45 million kwh. Each variable with second subscript 1 represents a shipment
of power to city 1, so we obtain the following constraint:
Similarly, we obtain a constraint for each of cities 2, 3, and 4. A constraint that ensures that
a location receives its demand is a demand constraint. Powerco must satisfy the following
four demand constraints:
Because all the xij’s must be nonnegative, we add the sign restrictions xij >= 0 (i = 1, 2,3 for
plants and j = 1, 2, 3, 4 for cties).
Combining the objective function, supply constraints, demand constraints, and sign
restrictions yields the following LP formulation of Powerco’s problem:
min z = 8*x11 + 6*x12 + 10*x13 + 9*x14 + 9*x21 + 12*x22 + 13*x23 + 7*x24 + 14*x31 +
9*x32 + 16*x33 + 5*x34
s.t:
xij >= 0 (i = 1, 2, 3; j = 1, 2, 3, 4)
9/5/2019 Power Company
# Power Company
In [2]:
import pulp
Then instantiate a problem class, we'll name it "My LP problem" and we're looking for an optimal minimum so
we use LpMinimize.
In [3]:
We then model our decision variables using the LpVariable class. In our example the variable xij had a lower
bound of 0.
In [4]:
The objective function and constraints are added to our model using the += operator, as usual.
In [5]:
# Objective function
my_lp_problem += 8*x11+6*x12+10*x13+9*x14+9*x21+12*x22+13*x23+7*x24+14*x31+9*x32+
16*x33+5*x34, "Z"
# Constraints
We have now constructed our problem and we can have a look at it.
In [6]:
my_lp_problem
Out[6]:
My LP Problem:
MINIMIZE
8*x11 + 6*x12 + 10*x13 + 9*x14 + 9*x21 + 12*x22 + 13*x23 + 7*x24 + 14*x31 +
9*x32 + 16*x33 + 5*x34 + 0
SUBJECT TO
_C1: x11 + x12 + x13 + x14 <= 35
VARIABLES
x11 Continuous
x12 Continuous
x13 Continuous
x14 Continuous
x21 Continuous
x22 Continuous
x23 Continuous
x24 Continuous
x31 Continuous
x32 Continuous
x33 Continuous
x34 Continuous
In [7]:
my_lp_problem.solve()
pulp.LpStatus[my_lp_problem.status]
Out[7]:
'Optimal'
We have also checked the status of the solver, there are 5 status codes:
We can now view our variable values and the minimum value of Z i.e the total cost in $.
We can use the varValue method to retrieve the values of our variables xij, and the pulp.value function to view
the minimum value of the objective function.
In [8]:
x11 0.0
x12 10.0
x13 25.0
x14 0.0
x21 45.0
x22 0.0
x23 5.0
x24 0.0
x31 0.0
x32 10.0
x33 0.0
x34 30.0
In [9]:
print (pulp.value(my_lp_problem.objective))
1020.0
In [ ]:
Introduction to Optimization
Numerical optimization is one of the most vital and common modern
applications of mathematics. It is considered a very important subject
and widely used in every engineering field, finance, industry
manufacturing etc.
Optimization is the problem of finding numerically the minimums (or
maximums or zeros) of a function. In this context, the function is called
cost function, or objective function, or energy.
Optimization is classified into Linear Optimization and Non-Linear
Op miza on. Linear op miza on covers 90% of the op miza on
problems.
13
Learning by examples with Anaconda 3 / 2019 - July
What is Pulp:
Pulp is a linear programming framework in Python. The aim of pulp is to
allow a practitioner or programmer to express Linear Programming (LP),
and Integer Programming (IP) models in python in a way similar to the
conventional mathematical notation.
Pulp can be interfaces with other solvers like, CPLEX, COIN, Gurobi,
COBYLA etc.
We will use Jupyter in Anaconda to solve optimization problems using
Pulp and SciPy.
14
Learning by examples with Anaconda 3 / 2019 - July
Optimization
of
Boiler Turbo Generators
15
Learning by examples with Anaconda 3 / 2019 - July
16
Learning by examples with Anaconda 3 / 2019 - July
17
Learning by examples with Anaconda 3 / 2019 - July
18
Learning by examples with Anaconda 3 / 2019 - July
Optimal Results
19
9/5/2019 Boiler Turbo Generators
import pulp
localhost:8888/notebooks/THE BOOK/5- Optimization Using Pulp/5-4 Boiler Turbo Generator/Boiler Turbo Generators.ipynb 1/5
9/5/2019 Boiler Turbo Generators
In [5]:
# Turbine1
# Turbine2
# Material Balances
# Power Purchased
# Demand
# Energy Balances
localhost:8888/notebooks/THE BOOK/5- Optimization Using Pulp/5-4 Boiler Turbo Generator/Boiler Turbo Generators.ipynb 2/5
9/5/2019 Boiler Turbo Generators
In [6]:
Model
Out[6]:
My LP Problem:
MINIMIZE
0.00983*Ep + 0.00261*HPS + 0.0239*PP + 0.0
SUBJECT TO
_C1: P1 <= 6250
VARIABLES
BF1 Continuous
BF2 Continuous
C Continuous
Ep Continuous
HE1 Continuous
HE2 Continuous
HPS Continuous
I1 Continuous
localhost:8888/notebooks/THE BOOK/5- Optimization Using Pulp/5-4 Boiler Turbo Generator/Boiler Turbo Generators.ipynb 3/5
9/5/2019 Boiler Turbo Generators
I2 Continuous
LE1 Continuous
LE2 Continuous
LPS Continuous
MPS Continuous
P1 Continuous
P2 Continuous
PP Continuous
Model.solve()
pulp.LpStatus[Model.status]
Out[7]:
'Optimal'
BF1 0.0
BF2 0.0
C 8169.7397
Ep 760.71409
HE1 128159.0
HE2 143377.0
HPS 380328.74
I1 136328.74
I2 244000.0
LE1 0.0
LE2 100623.0
LPS 100623.0
MPS 271536.0
P1 6250.0
P2 7060.7141
PP 11239.286
print (pulp.value(Model.objective))
1268.7547663046998
In [ ]:
localhost:8888/notebooks/THE BOOK/5- Optimization Using Pulp/5-4 Boiler Turbo Generator/Boiler Turbo Generators.ipynb 4/5
Learning by examples with Anaconda 3 / 2019 - July
20
Learning by examples with Anaconda 3 / 2019 - July
22
9/5/2019 Linear and Nonlinear Examples With SciPy
import numpy as np
from scipy.optimize import linprog
from numpy.linalg import solve
A_eq = np.array([[1,1,1]])
b_eq = np.array([999])
A_ub = np.array([
[1, 4, 8],
[40,30,20],
[3,2,4]])
b_ub = np.array([4500, 36000,2700])
c = np.array([70, 80, 85])
res = linprog(c, A_eq=A_eq, b_eq=b_eq, A_ub=A_ub, b_ub=b_ub,
bounds=(0, None))
print('Optimal value:', res.fun, '\nX:', res.x)
In [5]:
import numpy as np
from scipy.optimize import minimize
def objective(x):
return x[0]*x[3]*(x[0]+x[1]+x[2])+x[2]
def constraint1(x):
return x[0]*x[1]*x[2]*x[3]-25.0
def constraint2(x):
sum_eq = 40.0
for i in range(4):
sum_eq = sum_eq - x[i]**2
return sum_eq
# initial guesses
n = 4
x0 = np.zeros(n)
x0[0] = 1.0
x0[1] = 5.0
x0[2] = 5.0
x0[3] = 1.0
# show initial objective
print('Initial Objective: ' + str(objective(x0)))
# optimize
b = (1.0,5.0)
bnds = (b, b, b, b)
con1 = {'type': 'ineq', 'fun': constraint1}
con2 = {'type': 'eq', 'fun': constraint2}
cons = ([con1,con2])
solution = minimize(objective,x0,method='SLSQP',\
bounds=bnds,constraints=cons)
x = solution.x
# show final objective
print('Final Objective: ' + str(objective(x)))
# print solution
print('Solution')
print('x1 = ' + str(x[0]))
print('x2 = ' + str(x[1]))
print('x3 = ' + str(x[2]))
print('x4 = ' + str(x[3]))
Appendix
23
Learning by examples with Anaconda 3 / 2019 - July
References
24
Python - A Brief History
Python, was originally conceptualized by Guido van Rossum in
the late 1980s as a member of the National Research Institute of
Mathematics and Computer Science. Initially, it was designed as a
response to the ABC programming language that was also fore
grounded in the Netherlands. Among the main features of Python
compared to the ABC language was that Python had exception
handling and was targeted for the Amoeba operating system (go
Python!).
Python is not named after the snake. It’s named after the British
TV show Monty Python.
Python 3.0 was the next version and was released in December of
2008 (the latest version of Python is 3.6.4).