Skip to content

Commit cb04bb9

Browse files
Adding scipy & scikit-learn : codeiiest-dev#3
1 parent a0df922 commit cb04bb9

File tree

3 files changed

+251
-0
lines changed

3 files changed

+251
-0
lines changed
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# 3. SciPy
2+
3+
The scipy package contains various toolboxes dedicated to common issues in scientific computing. Its different submodules correspond to different applications, such as interpolation, integration, optimization, image processing, statistics, special functions, etc.
4+
5+
Scipy can be compared to other standard scientific-computing libraries, such as the GSL (GNU Scientific Library for C and C++), or Matlab’s toolboxes. scipy is the core package for scientific routines in Python; it is meant to operate efficiently on numpy arrays, so that numpy and scipy work hand in hand.
6+
7+
**Sub-packages of SciPy:**
8+
9+
- File input/output - scipy.io
10+
- Special Function - scipy.special
11+
- Linear Algebra Operation - scipy.linalg
12+
- Interpolation - scipy.interpolate
13+
- Optimization and fit - scipy.optimize
14+
- Statistics and random numbers - scipy.stats
15+
- Numerical Integration - scipy.integrate
16+
- Fast Fourier transforms - scipy.fftpack
17+
- Signal Processing - scipy.signal
18+
- Image manipulation – scipy.ndimage
19+
20+
#### **Import Syntax**
21+
*They all depend on numpy, but are mostly independent of each other. The standard way of importing Numpy and these Scipy modules is:*
22+
```py
23+
import numpy as np
24+
from scipy import stats
25+
```
26+
27+
### **File Input / Output package:**
28+
Scipy, I/O package, has a wide range of functions for work with different files format which are Matlab, Arff, Wave, Matrix Market, IDL, NetCDF, TXT, CSV and binary format :
29+
```py
30+
import numpy as np
31+
from scipy import io as sio
32+
array = np.ones((4, 4))
33+
sio.savemat('example.mat', {'ar': array})
34+
data = sio.loadmat(‘example.mat', struct_as_record=True)
35+
data['ar']
36+
```
37+
Output:
38+
```
39+
array([[ 1., 1., 1., 1.],
40+
[ 1., 1., 1., 1.],
41+
[ 1., 1., 1., 1.],
42+
[ 1., 1., 1., 1.]])
43+
```
44+
### **Special Function package**
45+
- scipy.special package contains numerous functions of mathematical physics.
46+
- SciPy special function includes Cubic Root, Exponential, Log sum Exponential, Lambert, Permutation and Combinations, Gamma, Bessel, hypergeometric, Kelvin, beta, parabolic cylinder, Relative Error Exponential, etc.
47+
48+
Examples :
49+
- ### **Cubic Root Function:**
50+
```py
51+
from scipy.special import cbrt
52+
scipy.special.cbrt(x)
53+
cb = cbrt([27, 64])
54+
print(cb)
55+
```
56+
Output :
57+
```
58+
array([3., 4.])
59+
```
60+
61+
* ### **Exponential Function:**
62+
63+
```py
64+
from scipy.special import exp10
65+
exp = exp10([1,10])
66+
print(exp)
67+
```
68+
Output :
69+
```
70+
[1.e+01 1.e+10]
71+
```
72+
- ### **Permutations & Combinations:**
73+
```py
74+
from scipy.special import perm
75+
per = perm(5, 2, exact = True)
76+
print(per)
77+
```
78+
Output :
79+
```
80+
20
81+
```
82+
## **Linear Algebra with SciPy**
83+
84+
- Linear Algebra of SciPy is an implementation of BLAS and ATLAS LAPACK libraries.
85+
- Performance of Linear Algebra is very fast compared to BLAS and LAPACK.
86+
- Linear algebra routine accepts two-dimensional array object and output is also a two-dimensional array.
87+
88+
### **Calculating determinant of a two-dimensional matrix** ###
89+
90+
```py
91+
from scipy import linalg
92+
import numpy as np
93+
#define square matrix
94+
two_d_array = np.array([ [4,5], [3,2] ])
95+
#pass values to det() function
96+
linalg.det( two_d_array )
97+
```
98+
Output :
99+
```
100+
-7.0
101+
```
102+
### **Calculating Inverse Matrix of a two-dimensional matrix** ###
103+
104+
```py
105+
from scipy import linalg
106+
import numpy as np
107+
# define square matrix
108+
two_d_array = np.array([ [4,5], [3,2] ])
109+
#pass value to function inv()
110+
linalg.inv( two_d_array )
111+
```
112+
Output :
113+
```
114+
array( [[-0.28571429, 0.71428571],
115+
[ 0.42857143, -0.57142857]] )
116+
```
117+
118+
### **Calculating Eigenvalues and Eigenvector of a two-dimensional matrix** ###
119+
120+
```py
121+
from scipy import linalg
122+
import numpy as np
123+
#define two dimensional array
124+
arr = np.array([[5,4],[6,3]])
125+
#pass value into function
126+
eg_val, eg_vect = linalg.eig(arr)
127+
#get eigenvalues
128+
print(eg_val)
129+
#get eigenvectors
130+
print(eg_vect)
131+
```
132+
Output :
133+
```
134+
[ 9.+0.j -1.+0.j]
135+
[ [ 0.70710678 -0.5547002 ]
136+
[ 0.70710678 0.83205029] ]
137+
```
138+
139+
### **Optimization and Fit in SciPy – scipy.optimize**
140+
141+
- Optimization provides a useful algorithm for minimization of curve fitting, multidimensional or scalar and root fitting.
142+
- Let's take an example of a Scalar Function, to find minimum scalar function.
143+
```py
144+
import matplotlib.pyplot as plt
145+
from scipy import optimize
146+
import numpy as np
147+
148+
def function(a):
149+
return a*2 + 20 * np.sin(a)
150+
plt.plot(a, function(a))
151+
plt.show()
152+
#use BFGS algorithm for optimization
153+
optimize.fmin_bfgs(function, 0)
154+
```
155+
Output :
156+
157+
![title](curve_fitting.PNG)
158+
159+
160+
### **Image Processing with SciPy – scipy.ndimage** ###
161+
* scipy.ndimage is a submodule of SciPy which is mostly used for performing an image related operation
162+
ndimage means the "n" dimensional image.
163+
* SciPy Image Processing provides Geometrics transformation (rotate, crop, flip), image filtering (sharp and de nosing), display image, image segmentation, classification and features extraction.
164+
165+
*MISC Package in SciPy contains prebuilt images which can be used to perform image manipulation task*
166+
167+
Example : Let's import an image from MISC and display it.
168+
```py
169+
from scipy import misc
170+
from matplotlib import pyplot as plt
171+
import numpy as np
172+
animal = misc.face()
173+
plt.imshow( animal )
174+
plt.show()
175+
```
176+
Output :
177+
(Try it yourself)
178+
179+
## References
180+
- [More on](https://scipy-lectures.org/intro/scipy.html#optimization-and-fit-scipy-optimize)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 4. Scikit-learn
2+
3+
Scikit-Learn, also known as sklearn, is Python’s premier general-purpose machine learning library. While you’ll find other packages that do better at certain tasks, Scikit-Learn’s versatility makes it the best starting place for most ML problems .It’s also a fantastic library for beginners because it offers a high-level interface for many tasks (e.g. preprocessing data, cross-validation, etc.).
4+
5+
Scikit-learn provides a range of supervised and unsupervised learning algorithms via a consistent interface in Python.The library is built upon the **SciPy (Scientific Python)** that must be installed before you can use scikit-learn.
6+
7+
Extensions or modules for SciPy care conventionally named [Scikits](http://scikits.appspot.com/scikits). As such, the module provides learning algorithms and is named scikit-learn.
8+
9+
Some popular groups of models provided by scikit-learn include:
10+
11+
- ***Clustering***: for grouping unlabeled data such as KMeans.
12+
- ***Cross Validation***: for estimating the performance of supervised models on unseen data.
13+
- ***Datasets***: for test datasets and for generating datasets with specific properties for investigating model behavior.
14+
- ***Dimensionality Reduction***: for reducing the number of attributes in data for summarization, visualization and feature selection such as Principal component analysis.
15+
- ***Ensemble methods***: for combining the predictions of multiple supervised models.
16+
- ***Feature extraction***: for defining attributes in image and text data.
17+
- ***Feature selection***: for identifying meaningful attributes from which to create supervised models.
18+
- ***Parameter Tuning***: for getting the most out of supervised models.
19+
- ***Manifold Learning***: For summarizing and depicting complex multi-dimensional data.
20+
- ***Supervised Models***: a vast array not limited to generalized linear models, discriminate analysis, naive bayes, lazy methods, neural networks, support vector machines and decision trees.
21+
22+
## Example: Classification and Regression Trees
23+
24+
An example to show you how easy it is to use the library.
25+
26+
In this example, we use the Classification and Regression decision tree algorithm (ahead in this tutorial) to model the Iris flower dataset (This dataset is provided as an example dataset with the library and is loaded. The classifier is fit on the data and then predictions are made on the training data).
27+
28+
Finally, the classification accuracy and a - [confusion matrix](https://machinelearningmastery.com/confusion-matrix-machine-learning/) is printed.
29+
30+
```py
31+
# Sample Decision Tree Classifier
32+
from sklearn import datasets
33+
from sklearn import metrics
34+
from sklearn.tree import DecisionTreeClassifier
35+
# load the iris datasets
36+
dataset = datasets.load_iris()
37+
# fit a CART model to the data
38+
model = DecisionTreeClassifier()
39+
model.fit(dataset.data, dataset.target)
40+
print(model)
41+
# make predictions
42+
expected = dataset.target
43+
predicted = model.predict(dataset.data)
44+
# summarize the fit of the model
45+
print(metrics.classification_report(expected, predicted))
46+
print(metrics.confusion_matrix(expected, predicted))
47+
```
48+
Running this example produces the following output, showing you the details of the trained model, the skill of the model according to some common metrics and a confusion matrix.
49+
50+
```
51+
DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
52+
max_features=None, max_leaf_nodes=None, min_samples_leaf=1,
53+
min_samples_split=2, min_weight_fraction_leaf=0.0,
54+
presort=False, random_state=None, splitter='best')
55+
precision recall f1-score support
56+
57+
0 1.00 1.00 1.00 50
58+
1 1.00 1.00 1.00 50
59+
2 1.00 1.00 1.00 50
60+
61+
avg / total 1.00 1.00 1.00 150
62+
63+
[[50 0 0]
64+
[ 0 50 0]
65+
[ 0 0 50]]
66+
```
67+
68+
We'll be using scikit-learn further to train various model on different supervised learning algorithms.
69+
70+
## References
71+
- [Scikit-Learn documentation](https://scikit-learn.org/stable/user_guide.html)
Loading

0 commit comments

Comments
 (0)