import matplotlib.pyplot as plt
from nltk.parse.stanford import StanfordParser
import numpy as np
%matplotlib inline
np.__version__
'1.13.1'
sentence = 'The quick brown fox jumps over the lazy dog'
# create parser object
scp = StanfordParser(path_to_jar='E:/stanford/stanford-parser-full-2015-04-20/stanford-parser.jar',
path_to_models_jar='E:/stanford/stanford-parser-full-2015-04-20/stanford-parser-3.5.2-models.jar')
scp
<nltk.parse.stanford.StanfordParser at 0x258638fe390>
# get parse tree
result = list(scp.raw_parse(sentence))
tree = result[0]
# print the constituency parse tree
print(tree)
(ROOT (NP (NP (DT The) (JJ quick) (JJ brown) (NN fox)) (NP (NP (NNS jumps)) (PP (IN over) (NP (DT the) (JJ lazy) (NN dog))))))
# visualize constituency parse tree
tree.draw()
from IPython.core.display import Image
Image('tree.png')
## vectors
x = [1, 2, 3, 4, 5]
x
[1, 2, 3, 4, 5]
# using numpy
import numpy as np
x = np.array([1, 2, 3, 4, 5])
print(x)
print(type(x))
[1 2 3 4 5] <class 'numpy.ndarray'>
## matrices
m = np.array([[1, 5, 2],
[4, 7, 4],
[2, 0, 9]])
# view matrix
print(m)
# view dimensions
print(m.shape)
[[1 5 2] [4 7 4] [2 0 9]] (3, 3)
# matrix transpose
print('Matrix Transpose:\n', m.transpose(), '\n')
Matrix Transpose: [[1 4 2] [5 7 0] [2 4 9]]
# matrix determinant
print ('Matrix Determinant:', np.linalg.det(m), '\n')
Matrix Determinant: -105.0
# matrix inverse
m_inv = np.linalg.inv(m)
print ('Matrix inverse:\n', m_inv, '\n')
Matrix inverse: [[-0.6 0.42857143 -0.05714286] [ 0.26666667 -0.04761905 -0.03809524] [ 0.13333333 -0.0952381 0.12380952]]
# identity matrix (result of matrix x matrix_inverse)
iden_m = np.dot(m, m_inv)
iden_m = np.round(np.abs(iden_m), 0)
print ('Product of matrix and its inverse:\n', iden_m)
Product of matrix and its inverse: [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]]
# eigendecomposition
m = np.array([[1, 5, 2],
[4, 7, 4],
[2, 0, 9]])
eigen_vals, eigen_vecs = np.linalg.eig(m)
print('Eigen Values:', eigen_vals, '\n')
print('Eigen Vectors:\n', eigen_vecs)
Eigen Values: [ -1.32455532 11.32455532 7. ] Eigen Vectors: [[-0.91761521 0.46120352 -0.46829291] [ 0.35550789 0.79362022 -0.74926865] [ 0.17775394 0.39681011 0.46829291]]
# SVD
m = np.array([[1, 5, 2],
[4, 7, 4],
[2, 0, 9]])
U, S, VT = np.linalg.svd(m)
print ('Getting SVD outputs:-\n')
print('U:\n', U, '\n')
print('S:\n', S, '\n')
print('VT:\n', VT, '\n')
Getting SVD outputs:- U: [[ 0.3831556 -0.39279153 0.83600634] [ 0.68811254 -0.48239977 -0.54202545] [ 0.61619228 0.78294653 0.0854506 ]] S: [ 12.10668383 6.91783499 1.25370079] VT: [[ 0.36079164 0.55610321 0.74871798] [-0.10935467 -0.7720271 0.62611158] [-0.92621323 0.30777163 0.21772844]]
# descriptive statistics
import scipy as sp
import numpy as np
# get data
nums = np.random.randint(1,20, size=(1,15))[0]
print('Data: ', nums)
Data: [ 6 13 3 8 16 14 15 6 11 16 16 9 2 18 1]
# get descriptive stats
print ('Mean:', sp.mean(nums))
print ('Median:', sp.median(nums))
print ('Mode:', sp.stats.mode(nums))
print ('Standard Deviation:', sp.std(nums))
print ('Variance:', sp.var(nums))
print ('Skew:', sp.stats.skew(nums))
print ('Kurtosis:', sp.stats.kurtosis(nums))
Mean: 10.2666666667 Median: 11.0 Mode: ModeResult(mode=array([16]), count=array([3])) Standard Deviation: 5.495048276 Variance: 30.1955555556 Skew: -0.283914098226024 Kurtosis: -1.3113890167335824