├── control ├── README.md ├── adaptive_control │ └── basic_first_order │ │ ├── README.md │ │ └── basic_first_order.py ├── fuzzy_control │ ├── README.md │ ├── __pycache__ │ │ └── fuzzy_tools.cpython-36.pyc │ ├── adaptive_fuzzy_SISO.py │ ├── adaptive_fuzzy_quadrotor.py │ ├── ball_beam.fis │ ├── ball_beam.slx │ ├── batch_least_squares_estimaton.py │ ├── example_ball_beam.py │ ├── example_plotting.py │ ├── fuzzy_batch_LSE.py │ ├── fuzzy_batch_LSE_multi_input.py │ ├── fuzzy_gradient.py │ ├── fuzzy_recursive_LSE.py │ ├── fuzzy_tools.py │ ├── fuzzy_tools.pyc │ ├── lilly_4_10.py │ └── lilly_6_6.py └── sliding_mode │ ├── dobby.slx │ ├── inverted_pendulum_HSMC.py │ ├── quad_1.py │ ├── slotine_7.1.1.py │ ├── slotine_7.1.3.py │ ├── smc.py │ └── temp.py ├── misc └── email.py ├── ml ├── Advanced Regression Techniques │ ├── .ipynb_checkpoints │ │ └── Advanced_Regression_Techniques-checkpoint.ipynb │ ├── Advanced_Regression_Techniques.ipynb │ ├── results.csv │ ├── test.csv │ └── train.csv ├── IITMAA │ ├── .ipynb_checkpoints │ │ └── practice-checkpoint.ipynb │ ├── Test.csv │ ├── Train.csv │ ├── practice.ipynb │ └── sample_submission.csv ├── Untitled.ipynb ├── code.py ├── educated_attempt.ipynb ├── movie_recommender.ipynb ├── sklearn.ipynb └── titanic │ ├── .ipynb_checkpoints │ └── titanic-checkpoint.ipynb │ ├── results.csv │ ├── test.csv │ ├── titanic.ipynb │ └── train.csv ├── practice └── bst │ ├── bst.py │ ├── bst_iterator.py │ └── t2sum_bst.py └── quad ├── .DS_Store ├── ARMING.h ├── ATTITUDE.h ├── INITIALISE.h ├── MPU9250.cpp ├── MPU9250.h ├── PERIPHERALS.h ├── PID.h ├── TELEMETRY.h ├── angledata.py ├── baseStaStuff.py ├── baseStation.py ├── fort.1 ├── main.cpp ├── pitchPrevData.png ├── quickPlot.py └── test.py /control/README.md: -------------------------------------------------------------------------------- 1 | # control_practice 2 | Attempts to learn non linear control 3 | 4 | * __sliding_mode:__ simulations of smc controllers 5 | * __adaptive_control:__ simulations of basic adaptive control 6 | * __fuzzy_control__: basic fuzzy toolkit, fuzzy control, estimation, fuzzy estimation 7 | -------------------------------------------------------------------------------- /control/adaptive_control/basic_first_order/README.md: -------------------------------------------------------------------------------- 1 | The richness of the reference signal has an impact on the parameter estimation: 2 | 3 | ![basic_first_order_1](https://user-images.githubusercontent.com/25063767/37861211-91bd2478-2f5a-11e8-8a4c-e525793583e0.png) 4 | ![basic_first_order_2](https://user-images.githubusercontent.com/25063767/37861204-7c7ef35c-2f5a-11e8-9ba7-cd0d10e4c4cb.png) 5 | 6 | As is seen above, with a richer reference signal, the parameter estimates also converge to the real values. However the response tracking always converges! 7 | -------------------------------------------------------------------------------- /control/adaptive_control/basic_first_order/basic_first_order.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | from scipy.integrate import ode 3 | import numpy as np 4 | from numpy import sin 5 | 6 | gamma = 2 7 | 8 | def model(t, X, params): 9 | 10 | y, ym, ar_cap, ay_cap = X 11 | 12 | if params == 1: 13 | r = 4*sin(3*t) 14 | else: 15 | r = 4 16 | 17 | e = y - ym 18 | u = ar_cap*r + ay_cap*y 19 | 20 | ydot = y + 3*u 21 | ymdot = -4*ym +4*r 22 | ar_cap_dot = -gamma*e*r 23 | ay_cap_dot = -gamma*e*y 24 | 25 | return [ydot, ymdot, ar_cap_dot, ay_cap_dot] 26 | 27 | def main(option): 28 | 29 | X0 = [0, 0, 0, 0] 30 | t0 = 0 31 | t1 = 6 32 | dt = 0.01 33 | 34 | r = ode(model).set_integrator('vode', method='bdf') 35 | r.set_initial_value(X0, t0).set_f_params(option) 36 | 37 | x = np.zeros((601, 4)) 38 | t = [] 39 | i = 0 40 | 41 | while r.successful() and r.t < t1: 42 | r.integrate(r.t + dt) 43 | x[i] = r.y 44 | t.append(r.t) 45 | i += 1 46 | 47 | return x, t 48 | 49 | if __name__ == '__main__': 50 | x1, t1 = main(1) 51 | x2, t2 = main(0) 52 | 53 | y1 = np.reshape(x1[:,0], (601,)) 54 | ym1 = np.reshape(x1[:,1], (601,)) 55 | ar_cap1 = np.reshape(x1[:,2], (601,)) 56 | ay_cap1 = np.reshape(x1[:,3], (601,)) 57 | ar_ideal1 = (4/3)*np.ones((601,)) 58 | ay_ideal1 = -(5/3)*np.ones((601,)) 59 | 60 | y2 = np.reshape(x2[:,0], (601,)) 61 | ym2 = np.reshape(x2[:,1], (601,)) 62 | ar_cap2 = np.reshape(x2[:,2], (601,)) 63 | ay_cap2 = np.reshape(x2[:,3], (601,)) 64 | ar_ideal2 = (4/3)*np.ones((601,)) 65 | ay_ideal2 = -(5/3)*np.ones((601,)) 66 | 67 | plt.subplot(211) 68 | plt.plot(t1 , y1, 'r', t1, ym1, 'k', t2, y2, 'b', t2, ym2, 'g') 69 | plt.ylabel('tracking performance') 70 | plt.grid() 71 | 72 | plt.subplot(212) 73 | plt.plot(t1, ar_cap1, 'b', t1, ay_cap1, 'k', t1, ar_ideal1, t1, ay_ideal1) 74 | plt.ylabel('parameter estimation') 75 | plt.grid() 76 | 77 | plt.show() 78 | -------------------------------------------------------------------------------- /control/fuzzy_control/README.md: -------------------------------------------------------------------------------- 1 | Toolkit for fuzzy logic implementation 2 | ======================================= 3 | 4 | __Version:__ 1.1.1 5 | 6 | Support: 7 | -------- 8 | 9 | 1. Membership functions (type) 10 | * Triangular 11 | * Singleton 12 | * Gaussian 13 | 14 | 2. Defuzzification (method) 15 | * Centre average defuzzification 16 | 17 | Guide: 18 | ------ 19 | 20 | `membership(type, params, univ, name)`: 21 | 22 | - the main class that holds info about a particular membership function, 23 | the different types of membership functions supported are listen in __Support__ 24 | for each membership function type, there is an expected parameter vector that 25 | must be supplied: 26 | 27 | - each class holds the following data: 28 | 1. `membership.type` - type of membership function 29 | 2. `membership.univ` - universe of definition membership function 30 | 3. `membership.params` - parameters defining the membership function 31 | 4. `membership.fuzz_val` - fuzzified value of a partiuclar variable related to the membership function 32 | 5. `membership.memship_arr` - the array used to plot the membership function 33 | 6. `membership.name` - the name of the memebrship function 34 | 35 | | membership | parameter form | description | 36 | | :--------: | :------------: | :---------: | 37 | | trimf | [a, b, c] | a, b, c represent the left, center and right points of the triangle | 38 | | singleton | [a] | a represents the location of the singleton | 39 | | gaussian | [a, b, c] | a, b represent the centre and spread, c is a keyword argument | 40 | 41 | `make_memship(membership)`: 42 | 43 | - __Input:__ membership class instance 44 | - __Operation:__ accesses the `membership.memship_arr` array by filling it with the y-values of the membership 45 | - __Use:__ for plotting membership functions 46 | 47 | `fuzzify(x, membership)`: 48 | - __Input:__ variable to be fuzzified, membership class instance of fuzzy set membership 49 | - __Returns:__ fuzzy value of variable corresponding to that membership 50 | 51 | -------------------------------------------------------------------------------- /control/fuzzy_control/__pycache__/fuzzy_tools.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Thalaivar/python/6702edc229e9938846f9ba757b759d0bb183aa1e/control/fuzzy_control/__pycache__/fuzzy_tools.cpython-36.pyc -------------------------------------------------------------------------------- /control/fuzzy_control/adaptive_fuzzy_SISO.py: -------------------------------------------------------------------------------- 1 | import fuzzy_tools as fuzz 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from scipy.integrate import ode 5 | import math 6 | 7 | def make_fuzzy_sets(): 8 | # define universe of e 9 | e_univ = np.linspace(-3, 3, 1000) 10 | 11 | # centres and spreads 12 | c = [-1.5, 0, 1.5] 13 | sig = 0.484 14 | 15 | # make the fuzzy sets 16 | names = ["F1", "F2", "F3"] 17 | memship = [] 18 | for i in range(len(c)): 19 | p = [c[i], sig, "none"] 20 | memship.append(fuzz.membership("gauss", p, e_univ, names[i])) 21 | 22 | return memship 23 | 24 | def model(t, X, params): 25 | x, theta = X 26 | lam, gam , memship = params 27 | 28 | phi = get_fuzzy_basis(x, memship) 29 | 30 | 31 | def get_fuzzy_basis(x, memship): 32 | fuzz_basis = np.zeros((len(memship,))) 33 | for i in range(len(memship)): 34 | fuzz.fuzzify(x, memship[i]) 35 | fuzz_basis[i] = memship[i].fuzz_val 36 | 37 | return fuzz_basis 38 | 39 | def simulate(memship): 40 | theta_0 = np.zeros((5,1)) 41 | x0 = [0, theta_0] 42 | t0 = 0 43 | dt = 0.01 44 | t1 = 10 45 | 46 | params = [1.2, 1.2, memship] 47 | r = ode(model).set_integrator("vode") 48 | r.set_initial_value(x0, t0).set_f_params(params) 49 | 50 | y = np.zeros((1001,)) 51 | t = [] 52 | i = 0 53 | 54 | while r.successful() and r.t < t1: 55 | r.integrate(r.t + dt) 56 | y[i] = r.y 57 | t.append(r.t) 58 | i += 1 59 | 60 | return [y, t] 61 | 62 | def main(): 63 | memship = make_fuzzy_sets() 64 | 65 | y, t = simulate(memship) 66 | 67 | if __name__ == '__main__': 68 | main() 69 | -------------------------------------------------------------------------------- /control/fuzzy_control/adaptive_fuzzy_quadrotor.py: -------------------------------------------------------------------------------- 1 | import fuzzy_tools as fuzz 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import math 5 | from numpy import tanh, sin, cos 6 | from scipy.integrate import ode 7 | 8 | # model parameters 9 | b = 0.0000313 10 | d = 0.00000075 11 | Ixx = 0.0075 12 | Iyy = Ixx 13 | Izz = 0.013 14 | l = 0.23 15 | 16 | # literals 17 | n_inputs = 6 18 | n_angles = 3 19 | n_sets = 5 20 | n_rules = 25 21 | 22 | # angle error to desired rate 23 | ang_to_rat_phi = 1.0 24 | ang_to_rat_theta = 1.0 25 | ang_to_rat_psi = 1.0 26 | 27 | # smc parameters 28 | phi_lam = 1.0 29 | theta_lam = 1.0 30 | psi_lam = 1.0 31 | phi_k = 1.0 32 | theta_k = 1.0 33 | psi_k = 1.0 34 | phi_eta = 1.0 35 | theta_eta = 1.0 36 | psi_eta = 1.0 37 | 38 | # adaptation rates 39 | adap_gain_phi = 1.0 40 | adap_gain_theta = 1.0 41 | adap_gain_psi = 1.0 42 | 43 | # global variables to calculate sdot 44 | s_prev = np.zeros((n_angles,)) 45 | 46 | # setup fuzzy sets 47 | def setup_fuzz_sys(): 48 | # define universes of input variables 49 | e_phi_univ = np.linspace(-math.pi/2.0, math.pi/2.0, 1000) 50 | e_theta_univ = np.linspace(-math.pi/2.0, math.pi/2.0, 1000) 51 | e_psi_univ = np.linspace(-math.pi, math.pi, 1000) 52 | de_phi_univ = np.linspace(-1.74, 1.74, 1000) 53 | de_theta_univ = np.linspace(-1.74, 1.74, 1000) 54 | de_psi_univ = np.linspace(-1.74, 1.74, 1000) 55 | 56 | e_univ = [e_phi_univ, e_theta_univ, e_psi_univ] 57 | de_univ = [de_phi_univ, de_theta_univ, de_psi_univ] 58 | 59 | # define centres for each input 60 | e_phi_c = np.array([-0.785, -0.3925, 0, 0.3925, 0.785]) 61 | e_theta_c = np.array([-0.785, -0.3925, 0, 0.3925, 0.785]) 62 | e_psi_c = np.array([-math.pi/2, -0.785, 0 , 0.785, math.pi/2]) 63 | de_phi_c = np.array([-0.872, -0.436, 0, 0.436, 0.785]) 64 | de_theta_c = np.array([-0.872, -0.436, 0, 0.436, 0.785]) 65 | de_psi_c = np.array([-0.872, -0.436, 0, 0.436, 0.785]) 66 | 67 | e_c = [e_phi_c, e_theta_c, e_psi_c] 68 | de_c = [de_phi_c, de_theta_c, de_psi_c] 69 | 70 | # define spreads 71 | sig = 0.184 72 | 73 | # make memberships 74 | e_membership = [] 75 | de_membership = [] 76 | for i in range(n_angles): 77 | temp1 = [] 78 | temp2 = [] 79 | 80 | # get parameters for input 81 | c = e_c[i] 82 | dc = de_c[i] 83 | for j in range(n_sets): 84 | # make parameter vectors for sets 85 | p1 = [c[j], sig, "none"] 86 | p2 = [dc[j], sig, "none"] 87 | temp1.append(fuzz.membership("gauss", p1, e_univ[i], "none")) 88 | temp2.append(fuzz.membership("gauss", p2, de_univ[i], "none")) 89 | 90 | e_membership.append(temp1) 91 | de_membership.append(temp2) 92 | 93 | return [e_membership, de_membership] 94 | 95 | # for one angle, and therefor for one control input 96 | def get_fuzz_basis(e, e_mem, de, de_mem): 97 | temp = np.zeros((n_rules, 1)) 98 | 99 | # get fuzzy membership values fo reach membership func 100 | for i in range(n_sets): 101 | fuzz.fuzzify(e, e_mem[i]) 102 | fuzz.fuzzify(de, de_mem[i]) 103 | 104 | # get rule premise values 105 | k = 0 106 | for i in range(n_sets): 107 | for j in range(n_sets): 108 | temp[k,0] = e_mem[i].fuzz_val*de_mem[j].fuzz_val 109 | k += 1 110 | 111 | # get basis funcs 112 | temp = temp/(np.sum(temp)) 113 | return temp 114 | 115 | def model(t, X, params): 116 | phi, dphi, theta, dtheta, psi, dpsi, b_phi, b_theta, b_psi = X 117 | e_mem, de_mem, step_size = params 118 | b = np.array([b_phi, b_theta, b_psi]) 119 | 120 | e, de = get_err(phi, dphi, theta, dtheta, psi, dpsi) 121 | 122 | # get basis funcs 123 | eps = np.zeros_like(e) 124 | for i in range(n_angles): 125 | eps[i] = get_fuzz_basis(e[i], e_mem[i], de[i], de_mem[i]) 126 | 127 | # get estimates of control outputs 128 | u = np.zeros_like(eps) 129 | for i in range(n_angles): 130 | u[i] = np.dot(np.transpose(eps[i]), b[i]) 131 | 132 | # get sliding surfaces 133 | s = get_s(e, de) 134 | 135 | # get s_dot 136 | sdot = (s - s_prev)/step_size 137 | s_prev = s 138 | 139 | x1dot = dphi 140 | x2dot = a1*dtheta*dpsi + b1*u[0] 141 | x3dot = dtheta 142 | x4dot = a2*dpsi*dphi + b2*u[1] 143 | x5dot = dpsi 144 | x6dot = a3*dphi*dtheta + b3*u[2] 145 | b_phi_dot = -0.5*adap_gain_phi*eps[0]*(sdot[0] + phi_k*s[0] + phi_eta*tanh(s[0])) 146 | b_theta_dot = -0.5*adap_gain_theta*eps[1]*(sdot[1] + theta_k*s[1] + theta_eta*tanh(s[1])) 147 | b_psi_dot = -0.5*adap_gain_psi*eps[2]*(sdot[2] + psi_k*s[2] + psi_eta*tanh(s[2])) 148 | 149 | return [x1dot, x2dot, x3dot, x4dot, x5dot, x6dot, b_phi_dot, b_theta_dot, b_psi_dot] 150 | 151 | def get_s(e, de): 152 | s = np.zeros((n_angles,)) 153 | params = np.array([phi_lam, theta_lam, psi_lam]) 154 | for i in range(n_angles): 155 | s[i] = de[i] + params[i]*e[i] 156 | 157 | return s 158 | 159 | def get_err(phi, dphi, theta, dtheta, psi, dpsi): 160 | # stationary desired values 161 | e_phi = phi - 0.1744 162 | e_theta = theta + 0.2616 163 | e_psi = psi 164 | e = np.array([e_phi, e_theta, e_psi]) 165 | 166 | # get desired rates by passing through P controller 167 | dphi_d = ang_to_rat_phi*e_phi 168 | dtheta_d = ang_to_rat_theta*e_theta 169 | dpsi_d = ang_to_rat_psi*e_psi 170 | 171 | # get angle rate errors 172 | de_phi = dphi - dphi_d 173 | de_theta = dtheta - dtheta_d 174 | de_psi = dpsi - dpsi_d 175 | de = np.array([de_phi, de_theta, de_psi]) 176 | 177 | return [e, de] 178 | 179 | def main(): 180 | e_mem, de_mem = setup_fuzz_sys() 181 | 182 | # initial condition 183 | b_phi_0 = np.zeros((n_rules, 1)) 184 | b_theta_0 = np.zeros((n_rules, 1)) 185 | b_psi_0 = np.zeros((n_rules, 1)) 186 | X0 = [0, 0, 0, 0, 0, 0, b_phi_0, b_theta_0, b_psi_0] 187 | t0 = 0 188 | # integrator parameters 189 | dt = 0.001 190 | t1 = 20 191 | params = [e_mem, de_mem, dt] 192 | 193 | x = np.zeros((20001, 6)) 194 | t = [] 195 | r = ode(model).set_integrator('vode').set_initial_value(X0, t0).set_f_params(params) 196 | i = 0 197 | while r.successful() and r.t < t1: 198 | r.integrate(r.t + dt) 199 | x[i] = r.y 200 | t.append(r.t) 201 | i += 1 202 | 203 | if __name__ == '__main__': 204 | main() 205 | -------------------------------------------------------------------------------- /control/fuzzy_control/ball_beam.fis: -------------------------------------------------------------------------------- 1 | [System] 2 | Name='ball_beam' 3 | Type='mamdani' 4 | Version=2.0 5 | NumInputs=2 6 | NumOutputs=1 7 | NumRules=25 8 | AndMethod='prod' 9 | OrMethod='max' 10 | ImpMethod='min' 11 | AggMethod='max' 12 | DefuzzMethod='centroid' 13 | 14 | [Input1] 15 | Name='e-fuzz' 16 | Range=[-0.5 0.5] 17 | NumMFs=5 18 | MF1='NL':'trimf',[-1 -0.5 -0.25] 19 | MF2='NS':'trimf',[-0.5 -0.25 0] 20 | MF3='Z':'trimf',[-0.25 0 0.25] 21 | MF4='PS':'trimf',[0 0.25 0.5] 22 | MF5='PL':'trimf',[0.25 0.5 1] 23 | 24 | [Input2] 25 | Name='edot-fuzz' 26 | Range=[-4 4] 27 | NumMFs=5 28 | MF1='NL':'trimf',[-6 -4 -2] 29 | MF2='NS':'trimf',[-4 -2 0] 30 | MF3='Z':'trimf',[-2 0 2] 31 | MF4='PS':'trimf',[0 2 4] 32 | MF5='PL':'trimf',[2 4 6] 33 | 34 | [Output1] 35 | Name='u' 36 | Range=[-10 10] 37 | NumMFs=5 38 | MF1='NL':'trimf',[-10 -10 -10] 39 | MF2='NS':'trimf',[-5 -5 -5] 40 | MF3='Z':'trimf',[0 0 0] 41 | MF4='PS':'trimf',[5 5 5] 42 | MF5='PL':'trimf',[10 10 10] 43 | 44 | [Rules] 45 | 1 1, 1 (1) : 1 46 | 1 2, 1 (1) : 1 47 | 1 3, 1 (1) : 1 48 | 1 4, 2 (1) : 1 49 | 1 5, 3 (1) : 1 50 | 2 1, 1 (1) : 1 51 | 2 2, 1 (1) : 1 52 | 2 3, 2 (1) : 1 53 | 2 4, 3 (1) : 1 54 | 2 5, 4 (1) : 1 55 | 3 1, 1 (1) : 1 56 | 3 2, 2 (1) : 1 57 | 3 3, 3 (1) : 1 58 | 3 4, 4 (1) : 1 59 | 3 5, 5 (1) : 1 60 | 4 1, 2 (1) : 1 61 | 4 2, 3 (1) : 1 62 | 4 3, 4 (1) : 1 63 | 4 4, 5 (1) : 1 64 | 4 5, 5 (1) : 1 65 | 5 1, 3 (1) : 1 66 | 5 2, 4 (1) : 1 67 | 5 3, 5 (1) : 1 68 | 5 4, 5 (1) : 1 69 | 5 5, 5 (1) : 1 70 | -------------------------------------------------------------------------------- /control/fuzzy_control/ball_beam.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Thalaivar/python/6702edc229e9938846f9ba757b759d0bb183aa1e/control/fuzzy_control/ball_beam.slx -------------------------------------------------------------------------------- /control/fuzzy_control/batch_least_squares_estimaton.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from numpy import sin, log 4 | import math 5 | from numpy.linalg import inv 6 | 7 | no_of_estimates = [] 8 | estimated_f = [] 9 | for i in range(3): 10 | no_of_estimates.append(i+5) 11 | x_1 = np.linspace(1, 2, 1000) 12 | f_1 = 3*sin(x_1**3) - 4*log(x_1) + 5*(x_1**2) 13 | for i in range(len(no_of_estimates)): 14 | x = np.linspace(1, 2, no_of_estimates[i]) 15 | f = 3*sin(x**3) - 4*log(x) + 5*(x**2) 16 | Y = np.reshape(f, (no_of_estimates[i],1)) 17 | phi = np.zeros((no_of_estimates[i], 3)) 18 | 19 | for j in range(no_of_estimates[i]): 20 | phi[j,0] = math.sin(x[j]**3) 21 | phi[j,1] = math.log(x[j]) 22 | phi[j,2] = x[j]**2 23 | 24 | phi_t = np.transpose(phi) 25 | phi_t_phi_inv = inv(np.dot(phi_t, phi)) 26 | 27 | theta_estimate = np.dot(phi_t_phi_inv, np.dot(phi_t, Y)) 28 | 29 | f_estimate = theta_estimate[0,0]*sin(x**3) + theta_estimate[1,0]*log(x) + theta_estimate[2,0]*(x**2) 30 | estimated_f.append(f_estimate) 31 | 32 | plt.plot(x_1, f_1, x, f_estimate) 33 | plt.show() 34 | -------------------------------------------------------------------------------- /control/fuzzy_control/example_ball_beam.py: -------------------------------------------------------------------------------- 1 | import fuzzy_tools as fuzz 2 | import numpy as np 3 | from scipy.integrate import ode 4 | import math 5 | import matplotlib.pyplot as plt 6 | 7 | u = [] 8 | 9 | def ball_beam_rule_set(x, y): 10 | z = np.zeros((5,)) 11 | z1 = 0 12 | 13 | # 0 - NL 14 | # 1 - NS 15 | # 2- Z 16 | # 3- PS 17 | # 4- PL 18 | 19 | # get firing of rule for output being NL 20 | for i in range(len(x)): 21 | z1 += x[0]*y[0] 22 | z1 += x[0]*y[1] 23 | z1 += x[0]*y[2] 24 | z1 += x[1]*y[0] 25 | z1 += x[1]*y[1] 26 | z1 += x[2]*y[0] 27 | z[0] = z1 28 | z1 = 0 29 | 30 | # get firing of rule for output being NS 31 | for i in range(len(x)): 32 | z1 += x[0]*y[3] 33 | z1 += x[1]*y[2] 34 | z1 += x[2]*y[1] 35 | z1 += x[3]*y[0] 36 | z[1] = z1 37 | z1 = 0 38 | 39 | # get firing of rule for output being Z 40 | for i in range(len(x)): 41 | z1 += x[0]*y[4] 42 | z1 += x[1]*y[3] 43 | z1 += x[2]*y[2] 44 | z1 += x[3]*y[1] 45 | z1 += x[4]*y[0] 46 | z[2] = z1 47 | z1 = 0 48 | 49 | # get firing of rule for output being PS 50 | for i in range(len(x)): 51 | z1 += x[1]*y[4] 52 | z1 += x[2]*y[3] 53 | z1 += x[3]*y[2] 54 | z1 += x[4]*y[1] 55 | z[3] = z1 56 | z1 = 0 57 | 58 | # get firing of rule of output being PL 59 | for i in range(len(x)): 60 | z1 += x[2]*y[4] 61 | z1 += x[3]*y[3] 62 | z1 += x[3]*y[4] 63 | z1 += x[4]*y[2] 64 | z1 += x[4]*y[3] 65 | z1 += x[4]*y[4] 66 | z[4] = z1 67 | z1 = 0 68 | 69 | return z 70 | 71 | def model(t, X, params): 72 | x1, x2 = X 73 | e = -x1 74 | edot = -x2 75 | e_mem, edot_mem, v_mem = params 76 | 77 | e_fuzz_mem = [] 78 | edot_fuzz_mem = [] 79 | for z in e_mem: 80 | fuzz.fuzzify(e, z) 81 | e_fuzz_mem.append(z.fuzz_val) 82 | 83 | 84 | for z in edot_mem: 85 | fuzz.fuzzify(edot, z) 86 | edot_fuzz_mem.append(z.fuzz_val) 87 | 88 | rule_out = ball_beam_rule_set(e_fuzz_mem, edot_fuzz_mem) 89 | 90 | v = fuzz.defuzzify(rule_out, v_mem, 'CAD') 91 | u.append(v) 92 | x1dot = x2 93 | x2dot = 9.81*math.sin(0.02*v) 94 | 95 | return [x1dot, x2dot] 96 | 97 | def main(): 98 | e_mem = [] 99 | edot_mem = [] 100 | v_mem = [] 101 | 102 | e_fuzz_set_1 = [-0.5, 0.5, 'l_inf'] 103 | e_fuzz_set_2 = [-0.25, 0.5, 'none'] 104 | e_fuzz_set_3 = [0, 0.5, 'none'] 105 | e_fuzz_set_4 = [0.25, 0.5, 'none'] 106 | e_fuzz_set_5 = [0.5, 0.5, 'r_inf'] 107 | e_univ = np.linspace(-20.5, 20.5, 120) 108 | 109 | edot_fuzz_set_1 = [-4, 0.5, 'l_inf'] 110 | edot_fuzz_set_3 = [-2, 0.5, 'none'] 111 | edot_fuzz_set_4 = [0, 0.5, 'none'] 112 | edot_fuzz_set_5 = [2, 0.5, 'none'] 113 | edot_fuzz_set_2 = [4, 0.5, 'r_inf'] 114 | edot_univ = np.linspace(-30, 30, 100) 115 | 116 | v_fuzz_set_1 = [-10.0] 117 | v_fuzz_set_2 = [-5.0] 118 | v_fuzz_set_3 = [0.0] 119 | v_fuzz_set_4 = [5.0] 120 | v_fuzz_set_5 = [10.0] 121 | v_univ = np.linspace(-30, 30, 200) 122 | 123 | e_fuzz_set = [e_fuzz_set_1, e_fuzz_set_2, e_fuzz_set_3, e_fuzz_set_4, e_fuzz_set_5] 124 | edot_fuzz_set = [edot_fuzz_set_1, edot_fuzz_set_2, edot_fuzz_set_3, edot_fuzz_set_4, edot_fuzz_set_5] 125 | v_fuzz_set = [v_fuzz_set_1, v_fuzz_set_2, v_fuzz_set_3, v_fuzz_set_4, v_fuzz_set_5] 126 | names = ['NL', 'NS', 'Z', 'PS', 'PL'] 127 | for i in range(len(edot_fuzz_set)): 128 | e_mem.append(fuzz.membership('gauss', e_fuzz_set[i], e_univ, names[i])) 129 | edot_mem.append(fuzz.membership('gauss', edot_fuzz_set[i], edot_univ, names[i])) 130 | v_mem.append(fuzz.membership('singleton', v_fuzz_set[i], v_univ, names[i])) 131 | 132 | params = [e_mem, edot_mem, v_mem] 133 | 134 | x0 = [-0.12, 0.0] 135 | t0 = 0 136 | t1 = 10 137 | dt = 0.01 138 | r = ode(model).set_integrator('vode').set_initial_value(x0, t0).set_f_params(params) 139 | state = np.zeros((1001,2)) 140 | t = [] 141 | i = 0 142 | 143 | while r.successful() and r.t < t1: 144 | r.integrate(r.t + dt) 145 | state[i] = r.y 146 | t.append(r.t) 147 | i += 1 148 | 149 | plt.plot(t, state[:,0]) 150 | plt.grid() 151 | plt.show() 152 | 153 | if __name__ == '__main__': 154 | main() 155 | -------------------------------------------------------------------------------- /control/fuzzy_control/example_plotting.py: -------------------------------------------------------------------------------- 1 | import fuzzy_tools as fuzz 2 | import numpy as np 3 | 4 | p1 = [-1, 0.5] 5 | p2 = [0, 0.5] 6 | p3 = [1, 0.5] 7 | p = [p1, p2, p3] 8 | univ = np.linspace(-3, 3, 1000) 9 | a = [] 10 | for x in p: 11 | a.append(fuzz.membership('gauss', x, univ, 'test')) 12 | for x in a: 13 | fuzz.make_memship(x) 14 | 15 | fuzz.plot_memship(a) 16 | -------------------------------------------------------------------------------- /control/fuzzy_control/fuzzy_batch_LSE.py: -------------------------------------------------------------------------------- 1 | import fuzzy_tools as fuzz 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from numpy import sin, cos, log 5 | from numpy.linalg import inv 6 | 7 | # universe 8 | x = np.linspace(0, 6, 100) 9 | 10 | # real thing 11 | g = x - cos(1.5*x) + sin(0.4*x) 12 | 13 | # datapoints for getting estimated g(x) 14 | x_estimate = [0, 1, 2, 3, 4, 5, 6] 15 | g_estimate = [-1.0, 1.3187, 3.7073, 4.1428, 4.0394, 5.5627, 7.5866] 16 | 17 | # fuzzy sets 18 | p1 = [0, 1.184, 'none'] 19 | p2 = [2, 1.184, 'none'] 20 | p3 = [4, 1.184, 'none'] 21 | p4 = [6, 1.184, 'none'] 22 | params = [p1, p2, p3, p4] 23 | memship = [] 24 | names = 'none' 25 | for z in params: 26 | memship.append(fuzz.membership('gauss', z, x, 'none')) 27 | 28 | # premise memship and fuzzy basis 29 | fuzz_val_set = np.zeros((len(x_estimate), len(memship))) 30 | temp = np.zeros((len(memship),)) 31 | for i in range(len(x_estimate)): 32 | for j in range(len(memship)): 33 | fuzz.fuzzify(x_estimate[i], memship[j]) 34 | temp[j] = memship[j].fuzz_val 35 | fuzz_val_set[i] = temp 36 | 37 | # LSE 38 | phi = np.zeros_like(fuzz_val_set) 39 | for i in range(len(x_estimate)): 40 | a1 = np.sum(fuzz_val_set[i]) 41 | b1 = (fuzz_val_set[i])/a1 42 | phi[i] = b1 43 | phi_t = np.transpose(phi) 44 | phi_t_phi = np.dot(phi_t, phi) 45 | phi_t_phi_inv = inv(phi_t_phi) 46 | Y = np.array(g_estimate).reshape((len(g_estimate),1)) 47 | theta_cap = np.dot(phi_t_phi_inv, (np.dot(phi_t, Y))) 48 | 49 | # getting estimated function 50 | b = theta_cap 51 | fuzz_val_set_2 = np.zeros((len(x), len(memship))) 52 | temp = np.zeros((len(memship),)) 53 | for i in range(len(x)): 54 | for j in range(len(memship)): 55 | fuzz.fuzzify(x[i], memship[j]) 56 | temp[j] = memship[j].fuzz_val 57 | fuzz_val_set_2[i] = temp 58 | fuzz_basis = np.zeros_like(fuzz_val_set_2) 59 | for i in range(len(x)): 60 | a1 = np.sum(fuzz_val_set_2[i]) 61 | b1 = (fuzz_val_set_2[i])/a1 62 | fuzz_basis[i] = b1 63 | g_cap = np.zeros((len(x),)) 64 | for i in range(len(x)): 65 | g_cap[i] = b[0]*fuzz_basis[i,0] + b[1]*fuzz_basis[i,1] + b[2]*fuzz_basis[i,2] + b[3]*fuzz_basis[i,3] 66 | 67 | # plotting g and g_cap 68 | plt.plot(x, g, x, g_cap, 'r--') 69 | plt.show() 70 | -------------------------------------------------------------------------------- /control/fuzzy_control/fuzzy_batch_LSE_multi_input.py: -------------------------------------------------------------------------------- 1 | import fuzzy_tools as fuzz 2 | import numpy as np 3 | from numpy import sin, cos 4 | import matplotlib.pyplot as plt 5 | from numpy.linalg import inv 6 | 7 | # universe for real function 8 | x1 = np.linspace(-1, 1, 100) 9 | x2 = np.linspace(-1, 1, 100) 10 | 11 | # real function 12 | g = sin(x1)*((cos(x2))**2) 13 | 14 | # data points for estimation 15 | x1_cap = np.zeros((11,)) 16 | x2_cap = np.zeros((11,)) 17 | x_cap = np.zeros((121,2)) 18 | k = 0 19 | for i in range(11): 20 | j = (2*i/10) 21 | x1_cap[i] = -1.0 + j 22 | x2_cap[i] = -1.0 + j 23 | for i in range(len(x1_cap)): 24 | for j in range(len(x2_cap)): 25 | x_cap[k] = np.array([x1_cap[i], x2_cap[j]]) 26 | k += 1 27 | g_cap = np.zeros_like(x1_cap) 28 | for i in range(len(x1_cap)): 29 | g_cap[i] = sin(x1_cap[i])*((cos(x2_cap[i]))**2) 30 | 31 | # fuzzy sets 32 | centres = [-1.0, -0.5, 0, 0.5, 1] 33 | spread = 0.2123 34 | option = 'none' 35 | params = [] 36 | names = 'none' 37 | memship_1 = [] 38 | memship_2 = [] 39 | for x in centres: 40 | params.append([x, spread, option]) 41 | for x in params: 42 | memship_1.append(fuzz.membership('gauss', x, x1, names)) 43 | memship_2.append(fuzz.membership('gauss', x, x2, names)) 44 | 45 | # fuzzified value set 46 | fuzz_val_set_1 = np.zeros((len(x1_cap),len(memship_1))) 47 | fuzz_val_set_2 = np.zeros((len(x2_cap),len(memship_2))) 48 | temp = np.zeros((len(memship_1),)) 49 | for i in range(len(x1_cap)): 50 | for j in range(len(memship_1)): 51 | fuzz.fuzzify(x1_cap[i], memship_1[j]) 52 | temp[j] = memship_1[j].fuzz_val 53 | fuzz_val_set_1[i] = temp 54 | for i in range(len(x2_cap)): 55 | for j in range(len(memship_2)): 56 | fuzz.fuzzify(x2_cap[i], memship_2[j]) 57 | temp[j] = memship_2[j].fuzz_val 58 | fuzz_val_set_2[i] = temp 59 | 60 | # get rule premise values 61 | fuzz_rule_premise = np.zeros((len(x1_cap), len(memship_1)*len(memship_2))) 62 | temp = np.zeros((len(memship_1)*len(memship_2), )) 63 | l = 0 64 | for i in range(len(x1_cap)): 65 | for j in range(len(memship_1)): 66 | for k in range(len(memship_2)): 67 | temp[l] = fuzz_val_set_1[i, j]*fuzz_val_set_2[i, k] 68 | l += 1 69 | fuzz_rule_premise[i] = temp 70 | l = 0 71 | 72 | # get fuzzy basis function values 73 | fuzz_basis = np.zeros_like(fuzz_rule_premise) 74 | for i in range(len(x1_cap)): 75 | a1 = np.sum(fuzz_rule_premise[i]) 76 | fuzz_basis[i] = (fuzz_rule_premise[i])/a1 77 | 78 | # LSE 79 | phi = fuzz_basis 80 | phi_t = np.transpose(phi) 81 | phi_t_phi = np.dot(phi_t, phi) 82 | phi_t_phi_inv = inv(phi_t_phi) 83 | Y = g_cap.reshape((len(x1_cap), 1)) 84 | theta_cap = np.dot(phi_t_phi_inv, (np.dot(phi_t, Y))) 85 | -------------------------------------------------------------------------------- /control/fuzzy_control/fuzzy_gradient.py: -------------------------------------------------------------------------------- 1 | import fuzzy_tools as fuzz 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | from numpy import cos, sin 5 | import random 6 | 7 | # no. of iterations 8 | n_steps = 200 9 | 10 | # no. of rules 11 | n_rules = 5 12 | 13 | lam1 = 0.001 14 | lam2 = 0.001 15 | lam3 = 0.001 16 | 17 | # universe 18 | x = np.linspace(0, 6, 1000) 19 | 20 | # function to estimate 21 | g = x - cos(1.5*x) + sin(0.4*x) 22 | 23 | # training data pairs 24 | x_train = np.random.rand(n_steps,)*6.0 25 | 26 | # to get output data point 27 | def get_y(data_point_x): 28 | return data_point_x - cos(1.5*data_point_x) + sin(0.4*data_point_x) 29 | 30 | # to update the fuzzy sets with lates paramter value 31 | def update_fuzzy_sets(memship, c, sig): 32 | for i in range(len(memship)): 33 | memship[i].params = [c[i, 0], sig[i, 0], "none"] 34 | 35 | return memship 36 | 37 | def get_rule_premise(x, memship): 38 | rule_premise = np.zeros((n_rules, 1)) 39 | for i in range(len(memship)): 40 | fuzz.fuzzify(x, memship[i]) 41 | rule_premise[i, 0] = memship[i].fuzz_val 42 | return rule_premise 43 | 44 | def fuzzy_grad_des(data_point_x, f, b, c, sig, premise): 45 | e = f - get_y(data_point_x) 46 | basis_func = premise/np.sum(premise) 47 | b_next = np.zeros_like(b) 48 | c_next = np.zeros_like(c) 49 | sig_next = np.zeros_like(sig) 50 | 51 | for i in range(n_rules): 52 | # update output singleton positions 53 | b_next[i,0] = b[i,0] - lam1*e*basis_func[i,0] 54 | 55 | # update input fuzzy set centres 56 | c_next[i,0] = c[i,0] - lam2*e*premise[i,0]*((data_point_x - c[i,0])/(sig[i,0]**2))*((b[i,0] - f)/np.sum(premise)) 57 | 58 | # update input fuzzy set spreads 59 | sig_next[i,0] = sig[i,0] - lam3*e*((b[i,0] - f)/np.sum(premise))*premise[i,0]*(((data_point_x - c[i,0])**2)/(sig[i,0])**3) 60 | 61 | return [b_next, c_next, sig_next] 62 | 63 | def simulate(memship, b_0, c_0, sig_0): 64 | for i in range(n_steps): 65 | data_point_x = x_train[i] 66 | 67 | # initial step 68 | if i == 0: 69 | # get estimate of function 70 | premise = get_rule_premise(data_point_x, memship) 71 | f = np.dot(np.transpose(b_0), premise) 72 | # get updated params 73 | b_next, c_next, sig_next = fuzzy_grad_des(data_point_x, f, b_0, c_0, sig_0, premise) 74 | memship = update_fuzzy_sets(memship, c_next, sig_next) 75 | 76 | else: 77 | # get estimate of function 78 | premise = get_rule_premise(data_point_x, memship) 79 | f = np.dot(np.transpose(b_next), premise) 80 | # get updated params 81 | b_next, c_next, sig_next = fuzzy_grad_des(data_point_x, f, b_next, c_next, sig_next, premise) 82 | memship = update_fuzzy_sets(memship, c_next, sig_next) 83 | 84 | return [b_next, c_next, sig_next] 85 | 86 | def compare(b, c, sig, memship): 87 | fuzz_basis = np.zeros((len(x) ,n_rules)) 88 | memship = update_fuzzy_sets(memship, c, sig) 89 | temp = np.zeros((n_rules,)) 90 | for i in range(len(x)): 91 | for j in range(n_rules): 92 | fuzz.fuzzify(x[i], memship[j]) 93 | temp[j] = memship[j].fuzz_val 94 | fuzz_basis[i] = temp/np.sum(temp) 95 | 96 | g_cap = np.zeros_like(x) 97 | for i in range(len(x)): 98 | g_cap[i] = b[0]*fuzz_basis[i,0] + b[1]*fuzz_basis[i,1] + b[2]*fuzz_basis[i,2] + b[3]*fuzz_basis[i,3] + b[4]*fuzz_basis[i,4] 99 | 100 | plt.plot(x, g, x, g_cap) 101 | plt.show() 102 | 103 | def main(): 104 | # create fuzzy system 105 | # initial fuzzy set parameters 106 | b_0 = np.random.rand(5, 1)*15.0 107 | c_0 = np.random.rand(5, 1)*6.0 108 | sig_0 = np.random.rand(5, 1)*2.0 109 | 110 | # create fuzzy sets 111 | memship = [] 112 | for i in range(len(b_0)): 113 | p = [c_0[i, 0], sig_0[i, 0], "none"] 114 | memship.append(fuzz.membership("gauss", p, x, "none")) 115 | 116 | b, c, sig = simulate(memship, b_0, c_0, sig_0) 117 | memship = update_fuzzy_sets(memship, c, sig) 118 | b, c, sig = simulate(memship, b, c, sig) 119 | memship = update_fuzzy_sets(memship, c, sig) 120 | b, c, sig = simulate(memship, b, c, sig) 121 | memship = update_fuzzy_sets(memship, c, sig) 122 | b, c, sig = simulate(memship, b, c, sig) 123 | memship = update_fuzzy_sets(memship, c, sig) 124 | b, c, sig = simulate(memship, b, c, sig) 125 | memship = update_fuzzy_sets(memship, c, sig) 126 | b, c, sig = simulate(memship, b, c, sig) 127 | memship = update_fuzzy_sets(memship, c, sig) 128 | 129 | compare(b, c, sig, memship) 130 | 131 | if __name__ == '__main__': 132 | main() 133 | -------------------------------------------------------------------------------- /control/fuzzy_control/fuzzy_recursive_LSE.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import fuzzy_tools as fuzz 4 | from numpy.linalg import inv 5 | from numpy import sin, cos 6 | import random 7 | 8 | # original function 9 | def data_point_y(data_point_x): 10 | return data_point_x - cos(1.5*data_point_x) + sin(0.4*data_point_x) 11 | 12 | # universe 13 | x = np.linspace(0, 6, 100) 14 | 15 | # no of iterations 16 | n_points = 200 17 | 18 | # fuzzy sets 19 | p1 = [0, 1.184, "none"] 20 | p2 = [2, 1.184, "none"] 21 | p3 = [4, 1.184, "none"] 22 | p4 = [6, 1.184, "none"] 23 | params = [p1, p2, p3, p4] 24 | input_fuzz_set = [] 25 | for z in params: 26 | input_fuzz_set.append(fuzz.membership("gauss", z, x, "none")) 27 | 28 | # to get parameter estimate 29 | def get_estimate(data_point_x, prev_lam, prev_theta): 30 | # getting fuzzified value 31 | fuzz_val = [] 32 | for mem in input_fuzz_set: 33 | fuzz.fuzzify(data_point_x, mem) 34 | fuzz_val.append(mem.fuzz_val) 35 | 36 | # premise values 37 | premise_val = np.zeros((len(fuzz_val),)) 38 | for i in range(len(fuzz_val)): 39 | premise_val[i] = fuzz_val[i] 40 | 41 | # implied fuzzy set membership equals the premise values as we are usign singleton sets 42 | # for output 43 | 44 | # fuzzy basis functions 45 | fuzz_basis = premise_val/np.sum(premise_val) 46 | 47 | # new phi vector 48 | phi = fuzz_basis 49 | phi_t = np.transpose(phi) 50 | 51 | # RLS update 52 | new_y = data_point_y(data_point_x) 53 | gamma = (np.dot(prev_lam, phi))/(1 + np.dot(phi_t, np.dot(prev_lam, phi))) 54 | R_x_R_identity = np.identity(4) 55 | new_lam = np.dot((R_x_R_identity - np.dot(gamma, phi_t)), prev_lam) 56 | new_theta = prev_theta + np.dot(gamma, (new_y - np.dot(phi_t, prev_theta))) 57 | 58 | return new_theta, new_lam 59 | 60 | # initial estimate 61 | def get_inital_estimate(data_point_x): 62 | # getting fuzzified value 63 | fuzz_val = [] 64 | for mem in input_fuzz_set: 65 | fuzz.fuzzify(data_point_x, mem) 66 | fuzz_val.append(mem.fuzz_val) 67 | 68 | # premise values 69 | premise_val = np.zeros((len(fuzz_val),)) 70 | for i in range(len(fuzz_val)): 71 | premise_val[i] = fuzz_val[i] 72 | 73 | # implied fuzzy set membership equals the premise values as we are usign singleton sets 74 | # for output 75 | 76 | # fuzzy basis functions 77 | fuzz_basis = premise_val/np.sum(premise_val) 78 | 79 | # new phi vector 80 | phi = fuzz_basis 81 | phi_t = np.transpose(phi) 82 | 83 | # getting RLS update variables 84 | lam = 1.0/np.dot(phi_t, phi) 85 | theta = np.dot(lam, phi_t*data_point_y(data_point_x)) 86 | 87 | return theta, lam 88 | 89 | # simulation 90 | def main(): 91 | data_point_x = random.random()*6.0 92 | 93 | #first_theta, first_lam = get_inital_estimate(data_point_x) 94 | 95 | first_theta = np.zeros((4,)) 96 | first_lam = 1000000*np.identity(4) 97 | 98 | for i in range(n_points): 99 | data_point_x = random.random()*6.0 100 | if i == 0: 101 | prev_theta, prev_lam = get_estimate(data_point_x, first_lam, first_theta) 102 | else: 103 | prev_theta, prev_lam = get_estimate(data_point_x, prev_lam, prev_theta) 104 | 105 | final_theta = prev_theta 106 | print(final_theta) 107 | 108 | # comparison 109 | new_func = np.zeros_like(x) 110 | b = final_theta 111 | fuzz_basis_2 = np.zeros((len(x), len(input_fuzz_set))) 112 | temp = np.zeros((4,)) 113 | for i in range(len(x)): 114 | for j in range(len(input_fuzz_set)): 115 | fuzz.fuzzify(x[i], input_fuzz_set[j]) 116 | temp[j] = input_fuzz_set[j].fuzz_val 117 | fuzz_basis_2[i] = temp/np.sum(temp) 118 | g_cap = np.zeros_like(x) 119 | for i in range(len(x)): 120 | g_cap[i] = b[0]*fuzz_basis_2[i,0] + b[1]*fuzz_basis_2[i,1] + b[2]*fuzz_basis_2[i,2] + b[3]*fuzz_basis_2[i,3] 121 | g = x - cos(1.5*x) + sin(0.4*x) 122 | plt.plot(x, g, x, g_cap) 123 | plt.show() 124 | 125 | 126 | 127 | if __name__ == '__main__': 128 | main() 129 | -------------------------------------------------------------------------------- /control/fuzzy_control/fuzzy_tools.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from scipy.integrate import ode 4 | import math 5 | 6 | # membership function class 7 | class membership(): 8 | """ 9 | Class to form the membership functions for a given universe: 10 | * type - singleton, trimf 11 | * params - the defining parameters of the corresponding type of fuzzy membership 12 | * univ - the universe on which the fuzzy membership is defined 13 | * name - the name of the membership 14 | """ 15 | def __init__(self, type, params, univ, name): 16 | self.type = type 17 | self.params = params 18 | self.univ = univ 19 | self.name = name 20 | self.fuzz_val = None 21 | self.memship_arr = np.zeros_like(univ) 22 | self.check_params() 23 | 24 | def check_params(self): 25 | params = self.params 26 | type = self.type 27 | univ = self.univ 28 | 29 | if type == 'trimf': 30 | if len(params) != 3: 31 | print("Check params for trimf: not proper no. of params") 32 | 33 | else: 34 | a, b ,c = params 35 | if a < np.amin(univ) or c > np.amax(univ): 36 | print("Check params for trimf: not in univ") 37 | print(params) 38 | 39 | if a > b or a > c or b > c: 40 | print("Check params for trimf: not in correct order") 41 | print(params) 42 | 43 | if type == 'singleton': 44 | if len(params) != 1: 45 | print("Check params for singleton: not proper of no. params") 46 | print(params) 47 | 48 | else: 49 | a = params 50 | if a < np.amin(univ) or a > np.amax(univ): 51 | print("Check params for singleton: not in univ") 52 | print(params) 53 | 54 | if type == 'gauss': 55 | if len(params) != 3: 56 | print("Check params for gaussian: not proper no. of params") 57 | print(params) 58 | 59 | else: 60 | a, b, c = params 61 | if a < np.amin(univ) or a > np.amax(univ): 62 | print("Check params for gaussian: not in univ") 63 | print(params) 64 | 65 | # plot the array returned by make_memship 66 | def plot_memship(memship): 67 | for i in range(len(memship)): 68 | plt.plot(memship[i].univ, memship[i].memship_arr, label=memship[i].name) 69 | plt.legend(loc='upper right') 70 | plt.show() 71 | 72 | # returns an array to use for plotting 73 | def make_memship(membership): 74 | type = membership.type 75 | params = membership.params 76 | univ = membership.univ 77 | 78 | if type == 'trimf': 79 | if len(params) == 3: 80 | a, b, c = params 81 | i = 0 82 | for x in univ: 83 | if a == b: 84 | if x <= a: 85 | membership.memship_arr[i] = 1 86 | if x >= b and x <= c: 87 | membership.memship_arr[i] = (1/(b - c))*(x - c) 88 | 89 | elif x >= a and x <= b: 90 | membership.memship_arr[i] = (1/(b - a))*(x - a) 91 | 92 | if b == c: 93 | if x >= b: 94 | membership.memship_arr[i] = 1 95 | if x >= a and x <= b: 96 | membership.memship_arr[i] = (1/(b - a))*(x - a) 97 | 98 | elif x >= b and x <= c: 99 | membership.memship_arr[i] = (1/(b - c))*(x - c) 100 | 101 | if x == b: 102 | membership.memship_arr[i] = 1 103 | i += 1 104 | 105 | if type == 'singleton': 106 | a = params 107 | i = 0 108 | for x in univ: 109 | if x == a: 110 | membership.memship_arr[i] = 1.0 111 | else: 112 | membership.memship_arr[i] = 0.0 113 | i += 1 114 | 115 | if type == 'gauss': 116 | a, b, c= params 117 | i = 0 118 | for x in univ: 119 | if c == 'l_inf': 120 | if x <= a: 121 | membership.memship_arr[i] = 1.0 122 | else: 123 | membership.memship_arr[i] = math.exp(-0.5*(((x - a)**2)/b)) 124 | elif c == 'r_inf': 125 | if x >= a: 126 | membership.memship_arr[i] = 1.0 127 | else: 128 | membership.memship_arr[i] = math.exp(-0.5*(((x - a)**2)/b)) 129 | else: 130 | membership.memship_arr[i] = math.exp(-0.5*(((x - a)**2)/b)) 131 | 132 | i += 1 133 | 134 | # returns fuzzy membership of x 135 | def fuzzify(x, membership): 136 | params = membership.params 137 | type = membership.type 138 | univ = membership.univ 139 | membership.fuzz_val = 0 140 | # make sure x is in the universe 141 | if x < np.amin(univ) or x > np.amax(univ): 142 | print("value to be fuzzified not in the universe") 143 | print(x) 144 | return None 145 | 146 | elif type == 'trimf': 147 | a, b, c = params 148 | if a == b: 149 | if x <= a: 150 | membership.fuzz_val = 1 151 | if x >= b and x <= c: 152 | membership.fuzz_val = (1/(b - c))*(x - c) 153 | 154 | elif x >= a and x <= b: 155 | membership.fuzz_val = (1/(b - a))*(x - a) 156 | 157 | if b == c: 158 | if x >= b: 159 | membership.fuzz_val = 1 160 | if x >= a and x <= b: 161 | membership.fuzz_val = (1/(b - a))*(x - a) 162 | 163 | elif x >= b and x <= c: 164 | membership.fuzz_val = (1/(b - c))*(x - c) 165 | 166 | if x == b: 167 | membership.fuzz_val = 1 168 | 169 | elif type == 'singleton': 170 | a = params[0] 171 | if x == a: 172 | membership.fuzz_val == 1.0 173 | else: 174 | membership.fuzz_val = 0.0 175 | 176 | elif type == 'gauss': 177 | a, b, c = params 178 | if c == 'l_inf': 179 | if x <= a: 180 | membership.fuzz_val = 1.0 181 | else: 182 | membership.fuzz_val = math.exp(-0.5*(((x - a)**2)/b)) 183 | elif c == 'r_inf': 184 | if x >= a: 185 | memebership.fuzz_val = 1.0 186 | else: 187 | membership.fuzz_val = math.exp(-0.5*(((x - a)**2)/b)) 188 | else: 189 | membership.fuzz_val = math.exp(-0.5*(((x - a)**2)/b)) 190 | 191 | # returns crisp value of a fuzzy memship 192 | def defuzzify(fuzz_val ,memship, method): 193 | y = 0 194 | z = 0 195 | if method == "CAD": 196 | for i in range(len(memship)): 197 | if memship[i].type == 'singleton': 198 | a = memship[i].params[0] 199 | y += a*fuzz_val[i] 200 | z += fuzz_val[i] 201 | if memship[i].type == 'trimf': 202 | a, b, c = memship[i].params 203 | if (b - a) == (c - b): 204 | y += b*fuzz_val[i] 205 | z += fuzz_val[i] 206 | if memship[i].type == 'gauss': 207 | a, b, c = memship[i].params 208 | y += a*fuzz_val[i] 209 | z += fuzz_val[i] 210 | return y/z 211 | -------------------------------------------------------------------------------- /control/fuzzy_control/fuzzy_tools.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Thalaivar/python/6702edc229e9938846f9ba757b759d0bb183aa1e/control/fuzzy_control/fuzzy_tools.pyc -------------------------------------------------------------------------------- /control/fuzzy_control/lilly_4_10.py: -------------------------------------------------------------------------------- 1 | import fuzzy_tools as fuzz 2 | from scipy.integrate import ode 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | import math 6 | 7 | def main(): 8 | e_univ = np.linspace(-2.57, 2.57, 1000) 9 | e_fuzzy_set_1 = [-1.285, 0.5, 'l_inf'] 10 | e_fuzzy_set_2 = [0, 0.5, 'none'] 11 | e_fuzzy_set_3 = [1.285, 0.5, 'r_inf'] 12 | 13 | e_dot_univ = np.linspace(-4.15, 4.15, 1000) 14 | e_dot_fuzzy_set_1 = [-1.57, 0.5, 'l_inf'] 15 | e_dot_fuzzy_set_2 = [0, 0.5, 'none'] 16 | e_dot_fuzzy_set_3 = [1.57, 0.5, 'r_inf'] 17 | 18 | i_univ = np.linspace(-30, 30, 1000) 19 | i_fuzzy_set_1 = [-15, 0.25, 'none'] 20 | i_fuzzy_set_2 = [0, 0.25, 'none'] 21 | i_fuzzy_set_3 = [15, 0.25, 'none'] 22 | 23 | e_fuzz_set = [e_fuzzy_set_1, e_fuzzy_set_2, e_fuzzy_set_3] 24 | e_dot_fuzz_set = [e_dot_fuzzy_set_1, e_dot_fuzzy_set_2, e_dot_fuzzy_set_3] 25 | i_fuzz_set = [i_fuzzy_set_1, i_fuzzy_set_2, i_fuzzy_set_3] 26 | 27 | names = ["N", "Z", "P"] 28 | 29 | e_memship = np.zeros((3,), dtype = object) 30 | e_dot_memship = np.zeros((3,), dtype = object) 31 | i_memship = np.zeros((3,), dtype = object) 32 | for i in range(len(e_fuzz_set)): 33 | e_memship[i] = fuzz.membership('gauss', e_fuzz_set[i], e_univ, names[i]) 34 | e_dot_memship[i] = fuzz.membership('gauss', e_dot_fuzz_set[i], e_dot_univ, names[i]) 35 | i_memship[i] = fuzz.membership('gauss', i_fuzz_set[i], i_univ, names[i]) 36 | 37 | params = [e_memship, e_dot_memship, i_memship] 38 | x0 = [1.57, 0] 39 | t0 = 0 40 | t1 = 10 41 | dt = 0.001 42 | state = np.zeros((10001,2)) 43 | i = 0 44 | t= [] 45 | r = ode(model).set_integrator('vode').set_f_params(params).set_initial_value(x0, t0) 46 | while r.successful() and r.t < t1: 47 | r.integrate(r.t + dt) 48 | xdes = (math.pi/2) + math.sin(math.pi*r.t) 49 | state[i] = r.y 50 | state[i,0] = xdes - state[i,0] 51 | t.append(r.t) 52 | i += 1 53 | plt.plot(t, state[:,0]) 54 | plt.show() 55 | def rule_set(x, y): 56 | 57 | # 0 - N 58 | # 1 - Z 59 | # 2 - P 60 | z = [0, 0, 0] 61 | z[0] = x[2]*y[2] 62 | z[0] += x[2]*y[1] 63 | z[1] = x[2]*y[0] 64 | z[0] += x[1]*y[2] 65 | z[1] += x[1]*y[1] 66 | z[2] = x[1]*y[0] 67 | z[1] += x[0]*y[2] 68 | z[2] += x[0]*y[1] 69 | z[2] += x[0]*y[0] 70 | 71 | return z 72 | 73 | def model(t, X, params): 74 | x1, x2 = X 75 | e_memship, e_dot_memship, i_memship = params 76 | 77 | r = (math.pi/2) + math.sin(math.pi*t) 78 | rdot = math.pi*math.cos(math.pi*t) 79 | e = x1 - r 80 | edot = x2 - rdot 81 | 82 | for i in range(len(e_memship)): 83 | fuzz.fuzzify(e, e_memship[i]) 84 | fuzz.fuzzify(edot, e_dot_memship[i]) 85 | e_fuzz = [] 86 | e_dot_fuzz = [] 87 | for i in range(len(e_memship)): 88 | e_fuzz.append(e_memship[i].fuzz_val) 89 | e_dot_fuzz.append(e_dot_memship[i].fuzz_val) 90 | 91 | i_fuzz = rule_set(e_fuzz, e_dot_fuzz) 92 | 93 | i = fuzz.defuzzify(i_fuzz, i_memship, 'CAD') 94 | 95 | x1dot = x2 96 | x2dot = -64*math.sin(x1) -5*x2 + 4*i 97 | 98 | return [x1dot, x2dot] 99 | 100 | if __name__ == '__main__': 101 | main() 102 | -------------------------------------------------------------------------------- /control/fuzzy_control/lilly_6_6.py: -------------------------------------------------------------------------------- 1 | import fuzzy_tools as fuzz 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | from scipy.integrate import ode 5 | import math 6 | 7 | A1 = np.array([[ 0.0, 1], 8 | [-2, -2]]) 9 | A2 = np.array([[ 0.0, 1], 10 | [-1, -2]]) 11 | A3 = np.array([[ 0.0, 1], 12 | [-3, -2]]) 13 | A4 = np.array([[ 1.0, 1], 14 | [-2, -2]]) 15 | A5 = np.array([[ 0.0, 1], 16 | [-2, -1]]) 17 | A6 = np.array([[ 0.0, 1], 18 | [-1, -2]]) 19 | A7 = np.array([[ 0.0, 1], 20 | [-2, -2]]) 21 | A8 = np.array([[-1.0, -1], 22 | [ 1, 0]]) 23 | A9 = np.array([[ 0.0, 1], 24 | [ 2, -2]]) 25 | B1 = np.array([[0.0], 26 | [2]]) 27 | B2 = np.array([[2.0], 28 | [2]]) 29 | B3 = np.array([[-2.0], 30 | [2]]) 31 | B4 = np.array([[0.0], 32 | [-2]]) 33 | B5 = np.array([[0.0], 34 | [1]]) 35 | B6 = np.array([[1.0], 36 | [1]]) 37 | B7 = np.array([[-1.0], 38 | [1]]) 39 | B8 = np.array([[0.0], 40 | [-1]]) 41 | B9 = np.array([[0], 42 | [1]]) 43 | A_i = [A1, A2, A3, A4, A5, A6, A7, A8, A9] 44 | B_i = [B1, B2, B3, B4, B5, B6, B7, B8, B9] 45 | 46 | def main(): 47 | x1_univ = np.linspace(-1000, 1000, 10000) 48 | x1_fuzz_set_1 = [-1, -1, 0] 49 | x1_fuzz_set_2 = [-1, 0 , 1] 50 | x1_fuzz_set_3 = [0, 1, 1] 51 | x2_univ = np.linspace(-1000, 1000, 10000) 52 | x2_fuzz_set_1 = [-1, -1, 0] 53 | x2_fuzz_set_2 = [-1, 0 , 1] 54 | x2_fuzz_set_3 = [0, 1, 1] 55 | names = ["left", "center", "right"] 56 | x1_fuzz_set = [x1_fuzz_set_1, x1_fuzz_set_2, x1_fuzz_set_3] 57 | x2_fuzz_set = [x2_fuzz_set_1, x2_fuzz_set_2, x2_fuzz_set_3] 58 | x1_fuzz_mem = [] 59 | x2_fuzz_mem = [] 60 | for i in range(len(x1_fuzz_set)): 61 | x1_fuzz_mem.append(fuzz.membership('trimf', x1_fuzz_set[i], x1_univ, names[i])) 62 | x2_fuzz_mem.append(fuzz.membership('trimf', x2_fuzz_set[i], x2_univ, names[i])) 63 | params = [x1_fuzz_mem, x2_fuzz_mem] 64 | x0 = [0, 0] 65 | t0 = 0 66 | t1 = 8 67 | dt = 0.001 68 | r = ode(model).set_integrator('vode').set_f_params(params).set_initial_value(x0, t0) 69 | state = np.zeros((8000, 2)) 70 | t = [] 71 | i = 0 72 | while r.successful() and r.t < t1: 73 | r.integrate(r.t + dt) 74 | state[i] = r.y 75 | t.append(r.t) 76 | i += 1 77 | plt.plot(t, state[:,0], t, state[:,1]) 78 | plt.show() 79 | 80 | def get_fuzzy_basis(fuzz_set_1, fuzz_set_2): 81 | a = len(fuzz_set_1)*len(fuzz_set_2) 82 | z = np.zeros((a,)) 83 | k = 0 84 | for i in range(len(fuzz_set_1)): 85 | for j in range(len(fuzz_set_2)): 86 | z[k] = fuzz_set_1[i]*fuzz_set_2[j] 87 | k += 1 88 | premise_sum = np.sum(z) 89 | z = z/premise_sum 90 | return z 91 | 92 | def model(t, X, params): 93 | x1, x2 = X 94 | x1_fuzz_mem, x2_fuzz_mem = params 95 | x1_fuzz = [] 96 | x2_fuzz = [] 97 | u = 3*math.sin(math.pi*t) 98 | 99 | for i in range(len(x1_fuzz_mem)): 100 | fuzz.fuzzify(x1, x1_fuzz_mem[i]) 101 | fuzz.fuzzify(x2, x2_fuzz_mem[i]) 102 | for i in range(len(x1_fuzz_mem)): 103 | x1_fuzz.append(x1_fuzz_mem[i].fuzz_val) 104 | x2_fuzz.append(x2_fuzz_mem[i].fuzz_val) 105 | fuzz_basis = get_fuzzy_basis(x1_fuzz, x2_fuzz) 106 | 107 | A = np.zeros_like(A1) 108 | B = np.zeros_like(B1) 109 | i = 0 110 | 111 | for x in fuzz_basis: 112 | A += A_i[i]*x 113 | B += B_i[i]*x 114 | i += 1 115 | 116 | x1dot = A[0,0]*x1 + A[0,1]*x2 + B[0,0]*u 117 | x2dot = A[1,0]*x1 + A[1,1]*x2 + B[1,0]*u 118 | return [x1dot, x2dot] 119 | 120 | if __name__ == '__main__': 121 | main() 122 | -------------------------------------------------------------------------------- /control/sliding_mode/dobby.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Thalaivar/python/6702edc229e9938846f9ba757b759d0bb183aa1e/control/sliding_mode/dobby.slx -------------------------------------------------------------------------------- /control/sliding_mode/inverted_pendulum_HSMC.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as chart 3 | import math 4 | from scipy.integrate import ode 5 | 6 | ##### define signum function ##### 7 | def sign(x): 8 | if x > 0: 9 | return 1 10 | elif x < 0: 11 | return -1 12 | elif x == 0: 13 | return 0 14 | else: 15 | return x 16 | 17 | 18 | def model(t, X, params): 19 | m, M, L, g, eta, l1, l2, gamma = params 20 | 21 | x1, x2, x3, x4 = X 22 | 23 | f1 = m*L*(x4**2)*math.sin(x3) - m*g*math.sin(2*x3)/2 24 | g1 = M + m*((math.sin(x3))**2) 25 | g2 = (M + m*((math.sin(x3))**2))/math.cos(x3) 26 | d1 = (g*math.sin(x3))/L 27 | 28 | s1 = x2 + l1*(x1 - 20) 29 | s2 = x4 + l2*(x3 - math.pi) 30 | 31 | S = s2 + gamma*s1 32 | 33 | v_eq = -d1 - l2*x4 - gamma*l1*x2 34 | v_sw = -eta*np.tanh(S) 35 | 36 | v = (1/((gamma/g1) - 1/g2))*(v_eq + v_sw) 37 | 38 | x1dot = x2 39 | x2dot = v/g1 40 | x3dot = x4 41 | x4dot = d1 - v/g2 42 | 43 | return [x1dot, x2dot, x3dot, x4dot] 44 | 45 | def solve(params): 46 | 47 | t0 = 0 48 | X0 = [0, 0, math.pi, 0] 49 | t1 = 20 50 | dt = 0.001 51 | 52 | r = ode(model).set_integrator('dopri5', nsteps = 10000, method='bdf') 53 | r.set_initial_value(X0, t0).set_f_params(params) 54 | 55 | Y = np.zeros((t1/dt + 1, 5)) 56 | t =[0] 57 | i = 0 58 | 59 | while r.successful() and r.t < t1: 60 | 61 | r.integrate(r.t + dt) 62 | x1, x2, x3, x4 = r.y 63 | m, M, L, g, eta, l1, l2, gamma = params 64 | e_theta = x3*180.0/math.pi - 180 65 | e_x = x1 - 20 66 | 67 | s1 = x2 + l1*(x1 - 20) 68 | s2 = x4 + l2*(x3 - math.pi) 69 | 70 | S = s2 + gamma*s1 71 | # v_eq = -d1 - l2*x4 - gamma*l1*x2 72 | # v_sw = -eta*np.tanh(S) 73 | # v = (1/((gamma/g1) - 1/g2))*(v_eq + v_sw) 74 | 75 | Y[i] = [e_theta, e_x, s1, s2, S] 76 | t.append(r.t) 77 | i = i + 1 78 | 79 | print(np.size(Y, 0)) 80 | print(len(t)) 81 | chart.plot(t, Y[:,0], label='error_theta') 82 | chart.plot(t, Y[:,1], label='error_x') 83 | chart.legend(loc='upper right') 84 | chart.grid() 85 | chart.show() 86 | 87 | labels = ['s_x', 's_theta', 'S'] 88 | 89 | for i in range(3): 90 | chart.plot(t, Y[:,i+2], label=labels[i]) 91 | chart.legend(loc='upper right') 92 | chart.grid() 93 | chart.show() 94 | ################################################################################ 95 | if __name__ == '__main__': 96 | 97 | #params m, M, L, g, eta, l1, l2, gamma 98 | params = [0.02, 2, 1, 9.8, 10, 0.01, 2, 30] 99 | solve(params) 100 | -------------------------------------------------------------------------------- /control/sliding_mode/quad_1.py: -------------------------------------------------------------------------------- 1 | from scipy.integrate import ode 2 | import matplotlib.pyplot as chart 3 | import numpy as np 4 | 5 | ##------------------------------------------## 6 | #define all model constants# 7 | ##------------------------------------------## 8 | model_constants = [-0.8033, 0.1721, -0.0206, 0.8033, -0.0206, 0.17, 0, -0.0545, 0.4545] 9 | smc_constants = [2.2, 1.2, 1.1] 10 | eta = [5.6, 7.2, 1.3] 11 | 12 | ##------------------------------------------## 13 | #define model# 14 | ##------------------------------------------## 15 | def model(t, X): 16 | x1, x2, x3, x4, x5, x6 = X 17 | 18 | # if t > 15 and t < 15.5: 19 | # x1 = x1 + 10 20 | # x3 = x3 - 10 21 | 22 | s_phi = x2 + smc_constants[0]*x1 23 | s_theta = x4 + smc_constants[1]*x3 24 | s_psi = x6 + smc_constants[2]*x5 25 | 26 | u2 = (-x4*x6*model_constants[0] - model_constants[2]*x2 - eta[0]*np.tanh(s_phi))/model_constants[1] 27 | u3 = (-x6*x2*model_constants[3] - model_constants[4]*x4 - eta[1]*np.tanh(s_theta))/model_constants[5] 28 | u4 = (-x4*x2*model_constants[6] - model_constants[7]*x6 - eta[2]*np.tanh(s_psi))/model_constants[8] 29 | 30 | x1dot = x2 31 | x2dot = -eta[0]*np.tanh(s_phi) 32 | x3dot = x4 33 | x4dot = -eta[1]*np.tanh(s_theta) 34 | x5dot = x6 35 | x6dot = -eta[2]*np.tanh(s_psi) 36 | 37 | return [x1dot, x2dot, x3dot, x4dot, x5dot, x6dot] 38 | 39 | 40 | X0 = [10, 0, -5, 0, -12, 0] 41 | t0 = 0 42 | t1 = 30 43 | dt = 0.01 44 | 45 | r = ode(model).set_integrator('dopri5', nsteps = 5000, method = 'bdf').set_initial_value(X0, t0) 46 | 47 | X = np.ones((3000, 6)) 48 | t = [] 49 | i = 0 50 | 51 | while r.successful() and r.t < t1: 52 | r.integrate(r.t+dt) 53 | X[i] = r.y 54 | #X[i, 6] = 55 | t.append(r.t) 56 | i = i + 1 57 | 58 | labels = ["roll_control_input", "pitch_control_input", "yaw_control_input"] 59 | 60 | s_phi = X[:,1] + smc_constants[0]*X[:,0] 61 | s_theta = X[:,3] + smc_constants[1]*X[:,2] 62 | s_psi = X[:,5] + smc_constants[2]*X[:,4] 63 | 64 | u2 = (-X[:,3]*X[:,5]*model_constants[0] - model_constants[2]*X[:,1] - eta[0]*np.tanh(s_phi))/model_constants[1] 65 | u3 = (-X[:,5]*X[:,1]*model_constants[3] - model_constants[4]*X[:,3] - eta[1]*np.tanh(s_theta))/model_constants[5] 66 | u4 = (-X[:,3]*X[:,1]*model_constants[6] - model_constants[7]*X[:,5] - eta[2]*np.tanh(s_psi))/model_constants[8] 67 | 68 | u = [u2, u3, u4] 69 | 70 | colors = ['r', 'b', 'y'] 71 | 72 | for i in range(3): 73 | chart.subplot(3, 1, i+1) 74 | chart.plot(t, u[i], colors[i], label=labels[i]) 75 | chart.legend(loc='upper right') 76 | chart.grid() 77 | chart.show() 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /control/sliding_mode/slotine_7.1.1.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as chart 2 | import numpy as np 3 | from scipy.integrate import ode 4 | from scipy.integrate import odeint 5 | import math 6 | 7 | ##### define lambda and eta ##### 8 | gamma = 10 9 | eta = 0.3 10 | m_cap = 7 11 | c_cap = 3 12 | 13 | ##### gets time-dependant model params ##### 14 | def getParams(t, X): 15 | x1, x2 = X 16 | 17 | m = 3 + 1.5*math.sin(x2*np.tanh(x2)*t) 18 | c = 1.2 + 0.2*math.sin(x2*np.tanh(x2)*t) 19 | 20 | return [m, c] 21 | 22 | ##### gets desired trajectory##### 23 | def getDesiredTraj(t): 24 | if t >= 0 and t < 2: 25 | xd_ddot = 2 26 | xd_dot = 2*t 27 | xd = t**2 28 | 29 | elif t >= 2 and t < 4: 30 | xd_ddot = 0 31 | xd_dot = 4 32 | xd = 4*(t - 2) + 4 33 | 34 | elif t >= 4 and t <= 6.01: 35 | xd_ddot = -2 36 | xd_dot = -2*(t - 4) + 4 37 | xd = -((t - 4)**2) + 4*(t - 4) + 12 38 | 39 | return [xd, xd_dot, xd_ddot] 40 | 41 | ##### define sliding surface ##### 42 | def getSlidingSurface(t, X): 43 | x1, x2 = X 44 | 45 | x_d = getDesiredTraj(t) 46 | s = x2 - x_d[1] + gamma*(x1 - x_d[0]) 47 | 48 | return s 49 | 50 | ##### define signum function ##### 51 | def sign(x): 52 | if x > 0: 53 | return 1 54 | elif x < 0: 55 | return -1 56 | elif x == 0: 57 | return 0 58 | else: 59 | return x 60 | 61 | ##### define model ##### 62 | def model(t, X): 63 | x1, x2 = X 64 | 65 | m, c = getParams(t, X) 66 | s = getSlidingSurface(t, X) 67 | 68 | x_d = getDesiredTraj(t) 69 | u = -m_cap*(x_d[2] - gamma*(x2 - x_d[1]))*np.tanh(x_d[2] - gamma*(x2 - x_d[1])) - c_cap*(x2**2) - eta*np.tanh(s) 70 | 71 | x1dot = x2 72 | x2dot = (u - c*x2*np.tanh(x2)*x2)/m 73 | 74 | return [x1dot, x2dot] 75 | 76 | ##### solver #### 77 | def simulateSMC(): 78 | X0 = [0, 0] 79 | t0 = 0 80 | t1 = 6 81 | dt = 0.01 82 | 83 | solver = ode(model).set_integrator('dopri5', nsteps= 1000000, method = 'bdf').set_initial_value(X0, t0) 84 | 85 | data = np.ones((601, 2)) 86 | i = 0 87 | 88 | while solver.successful() and solver.t < t1: 89 | solver.integrate(solver.t + dt) 90 | print(solver.t) 91 | X = solver.y 92 | x_d = getDesiredTraj(solver.t) 93 | s = getSlidingSurface(solver.t, X) 94 | 95 | u = -m_cap*abs(x_d[2] - gamma*(X[1] - x_d[1])) - c_cap*(X[1]**2) - eta*sign(s) 96 | error = X[0] - x_d[0] 97 | 98 | data[i] = [u, error] 99 | i = i + 1 100 | 101 | return data 102 | 103 | data = simulateSMC() 104 | 105 | figure = chart.figure() 106 | 107 | fig1 = figure.add_subplot(2, 1, 1) 108 | fig1.plot(data[:,0]) 109 | fig2 = figure.add_subplot(2,1, 2) 110 | fig2.plot(data[:,1]) 111 | fig1.grid() 112 | fig2.grid() 113 | chart.show() 114 | 115 | -------------------------------------------------------------------------------- /control/sliding_mode/slotine_7.1.3.py: -------------------------------------------------------------------------------- 1 | import math 2 | import numpy as np 3 | from scipy.integrate import ode 4 | import matplotlib.pyplot as chart 5 | 6 | gamma = 20 7 | eta = 0.3 8 | 9 | def sign(x): 10 | if x > 0: 11 | return 1 12 | elif x < 0: 13 | return -1 14 | elif x == 0: 15 | return 0 16 | else: 17 | return x 18 | 19 | def model(t, X): 20 | x1 = X[0] 21 | x2 = X[1] 22 | 23 | #unknown model paramter 24 | a = abs(math.sin(t)) + 1 25 | 26 | #desired trajectories 27 | x_d = math.sin(math.pi*t/2) 28 | x_ddot = (math.pi/2)*math.cos(math.pi*t/2) 29 | x_dddot = -((math.pi/2)**2)*math.sin(math.pi*t/2) 30 | 31 | #sliding surface 32 | s = (x2 - x_ddot) + gamma*(x1 - x_d) 33 | 34 | #control input 35 | u = -2*(x1**2)*abs(math.cos(3*x1))*sign(s) - eta*sign(s) + x_dddot - gamma*(x2 - x_ddot) 36 | 37 | #dynamics 38 | x1dot = x2 39 | x2dot = -a*x2*math.cos(3*x1) + u 40 | 41 | Xdot = [x1dot, x2dot] 42 | 43 | error.append(x1 - x_d) 44 | return Xdot 45 | 46 | X0 = [0, 0] 47 | t0 = 0 48 | t1 = 300 49 | dt = 0.1 50 | 51 | error = [] 52 | t = [] 53 | 54 | r = ode(model).set_integrator('vode', method = 'bdf') 55 | r.set_initial_value(X0, t0) 56 | 57 | #basic check to see if integrator working 58 | while r.successful() and r.t < t1: 59 | r.integrate(r.t+dt) 60 | Y.append(r.y) 61 | 62 | print(Y) 63 | 64 | #chart.plot(error) 65 | #chart.grid() 66 | #chart.show() 67 | -------------------------------------------------------------------------------- /control/sliding_mode/smc.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | from scipy.integrate import ode 4 | from numpy import sin, cos, tan, tanh 5 | from mpl_toolkits.mplot3d import Axes3D 6 | 7 | DEG_TO_RAD = 0.0174533 8 | 9 | PHI = 0 10 | THETA = 1 11 | PSI = 2 12 | 13 | Ix = (0.000081599 + 0.00032233 + 0.0133609 + 0.0044966) 14 | Iy = (0.000586590 + 0.00032233 + 0.0133609 + 0.0044966) 15 | Iz = (0.000621150 + 0.00032968 + 0.0163521 + 0.0118809) 16 | 17 | u_x = [] 18 | u_y = [] 19 | u_z = [] 20 | 21 | def model(t, X, params): 22 | 23 | phi, theta, psi, wx, wy, wz, ie_wx, ie_wy, ie_wz= X 24 | 25 | kp = params[0] 26 | eta = params[1] 27 | l = params[2] 28 | 29 | # get desired attitude 30 | if t > 4: 31 | desired_attitude = np.array([40, -10, 0]) 32 | else: 33 | desired_attitude = np.array([0, 0, 0]) 34 | attitude = np.array([phi, theta, psi]) 35 | 36 | # error in desired attitude 37 | e_angle = attitude - desired_attitude 38 | 39 | # desired body rates 40 | w_d = -e_angle*kp 41 | 42 | # controller error 43 | w = np.array([wx, wy, wz]) 44 | 45 | e = w - w_d 46 | 47 | # sliding surfaces 48 | sx = Ix*e[0] + l[0]*ie_wx 49 | sy = Iy*e[1] + l[1]*ie_wy 50 | sz = Iz*e[2] + l[2]*ie_wz 51 | 52 | # controller outputs 53 | ux = (Iz - Iy)*wy*wz - l[0]*e[0] - eta[0]*tanh(sx) 54 | uy = (Ix - Iz)*wx*wz - l[1]*e[1] - eta[1]*tanh(sy) 55 | uz = (Iy - Ix)*wy*wx - l[2]*e[2] - eta[2]*tanh(sz) 56 | 57 | # return dot vector 58 | attitude = attitude*DEG_TO_RAD 59 | 60 | phi_dot = wx + sin(attitude[PSI])*tan(attitude[THETA])*wy + cos(attitude[PSI])*tan(attitude[THETA])*wz 61 | theta_dot = cos(attitude[PSI])*wy - sin(attitude[PSI])*wz 62 | psi_dot = (sin(attitude[PSI])/cos(attitude[THETA]))*wy + (cos(attitude[PSI])/cos(attitude[THETA]))*wz 63 | wx_dot = (1/Ix)*(ux - (Iz - Iy)*wy*wz) 64 | wy_dot = (1/Iy)*(uy - (Ix - Iz)*wx*wz) 65 | wz_dot = (1/Iz)*(uz - (Iy - Ix)*wx*wy) 66 | ie_wx_dot = e[0] 67 | ie_wy_dot = e[1] 68 | ie_wz_dot = e[2] 69 | 70 | return [phi_dot, theta_dot, psi_dot, wx_dot, wy_dot, wz_dot, ie_wx_dot, ie_wy_dot, ie_wz_dot] 71 | 72 | def check_list_for_item(x, y): 73 | j = 0 74 | for i in range(len(x)): 75 | if x[i] < y: 76 | j += 1 77 | 78 | elif x[i] > y: 79 | return j 80 | 81 | def main(kp, eta, l): 82 | 83 | t1 = 10 84 | t0 = 0 85 | dt = 0.005 86 | 87 | X0 = [0, 0, 0, 0, 0, 0, 0, 0, 0] 88 | 89 | x = np.zeros((2000, 9)) 90 | t = [] 91 | 92 | params = [kp, eta, l] 93 | 94 | r = ode(model).set_integrator('dopri5', method='bdf') 95 | r.set_initial_value(X0, t0).set_f_params(params) 96 | 97 | i = 0 98 | while r.successful() and r.t < t1: 99 | r.integrate(r.t + dt) 100 | x[i] = r.y 101 | t.append(r.t) 102 | i = i + 1 103 | 104 | return x, t 105 | 106 | if __name__ == '__main__': 107 | 108 | kp = [1.54, 1.54, 1.54] 109 | eta = [1.21, 1.01, 1.01] 110 | l = [1.04, 1.04, 1.04] 111 | 112 | x, t = main(kp, eta, l) 113 | 114 | j = check_list_for_item(t, 4) 115 | 116 | i = 0 117 | 118 | phi = np.reshape(x[:,0], (2000,)) 119 | theta = np.reshape(x[:,1], (2000,)) 120 | psi = np.reshape(x[:,2], (2000,)) 121 | wx = np.reshape(x[:,3], (2000,)) 122 | wy = np.reshape(x[:,4], (2000,)) 123 | wz = np.reshape(x[:,5], (2000,)) 124 | ie_wx = np.reshape(x[:,6], (2000,)) 125 | ie_wy = np.reshape(x[:,7], (2000,)) 126 | ie_wz = np.reshape(x[:,8], (2000,)) 127 | 128 | e = np.zeros((2000, 3)) 129 | 130 | for i in range(len(phi)): 131 | if i < j: 132 | ex = phi[i] 133 | ey = theta[i] 134 | ez = psi[i] 135 | e[i] = np.array([ex, ey, ez]) 136 | 137 | else: 138 | ex = phi[i] - 40 139 | ey = theta[i] + 10 140 | ez = psi[i] 141 | e[i] = np.array([ex, ey, ez]) 142 | 143 | sx = Ix*e[:,0] + l[0]*ie_wx 144 | sy = Iy*e[:,1] + l[1]*ie_wy 145 | sz = Iz*e[:,2] + l[2]*ie_wz 146 | 147 | ux = (Iz - Iy)*wy*wz - l[0]*e[:,0] - eta[0]*tanh(sx) 148 | uy = (Ix - Iz)*wx*wz - l[1]*e[:,1] - eta[1]*tanh(sy) 149 | uz = (Iy - Ix)*wy*wx - l[2]*e[:,2] - eta[2]*tanh(sz) 150 | 151 | u = [ux, uy, uz] 152 | 153 | for i in range(3): 154 | plt.subplot(3, 1, i+1) 155 | plt.plot(t, u[i]) 156 | plt.grid(); 157 | 158 | plt.show() 159 | -------------------------------------------------------------------------------- /control/sliding_mode/temp.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | from scipy.integrate import ode 3 | import numpy as np 4 | from numpy import sin,cos,tan,zeros,exp,tanh,dot,array 5 | from scipy.interpolate import interp1d 6 | from matplotlib import rc 7 | 8 | def f(t,Y,param): 9 | x1,x2=Y[0],Y[1] 10 | eta,k,lam=param[0],param[1],param[2] 11 | e=x1-1 12 | de=x2 13 | s=de+lam*e 14 | m,c=3+1.5*sin(x2*tanh(x2)*t),1.2+.2*sin(x2*tanh(x2)*t) 15 | u=m*(-eta*tanh(s)-k*s-lam*de)+c*x2**2*tanh(x2) 16 | x1dot=x2 17 | x2dot=(1/m)*(u-c*x2**2*tanh(x2)) 18 | return [x1dot,x2dot] 19 | 20 | def solver(t0,y0,dt,t1,param): 21 | x,t=[[] for i in range(2)],[] 22 | r=ode(f).set_integrator('dopri5',method='bdf') 23 | r.set_initial_value(y0,t0).set_f_params(param) 24 | while r.successful() and r.t 0.75].index\n", 34 | "data = pd.get_dummies(data)\n", 35 | "data = data.fillna(data.mean())\n", 36 | "data[skewed_feats] = np.log1p(data[skewed_feats])\n", 37 | "Xtrain = data[:train.shape[0]].values\n", 38 | "Xtest = data[train.shape[0]:].values\n", 39 | "y = train.SalePrice.values" 40 | ] 41 | }, 42 | { 43 | "cell_type": "code", 44 | "execution_count": 47, 45 | "metadata": {}, 46 | "outputs": [], 47 | "source": [ 48 | "def rmse_cv(model):\n", 49 | " rmse = np.sqrt(-cross_val_score(model, Xtrain, y, scoring='neg_mean_squared_error', cv = 5))\n", 50 | " return(rmse)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "code", 55 | "execution_count": 53, 56 | "metadata": {}, 57 | "outputs": [], 58 | "source": [ 59 | "model_lasso = LassoCV(alphas = [1, 0.1, 0.001, 0.0005], cv = 5).fit(Xtrain, y)\n", 60 | "preds = np.expm1(model_lasso.predict(Xtest))\n", 61 | "solution = pd.DataFrame({\"id\":test.Id, \"SalePrice\":preds})\n", 62 | "solution.to_cs" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": null, 68 | "metadata": {}, 69 | "outputs": [], 70 | "source": [] 71 | } 72 | ], 73 | "metadata": { 74 | "kernelspec": { 75 | "display_name": "Python 3", 76 | "language": "python", 77 | "name": "python3" 78 | }, 79 | "language_info": { 80 | "codemirror_mode": { 81 | "name": "ipython", 82 | "version": 3 83 | }, 84 | "file_extension": ".py", 85 | "mimetype": "text/x-python", 86 | "name": "python", 87 | "nbconvert_exporter": "python", 88 | "pygments_lexer": "ipython3", 89 | "version": "3.7.1" 90 | } 91 | }, 92 | "nbformat": 4, 93 | "nbformat_minor": 2 94 | } 95 | -------------------------------------------------------------------------------- /ml/IITMAA/.ipynb_checkpoints/practice-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /ml/IITMAA/practice.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import pandas as pd\n", 10 | "import matplotlib.pyplot as plt\n", 11 | "import seaborn as sns\n", 12 | "import numpy as np\n", 13 | "from sklearn.preprocessing import LabelEncoder\n", 14 | "from sklearn.model_selection import train_test_split, KFold, cross_val_score\n", 15 | "from sklearn.metrics import log_loss\n", 16 | "from sklearn.kernel_ridge import KernelRidge" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "df_train = pd.read_csv('train.csv');\n", 26 | "trainMaster = df_train.copy(deep = True)" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 3, 32 | "metadata": {}, 33 | "outputs": [], 34 | "source": [ 35 | "trainMaster.drop(\"date_time\", axis = 1, inplace = True)\n", 36 | "trainMaster.drop(\"is_holiday\", axis = 1, inplace = True)\n", 37 | "trainMaster.drop(\"weather_type\", axis = 1, inplace = True)\n", 38 | "trainMaster.drop(\"weather_description\", axis = 1, inplace = True)\n", 39 | "yTrain = trainMaster['traffic_volume'].values\n", 40 | "trainMaster.drop(\"traffic_volume\", axis = 1, inplace = True)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 4, 46 | "metadata": {}, 47 | "outputs": [], 48 | "source": [ 49 | "n_folds = 3\n", 50 | "def rmse_cv(model):\n", 51 | " kf = KFold(n_folds, shuffle=True, random_state=42).get_n_splits(trainMaster.values)\n", 52 | " rmse= np.sqrt(-cross_val_score(model, trainMaster.values, yTrain, scoring=\"neg_mean_squared_error\", cv = kf))\n", 53 | " return(rmse)" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 5, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "[ 2001.26600431 2110.01022445 895572.3621009 ]\n" 66 | ] 67 | } 68 | ], 69 | "source": [ 70 | "KRR = KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5)\n", 71 | "print(rmse_cv(KRR))" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": null, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [] 80 | } 81 | ], 82 | "metadata": { 83 | "kernelspec": { 84 | "display_name": "Python 3", 85 | "language": "python", 86 | "name": "python3" 87 | }, 88 | "language_info": { 89 | "codemirror_mode": { 90 | "name": "ipython", 91 | "version": 3 92 | }, 93 | "file_extension": ".py", 94 | "mimetype": "text/x-python", 95 | "name": "python", 96 | "nbconvert_exporter": "python", 97 | "pygments_lexer": "ipython3", 98 | "version": "3.7.1" 99 | } 100 | }, 101 | "nbformat": 4, 102 | "nbformat_minor": 2 103 | } 104 | -------------------------------------------------------------------------------- /ml/IITMAA/sample_submission.csv: -------------------------------------------------------------------------------- 1 | date_time,traffic_volume 2 | 1969-05-17 21:00:00,500 3 | 1969-05-17 21:00:00,530 4 | 1969-05-17 21:00:00,545 5 | 1969-05-17 22:00:00,750 6 | -------------------------------------------------------------------------------- /ml/Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [], 3 | "metadata": {}, 4 | "nbformat": 4, 5 | "nbformat_minor": 2 6 | } 7 | -------------------------------------------------------------------------------- /ml/code.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import numpy as np 3 | import time 4 | import math 5 | import matplotlib.pyplot as plt 6 | 7 | ATTR_DIR = 'data/genome_scores.csv' 8 | TRAIN_DIR = 'data/train.csv' 9 | TEST_DIR = 'data/test.csv' 10 | GENRE_DIR = 'data/movies.csv' 11 | ATTRIBUTE_SZ = 1128 12 | USER_SZ = 7632 13 | GENRE_SZ = 19 14 | 15 | def process_genome_scores(): 16 | i = 0 17 | data = {} 18 | print("Processing movie attribute data...") 19 | GENOME_DATA = pd.read_csv(ATTR_DIR) 20 | MOVIE_SZ = GENOME_DATA.shape[0]/ATTRIBUTE_SZ 21 | while(i < MOVIE_SZ): 22 | movie_data = GENOME_DATA.iloc[i*ATTRIBUTE_SZ:(i+1)*ATTRIBUTE_SZ,2] 23 | data[GENOME_DATA.iloc[i*ATTRIBUTE_SZ,0]] = movie_data.values.reshape(ATTRIBUTE_SZ,1) 24 | i += 1 25 | del GENOME_DATA 26 | print("Movie data processed!") 27 | time.sleep(2) 28 | return data 29 | 30 | def process_genres(): 31 | # to hold genre vectors 32 | genre_data = {} 33 | # to hold movies under a genre 34 | genre_movieid = {} 35 | # to holds genres for movieid 36 | movie_genres = {} 37 | 38 | print("Retrieving genres for movies...") 39 | GENRE_DATA = pd.read_csv(GENRE_DIR) 40 | for i in range(GENRE_DATA.shape[0]): 41 | movieid = GENRE_DATA.iloc[i,0] 42 | genres = GENRE_DATA.iloc[i,2].split("|") 43 | genre_vec = convert_genres(genres) 44 | genre_data[movieid] = genre_vec 45 | 46 | movie_genres[movieid] = genres 47 | 48 | for genre in genres: 49 | if genre not in genre_movieid: 50 | genre_movieid[genre] = [movieid] 51 | else: 52 | (genre_movieid[genre]).append(movieid) 53 | 54 | del GENRE_DATA 55 | return genre_data, genre_movieid, movie_genres 56 | 57 | def generate_mean_attributes(movieid, genre_movieid, movie_genres, movie_data): 58 | phi = np.zeros((ATTRIBUTE_SZ,1)) 59 | # get all genres for the movie 60 | genres = movie_genres[movieid] 61 | for genre in genres: 62 | # get all movies with the current genre 63 | movies = genre_movieid[genre] 64 | for i in range(len(movies)): 65 | # get average attributes for all movies of this genre 66 | if movies[i] in movie_data: 67 | phi += movie_data[movies[i]] 68 | phi = phi/len(movies) 69 | 70 | phi = phi/len(genres) 71 | return phi 72 | 73 | def convert_genres(genres): 74 | genre_vec = np.zeros((19,1)) 75 | for i in range(len(genres)): 76 | if(genres[i] == 'Action'): 77 | genre_vec[0] = 1 78 | elif(genres[i] == 'Adventure'): 79 | genre_vec[1] = 1 80 | elif(genres[i] == 'Animation'): 81 | genre_vec[2] = 1 82 | elif(genres[i] == 'Children'): 83 | genre_vec[3] = 1 84 | elif(genres[i] == 'Comedy'): 85 | genre_vec[4] = 1 86 | elif(genres[i] == 'Crime'): 87 | genre_vec[5] = 1 88 | elif(genres[i] == 'Documentary'): 89 | genre_vec[6] = 1 90 | elif(genres[i] == 'Drama'): 91 | genre_vec[7] = 1 92 | elif(genres[i] == 'Fantasy'): 93 | genre_vec[8] = 1 94 | elif(genres[i] == 'Film-Noir'): 95 | genre_vec[9] = 1 96 | elif(genres[i] == 'Horror'): 97 | genre_vec[10] = 1 98 | elif(genres[i] == 'Musical'): 99 | genre_vec[11] = 1 100 | elif(genres[i] == 'Mystery'): 101 | genre_vec[12] = 1 102 | elif(genres[i] == 'Romance'): 103 | genre_vec[13] = 1 104 | elif(genres[i] == 'Sci-Fi'): 105 | genre_vec[14] = 1 106 | elif(genres[i] == 'Thriller'): 107 | genre_vec[15] = 1 108 | elif(genres[i] == 'War'): 109 | genre_vec[16] = 1 110 | elif(genres[i] == 'Western'): 111 | genre_vec[17] = 1 112 | else: 113 | genre_vec[18] = 1 114 | return genre_vec 115 | 116 | def train_user_model(data, W, aux_data, eta, lam): 117 | userid, movieid, rating = data 118 | movie_data, genre_data, genre_movieid, ratings_mean, movie_genres = aux_data 119 | 120 | # if new user, then create an entry to weight matrix 121 | if userid not in W: 122 | W[userid] = np.zeros((ATTRIBUTE_SZ+GENRE_SZ+1,1)) 123 | 124 | # if movie does not have genome scores, generate average attributes based on genres 125 | if movieid not in movie_data: 126 | phi = generate_mean_attributes(movieid, genre_movieid, movie_genres, movie_data) 127 | # phi = np.zeros((ATTRIBUTE_SZ,1)) 128 | else: 129 | # get feature vector for movie 130 | phi = movie_data[movieid] 131 | 132 | phi = np.vstack((1,phi,genre_data[movieid])) 133 | phi = phi/np.linalg.norm(phi) 134 | 135 | # get linear regression weights for user 136 | w = W[userid] 137 | # update regression weights for user 138 | w = w + eta*((rating - (w.T).dot(phi))*phi - lam*w) 139 | # update the weight matrix 140 | W[userid] = w 141 | # update movie mean ratings 142 | if movieid not in ratings_mean: 143 | ratings_mean[movieid] = np.array([rating, 1]) 144 | else: 145 | ratings_mean[movieid][0] += rating 146 | ratings_mean[movieid][1] += 1 147 | 148 | 149 | def train_model(aux_data, eta, epochs, lam = 0): 150 | # initialise dict to hold weights for users 151 | W = {} 152 | # initialise dict to hold movie mean ratings 153 | ratings_mean = {} 154 | 155 | movie_data, genre_data, genre_movieid, movie_genres = aux_data 156 | aux_data = [movie_data, genre_data, genre_movieid, ratings_mean, movie_genres] 157 | 158 | train_data = pd.read_csv('data/split_train.csv') 159 | train_data = train_data.values 160 | time.sleep(1) 161 | print("Training model:") 162 | epoch = 0 163 | while(epoch < epochs): 164 | print("Epoch No: %d" % (epoch)) 165 | for i in range(train_data.shape[0]): 166 | data = train_data[i,:] 167 | train_user_model(data, W, aux_data, eta, lam) 168 | if(i%400000 == 0): 169 | print("Progress: %.2f percent" %(i/train_data.shape[0]*100)) 170 | 171 | epoch += 1 172 | 173 | mean = np.mean(train_data[:,2]) 174 | del train_data 175 | return W, ratings_mean, mean 176 | 177 | def predict_rating(userid, movieid, W, aux_data): 178 | movie_data, genre_data, genre_movieid, movie_genres, ratings_mean, mean = aux_data 179 | # if user not encountered during training 180 | if userid not in W: 181 | # if movie not encountered during training predict mean rating 182 | if movieid not in ratings_mean: 183 | pred = mean 184 | else: 185 | # movie encountered, predict corresponding mean rating 186 | pred = ratings_mean[movieid][0]/ratings_mean[movieid][1] 187 | else: 188 | w = W[userid] 189 | if movieid not in movie_data: 190 | # if movie does not have genome scores, use mean attributes 191 | phi = generate_mean_attributes(movieid, genre_movieid, movie_genres, movie_data) 192 | phi = np.vstack((1,phi,genre_data[movieid])) 193 | phi = phi/np.linalg.norm(phi) 194 | if movieid in ratings_mean: 195 | # if movie encountered during training, take weighted average 196 | pred = 0.7*(w.T).dot(phi) + 0.3*ratings_mean[movieid][0]/ratings_mean[movieid][1] 197 | else: 198 | # if movie not encountered during training, predict based on mean attributes 199 | pred = (w.T).dot(phi) 200 | 201 | # ============================================================================= 202 | # # if movie not encountered during training predict mean rating 203 | # phi = np.zeros((ATTRIBUTE_SZ,1)) 204 | # phi = np.vstack((1,phi,genre_data[movieid])) 205 | # phi = phi/np.linalg.norm(phi) 206 | # if movieid not in ratings_mean: 207 | # pred = (w.T).dot(phi) 208 | # else: 209 | # pred = 0.4*(w.T).dot(phi) + 0.6*ratings_mean[movieid][0]/ratings_mean[movieid][1] 210 | # ============================================================================= 211 | else: 212 | phi = movie_data[movieid] 213 | phi = np.vstack((1,phi,genre_data[movieid])) 214 | phi = phi/np.linalg.norm(phi) 215 | pred = (w.T).dot(phi) 216 | 217 | pred = round(float(pred),1) 218 | 219 | if pred > 5.0: 220 | pred = 5.0 221 | elif pred < 0.5: 222 | pred = 0.5 223 | 224 | return pred 225 | 226 | def predictions(W, aux_data): 227 | # movie_data, genre_data, genre_movieid, movie_genres, ratings_mean, mean = aux_data 228 | 229 | print("Beginning test data evaluation...") 230 | test_data = pd.read_csv(TEST_DIR) 231 | test_data = test_data.values 232 | pred = np.zeros((test_data.shape[0],1)) 233 | idd = np.arange(0, test_data.shape[0],1) 234 | for i in range(test_data.shape[0]): 235 | ids = test_data[i,:] 236 | pred[i] = predict_rating(ids[0], ids[1], W, aux_data) 237 | if(i%100000 == 0): 238 | print("Progress: %.2f percent" %(i/test_data.shape[0]*100)) 239 | 240 | df = pd.DataFrame(idd, columns = ['Id']) 241 | df['Prediction'] = pred 242 | df.to_csv('data/results.csv', index=False) 243 | del test_data 244 | 245 | def split_pred(W, aux_data): 246 | print("Beginning test data evaluation...") 247 | test_data = pd.read_csv('data/split_test.csv') 248 | test_data = test_data.values 249 | error = 0; total = 0; 250 | for i in range(test_data.shape[0]): 251 | ids = test_data[i,:] 252 | pred = predict_rating(ids[0], ids[1], W, aux_data) 253 | error += (pred-ids[2])**2 254 | total += 1 255 | if(i%400000 == 0): 256 | print("Progress: %.2f percent" %(i/test_data.shape[0]*100)) 257 | return error/total 258 | 259 | 260 | 261 | def main(): 262 | # get attribute relevance scores for each movie 263 | movie_data = process_genome_scores() 264 | genre_data, genre_movieid, movie_genres = process_genres() 265 | aux_data = [movie_data, genre_data, genre_movieid, movie_genres] 266 | W, ratings_mean, mean = train_model(aux_data, 0.5, 4) 267 | aux_data = [movie_data, genre_data, genre_movieid, movie_genres, ratings_mean, mean] 268 | print("MSE = %f" % (split_pred(W, movie_data, genre_data, ratings_mean, mean))) 269 | #predictions(W, aux_data) 270 | 271 | if __name__ == '__main__': 272 | main() 273 | -------------------------------------------------------------------------------- /ml/educated_attempt.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import pandas as pd\n", 10 | "import numpy as np\n", 11 | "from sklearn.linear_model import LassoCV\n", 12 | "from scipy.stats import skew\n", 13 | "ATTRIBUTE_SZ = 1128\n", 14 | "TRAIN_FILE = '../data/train.csv'\n", 15 | "SPLIT_TRAIN = '../data/split_train.csv'\n", 16 | "TEST_FILE = '../data/test.csv'\n", 17 | "SPLIT_TEST = '../data/split_test.csv'\n", 18 | "ATTR_DIR = '../data/genome_scores.csv'" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": null, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "data = " 28 | ] 29 | } 30 | ], 31 | "metadata": { 32 | "kernelspec": { 33 | "display_name": "Python 3", 34 | "language": "python", 35 | "name": "python3" 36 | }, 37 | "language_info": { 38 | "codemirror_mode": { 39 | "name": "ipython", 40 | "version": 3 41 | }, 42 | "file_extension": ".py", 43 | "mimetype": "text/x-python", 44 | "name": "python", 45 | "nbconvert_exporter": "python", 46 | "pygments_lexer": "ipython3", 47 | "version": "3.7.1" 48 | } 49 | }, 50 | "nbformat": 4, 51 | "nbformat_minor": 2 52 | } 53 | -------------------------------------------------------------------------------- /ml/movie_recommender.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import pandas as pd\n", 10 | "import numpy as np\n", 11 | "import time\n", 12 | "import math\n", 13 | "import matplotlib.pyplot as plt\n", 14 | "import sys\n", 15 | "\n", 16 | "ATTR_DIR = 'data/genome_scores.csv'\n", 17 | "TRAIN_DIR = 'data/train.csv'\n", 18 | "TEST_DIR = 'data/test.csv'\n", 19 | "GENRE_DIR = 'data/movies.csv'\n", 20 | "ATTRIBUTE_SZ = 1128\n", 21 | "USER_SZ = 7632\n", 22 | "GENRE_SZ = 19" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": {}, 29 | "outputs": [], 30 | "source": [ 31 | "def process_genome_scores():\n", 32 | " i = 0\n", 33 | " data = {}\n", 34 | " print(\"Processing movie attribute data...\")\n", 35 | " GENOME_DATA = pd.read_csv(ATTR_DIR)\n", 36 | " MOVIE_SZ = GENOME_DATA.shape[0]/ATTRIBUTE_SZ\n", 37 | " while(i < MOVIE_SZ):\n", 38 | " movie_data = GENOME_DATA.iloc[i*ATTRIBUTE_SZ:(i+1)*ATTRIBUTE_SZ,2]\n", 39 | " data[GENOME_DATA.iloc[i*ATTRIBUTE_SZ,0]] = movie_data.values.reshape(ATTRIBUTE_SZ,1)\n", 40 | " i += 1\n", 41 | " del GENOME_DATA\n", 42 | " print(\"Movie data processed!\")\n", 43 | " return data\n", 44 | "\n", 45 | "def process_genres():\n", 46 | "# # to hold genre vectors\n", 47 | " genre_data = {}\n", 48 | " # to hold movies under a genre\n", 49 | " genre_movieid = {}\n", 50 | " # to holds genres for movieid\n", 51 | " movie_genres = {}\n", 52 | " \n", 53 | " print(\"Retrieving genres for movies...\")\n", 54 | " GENRE_DATA = pd.read_csv(GENRE_DIR)\n", 55 | " for i in range(GENRE_DATA.shape[0]):\n", 56 | " movieid = GENRE_DATA.iloc[i,0]\n", 57 | " genres = GENRE_DATA.iloc[i,2].split(\"|\")\n", 58 | " genre_vec = convert_genres(genres)\n", 59 | " genre_data[movieid] = genre_vec\n", 60 | " \n", 61 | " movie_genres[movieid] = genres\n", 62 | " \n", 63 | " for genre in genres:\n", 64 | " if genre not in genre_movieid:\n", 65 | " genre_movieid[genre] = [movieid]\n", 66 | " else:\n", 67 | " (genre_movieid[genre]).append(movieid)\n", 68 | " \n", 69 | " del GENRE_DATA\n", 70 | " return genre_movieid, movie_genres, genre_data\n", 71 | "\n", 72 | "def get_genres_mean(genres, genre_mean, genre_movieid):\n", 73 | " mean = 0\n", 74 | " for genre in genres:\n", 75 | " mean += genre_mean[genre][0]/genre_mean[genre][1]\n", 76 | " return mean/len(genres)\n", 77 | " \n", 78 | "def convert_genres(genres):\n", 79 | " genre_vec = np.zeros((19,1))\n", 80 | " for i in range(len(genres)):\n", 81 | " if(genres[i] == 'Action'):\n", 82 | " genre_vec[0] = 1\n", 83 | " elif(genres[i] == 'Adventure'):\n", 84 | " genre_vec[1] = 1\n", 85 | " elif(genres[i] == 'Animation'):\n", 86 | " genre_vec[2] = 1\n", 87 | " elif(genres[i] == 'Children'):\n", 88 | " genre_vec[3] = 1\n", 89 | " elif(genres[i] == 'Comedy'):\n", 90 | " genre_vec[4] = 1\n", 91 | " elif(genres[i] == 'Crime'):\n", 92 | " genre_vec[5] = 1\n", 93 | " elif(genres[i] == 'Documentary'):\n", 94 | " genre_vec[6] = 1\n", 95 | " elif(genres[i] == 'Drama'):\n", 96 | " genre_vec[7] = 1\n", 97 | " elif(genres[i] == 'Fantasy'):\n", 98 | " genre_vec[8] = 1\n", 99 | " elif(genres[i] == 'Film-Noir'):\n", 100 | " genre_vec[9] = 1\n", 101 | " elif(genres[i] == 'Horror'):\n", 102 | " genre_vec[10] = 1\n", 103 | " elif(genres[i] == 'Musical'):\n", 104 | " genre_vec[11] = 1\n", 105 | " elif(genres[i] == 'Mystery'):\n", 106 | " genre_vec[12] = 1\n", 107 | " elif(genres[i] == 'Romance'):\n", 108 | " genre_vec[13] = 1\n", 109 | " elif(genres[i] == 'Sci-Fi'):\n", 110 | " genre_vec[14] = 1\n", 111 | " elif(genres[i] == 'Thriller'):\n", 112 | " genre_vec[15] = 1\n", 113 | " elif(genres[i] == 'War'):\n", 114 | " genre_vec[16] = 1\n", 115 | " elif(genres[i] == 'Western'):\n", 116 | " genre_vec[17] = 1\n", 117 | " else:\n", 118 | " genre_vec[18] = 1\n", 119 | " return genre_vec" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 3, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "def train_user_model(data, W, aux_data, eta, lam):\n", 129 | " userid, movieid, rating = data\n", 130 | " movie_data, genre_movieid, ratings_mean, movie_genres, genre_mean, mean = aux_data\n", 131 | " \n", 132 | " # if new user, then create an entry to weight matrix\n", 133 | " if userid not in W:\n", 134 | " W[userid] = np.zeros((ATTRIBUTE_SZ,1))\n", 135 | "\n", 136 | " # if movie does not have genome scores ignore\n", 137 | " if movieid not in movie_data:\n", 138 | " phi = np.zeros((ATTRIBUTE_SZ,1))\n", 139 | " else:\n", 140 | " # get feature vector for movie\n", 141 | " phi = movie_data[movieid]\n", 142 | " phi = phi/np.linalg.norm(phi) \n", 143 | "\n", 144 | " # get linear regression weights for user\n", 145 | " w = W[userid]\n", 146 | " # update regression weights for user\n", 147 | " w = w + eta*((rating - (w.T).dot(phi))*phi - lam*w)\n", 148 | " # update the weight matrix\n", 149 | " W[userid] = w\n", 150 | " # update movie mean ratings\n", 151 | " if movieid not in ratings_mean:\n", 152 | " ratings_mean[movieid] = np.array([rating, 1])\n", 153 | " else:\n", 154 | " ratings_mean[movieid][0] += rating\n", 155 | " ratings_mean[movieid][1] += 1\n", 156 | " # update genre mean rating\n", 157 | " genres = movie_genres[movieid]\n", 158 | " for genre in genres:\n", 159 | " if genre not in genre_mean:\n", 160 | " genre_mean[genre] = np.array([rating, 1])\n", 161 | " else:\n", 162 | " genre_mean[genre][0] += rating\n", 163 | " genre_mean[genre][1] += 1\n", 164 | " \n", 165 | "def train_model(aux_data, eta, epochs, lam = 0):\n", 166 | " # initialise dict to hold weights for users\n", 167 | " W = {}\n", 168 | " # initialise dict to hold movie mean ratings\n", 169 | " ratings_mean = {}\n", 170 | " # initialise dict to hold genre mean ratings\n", 171 | " genre_mean = {}\n", 172 | "\n", 173 | " movie_data, genre_movieid, movie_genres = aux_data\n", 174 | " \n", 175 | " train_data = pd.read_csv(TRAIN_DIR)\n", 176 | " train_data = train_data.values\n", 177 | " mean = np.mean(train_data[:,2])\n", 178 | " aux_data = [movie_data, genre_movieid, ratings_mean, movie_genres, genre_mean, mean]\n", 179 | " print(\"Training model:\")\n", 180 | " epoch = 0\n", 181 | " while(epoch < epochs):\n", 182 | " print(\"Epoch No: %d\" % (epoch))\n", 183 | " for i in range(train_data.shape[0]):\n", 184 | " data = train_data[i,:]\n", 185 | " train_user_model(data, W, aux_data, eta, lam)\n", 186 | " if(i%400000 == 0):\n", 187 | " print(\"Progress: %.2f percent\" %(i/train_data.shape[0]*100))\n", 188 | " epoch += 1\n", 189 | " \n", 190 | " del train_data\n", 191 | " return W, aux_data" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 4, 197 | "metadata": {}, 198 | "outputs": [], 199 | "source": [ 200 | "def predict_rating(userid, movieid, W, aux_data):\n", 201 | " movie_data, genre_movieid, ratings_mean, movie_genres, genre_mean, mean = aux_data\n", 202 | " genres = movie_genres[movieid]\n", 203 | " # if user not encountered during training\n", 204 | " if userid not in W:\n", 205 | " # if movie not encountered during training predict mean rating for the genre\n", 206 | " if movieid not in ratings_mean:\n", 207 | " pred = (0.7*mean + 0.3*get_genres_mean(genres, genre_mean, genre_movieid))\n", 208 | " else:\n", 209 | " # movie encountered, predict weighted average of genre mean rating and movie mean rating\n", 210 | " pred = 0.3*mean + 0.1*get_genres_mean(genres, genre_mean, genre_movieid) + 0.6*ratings_mean[movieid][0]/ratings_mean[movieid][1]\n", 211 | "\n", 212 | " else:\n", 213 | " w = W[userid]\n", 214 | " # movie does not have feature vector\n", 215 | " if movieid not in movie_data:\n", 216 | " if movieid in ratings_mean:\n", 217 | " # if movie encountered during training, take weighted average \n", 218 | " pred = 0.1*mean + 0.3*get_genres_mean(genres, genre_mean, genre_movieid) + 0.6*ratings_mean[movieid][0]/ratings_mean[movieid][1]\n", 219 | " else:\n", 220 | " # if movie not encountered during training, predict genre mean rating\n", 221 | " pred = 0.2*mean + 0.8*get_genres_mean(genres, genre_mean, genre_movieid)\n", 222 | " \n", 223 | " else:\n", 224 | " phi = movie_data[movieid]\n", 225 | " # phi = np.vstack((1,phi))\n", 226 | " phi = phi/np.linalg.norm(phi)\n", 227 | " pred = (w.T).dot(phi)\n", 228 | "\n", 229 | " pred = round(float(pred),1)\n", 230 | "\n", 231 | " if pred > 5.0:\n", 232 | " pred = 5.0\n", 233 | " elif pred < 0.5:\n", 234 | " pred = 0.5\n", 235 | "\n", 236 | " return pred\n", 237 | "\n", 238 | "def predictions(W, aux_data): \n", 239 | " # movie_data, genre_movieid, ratings_mean, movie_genres, genre_mean, mean = aux_data\n", 240 | " print(\"Beginning test data evaluation...\")\n", 241 | " test_data = pd.read_csv(TEST_DIR)\n", 242 | " test_data = test_data.values\n", 243 | " pred = np.zeros((test_data.shape[0],1))\n", 244 | " idd = np.arange(0, test_data.shape[0],1)\n", 245 | " for i in range(test_data.shape[0]):\n", 246 | " ids = test_data[i,:]\n", 247 | " pred[i] = predict_rating(ids[0], ids[1], W, aux_data)\n", 248 | " if(i%500000 == 0):\n", 249 | " print(\"Progress: %.2f percent\" %(i/test_data.shape[0]*100))\n", 250 | "\n", 251 | " df = pd.DataFrame(idd, columns = ['Id'])\n", 252 | " df['Prediction'] = pred\n", 253 | " df.to_csv('data/results.csv', index=False)\n", 254 | " del test_data\n", 255 | "\n", 256 | "def split_pred(W, aux_data):\n", 257 | " print(\"Beginning test data evaluation...\")\n", 258 | " test_data = pd.read_csv('data/split_test.csv')\n", 259 | " test_data = test_data.values\n", 260 | " error = 0; total = 0;\n", 261 | " for i in range(test_data.shape[0]):\n", 262 | " ids = test_data[i,:]\n", 263 | " pred = predict_rating(ids[0], ids[1], W, aux_data)\n", 264 | " error += (pred-ids[2])**2\n", 265 | " total += 1\n", 266 | " if(i%400000 == 0):\n", 267 | " print(\"Progress: %.2f percent\" %(i/test_data.shape[0]*100))\n", 268 | " return error/total" 269 | ] 270 | }, 271 | { 272 | "cell_type": "code", 273 | "execution_count": null, 274 | "metadata": {}, 275 | "outputs": [], 276 | "source": [ 277 | "def RSA_training(data, W, nvar):\n", 278 | " userid, movieid, rating = data\n", 279 | " # if new user, then create an entry to weight matrix\n", 280 | " if userid not in W:\n", 281 | " W[userid] = np.zeros((nvar,1))\n", 282 | "\n", 283 | " # if movie does not have genome scores ignore\n", 284 | " if movieid not in movie_data:\n", 285 | " phi = np.zeros((nvar,1))\n", 286 | " else:\n", 287 | " # get feature vector for movie\n", 288 | " phi = movie_data[movieid][0:nvar,0]\n", 289 | " # phi = np.vstack((1,phi))\n", 290 | " phi = phi/np.linalg.norm(phi) \n", 291 | "\n", 292 | " # get linear regression weights for user\n", 293 | " w = W[userid]\n", 294 | " # update regression weights for user\n", 295 | " w = w + eta*((rating - (w.T).dot(phi))*phi - lam*w)\n", 296 | " # update the weight matrix\n", 297 | " W[userid] = w\n", 298 | "\n", 299 | "def calculate_SSR(test_file, train_file, )\n", 300 | "def r_squared_analysis():\n", 301 | " " 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 5, 307 | "metadata": { 308 | "scrolled": false 309 | }, 310 | "outputs": [ 311 | { 312 | "name": "stdout", 313 | "output_type": "stream", 314 | "text": [ 315 | "Processing movie attribute data...\n", 316 | "Movie data processed!\n", 317 | "Retrieving genres for movies...\n", 318 | "Training model:\n", 319 | "Epoch No: 0\n", 320 | "Progress: 0.00 percent\n", 321 | "Progress: 7.60 percent\n", 322 | "Progress: 15.20 percent\n", 323 | "Progress: 22.79 percent\n", 324 | "Progress: 30.39 percent\n", 325 | "Progress: 37.99 percent\n", 326 | "Progress: 45.59 percent\n", 327 | "Progress: 53.19 percent\n", 328 | "Progress: 60.79 percent\n", 329 | "Progress: 68.38 percent\n", 330 | "Progress: 75.98 percent\n", 331 | "Progress: 83.58 percent\n", 332 | "Progress: 91.18 percent\n", 333 | "Progress: 98.78 percent\n", 334 | "Epoch No: 1\n", 335 | "Progress: 0.00 percent\n", 336 | "Progress: 7.60 percent\n", 337 | "Progress: 15.20 percent\n", 338 | "Progress: 22.79 percent\n", 339 | "Progress: 30.39 percent\n", 340 | "Progress: 37.99 percent\n", 341 | "Progress: 45.59 percent\n", 342 | "Progress: 53.19 percent\n", 343 | "Progress: 60.79 percent\n", 344 | "Progress: 68.38 percent\n", 345 | "Progress: 75.98 percent\n", 346 | "Progress: 83.58 percent\n", 347 | "Progress: 91.18 percent\n", 348 | "Progress: 98.78 percent\n" 349 | ] 350 | } 351 | ], 352 | "source": [ 353 | "# get attribute relevance scores for each movie\n", 354 | "movie_data = process_genome_scores()\n", 355 | "genre_movieid, movie_genres, genre_data = process_genres()\n", 356 | "aux_data = [movie_data, genre_movieid, movie_genres]\n", 357 | "W, aux_data = train_model(aux_data, 0.5, 2)" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 7, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "name": "stdout", 367 | "output_type": "stream", 368 | "text": [ 369 | "Beginning test data evaluation...\n", 370 | "Progress: 0.00 percent\n", 371 | "Progress: 21.69 percent\n", 372 | "Progress: 43.38 percent\n", 373 | "Progress: 65.08 percent\n", 374 | "Progress: 86.77 percent\n" 375 | ] 376 | } 377 | ], 378 | "source": [ 379 | "#print(\"MSE = %f\" % (split_pred(W, aux_data)))\n", 380 | "predictions(W, aux_data)" 381 | ] 382 | }, 383 | { 384 | "cell_type": "code", 385 | "execution_count": null, 386 | "metadata": {}, 387 | "outputs": [], 388 | "source": [] 389 | } 390 | ], 391 | "metadata": { 392 | "kernelspec": { 393 | "display_name": "Python 3", 394 | "language": "python", 395 | "name": "python3" 396 | }, 397 | "language_info": { 398 | "codemirror_mode": { 399 | "name": "ipython", 400 | "version": 3 401 | }, 402 | "file_extension": ".py", 403 | "mimetype": "text/x-python", 404 | "name": "python", 405 | "nbconvert_exporter": "python", 406 | "pygments_lexer": "ipython3", 407 | "version": "3.7.1" 408 | } 409 | }, 410 | "nbformat": 4, 411 | "nbformat_minor": 2 412 | } 413 | -------------------------------------------------------------------------------- /ml/sklearn.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 48, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import pandas as pd\n", 10 | "import numpy as np\n", 11 | "import sys\n", 12 | "from sklearn.linear_model import LinearRegression\n", 13 | "from sklearn.linear_model import RidgeCV\n", 14 | "from sklearn.svm import SVR\n", 15 | "ATTRIBUTE_SZ = 1128\n", 16 | "TRAIN_FILE = '../data/train.csv'\n", 17 | "SPLIT_TRAIN = '../data/split_train.csv'\n", 18 | "TEST_FILE = '../data/test.csv'\n", 19 | "SPLIT_TEST = '../data/split_test.csv'\n", 20 | "ATTR_DIR = '../data/genome_scores.csv'" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 50, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "name": "stdout", 30 | "output_type": "stream", 31 | "text": [ 32 | "Processing movie attribute data...\n", 33 | "Movie data processed!\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "def process_genome_scores():\n", 39 | " i = 0\n", 40 | " data = {}\n", 41 | " print(\"Processing movie attribute data...\")\n", 42 | " GENOME_DATA = pd.read_csv(ATTR_DIR)\n", 43 | " MOVIE_SZ = GENOME_DATA.shape[0]/ATTRIBUTE_SZ\n", 44 | " while(i < MOVIE_SZ):\n", 45 | " movie_data = GENOME_DATA.iloc[i*ATTRIBUTE_SZ:(i+1)*ATTRIBUTE_SZ,2]\n", 46 | " data[GENOME_DATA.iloc[i*ATTRIBUTE_SZ,0]] = movie_data.values.reshape(ATTRIBUTE_SZ,)\n", 47 | " i += 1\n", 48 | " del GENOME_DATA\n", 49 | " print(\"Movie data processed!\")\n", 50 | " return data\n", 51 | "\n", 52 | "def group_train_data(train_file):\n", 53 | " train_data = pd.read_csv(train_file)\n", 54 | " train_data = train_data.groupby([\"userId\"])\n", 55 | " grouped_data = {}\n", 56 | " for group in train_data.groups:\n", 57 | " grouped_data[group] = train_data.get_group(group).values\n", 58 | " \n", 59 | " del train_data\n", 60 | " return grouped_data\n", 61 | "\n", 62 | "def get_mean_ratings(train_file):\n", 63 | " train_data = pd.read_csv(train_file)\n", 64 | " train_data = train_data.values\n", 65 | " movie_mean_rating = {}\n", 66 | " user_mean_rating = {}\n", 67 | " for i in range(train_data.shape[0]):\n", 68 | " userid = train_data[i,0]\n", 69 | " movieid = train_data[i,1]\n", 70 | " rating = train_data[i,2]\n", 71 | " if userid not in user_mean_rating:\n", 72 | " user_mean_rating[userid] = np.array([rating,1])\n", 73 | " else:\n", 74 | " user_mean_rating[userid][0] += rating\n", 75 | " user_mean_rating[userid][1] += 1\n", 76 | " if movieid not in movie_mean_rating:\n", 77 | " movie_mean_rating[movieid] = np.array([rating,1])\n", 78 | " else:\n", 79 | " movie_mean_rating[movieid][0] += rating\n", 80 | " movie_mean_rating[movieid][1] += 1\n", 81 | " mean_rating = np.mean(train_data[:,2])\n", 82 | " \n", 83 | " for k in movie_mean_rating:\n", 84 | " movie_mean_rating[k][0] = movie_mean_rating[k][0]/movie_mean_rating[k][1]\n", 85 | " for k in user_mean_rating:\n", 86 | " user_mean_rating[k][0] = user_mean_rating[k][0]/user_mean_rating[k][1]\n", 87 | "\n", 88 | " del train_data\n", 89 | " return movie_mean_rating, user_mean_rating, mean_rating\n", 90 | "\n", 91 | "genome_scores = process_genome_scores()\n", 92 | "train_data = group_train_data(SPLIT_TRAIN)\n", 93 | "movie_mean_rating, user_mean_rating, mean_rating = get_mean_ratings(SPLIT_TRAIN)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": null, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "Training linear regression models.................." 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "def process_train_data(train_data, genome_scores):\n", 111 | " N = train_data.shape[0]\n", 112 | " d = ATTRIBUTE_SZ\n", 113 | " X = np.zeros((N,d))\n", 114 | " y = np.zeros((N,))\n", 115 | " train_data = train_data[:,1:3]\n", 116 | " for i in range(N):\n", 117 | " movieid = train_data[i,0]\n", 118 | " if movieid in genome_scores:\n", 119 | " X[i,:] = genome_scores[movieid]\n", 120 | " y[i] = train_data[i,1]\n", 121 | " return X, y\n", 122 | "\n", 123 | "def train_model(train_data, genome_scores, kernel = 'none', type = 'LR'):\n", 124 | " N = train_data.shape[0]\n", 125 | " d = ATTRIBUTE_SZ\n", 126 | " \n", 127 | " X, y = process_train_data(train_data, genome_scores)\n", 128 | " if type == 'Ridge':\n", 129 | " regr = RidgeCV(alphas = [0.05, 0.5, 1.0])\n", 130 | " elif(type == 'LR'):\n", 131 | " regr = LinearRegression()\n", 132 | " elif(type == 'SVR'):\n", 133 | " regr = SVR(gamma = 'scale')\n", 134 | " regr.fit(X, y)\n", 135 | " return regr\n", 136 | "\n", 137 | "def train(train_data, genome_scores):\n", 138 | " print(\"Training linear regression models...\", end =\"\")\n", 139 | " models = {}\n", 140 | " i = 0\n", 141 | " for user in train_data:\n", 142 | " models[user] = train_model(train_data[user], genome_scores, type = 'SVR')\n", 143 | " if i%100 == 0:\n", 144 | " print(\"...\", end =\"\")\n", 145 | " if i%1000 == 0 and i > 0:\n", 146 | " print(\"\\n %d users trained\" % (i))\n", 147 | " print(\"Still training\",end=\"\")\n", 148 | " i += 1\n", 149 | " \n", 150 | " return models\n", 151 | "\n", 152 | "models = train(train_data, genome_scores)" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 45, 158 | "metadata": {}, 159 | "outputs": [], 160 | "source": [ 161 | "def predict_rating(test_file, models, genome_scores, mean, test = False):\n", 162 | " test_data = pd.read_csv(test_file).values\n", 163 | " print(\"Evaluating model...\",end=\"\") \n", 164 | " movie_mean_rating, user_mean_rating, mean_rating = mean\n", 165 | " N = test_data.shape[0]\n", 166 | " ids = np.arange(0, N, 1)\n", 167 | " pred = np.zeros((N,1))\n", 168 | " counter = 0\n", 169 | " for i in range(N):\n", 170 | " userid = test_data[i,0]; movieid = test_data[i,1]\n", 171 | " if userid in models:\n", 172 | " # if movie has genome score\n", 173 | " if movieid in genome_scores:\n", 174 | " phi = genome_scores[movieid]\n", 175 | " phi = phi.reshape(1,len(phi))\n", 176 | " pred[i,0] = models[userid].predict(phi)\n", 177 | " # if genome score not available\n", 178 | " else:\n", 179 | " if movieid in movie_mean_rating:\n", 180 | " if userid in user_mean_rating:\n", 181 | " pred[i,0] = 0.1*mean_rating + 0.2*movie_mean_rating[movieid][0] + 0.7*user_mean_rating[userid][0]\n", 182 | " else:\n", 183 | " pred[i,0] = 0.6*mean_rating + 0.4*movie_mean_rating[movieid][0]\n", 184 | " else:\n", 185 | " if userid in user_mean_rating:\n", 186 | " pred[i,0] = 0.4*mean_rating + 0.6*user_mean_rating[userid][0]\n", 187 | " else:\n", 188 | " pred[i,0] = mean_rating\n", 189 | " pred[i,0] = round(float(pred[i,0]),1)\n", 190 | " if pred[i,0] > 5.0:\n", 191 | " pred[i,0] = 5.0\n", 192 | " elif pred[i,0] < 0.5:\n", 193 | " pred[i,0] = 0.5 \n", 194 | " \n", 195 | " if test == True:\n", 196 | " t = test_data[i,2]\n", 197 | " pred[i,0] = (pred[i,0] - t)**2\n", 198 | " \n", 199 | " if(counter%500000 == 0):\n", 200 | " print(\"...\", end=\"\")\n", 201 | " counter += 1\n", 202 | " \n", 203 | " print(\"\\nDone!\")\n", 204 | " del test_data\n", 205 | " \n", 206 | " if test == True:\n", 207 | " return np.sum(pred)/len(pred)\n", 208 | " else:\n", 209 | " df = pd.DataFrame(idd, columns = ['Id'])\n", 210 | " df['Prediction'] = pred\n", 211 | " df.to_csv('data/results.csv', index=False) " 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 38, 217 | "metadata": { 218 | "scrolled": true 219 | }, 220 | "outputs": [ 221 | { 222 | "name": "stdout", 223 | "output_type": "stream", 224 | "text": [ 225 | "Evaluating model...........................\n", 226 | "Done!\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "mean = [movie_mean_rating, user_mean_rating, mean_rating]\n", 232 | "mse = predict_rating(SPLIT_TRAIN, models, genome_scores, mean, test = True)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 40, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "0.026369160672829423\n" 245 | ] 246 | } 247 | ], 248 | "source": [] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [] 256 | } 257 | ], 258 | "metadata": { 259 | "kernelspec": { 260 | "display_name": "Python 3", 261 | "language": "python", 262 | "name": "python3" 263 | }, 264 | "language_info": { 265 | "codemirror_mode": { 266 | "name": "ipython", 267 | "version": 3 268 | }, 269 | "file_extension": ".py", 270 | "mimetype": "text/x-python", 271 | "name": "python", 272 | "nbconvert_exporter": "python", 273 | "pygments_lexer": "ipython3", 274 | "version": "3.7.1" 275 | } 276 | }, 277 | "nbformat": 4, 278 | "nbformat_minor": 2 279 | } 280 | -------------------------------------------------------------------------------- /ml/titanic/.ipynb_checkpoints/titanic-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import pandas as pd\n", 10 | "import numpy as np\n", 11 | "import matplotlib.pyplot as plt\n", 12 | "from sklearn.linear_model import LogisticRegression\n", 13 | "from sklearn.svm import SVC\n", 14 | "from sklearn.model_selection import cross_val_score" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 17, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "name": "stdout", 24 | "output_type": "stream", 25 | "text": [ 26 | "1\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "train = pd.read_csv('train.csv')\n", 32 | "test = pd.read_csv('test.csv')\n", 33 | "drop_cols = ['PassengerId', 'Ticket', 'Cabin']\n", 34 | "train.drop(drop_cols, axis=1, inplace=True)\n", 35 | "test.drop(drop_cols, axis=1, inplace=True)\n", 36 | "y = train['Survived']\n", 37 | "train.drop(['Survived'], axis=1, inplace=True)\n", 38 | "data = pd.concat([train, test])\n", 39 | "data['Age'].fillna(data['Age'].median(), inplace=True)\n", 40 | "data['Embarked'].fillna(data['Embarked'].mode(), inplace=True)\n", 41 | "data['Fare'].fillna(data['Fare'].mode(), inplace=True)\n", 42 | "for x in data['Fare']:\n", 43 | " if(np.isnan(x)):\n", 44 | " print(x)\n", 45 | " x = data['Fare'].mode()\n", 46 | "print(data['Fare'].isnull().sum())" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 3, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 10, 59 | "metadata": {}, 60 | "outputs": [], 61 | "source": [] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 11, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": null, 73 | "metadata": {}, 74 | "outputs": [], 75 | "source": [] 76 | } 77 | ], 78 | "metadata": { 79 | "kernelspec": { 80 | "display_name": "Python 3", 81 | "language": "python", 82 | "name": "python3" 83 | }, 84 | "language_info": { 85 | "codemirror_mode": { 86 | "name": "ipython", 87 | "version": 3 88 | }, 89 | "file_extension": ".py", 90 | "mimetype": "text/x-python", 91 | "name": "python", 92 | "nbconvert_exporter": "python", 93 | "pygments_lexer": "ipython3", 94 | "version": "3.6.8" 95 | } 96 | }, 97 | "nbformat": 4, 98 | "nbformat_minor": 2 99 | } 100 | -------------------------------------------------------------------------------- /ml/titanic/results.csv: -------------------------------------------------------------------------------- 1 | PassengerId,Survived 2 | 892,0 3 | 893,1 4 | 894,0 5 | 895,0 6 | 896,1 7 | 897,0 8 | 898,1 9 | 899,0 10 | 900,1 11 | 901,0 12 | 902,0 13 | 903,0 14 | 904,1 15 | 905,0 16 | 906,1 17 | 907,1 18 | 908,0 19 | 909,0 20 | 910,1 21 | 911,1 22 | 912,0 23 | 913,0 24 | 914,1 25 | 915,1 26 | 916,1 27 | 917,0 28 | 918,1 29 | 919,0 30 | 920,0 31 | 921,0 32 | 922,0 33 | 923,0 34 | 924,1 35 | 925,1 36 | 926,1 37 | 927,0 38 | 928,1 39 | 929,1 40 | 930,0 41 | 931,1 42 | 932,0 43 | 933,0 44 | 934,0 45 | 935,1 46 | 936,1 47 | 937,0 48 | 938,0 49 | 939,0 50 | 940,1 51 | 941,1 52 | 942,1 53 | 943,0 54 | 944,1 55 | 945,1 56 | 946,0 57 | 947,0 58 | 948,0 59 | 949,0 60 | 950,0 61 | 951,1 62 | 952,0 63 | 953,0 64 | 954,0 65 | 955,1 66 | 956,1 67 | 957,1 68 | 958,1 69 | 959,0 70 | 960,0 71 | 961,0 72 | 962,1 73 | 963,0 74 | 964,1 75 | 965,0 76 | 966,1 77 | 967,1 78 | 968,0 79 | 969,1 80 | 970,0 81 | 971,1 82 | 972,1 83 | 973,1 84 | 974,0 85 | 975,0 86 | 976,0 87 | 977,0 88 | 978,1 89 | 979,1 90 | 980,1 91 | 981,1 92 | 982,1 93 | 983,0 94 | 984,1 95 | 985,0 96 | 986,0 97 | 987,0 98 | 988,1 99 | 989,0 100 | 990,1 101 | 991,0 102 | 992,1 103 | 993,0 104 | 994,0 105 | 995,0 106 | 996,1 107 | 997,0 108 | 998,0 109 | 999,0 110 | 1000,0 111 | 1001,0 112 | 1002,0 113 | 1003,1 114 | 1004,1 115 | 1005,1 116 | 1006,1 117 | 1007,0 118 | 1008,0 119 | 1009,1 120 | 1010,1 121 | 1011,1 122 | 1012,1 123 | 1013,0 124 | 1014,1 125 | 1015,0 126 | 1016,0 127 | 1017,1 128 | 1018,0 129 | 1019,1 130 | 1020,0 131 | 1021,0 132 | 1022,0 133 | 1023,0 134 | 1024,1 135 | 1025,0 136 | 1026,0 137 | 1027,0 138 | 1028,0 139 | 1029,0 140 | 1030,1 141 | 1031,0 142 | 1032,0 143 | 1033,1 144 | 1034,0 145 | 1035,0 146 | 1036,0 147 | 1037,0 148 | 1038,1 149 | 1039,0 150 | 1040,0 151 | 1041,0 152 | 1042,1 153 | 1043,0 154 | 1044,0 155 | 1045,1 156 | 1046,0 157 | 1047,0 158 | 1048,0 159 | 1049,1 160 | 1050,0 161 | 1051,1 162 | 1052,1 163 | 1053,1 164 | 1054,1 165 | 1055,0 166 | 1056,0 167 | 1057,1 168 | 1058,0 169 | 1059,0 170 | 1060,1 171 | 1061,1 172 | 1062,0 173 | 1063,0 174 | 1064,0 175 | 1065,0 176 | 1066,0 177 | 1067,1 178 | 1068,1 179 | 1069,0 180 | 1070,1 181 | 1071,1 182 | 1072,0 183 | 1073,1 184 | 1074,1 185 | 1075,0 186 | 1076,0 187 | 1077,0 188 | 1078,1 189 | 1079,0 190 | 1080,0 191 | 1081,0 192 | 1082,0 193 | 1083,0 194 | 1084,0 195 | 1085,0 196 | 1086,1 197 | 1087,0 198 | 1088,1 199 | 1089,1 200 | 1090,0 201 | 1091,1 202 | 1092,1 203 | 1093,1 204 | 1094,1 205 | 1095,1 206 | 1096,0 207 | 1097,0 208 | 1098,1 209 | 1099,0 210 | 1100,1 211 | 1101,0 212 | 1102,0 213 | 1103,0 214 | 1104,1 215 | 1105,0 216 | 1106,1 217 | 1107,0 218 | 1108,1 219 | 1109,1 220 | 1110,1 221 | 1111,0 222 | 1112,1 223 | 1113,0 224 | 1114,1 225 | 1115,0 226 | 1116,1 227 | 1117,1 228 | 1118,0 229 | 1119,1 230 | 1120,0 231 | 1121,0 232 | 1122,1 233 | 1123,1 234 | 1124,0 235 | 1125,0 236 | 1126,1 237 | 1127,0 238 | 1128,1 239 | 1129,0 240 | 1130,1 241 | 1131,1 242 | 1132,1 243 | 1133,1 244 | 1134,1 245 | 1135,0 246 | 1136,0 247 | 1137,0 248 | 1138,1 249 | 1139,0 250 | 1140,1 251 | 1141,1 252 | 1142,1 253 | 1143,0 254 | 1144,1 255 | 1145,0 256 | 1146,0 257 | 1147,0 258 | 1148,0 259 | 1149,0 260 | 1150,1 261 | 1151,0 262 | 1152,0 263 | 1153,0 264 | 1154,1 265 | 1155,1 266 | 1156,0 267 | 1157,0 268 | 1158,0 269 | 1159,0 270 | 1160,1 271 | 1161,0 272 | 1162,1 273 | 1163,0 274 | 1164,1 275 | 1165,1 276 | 1166,0 277 | 1167,1 278 | 1168,0 279 | 1169,0 280 | 1170,0 281 | 1171,0 282 | 1172,1 283 | 1173,1 284 | 1174,1 285 | 1175,1 286 | 1176,1 287 | 1177,0 288 | 1178,0 289 | 1179,1 290 | 1180,0 291 | 1181,0 292 | 1182,0 293 | 1183,1 294 | 1184,0 295 | 1185,1 296 | 1186,0 297 | 1187,0 298 | 1188,1 299 | 1189,0 300 | 1190,0 301 | 1191,0 302 | 1192,0 303 | 1193,0 304 | 1194,0 305 | 1195,0 306 | 1196,1 307 | 1197,0 308 | 1198,1 309 | 1199,1 310 | 1200,1 311 | 1201,1 312 | 1202,0 313 | 1203,0 314 | 1204,0 315 | 1205,1 316 | 1206,1 317 | 1207,1 318 | 1208,1 319 | 1209,0 320 | 1210,0 321 | 1211,0 322 | 1212,0 323 | 1213,0 324 | 1214,0 325 | 1215,0 326 | 1216,1 327 | 1217,0 328 | 1218,1 329 | 1219,1 330 | 1220,0 331 | 1221,0 332 | 1222,0 333 | 1223,0 334 | 1224,0 335 | 1225,1 336 | 1226,0 337 | 1227,0 338 | 1228,0 339 | 1229,0 340 | 1230,0 341 | 1231,0 342 | 1232,0 343 | 1233,0 344 | 1234,0 345 | 1235,1 346 | 1236,0 347 | 1237,1 348 | 1238,0 349 | 1239,1 350 | 1240,0 351 | 1241,1 352 | 1242,1 353 | 1243,0 354 | 1244,1 355 | 1245,1 356 | 1246,1 357 | 1247,0 358 | 1248,0 359 | 1249,0 360 | 1250,0 361 | 1251,1 362 | 1252,0 363 | 1253,1 364 | 1254,1 365 | 1255,0 366 | 1256,1 367 | 1257,1 368 | 1258,0 369 | 1259,1 370 | 1260,1 371 | 1261,0 372 | 1262,0 373 | 1263,1 374 | 1264,0 375 | 1265,0 376 | 1266,1 377 | 1267,1 378 | 1268,1 379 | 1269,0 380 | 1270,0 381 | 1271,0 382 | 1272,0 383 | 1273,0 384 | 1274,1 385 | 1275,1 386 | 1276,0 387 | 1277,1 388 | 1278,0 389 | 1279,0 390 | 1280,0 391 | 1281,0 392 | 1282,1 393 | 1283,0 394 | 1284,0 395 | 1285,0 396 | 1286,0 397 | 1287,1 398 | 1288,0 399 | 1289,1 400 | 1290,0 401 | 1291,0 402 | 1292,1 403 | 1293,0 404 | 1294,1 405 | 1295,1 406 | 1296,0 407 | 1297,0 408 | 1298,0 409 | 1299,1 410 | 1300,1 411 | 1301,1 412 | 1302,1 413 | 1303,1 414 | 1304,1 415 | 1305,0 416 | 1306,1 417 | 1307,0 418 | 1308,0 419 | 1309,0 420 | -------------------------------------------------------------------------------- /ml/titanic/test.csv: -------------------------------------------------------------------------------- 1 | PassengerId,Pclass,Name,Sex,Age,SibSp,Parch,Ticket,Fare,Cabin,Embarked 2 | 892,3,"Kelly, Mr. James",male,34.5,0,0,330911,7.8292,,Q 3 | 893,3,"Wilkes, Mrs. James (Ellen Needs)",female,47,1,0,363272,7,,S 4 | 894,2,"Myles, Mr. Thomas Francis",male,62,0,0,240276,9.6875,,Q 5 | 895,3,"Wirz, Mr. Albert",male,27,0,0,315154,8.6625,,S 6 | 896,3,"Hirvonen, Mrs. Alexander (Helga E Lindqvist)",female,22,1,1,3101298,12.2875,,S 7 | 897,3,"Svensson, Mr. Johan Cervin",male,14,0,0,7538,9.225,,S 8 | 898,3,"Connolly, Miss. Kate",female,30,0,0,330972,7.6292,,Q 9 | 899,2,"Caldwell, Mr. Albert Francis",male,26,1,1,248738,29,,S 10 | 900,3,"Abrahim, Mrs. Joseph (Sophie Halaut Easu)",female,18,0,0,2657,7.2292,,C 11 | 901,3,"Davies, Mr. John Samuel",male,21,2,0,A/4 48871,24.15,,S 12 | 902,3,"Ilieff, Mr. Ylio",male,,0,0,349220,7.8958,,S 13 | 903,1,"Jones, Mr. Charles Cresson",male,46,0,0,694,26,,S 14 | 904,1,"Snyder, Mrs. John Pillsbury (Nelle Stevenson)",female,23,1,0,21228,82.2667,B45,S 15 | 905,2,"Howard, Mr. Benjamin",male,63,1,0,24065,26,,S 16 | 906,1,"Chaffee, Mrs. Herbert Fuller (Carrie Constance Toogood)",female,47,1,0,W.E.P. 5734,61.175,E31,S 17 | 907,2,"del Carlo, Mrs. Sebastiano (Argenia Genovesi)",female,24,1,0,SC/PARIS 2167,27.7208,,C 18 | 908,2,"Keane, Mr. Daniel",male,35,0,0,233734,12.35,,Q 19 | 909,3,"Assaf, Mr. Gerios",male,21,0,0,2692,7.225,,C 20 | 910,3,"Ilmakangas, Miss. Ida Livija",female,27,1,0,STON/O2. 3101270,7.925,,S 21 | 911,3,"Assaf Khalil, Mrs. Mariana (Miriam"")""",female,45,0,0,2696,7.225,,C 22 | 912,1,"Rothschild, Mr. Martin",male,55,1,0,PC 17603,59.4,,C 23 | 913,3,"Olsen, Master. Artur Karl",male,9,0,1,C 17368,3.1708,,S 24 | 914,1,"Flegenheim, Mrs. Alfred (Antoinette)",female,,0,0,PC 17598,31.6833,,S 25 | 915,1,"Williams, Mr. Richard Norris II",male,21,0,1,PC 17597,61.3792,,C 26 | 916,1,"Ryerson, Mrs. Arthur Larned (Emily Maria Borie)",female,48,1,3,PC 17608,262.375,B57 B59 B63 B66,C 27 | 917,3,"Robins, Mr. Alexander A",male,50,1,0,A/5. 3337,14.5,,S 28 | 918,1,"Ostby, Miss. Helene Ragnhild",female,22,0,1,113509,61.9792,B36,C 29 | 919,3,"Daher, Mr. Shedid",male,22.5,0,0,2698,7.225,,C 30 | 920,1,"Brady, Mr. John Bertram",male,41,0,0,113054,30.5,A21,S 31 | 921,3,"Samaan, Mr. Elias",male,,2,0,2662,21.6792,,C 32 | 922,2,"Louch, Mr. Charles Alexander",male,50,1,0,SC/AH 3085,26,,S 33 | 923,2,"Jefferys, Mr. Clifford Thomas",male,24,2,0,C.A. 31029,31.5,,S 34 | 924,3,"Dean, Mrs. Bertram (Eva Georgetta Light)",female,33,1,2,C.A. 2315,20.575,,S 35 | 925,3,"Johnston, Mrs. Andrew G (Elizabeth Lily"" Watson)""",female,,1,2,W./C. 6607,23.45,,S 36 | 926,1,"Mock, Mr. Philipp Edmund",male,30,1,0,13236,57.75,C78,C 37 | 927,3,"Katavelas, Mr. Vassilios (Catavelas Vassilios"")""",male,18.5,0,0,2682,7.2292,,C 38 | 928,3,"Roth, Miss. Sarah A",female,,0,0,342712,8.05,,S 39 | 929,3,"Cacic, Miss. Manda",female,21,0,0,315087,8.6625,,S 40 | 930,3,"Sap, Mr. Julius",male,25,0,0,345768,9.5,,S 41 | 931,3,"Hee, Mr. Ling",male,,0,0,1601,56.4958,,S 42 | 932,3,"Karun, Mr. Franz",male,39,0,1,349256,13.4167,,C 43 | 933,1,"Franklin, Mr. Thomas Parham",male,,0,0,113778,26.55,D34,S 44 | 934,3,"Goldsmith, Mr. Nathan",male,41,0,0,SOTON/O.Q. 3101263,7.85,,S 45 | 935,2,"Corbett, Mrs. Walter H (Irene Colvin)",female,30,0,0,237249,13,,S 46 | 936,1,"Kimball, Mrs. Edwin Nelson Jr (Gertrude Parsons)",female,45,1,0,11753,52.5542,D19,S 47 | 937,3,"Peltomaki, Mr. Nikolai Johannes",male,25,0,0,STON/O 2. 3101291,7.925,,S 48 | 938,1,"Chevre, Mr. Paul Romaine",male,45,0,0,PC 17594,29.7,A9,C 49 | 939,3,"Shaughnessy, Mr. Patrick",male,,0,0,370374,7.75,,Q 50 | 940,1,"Bucknell, Mrs. William Robert (Emma Eliza Ward)",female,60,0,0,11813,76.2917,D15,C 51 | 941,3,"Coutts, Mrs. William (Winnie Minnie"" Treanor)""",female,36,0,2,C.A. 37671,15.9,,S 52 | 942,1,"Smith, Mr. Lucien Philip",male,24,1,0,13695,60,C31,S 53 | 943,2,"Pulbaum, Mr. Franz",male,27,0,0,SC/PARIS 2168,15.0333,,C 54 | 944,2,"Hocking, Miss. Ellen Nellie""""",female,20,2,1,29105,23,,S 55 | 945,1,"Fortune, Miss. Ethel Flora",female,28,3,2,19950,263,C23 C25 C27,S 56 | 946,2,"Mangiavacchi, Mr. Serafino Emilio",male,,0,0,SC/A.3 2861,15.5792,,C 57 | 947,3,"Rice, Master. Albert",male,10,4,1,382652,29.125,,Q 58 | 948,3,"Cor, Mr. Bartol",male,35,0,0,349230,7.8958,,S 59 | 949,3,"Abelseth, Mr. Olaus Jorgensen",male,25,0,0,348122,7.65,F G63,S 60 | 950,3,"Davison, Mr. Thomas Henry",male,,1,0,386525,16.1,,S 61 | 951,1,"Chaudanson, Miss. Victorine",female,36,0,0,PC 17608,262.375,B61,C 62 | 952,3,"Dika, Mr. Mirko",male,17,0,0,349232,7.8958,,S 63 | 953,2,"McCrae, Mr. Arthur Gordon",male,32,0,0,237216,13.5,,S 64 | 954,3,"Bjorklund, Mr. Ernst Herbert",male,18,0,0,347090,7.75,,S 65 | 955,3,"Bradley, Miss. Bridget Delia",female,22,0,0,334914,7.725,,Q 66 | 956,1,"Ryerson, Master. John Borie",male,13,2,2,PC 17608,262.375,B57 B59 B63 B66,C 67 | 957,2,"Corey, Mrs. Percy C (Mary Phyllis Elizabeth Miller)",female,,0,0,F.C.C. 13534,21,,S 68 | 958,3,"Burns, Miss. Mary Delia",female,18,0,0,330963,7.8792,,Q 69 | 959,1,"Moore, Mr. Clarence Bloomfield",male,47,0,0,113796,42.4,,S 70 | 960,1,"Tucker, Mr. Gilbert Milligan Jr",male,31,0,0,2543,28.5375,C53,C 71 | 961,1,"Fortune, Mrs. Mark (Mary McDougald)",female,60,1,4,19950,263,C23 C25 C27,S 72 | 962,3,"Mulvihill, Miss. Bertha E",female,24,0,0,382653,7.75,,Q 73 | 963,3,"Minkoff, Mr. Lazar",male,21,0,0,349211,7.8958,,S 74 | 964,3,"Nieminen, Miss. Manta Josefina",female,29,0,0,3101297,7.925,,S 75 | 965,1,"Ovies y Rodriguez, Mr. Servando",male,28.5,0,0,PC 17562,27.7208,D43,C 76 | 966,1,"Geiger, Miss. Amalie",female,35,0,0,113503,211.5,C130,C 77 | 967,1,"Keeping, Mr. Edwin",male,32.5,0,0,113503,211.5,C132,C 78 | 968,3,"Miles, Mr. Frank",male,,0,0,359306,8.05,,S 79 | 969,1,"Cornell, Mrs. Robert Clifford (Malvina Helen Lamson)",female,55,2,0,11770,25.7,C101,S 80 | 970,2,"Aldworth, Mr. Charles Augustus",male,30,0,0,248744,13,,S 81 | 971,3,"Doyle, Miss. Elizabeth",female,24,0,0,368702,7.75,,Q 82 | 972,3,"Boulos, Master. Akar",male,6,1,1,2678,15.2458,,C 83 | 973,1,"Straus, Mr. Isidor",male,67,1,0,PC 17483,221.7792,C55 C57,S 84 | 974,1,"Case, Mr. Howard Brown",male,49,0,0,19924,26,,S 85 | 975,3,"Demetri, Mr. Marinko",male,,0,0,349238,7.8958,,S 86 | 976,2,"Lamb, Mr. John Joseph",male,,0,0,240261,10.7083,,Q 87 | 977,3,"Khalil, Mr. Betros",male,,1,0,2660,14.4542,,C 88 | 978,3,"Barry, Miss. Julia",female,27,0,0,330844,7.8792,,Q 89 | 979,3,"Badman, Miss. Emily Louisa",female,18,0,0,A/4 31416,8.05,,S 90 | 980,3,"O'Donoghue, Ms. Bridget",female,,0,0,364856,7.75,,Q 91 | 981,2,"Wells, Master. Ralph Lester",male,2,1,1,29103,23,,S 92 | 982,3,"Dyker, Mrs. Adolf Fredrik (Anna Elisabeth Judith Andersson)",female,22,1,0,347072,13.9,,S 93 | 983,3,"Pedersen, Mr. Olaf",male,,0,0,345498,7.775,,S 94 | 984,1,"Davidson, Mrs. Thornton (Orian Hays)",female,27,1,2,F.C. 12750,52,B71,S 95 | 985,3,"Guest, Mr. Robert",male,,0,0,376563,8.05,,S 96 | 986,1,"Birnbaum, Mr. Jakob",male,25,0,0,13905,26,,C 97 | 987,3,"Tenglin, Mr. Gunnar Isidor",male,25,0,0,350033,7.7958,,S 98 | 988,1,"Cavendish, Mrs. Tyrell William (Julia Florence Siegel)",female,76,1,0,19877,78.85,C46,S 99 | 989,3,"Makinen, Mr. Kalle Edvard",male,29,0,0,STON/O 2. 3101268,7.925,,S 100 | 990,3,"Braf, Miss. Elin Ester Maria",female,20,0,0,347471,7.8542,,S 101 | 991,3,"Nancarrow, Mr. William Henry",male,33,0,0,A./5. 3338,8.05,,S 102 | 992,1,"Stengel, Mrs. Charles Emil Henry (Annie May Morris)",female,43,1,0,11778,55.4417,C116,C 103 | 993,2,"Weisz, Mr. Leopold",male,27,1,0,228414,26,,S 104 | 994,3,"Foley, Mr. William",male,,0,0,365235,7.75,,Q 105 | 995,3,"Johansson Palmquist, Mr. Oskar Leander",male,26,0,0,347070,7.775,,S 106 | 996,3,"Thomas, Mrs. Alexander (Thamine Thelma"")""",female,16,1,1,2625,8.5167,,C 107 | 997,3,"Holthen, Mr. Johan Martin",male,28,0,0,C 4001,22.525,,S 108 | 998,3,"Buckley, Mr. Daniel",male,21,0,0,330920,7.8208,,Q 109 | 999,3,"Ryan, Mr. Edward",male,,0,0,383162,7.75,,Q 110 | 1000,3,"Willer, Mr. Aaron (Abi Weller"")""",male,,0,0,3410,8.7125,,S 111 | 1001,2,"Swane, Mr. George",male,18.5,0,0,248734,13,F,S 112 | 1002,2,"Stanton, Mr. Samuel Ward",male,41,0,0,237734,15.0458,,C 113 | 1003,3,"Shine, Miss. Ellen Natalia",female,,0,0,330968,7.7792,,Q 114 | 1004,1,"Evans, Miss. Edith Corse",female,36,0,0,PC 17531,31.6792,A29,C 115 | 1005,3,"Buckley, Miss. Katherine",female,18.5,0,0,329944,7.2833,,Q 116 | 1006,1,"Straus, Mrs. Isidor (Rosalie Ida Blun)",female,63,1,0,PC 17483,221.7792,C55 C57,S 117 | 1007,3,"Chronopoulos, Mr. Demetrios",male,18,1,0,2680,14.4542,,C 118 | 1008,3,"Thomas, Mr. John",male,,0,0,2681,6.4375,,C 119 | 1009,3,"Sandstrom, Miss. Beatrice Irene",female,1,1,1,PP 9549,16.7,G6,S 120 | 1010,1,"Beattie, Mr. Thomson",male,36,0,0,13050,75.2417,C6,C 121 | 1011,2,"Chapman, Mrs. John Henry (Sara Elizabeth Lawry)",female,29,1,0,SC/AH 29037,26,,S 122 | 1012,2,"Watt, Miss. Bertha J",female,12,0,0,C.A. 33595,15.75,,S 123 | 1013,3,"Kiernan, Mr. John",male,,1,0,367227,7.75,,Q 124 | 1014,1,"Schabert, Mrs. Paul (Emma Mock)",female,35,1,0,13236,57.75,C28,C 125 | 1015,3,"Carver, Mr. Alfred John",male,28,0,0,392095,7.25,,S 126 | 1016,3,"Kennedy, Mr. John",male,,0,0,368783,7.75,,Q 127 | 1017,3,"Cribb, Miss. Laura Alice",female,17,0,1,371362,16.1,,S 128 | 1018,3,"Brobeck, Mr. Karl Rudolf",male,22,0,0,350045,7.7958,,S 129 | 1019,3,"McCoy, Miss. Alicia",female,,2,0,367226,23.25,,Q 130 | 1020,2,"Bowenur, Mr. Solomon",male,42,0,0,211535,13,,S 131 | 1021,3,"Petersen, Mr. Marius",male,24,0,0,342441,8.05,,S 132 | 1022,3,"Spinner, Mr. Henry John",male,32,0,0,STON/OQ. 369943,8.05,,S 133 | 1023,1,"Gracie, Col. Archibald IV",male,53,0,0,113780,28.5,C51,C 134 | 1024,3,"Lefebre, Mrs. Frank (Frances)",female,,0,4,4133,25.4667,,S 135 | 1025,3,"Thomas, Mr. Charles P",male,,1,0,2621,6.4375,,C 136 | 1026,3,"Dintcheff, Mr. Valtcho",male,43,0,0,349226,7.8958,,S 137 | 1027,3,"Carlsson, Mr. Carl Robert",male,24,0,0,350409,7.8542,,S 138 | 1028,3,"Zakarian, Mr. Mapriededer",male,26.5,0,0,2656,7.225,,C 139 | 1029,2,"Schmidt, Mr. August",male,26,0,0,248659,13,,S 140 | 1030,3,"Drapkin, Miss. Jennie",female,23,0,0,SOTON/OQ 392083,8.05,,S 141 | 1031,3,"Goodwin, Mr. Charles Frederick",male,40,1,6,CA 2144,46.9,,S 142 | 1032,3,"Goodwin, Miss. Jessie Allis",female,10,5,2,CA 2144,46.9,,S 143 | 1033,1,"Daniels, Miss. Sarah",female,33,0,0,113781,151.55,,S 144 | 1034,1,"Ryerson, Mr. Arthur Larned",male,61,1,3,PC 17608,262.375,B57 B59 B63 B66,C 145 | 1035,2,"Beauchamp, Mr. Henry James",male,28,0,0,244358,26,,S 146 | 1036,1,"Lindeberg-Lind, Mr. Erik Gustaf (Mr Edward Lingrey"")""",male,42,0,0,17475,26.55,,S 147 | 1037,3,"Vander Planke, Mr. Julius",male,31,3,0,345763,18,,S 148 | 1038,1,"Hilliard, Mr. Herbert Henry",male,,0,0,17463,51.8625,E46,S 149 | 1039,3,"Davies, Mr. Evan",male,22,0,0,SC/A4 23568,8.05,,S 150 | 1040,1,"Crafton, Mr. John Bertram",male,,0,0,113791,26.55,,S 151 | 1041,2,"Lahtinen, Rev. William",male,30,1,1,250651,26,,S 152 | 1042,1,"Earnshaw, Mrs. Boulton (Olive Potter)",female,23,0,1,11767,83.1583,C54,C 153 | 1043,3,"Matinoff, Mr. Nicola",male,,0,0,349255,7.8958,,C 154 | 1044,3,"Storey, Mr. Thomas",male,60.5,0,0,3701,,,S 155 | 1045,3,"Klasen, Mrs. (Hulda Kristina Eugenia Lofqvist)",female,36,0,2,350405,12.1833,,S 156 | 1046,3,"Asplund, Master. Filip Oscar",male,13,4,2,347077,31.3875,,S 157 | 1047,3,"Duquemin, Mr. Joseph",male,24,0,0,S.O./P.P. 752,7.55,,S 158 | 1048,1,"Bird, Miss. Ellen",female,29,0,0,PC 17483,221.7792,C97,S 159 | 1049,3,"Lundin, Miss. Olga Elida",female,23,0,0,347469,7.8542,,S 160 | 1050,1,"Borebank, Mr. John James",male,42,0,0,110489,26.55,D22,S 161 | 1051,3,"Peacock, Mrs. Benjamin (Edith Nile)",female,26,0,2,SOTON/O.Q. 3101315,13.775,,S 162 | 1052,3,"Smyth, Miss. Julia",female,,0,0,335432,7.7333,,Q 163 | 1053,3,"Touma, Master. Georges Youssef",male,7,1,1,2650,15.2458,,C 164 | 1054,2,"Wright, Miss. Marion",female,26,0,0,220844,13.5,,S 165 | 1055,3,"Pearce, Mr. Ernest",male,,0,0,343271,7,,S 166 | 1056,2,"Peruschitz, Rev. Joseph Maria",male,41,0,0,237393,13,,S 167 | 1057,3,"Kink-Heilmann, Mrs. Anton (Luise Heilmann)",female,26,1,1,315153,22.025,,S 168 | 1058,1,"Brandeis, Mr. Emil",male,48,0,0,PC 17591,50.4958,B10,C 169 | 1059,3,"Ford, Mr. Edward Watson",male,18,2,2,W./C. 6608,34.375,,S 170 | 1060,1,"Cassebeer, Mrs. Henry Arthur Jr (Eleanor Genevieve Fosdick)",female,,0,0,17770,27.7208,,C 171 | 1061,3,"Hellstrom, Miss. Hilda Maria",female,22,0,0,7548,8.9625,,S 172 | 1062,3,"Lithman, Mr. Simon",male,,0,0,S.O./P.P. 251,7.55,,S 173 | 1063,3,"Zakarian, Mr. Ortin",male,27,0,0,2670,7.225,,C 174 | 1064,3,"Dyker, Mr. Adolf Fredrik",male,23,1,0,347072,13.9,,S 175 | 1065,3,"Torfa, Mr. Assad",male,,0,0,2673,7.2292,,C 176 | 1066,3,"Asplund, Mr. Carl Oscar Vilhelm Gustafsson",male,40,1,5,347077,31.3875,,S 177 | 1067,2,"Brown, Miss. Edith Eileen",female,15,0,2,29750,39,,S 178 | 1068,2,"Sincock, Miss. Maude",female,20,0,0,C.A. 33112,36.75,,S 179 | 1069,1,"Stengel, Mr. Charles Emil Henry",male,54,1,0,11778,55.4417,C116,C 180 | 1070,2,"Becker, Mrs. Allen Oliver (Nellie E Baumgardner)",female,36,0,3,230136,39,F4,S 181 | 1071,1,"Compton, Mrs. Alexander Taylor (Mary Eliza Ingersoll)",female,64,0,2,PC 17756,83.1583,E45,C 182 | 1072,2,"McCrie, Mr. James Matthew",male,30,0,0,233478,13,,S 183 | 1073,1,"Compton, Mr. Alexander Taylor Jr",male,37,1,1,PC 17756,83.1583,E52,C 184 | 1074,1,"Marvin, Mrs. Daniel Warner (Mary Graham Carmichael Farquarson)",female,18,1,0,113773,53.1,D30,S 185 | 1075,3,"Lane, Mr. Patrick",male,,0,0,7935,7.75,,Q 186 | 1076,1,"Douglas, Mrs. Frederick Charles (Mary Helene Baxter)",female,27,1,1,PC 17558,247.5208,B58 B60,C 187 | 1077,2,"Maybery, Mr. Frank Hubert",male,40,0,0,239059,16,,S 188 | 1078,2,"Phillips, Miss. Alice Frances Louisa",female,21,0,1,S.O./P.P. 2,21,,S 189 | 1079,3,"Davies, Mr. Joseph",male,17,2,0,A/4 48873,8.05,,S 190 | 1080,3,"Sage, Miss. Ada",female,,8,2,CA. 2343,69.55,,S 191 | 1081,2,"Veal, Mr. James",male,40,0,0,28221,13,,S 192 | 1082,2,"Angle, Mr. William A",male,34,1,0,226875,26,,S 193 | 1083,1,"Salomon, Mr. Abraham L",male,,0,0,111163,26,,S 194 | 1084,3,"van Billiard, Master. Walter John",male,11.5,1,1,A/5. 851,14.5,,S 195 | 1085,2,"Lingane, Mr. John",male,61,0,0,235509,12.35,,Q 196 | 1086,2,"Drew, Master. Marshall Brines",male,8,0,2,28220,32.5,,S 197 | 1087,3,"Karlsson, Mr. Julius Konrad Eugen",male,33,0,0,347465,7.8542,,S 198 | 1088,1,"Spedden, Master. Robert Douglas",male,6,0,2,16966,134.5,E34,C 199 | 1089,3,"Nilsson, Miss. Berta Olivia",female,18,0,0,347066,7.775,,S 200 | 1090,2,"Baimbrigge, Mr. Charles Robert",male,23,0,0,C.A. 31030,10.5,,S 201 | 1091,3,"Rasmussen, Mrs. (Lena Jacobsen Solvang)",female,,0,0,65305,8.1125,,S 202 | 1092,3,"Murphy, Miss. Nora",female,,0,0,36568,15.5,,Q 203 | 1093,3,"Danbom, Master. Gilbert Sigvard Emanuel",male,0.33,0,2,347080,14.4,,S 204 | 1094,1,"Astor, Col. John Jacob",male,47,1,0,PC 17757,227.525,C62 C64,C 205 | 1095,2,"Quick, Miss. Winifred Vera",female,8,1,1,26360,26,,S 206 | 1096,2,"Andrew, Mr. Frank Thomas",male,25,0,0,C.A. 34050,10.5,,S 207 | 1097,1,"Omont, Mr. Alfred Fernand",male,,0,0,F.C. 12998,25.7417,,C 208 | 1098,3,"McGowan, Miss. Katherine",female,35,0,0,9232,7.75,,Q 209 | 1099,2,"Collett, Mr. Sidney C Stuart",male,24,0,0,28034,10.5,,S 210 | 1100,1,"Rosenbaum, Miss. Edith Louise",female,33,0,0,PC 17613,27.7208,A11,C 211 | 1101,3,"Delalic, Mr. Redjo",male,25,0,0,349250,7.8958,,S 212 | 1102,3,"Andersen, Mr. Albert Karvin",male,32,0,0,C 4001,22.525,,S 213 | 1103,3,"Finoli, Mr. Luigi",male,,0,0,SOTON/O.Q. 3101308,7.05,,S 214 | 1104,2,"Deacon, Mr. Percy William",male,17,0,0,S.O.C. 14879,73.5,,S 215 | 1105,2,"Howard, Mrs. Benjamin (Ellen Truelove Arman)",female,60,1,0,24065,26,,S 216 | 1106,3,"Andersson, Miss. Ida Augusta Margareta",female,38,4,2,347091,7.775,,S 217 | 1107,1,"Head, Mr. Christopher",male,42,0,0,113038,42.5,B11,S 218 | 1108,3,"Mahon, Miss. Bridget Delia",female,,0,0,330924,7.8792,,Q 219 | 1109,1,"Wick, Mr. George Dennick",male,57,1,1,36928,164.8667,,S 220 | 1110,1,"Widener, Mrs. George Dunton (Eleanor Elkins)",female,50,1,1,113503,211.5,C80,C 221 | 1111,3,"Thomson, Mr. Alexander Morrison",male,,0,0,32302,8.05,,S 222 | 1112,2,"Duran y More, Miss. Florentina",female,30,1,0,SC/PARIS 2148,13.8583,,C 223 | 1113,3,"Reynolds, Mr. Harold J",male,21,0,0,342684,8.05,,S 224 | 1114,2,"Cook, Mrs. (Selena Rogers)",female,22,0,0,W./C. 14266,10.5,F33,S 225 | 1115,3,"Karlsson, Mr. Einar Gervasius",male,21,0,0,350053,7.7958,,S 226 | 1116,1,"Candee, Mrs. Edward (Helen Churchill Hungerford)",female,53,0,0,PC 17606,27.4458,,C 227 | 1117,3,"Moubarek, Mrs. George (Omine Amenia"" Alexander)""",female,,0,2,2661,15.2458,,C 228 | 1118,3,"Asplund, Mr. Johan Charles",male,23,0,0,350054,7.7958,,S 229 | 1119,3,"McNeill, Miss. Bridget",female,,0,0,370368,7.75,,Q 230 | 1120,3,"Everett, Mr. Thomas James",male,40.5,0,0,C.A. 6212,15.1,,S 231 | 1121,2,"Hocking, Mr. Samuel James Metcalfe",male,36,0,0,242963,13,,S 232 | 1122,2,"Sweet, Mr. George Frederick",male,14,0,0,220845,65,,S 233 | 1123,1,"Willard, Miss. Constance",female,21,0,0,113795,26.55,,S 234 | 1124,3,"Wiklund, Mr. Karl Johan",male,21,1,0,3101266,6.4958,,S 235 | 1125,3,"Linehan, Mr. Michael",male,,0,0,330971,7.8792,,Q 236 | 1126,1,"Cumings, Mr. John Bradley",male,39,1,0,PC 17599,71.2833,C85,C 237 | 1127,3,"Vendel, Mr. Olof Edvin",male,20,0,0,350416,7.8542,,S 238 | 1128,1,"Warren, Mr. Frank Manley",male,64,1,0,110813,75.25,D37,C 239 | 1129,3,"Baccos, Mr. Raffull",male,20,0,0,2679,7.225,,C 240 | 1130,2,"Hiltunen, Miss. Marta",female,18,1,1,250650,13,,S 241 | 1131,1,"Douglas, Mrs. Walter Donald (Mahala Dutton)",female,48,1,0,PC 17761,106.425,C86,C 242 | 1132,1,"Lindstrom, Mrs. Carl Johan (Sigrid Posse)",female,55,0,0,112377,27.7208,,C 243 | 1133,2,"Christy, Mrs. (Alice Frances)",female,45,0,2,237789,30,,S 244 | 1134,1,"Spedden, Mr. Frederic Oakley",male,45,1,1,16966,134.5,E34,C 245 | 1135,3,"Hyman, Mr. Abraham",male,,0,0,3470,7.8875,,S 246 | 1136,3,"Johnston, Master. William Arthur Willie""""",male,,1,2,W./C. 6607,23.45,,S 247 | 1137,1,"Kenyon, Mr. Frederick R",male,41,1,0,17464,51.8625,D21,S 248 | 1138,2,"Karnes, Mrs. J Frank (Claire Bennett)",female,22,0,0,F.C.C. 13534,21,,S 249 | 1139,2,"Drew, Mr. James Vivian",male,42,1,1,28220,32.5,,S 250 | 1140,2,"Hold, Mrs. Stephen (Annie Margaret Hill)",female,29,1,0,26707,26,,S 251 | 1141,3,"Khalil, Mrs. Betros (Zahie Maria"" Elias)""",female,,1,0,2660,14.4542,,C 252 | 1142,2,"West, Miss. Barbara J",female,0.92,1,2,C.A. 34651,27.75,,S 253 | 1143,3,"Abrahamsson, Mr. Abraham August Johannes",male,20,0,0,SOTON/O2 3101284,7.925,,S 254 | 1144,1,"Clark, Mr. Walter Miller",male,27,1,0,13508,136.7792,C89,C 255 | 1145,3,"Salander, Mr. Karl Johan",male,24,0,0,7266,9.325,,S 256 | 1146,3,"Wenzel, Mr. Linhart",male,32.5,0,0,345775,9.5,,S 257 | 1147,3,"MacKay, Mr. George William",male,,0,0,C.A. 42795,7.55,,S 258 | 1148,3,"Mahon, Mr. John",male,,0,0,AQ/4 3130,7.75,,Q 259 | 1149,3,"Niklasson, Mr. Samuel",male,28,0,0,363611,8.05,,S 260 | 1150,2,"Bentham, Miss. Lilian W",female,19,0,0,28404,13,,S 261 | 1151,3,"Midtsjo, Mr. Karl Albert",male,21,0,0,345501,7.775,,S 262 | 1152,3,"de Messemaeker, Mr. Guillaume Joseph",male,36.5,1,0,345572,17.4,,S 263 | 1153,3,"Nilsson, Mr. August Ferdinand",male,21,0,0,350410,7.8542,,S 264 | 1154,2,"Wells, Mrs. Arthur Henry (Addie"" Dart Trevaskis)""",female,29,0,2,29103,23,,S 265 | 1155,3,"Klasen, Miss. Gertrud Emilia",female,1,1,1,350405,12.1833,,S 266 | 1156,2,"Portaluppi, Mr. Emilio Ilario Giuseppe",male,30,0,0,C.A. 34644,12.7375,,C 267 | 1157,3,"Lyntakoff, Mr. Stanko",male,,0,0,349235,7.8958,,S 268 | 1158,1,"Chisholm, Mr. Roderick Robert Crispin",male,,0,0,112051,0,,S 269 | 1159,3,"Warren, Mr. Charles William",male,,0,0,C.A. 49867,7.55,,S 270 | 1160,3,"Howard, Miss. May Elizabeth",female,,0,0,A. 2. 39186,8.05,,S 271 | 1161,3,"Pokrnic, Mr. Mate",male,17,0,0,315095,8.6625,,S 272 | 1162,1,"McCaffry, Mr. Thomas Francis",male,46,0,0,13050,75.2417,C6,C 273 | 1163,3,"Fox, Mr. Patrick",male,,0,0,368573,7.75,,Q 274 | 1164,1,"Clark, Mrs. Walter Miller (Virginia McDowell)",female,26,1,0,13508,136.7792,C89,C 275 | 1165,3,"Lennon, Miss. Mary",female,,1,0,370371,15.5,,Q 276 | 1166,3,"Saade, Mr. Jean Nassr",male,,0,0,2676,7.225,,C 277 | 1167,2,"Bryhl, Miss. Dagmar Jenny Ingeborg ",female,20,1,0,236853,26,,S 278 | 1168,2,"Parker, Mr. Clifford Richard",male,28,0,0,SC 14888,10.5,,S 279 | 1169,2,"Faunthorpe, Mr. Harry",male,40,1,0,2926,26,,S 280 | 1170,2,"Ware, Mr. John James",male,30,1,0,CA 31352,21,,S 281 | 1171,2,"Oxenham, Mr. Percy Thomas",male,22,0,0,W./C. 14260,10.5,,S 282 | 1172,3,"Oreskovic, Miss. Jelka",female,23,0,0,315085,8.6625,,S 283 | 1173,3,"Peacock, Master. Alfred Edward",male,0.75,1,1,SOTON/O.Q. 3101315,13.775,,S 284 | 1174,3,"Fleming, Miss. Honora",female,,0,0,364859,7.75,,Q 285 | 1175,3,"Touma, Miss. Maria Youssef",female,9,1,1,2650,15.2458,,C 286 | 1176,3,"Rosblom, Miss. Salli Helena",female,2,1,1,370129,20.2125,,S 287 | 1177,3,"Dennis, Mr. William",male,36,0,0,A/5 21175,7.25,,S 288 | 1178,3,"Franklin, Mr. Charles (Charles Fardon)",male,,0,0,SOTON/O.Q. 3101314,7.25,,S 289 | 1179,1,"Snyder, Mr. John Pillsbury",male,24,1,0,21228,82.2667,B45,S 290 | 1180,3,"Mardirosian, Mr. Sarkis",male,,0,0,2655,7.2292,F E46,C 291 | 1181,3,"Ford, Mr. Arthur",male,,0,0,A/5 1478,8.05,,S 292 | 1182,1,"Rheims, Mr. George Alexander Lucien",male,,0,0,PC 17607,39.6,,S 293 | 1183,3,"Daly, Miss. Margaret Marcella Maggie""""",female,30,0,0,382650,6.95,,Q 294 | 1184,3,"Nasr, Mr. Mustafa",male,,0,0,2652,7.2292,,C 295 | 1185,1,"Dodge, Dr. Washington",male,53,1,1,33638,81.8583,A34,S 296 | 1186,3,"Wittevrongel, Mr. Camille",male,36,0,0,345771,9.5,,S 297 | 1187,3,"Angheloff, Mr. Minko",male,26,0,0,349202,7.8958,,S 298 | 1188,2,"Laroche, Miss. Louise",female,1,1,2,SC/Paris 2123,41.5792,,C 299 | 1189,3,"Samaan, Mr. Hanna",male,,2,0,2662,21.6792,,C 300 | 1190,1,"Loring, Mr. Joseph Holland",male,30,0,0,113801,45.5,,S 301 | 1191,3,"Johansson, Mr. Nils",male,29,0,0,347467,7.8542,,S 302 | 1192,3,"Olsson, Mr. Oscar Wilhelm",male,32,0,0,347079,7.775,,S 303 | 1193,2,"Malachard, Mr. Noel",male,,0,0,237735,15.0458,D,C 304 | 1194,2,"Phillips, Mr. Escott Robert",male,43,0,1,S.O./P.P. 2,21,,S 305 | 1195,3,"Pokrnic, Mr. Tome",male,24,0,0,315092,8.6625,,S 306 | 1196,3,"McCarthy, Miss. Catherine Katie""""",female,,0,0,383123,7.75,,Q 307 | 1197,1,"Crosby, Mrs. Edward Gifford (Catherine Elizabeth Halstead)",female,64,1,1,112901,26.55,B26,S 308 | 1198,1,"Allison, Mr. Hudson Joshua Creighton",male,30,1,2,113781,151.55,C22 C26,S 309 | 1199,3,"Aks, Master. Philip Frank",male,0.83,0,1,392091,9.35,,S 310 | 1200,1,"Hays, Mr. Charles Melville",male,55,1,1,12749,93.5,B69,S 311 | 1201,3,"Hansen, Mrs. Claus Peter (Jennie L Howard)",female,45,1,0,350026,14.1083,,S 312 | 1202,3,"Cacic, Mr. Jego Grga",male,18,0,0,315091,8.6625,,S 313 | 1203,3,"Vartanian, Mr. David",male,22,0,0,2658,7.225,,C 314 | 1204,3,"Sadowitz, Mr. Harry",male,,0,0,LP 1588,7.575,,S 315 | 1205,3,"Carr, Miss. Jeannie",female,37,0,0,368364,7.75,,Q 316 | 1206,1,"White, Mrs. John Stuart (Ella Holmes)",female,55,0,0,PC 17760,135.6333,C32,C 317 | 1207,3,"Hagardon, Miss. Kate",female,17,0,0,AQ/3. 30631,7.7333,,Q 318 | 1208,1,"Spencer, Mr. William Augustus",male,57,1,0,PC 17569,146.5208,B78,C 319 | 1209,2,"Rogers, Mr. Reginald Harry",male,19,0,0,28004,10.5,,S 320 | 1210,3,"Jonsson, Mr. Nils Hilding",male,27,0,0,350408,7.8542,,S 321 | 1211,2,"Jefferys, Mr. Ernest Wilfred",male,22,2,0,C.A. 31029,31.5,,S 322 | 1212,3,"Andersson, Mr. Johan Samuel",male,26,0,0,347075,7.775,,S 323 | 1213,3,"Krekorian, Mr. Neshan",male,25,0,0,2654,7.2292,F E57,C 324 | 1214,2,"Nesson, Mr. Israel",male,26,0,0,244368,13,F2,S 325 | 1215,1,"Rowe, Mr. Alfred G",male,33,0,0,113790,26.55,,S 326 | 1216,1,"Kreuchen, Miss. Emilie",female,39,0,0,24160,211.3375,,S 327 | 1217,3,"Assam, Mr. Ali",male,23,0,0,SOTON/O.Q. 3101309,7.05,,S 328 | 1218,2,"Becker, Miss. Ruth Elizabeth",female,12,2,1,230136,39,F4,S 329 | 1219,1,"Rosenshine, Mr. George (Mr George Thorne"")""",male,46,0,0,PC 17585,79.2,,C 330 | 1220,2,"Clarke, Mr. Charles Valentine",male,29,1,0,2003,26,,S 331 | 1221,2,"Enander, Mr. Ingvar",male,21,0,0,236854,13,,S 332 | 1222,2,"Davies, Mrs. John Morgan (Elizabeth Agnes Mary White) ",female,48,0,2,C.A. 33112,36.75,,S 333 | 1223,1,"Dulles, Mr. William Crothers",male,39,0,0,PC 17580,29.7,A18,C 334 | 1224,3,"Thomas, Mr. Tannous",male,,0,0,2684,7.225,,C 335 | 1225,3,"Nakid, Mrs. Said (Waika Mary"" Mowad)""",female,19,1,1,2653,15.7417,,C 336 | 1226,3,"Cor, Mr. Ivan",male,27,0,0,349229,7.8958,,S 337 | 1227,1,"Maguire, Mr. John Edward",male,30,0,0,110469,26,C106,S 338 | 1228,2,"de Brito, Mr. Jose Joaquim",male,32,0,0,244360,13,,S 339 | 1229,3,"Elias, Mr. Joseph",male,39,0,2,2675,7.2292,,C 340 | 1230,2,"Denbury, Mr. Herbert",male,25,0,0,C.A. 31029,31.5,,S 341 | 1231,3,"Betros, Master. Seman",male,,0,0,2622,7.2292,,C 342 | 1232,2,"Fillbrook, Mr. Joseph Charles",male,18,0,0,C.A. 15185,10.5,,S 343 | 1233,3,"Lundstrom, Mr. Thure Edvin",male,32,0,0,350403,7.5792,,S 344 | 1234,3,"Sage, Mr. John George",male,,1,9,CA. 2343,69.55,,S 345 | 1235,1,"Cardeza, Mrs. James Warburton Martinez (Charlotte Wardle Drake)",female,58,0,1,PC 17755,512.3292,B51 B53 B55,C 346 | 1236,3,"van Billiard, Master. James William",male,,1,1,A/5. 851,14.5,,S 347 | 1237,3,"Abelseth, Miss. Karen Marie",female,16,0,0,348125,7.65,,S 348 | 1238,2,"Botsford, Mr. William Hull",male,26,0,0,237670,13,,S 349 | 1239,3,"Whabee, Mrs. George Joseph (Shawneene Abi-Saab)",female,38,0,0,2688,7.2292,,C 350 | 1240,2,"Giles, Mr. Ralph",male,24,0,0,248726,13.5,,S 351 | 1241,2,"Walcroft, Miss. Nellie",female,31,0,0,F.C.C. 13528,21,,S 352 | 1242,1,"Greenfield, Mrs. Leo David (Blanche Strouse)",female,45,0,1,PC 17759,63.3583,D10 D12,C 353 | 1243,2,"Stokes, Mr. Philip Joseph",male,25,0,0,F.C.C. 13540,10.5,,S 354 | 1244,2,"Dibden, Mr. William",male,18,0,0,S.O.C. 14879,73.5,,S 355 | 1245,2,"Herman, Mr. Samuel",male,49,1,2,220845,65,,S 356 | 1246,3,"Dean, Miss. Elizabeth Gladys Millvina""""",female,0.17,1,2,C.A. 2315,20.575,,S 357 | 1247,1,"Julian, Mr. Henry Forbes",male,50,0,0,113044,26,E60,S 358 | 1248,1,"Brown, Mrs. John Murray (Caroline Lane Lamson)",female,59,2,0,11769,51.4792,C101,S 359 | 1249,3,"Lockyer, Mr. Edward",male,,0,0,1222,7.8792,,S 360 | 1250,3,"O'Keefe, Mr. Patrick",male,,0,0,368402,7.75,,Q 361 | 1251,3,"Lindell, Mrs. Edvard Bengtsson (Elin Gerda Persson)",female,30,1,0,349910,15.55,,S 362 | 1252,3,"Sage, Master. William Henry",male,14.5,8,2,CA. 2343,69.55,,S 363 | 1253,2,"Mallet, Mrs. Albert (Antoinette Magnin)",female,24,1,1,S.C./PARIS 2079,37.0042,,C 364 | 1254,2,"Ware, Mrs. John James (Florence Louise Long)",female,31,0,0,CA 31352,21,,S 365 | 1255,3,"Strilic, Mr. Ivan",male,27,0,0,315083,8.6625,,S 366 | 1256,1,"Harder, Mrs. George Achilles (Dorothy Annan)",female,25,1,0,11765,55.4417,E50,C 367 | 1257,3,"Sage, Mrs. John (Annie Bullen)",female,,1,9,CA. 2343,69.55,,S 368 | 1258,3,"Caram, Mr. Joseph",male,,1,0,2689,14.4583,,C 369 | 1259,3,"Riihivouri, Miss. Susanna Juhantytar Sanni""""",female,22,0,0,3101295,39.6875,,S 370 | 1260,1,"Gibson, Mrs. Leonard (Pauline C Boeson)",female,45,0,1,112378,59.4,,C 371 | 1261,2,"Pallas y Castello, Mr. Emilio",male,29,0,0,SC/PARIS 2147,13.8583,,C 372 | 1262,2,"Giles, Mr. Edgar",male,21,1,0,28133,11.5,,S 373 | 1263,1,"Wilson, Miss. Helen Alice",female,31,0,0,16966,134.5,E39 E41,C 374 | 1264,1,"Ismay, Mr. Joseph Bruce",male,49,0,0,112058,0,B52 B54 B56,S 375 | 1265,2,"Harbeck, Mr. William H",male,44,0,0,248746,13,,S 376 | 1266,1,"Dodge, Mrs. Washington (Ruth Vidaver)",female,54,1,1,33638,81.8583,A34,S 377 | 1267,1,"Bowen, Miss. Grace Scott",female,45,0,0,PC 17608,262.375,,C 378 | 1268,3,"Kink, Miss. Maria",female,22,2,0,315152,8.6625,,S 379 | 1269,2,"Cotterill, Mr. Henry Harry""""",male,21,0,0,29107,11.5,,S 380 | 1270,1,"Hipkins, Mr. William Edward",male,55,0,0,680,50,C39,S 381 | 1271,3,"Asplund, Master. Carl Edgar",male,5,4,2,347077,31.3875,,S 382 | 1272,3,"O'Connor, Mr. Patrick",male,,0,0,366713,7.75,,Q 383 | 1273,3,"Foley, Mr. Joseph",male,26,0,0,330910,7.8792,,Q 384 | 1274,3,"Risien, Mrs. Samuel (Emma)",female,,0,0,364498,14.5,,S 385 | 1275,3,"McNamee, Mrs. Neal (Eileen O'Leary)",female,19,1,0,376566,16.1,,S 386 | 1276,2,"Wheeler, Mr. Edwin Frederick""""",male,,0,0,SC/PARIS 2159,12.875,,S 387 | 1277,2,"Herman, Miss. Kate",female,24,1,2,220845,65,,S 388 | 1278,3,"Aronsson, Mr. Ernst Axel Algot",male,24,0,0,349911,7.775,,S 389 | 1279,2,"Ashby, Mr. John",male,57,0,0,244346,13,,S 390 | 1280,3,"Canavan, Mr. Patrick",male,21,0,0,364858,7.75,,Q 391 | 1281,3,"Palsson, Master. Paul Folke",male,6,3,1,349909,21.075,,S 392 | 1282,1,"Payne, Mr. Vivian Ponsonby",male,23,0,0,12749,93.5,B24,S 393 | 1283,1,"Lines, Mrs. Ernest H (Elizabeth Lindsey James)",female,51,0,1,PC 17592,39.4,D28,S 394 | 1284,3,"Abbott, Master. Eugene Joseph",male,13,0,2,C.A. 2673,20.25,,S 395 | 1285,2,"Gilbert, Mr. William",male,47,0,0,C.A. 30769,10.5,,S 396 | 1286,3,"Kink-Heilmann, Mr. Anton",male,29,3,1,315153,22.025,,S 397 | 1287,1,"Smith, Mrs. Lucien Philip (Mary Eloise Hughes)",female,18,1,0,13695,60,C31,S 398 | 1288,3,"Colbert, Mr. Patrick",male,24,0,0,371109,7.25,,Q 399 | 1289,1,"Frolicher-Stehli, Mrs. Maxmillian (Margaretha Emerentia Stehli)",female,48,1,1,13567,79.2,B41,C 400 | 1290,3,"Larsson-Rondberg, Mr. Edvard A",male,22,0,0,347065,7.775,,S 401 | 1291,3,"Conlon, Mr. Thomas Henry",male,31,0,0,21332,7.7333,,Q 402 | 1292,1,"Bonnell, Miss. Caroline",female,30,0,0,36928,164.8667,C7,S 403 | 1293,2,"Gale, Mr. Harry",male,38,1,0,28664,21,,S 404 | 1294,1,"Gibson, Miss. Dorothy Winifred",female,22,0,1,112378,59.4,,C 405 | 1295,1,"Carrau, Mr. Jose Pedro",male,17,0,0,113059,47.1,,S 406 | 1296,1,"Frauenthal, Mr. Isaac Gerald",male,43,1,0,17765,27.7208,D40,C 407 | 1297,2,"Nourney, Mr. Alfred (Baron von Drachstedt"")""",male,20,0,0,SC/PARIS 2166,13.8625,D38,C 408 | 1298,2,"Ware, Mr. William Jeffery",male,23,1,0,28666,10.5,,S 409 | 1299,1,"Widener, Mr. George Dunton",male,50,1,1,113503,211.5,C80,C 410 | 1300,3,"Riordan, Miss. Johanna Hannah""""",female,,0,0,334915,7.7208,,Q 411 | 1301,3,"Peacock, Miss. Treasteall",female,3,1,1,SOTON/O.Q. 3101315,13.775,,S 412 | 1302,3,"Naughton, Miss. Hannah",female,,0,0,365237,7.75,,Q 413 | 1303,1,"Minahan, Mrs. William Edward (Lillian E Thorpe)",female,37,1,0,19928,90,C78,Q 414 | 1304,3,"Henriksson, Miss. Jenny Lovisa",female,28,0,0,347086,7.775,,S 415 | 1305,3,"Spector, Mr. Woolf",male,,0,0,A.5. 3236,8.05,,S 416 | 1306,1,"Oliva y Ocana, Dona. Fermina",female,39,0,0,PC 17758,108.9,C105,C 417 | 1307,3,"Saether, Mr. Simon Sivertsen",male,38.5,0,0,SOTON/O.Q. 3101262,7.25,,S 418 | 1308,3,"Ware, Mr. Frederick",male,,0,0,359309,8.05,,S 419 | 1309,3,"Peter, Master. Michael J",male,,1,1,2668,22.3583,,C 420 | -------------------------------------------------------------------------------- /ml/titanic/titanic.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import pandas as pd\n", 10 | "import numpy as np\n", 11 | "import matplotlib.pyplot as plt\n", 12 | "from sklearn.linear_model import LogisticRegression\n", 13 | "from sklearn.svm import SVC\n", 14 | "from sklearn.model_selection import cross_val_score" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 43, 20 | "metadata": {}, 21 | "outputs": [ 22 | { 23 | "ename": "KeyError", 24 | "evalue": "1043", 25 | "output_type": "error", 26 | "traceback": [ 27 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 28 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 29 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Fare'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mif\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misnan\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Fare'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Fare'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mat\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 15\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Fare'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misnull\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 30 | "\u001b[0;32m/anaconda3/envs/chumma/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 2268\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2269\u001b[0m \u001b[0mkey\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_convert_key\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2270\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtakeable\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_takeable\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2271\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2272\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__setitem__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 31 | "\u001b[0;32m/anaconda3/envs/chumma/lib/python3.6/site-packages/pandas/core/series.py\u001b[0m in \u001b[0;36m_get_value\u001b[0;34m(self, label, takeable)\u001b[0m\n\u001b[1;32m 1185\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mtakeable\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1186\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mcom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmaybe_box_datetimelike\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mlabel\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1187\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_values\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1188\u001b[0m \u001b[0m_get_value\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__doc__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_value\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__doc__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1189\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 32 | "\u001b[0;32m/anaconda3/envs/chumma/lib/python3.6/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_value\u001b[0;34m(self, series, key)\u001b[0m\n\u001b[1;32m 4373\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4374\u001b[0m return self._engine.get_value(s, k,\n\u001b[0;32m-> 4375\u001b[0;31m tz=getattr(series.dtype, 'tz', None))\n\u001b[0m\u001b[1;32m 4376\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4377\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mholds_integer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_boolean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 33 | "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_value\u001b[0;34m()\u001b[0m\n", 34 | "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_value\u001b[0;34m()\u001b[0m\n", 35 | "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n", 36 | "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine._get_loc_duplicates\u001b[0;34m()\u001b[0m\n", 37 | "\u001b[0;32mpandas/_libs/index_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.index.Int64Engine._maybe_get_bool_indexer\u001b[0;34m()\u001b[0m\n", 38 | "\u001b[0;31mKeyError\u001b[0m: 1043" 39 | ] 40 | } 41 | ], 42 | "source": [ 43 | "train = pd.read_csv('train.csv')\n", 44 | "test = pd.read_csv('test.csv')\n", 45 | "drop_cols = ['PassengerId', 'Ticket', 'Cabin']\n", 46 | "train.drop(drop_cols, axis=1, inplace=True)\n", 47 | "test.drop(drop_cols, axis=1, inplace=True)\n", 48 | "y = train['Survived']\n", 49 | "train.drop(['Survived'], axis=1, inplace=True)\n", 50 | "data = pd.concat([train, test])\n", 51 | "data['Age'].fillna(data['Age'].median(), inplace=True)\n", 52 | "data['Embarked'].fillna(data['Embarked'].mode(), inplace=True)\n", 53 | "data['Fare'].fillna(data['Fare'].mode(), inplace=True)\n", 54 | "for i in range(data['Fare'].size):\n", 55 | " if(np.isnan((data['Fare'].iloc[i]))):\n", 56 | " print(data['Fare'].at[i])\n", 57 | "print(data['Fare'].isnull().sum())" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "outputs": [], 65 | "source": [] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 10, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 11, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [] 80 | }, 81 | { 82 | "cell_type": "code", 83 | "execution_count": null, 84 | "metadata": {}, 85 | "outputs": [], 86 | "source": [] 87 | } 88 | ], 89 | "metadata": { 90 | "kernelspec": { 91 | "display_name": "Python 3", 92 | "language": "python", 93 | "name": "python3" 94 | }, 95 | "language_info": { 96 | "codemirror_mode": { 97 | "name": "ipython", 98 | "version": 3 99 | }, 100 | "file_extension": ".py", 101 | "mimetype": "text/x-python", 102 | "name": "python", 103 | "nbconvert_exporter": "python", 104 | "pygments_lexer": "ipython3", 105 | "version": "3.6.8" 106 | } 107 | }, 108 | "nbformat": 4, 109 | "nbformat_minor": 2 110 | } 111 | -------------------------------------------------------------------------------- /practice/bst/bst.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class Node: 5 | def __init__(self, val, left=None, right=None) -> None: 6 | self.val = val 7 | self.left = left 8 | self.right = right 9 | 10 | class Tree: 11 | def __init__(self, root_val) -> None: 12 | self.root = Node(root_val) 13 | self.height = 0 14 | 15 | def insert(self, cnode, val) -> Node: 16 | if cnode.left is None and cnode.right is None: 17 | self.height += 1 18 | if val < cnode.val: 19 | cnode.left = Node(val) 20 | else: 21 | cnode.right = Node(val) 22 | 23 | return cnode 24 | 25 | if val < cnode.val: 26 | if cnode.left is None: 27 | cnode.left = Node(val) 28 | return cnode 29 | else: 30 | return self.insert(cnode.left, val) 31 | else: 32 | if cnode.right is None: 33 | cnode.right = Node(val) 34 | return cnode 35 | else: 36 | return self.insert(cnode.right, val) 37 | 38 | def traverse(self, node: Node, kind='io') -> list: 39 | if node is None: 40 | return [] 41 | 42 | if kind is 'io': 43 | return self.traverse(node.left, kind) + [node.val] + self.traverse(node.right, kind) 44 | elif kind is 'pr': 45 | return [node.val] + self.traverse(node.left, kind) + self.traverse(node.right, kind) 46 | elif kind is 'po': 47 | return self.traverse(node.left, kind) + self.traverse(node.right, kind) + [node.val] 48 | 49 | def from_list(l: list) -> Tree: 50 | bst = Tree(l[0]) 51 | 52 | for x in l[1:]: 53 | bst.insert(bst.root, x) 54 | 55 | return bst 56 | 57 | 58 | def traversal_test(x: list): 59 | bst = Tree(x[0]) 60 | 61 | for val in x[1:]: 62 | bst.insert(bst.root, val) 63 | 64 | print(bst.traverse(bst.root, kind='io')) 65 | print(bst.traverse(bst.root, kind='pr')) 66 | print(bst.traverse(bst.root, kind='po')) 67 | 68 | if __name__ == '__main__': 69 | x = [10, 33, 1, 5, 65, 3, 25, 167, 34, 6 , 15, 28] 70 | traversal_test(x) 71 | 72 | -------------------------------------------------------------------------------- /practice/bst/bst_iterator.py: -------------------------------------------------------------------------------- 1 | class BSTIterator: 2 | # @param root, a binary search tree's root node 3 | def __init__(self, root): 4 | self.root = root 5 | self.stack = [] 6 | 7 | cnode = root 8 | while cnode is not None: 9 | self.stack.append(cnode) 10 | cnode = cnode.left 11 | 12 | # @return a boolean, whether we have a next smallest number 13 | def hasNext(self): 14 | return len(self.stack) > 0 15 | 16 | # @return an integer, the next smallest number 17 | def next(self): 18 | cnode = self.stack[-1] 19 | del self.stack[-1] 20 | 21 | if cnode.right is not None: 22 | tnode = cnode.right 23 | self.stack.append(tnode) 24 | while tnode.left is not None: 25 | self.stack.append(tnode.left) 26 | tnode = tnode.left 27 | 28 | return cnode.val 29 | 30 | 31 | def test(): 32 | from bst import Tree 33 | 34 | x = [10, 33, 1, 5, 65, 3, 25, 167, 34, 6 , 15, 28] 35 | bst = Tree(x[0]) 36 | for val in x[1:]: 37 | bst.insert(bst.root, val) 38 | 39 | i = BSTIterator(bst.root) 40 | while i.hasNext(): 41 | print(i.next()) 42 | 43 | if __name__ == '__main__': 44 | test() -------------------------------------------------------------------------------- /practice/bst/t2sum_bst.py: -------------------------------------------------------------------------------- 1 | class bst_iterator: 2 | def __init__(self, root, reverse=False): 3 | self.stack = [] 4 | self.reverse = reverse 5 | 6 | cnode = root 7 | while cnode is not None: 8 | self.stack.append(cnode) 9 | cnode = self.get_next_node(cnode) 10 | 11 | def get_next_node(self, cnode): 12 | if self.reverse: 13 | return cnode.right 14 | else: 15 | return cnode.left 16 | 17 | def get_other_node(self, cnode): 18 | if not self.reverse: 19 | return cnode.right 20 | else: 21 | return cnode.left 22 | 23 | def has_next(self): 24 | return len(self.stack) > 0 25 | 26 | def next(self): 27 | cnode = self.stack[-1] 28 | del self.stack[-1] 29 | 30 | if self.get_other_node(cnode) is not None: 31 | tnode = self.get_other_node(cnode) 32 | while tnode is not None: 33 | self.stack.append(tnode) 34 | tnode = self.get_next_node(tnode) 35 | 36 | return cnode.val 37 | 38 | class Solution: 39 | # @param A : root node of tree 40 | # @param B : integer 41 | # @return an integer 42 | def t2Sum(self, A, B): 43 | if A is None: 44 | return 0 45 | 46 | fw_it, rv_it = bst_iterator(A), bst_iterator(A, reverse=True) 47 | 48 | l, r = fw_it.next(), rv_it.next() 49 | while fw_it.has_next() and rv_it.has_next() and (l < r): 50 | if (l + r) == B: 51 | return 1 52 | 53 | elif (l + r) < B: 54 | r = rv_it.next() 55 | else: 56 | l = fw_it.next() 57 | 58 | return 0 59 | 60 | 61 | if __name__ == '__main__': 62 | from bst import from_list 63 | x = [7, 10, 9, 20] 64 | bst = from_list(x) 65 | 66 | sol = Solution() 67 | sol.t2Sum(bst.root, 19) -------------------------------------------------------------------------------- /quad/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Thalaivar/python/6702edc229e9938846f9ba757b759d0bb183aa1e/quad/.DS_Store -------------------------------------------------------------------------------- /quad/ARMING.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifndef ARMING_H 4 | #define ARMING_H 5 | 6 | #include "PERIPHERALS.h" 7 | #include "PPM.h" 8 | 9 | void armQuad(); 10 | void disarmCheck(); //called every loop to check fo disarm 11 | bool quadTestMode(bool enable); //when testing in room, call this function to avoid uneccesary arming 12 | void shutdown(); 13 | bool armCheck; 14 | 15 | void armQuad(){ 16 | myled = 1; 17 | armCheck = false; 18 | t.start(); 19 | pc.printf("Awaiting Arming\n"); 20 | while(!armCheck){ 21 | if(channelVal[1] < 1050 && channelVal[2] < 1050){ 22 | wait(2); 23 | if(channelVal[1] < 1050 && channelVal[2] < 1050){ 24 | armCheck = true; 25 | pc.printf("\n"); 26 | pc.printf("ARMED!\n"); 27 | esc1.pulsewidth_us(1000); 28 | esc2.pulsewidth_us(1000); 29 | esc3.pulsewidth_us(1000); 30 | esc4.pulsewidth_us(1000); 31 | myled=0; 32 | wait(1); 33 | myled = 1; 34 | if(armCheck) pc.printf("QUAD IS ARMED!!!\n"); 35 | wait(10); 36 | myled = 0; 37 | break; 38 | } 39 | } 40 | } 41 | } 42 | 43 | 44 | void disarmCheck(){ 45 | if(channelVal[1] < 1050 && channelVal[2] < 1050){ 46 | shutdown(); 47 | armCheck = false; 48 | } 49 | } 50 | 51 | 52 | bool quadTestMode(bool enable){ 53 | armCheck = enable; 54 | esc1.pulsewidth_us(1000); 55 | esc2.pulsewidth_us(1000); 56 | esc3.pulsewidth_us(1000); 57 | esc4.pulsewidth_us(1000); 58 | return enable; 59 | 60 | } 61 | 62 | void shutdown() { 63 | esc1.pulsewidth_us(1000); 64 | esc2.pulsewidth_us(1000); 65 | esc3.pulsewidth_us(1000); 66 | esc4.pulsewidth_us(1000); 67 | pc.printf("I AM DEAD MAN!"); 68 | } 69 | 70 | #endif 71 | -------------------------------------------------------------------------------- /quad/ATTITUDE.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef ATTITUDE_H 3 | #define ATTITUDE_H 4 | 5 | #include "PERIPHERALS.h" 6 | #include "math.h" 7 | 8 | void getAngles(float deltat); 9 | 10 | void getAngles(float deltat) { 11 | 12 | imu.readAccelData(accelCount); 13 | ax = (float)accelCount[0]*aRes - accelBias[0]; 14 | ay = (float)accelCount[1]*aRes - accelBias[1]; 15 | az = (float)accelCount[2]*aRes - accelBias[2]; 16 | 17 | imu.readGyroData(gyroCount); 18 | gx = (float)gyroCount[0]*gRes - gyroBias[0]; 19 | gy = (float)gyroCount[1]*gRes - gyroBias[1]; 20 | gz = (float)gyroCount[2]*gRes - gyroBias[2]; 21 | 22 | imu.readMagData(magCount); 23 | mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; 24 | my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; 25 | mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; 26 | 27 | float G = sqrt(ax*ax + ay*ay + az*az); 28 | 29 | float pitchAcc = asin(-ax/G)*180.0f/PI; 30 | float rollAcc = asin(ay/(G*cos(pitch*PI/180.0f)))*180.0f/PI; 31 | float yawAcc = atan2(my, mx); 32 | yawAcc*=180.0f/PI; 33 | 34 | pitch = ((gx*deltat)*180.0f/PI + pitch)*0.98 + pitchAcc*0.02; 35 | roll = ((gy*deltat)*180.0f/PI + roll)*0.98 + rollAcc*0.02; 36 | yaw = ((gz*deltat)*180.0f/PI + yaw)*0.98 + yawAcc*0.02; 37 | 38 | } 39 | 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /quad/INITIALISE.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifndef INITIALISE_H 4 | #define INITIALISE_H 5 | 6 | #include "PERIPHERALS.h" 7 | #include "PPM.h" 8 | #include "PID.h" 9 | 10 | 11 | #define MAGBIAS_X 98.89 12 | #define MAGBIAS_Y 181.041 13 | #define MAGBIAS_Z -20.38 14 | 15 | float KP_ROLL = 1.5; 16 | float KP_PITCH = 1.4; 17 | float KP_YAW = 0; 18 | float KD_ROLL = 0.0775; 19 | float KD_PITCH = 0.0755; 20 | float KD_YAW = 0; 21 | float KI_ROLL = 0.0; 22 | float KI_PITCH = 0.0; 23 | float KI_YAW = 0; 24 | 25 | void initialiseIMU(); 26 | void initialisePeripherals(); //call first 27 | void initialiseErrInt(float* roll_integ, float* pitch_integ, float* yaw_integ); 28 | void initialiseTimers(); //call before main loop 29 | void initialiseGains(float* roll_kp, float* pitch_kp, float* yaw_kp, float* roll_kd, float* pitch_kd, float* yaw_kd, float* roll_ki, float* pitch_ki, float* yaw_ki); 30 | void initialiseMagBias(); 31 | 32 | void initialiseIMU(){ 33 | 34 | uint8_t address = imu.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); 35 | pc.printf("%x\n",address); 36 | if(address == 0x73){ 37 | pc.printf("Resetting IMU...."); 38 | imu.resetMPU9250(); 39 | myled=!myled; 40 | wait(1); 41 | myled=!myled; 42 | pc.printf("Calibrating IMU..."); 43 | imu.calibrateMPU9250(gyroBias, accelBias); 44 | myled=!myled; 45 | wait(1); 46 | myled=!myled; 47 | imu.initMPU9250(); 48 | pc.printf("IMU initialised!\n"); 49 | myled=!myled; 50 | wait(2); 51 | myled=!myled; 52 | imu.initAK8963(magCalibration); 53 | myled=!myled; 54 | wait(1); 55 | myled=!myled; 56 | pc.printf("AK8963 initialized for active data mode....\n\r"); 57 | imu.getAres(); 58 | imu.getGres(); 59 | imu.getMres(); 60 | } 61 | 62 | else{ 63 | while(1){ 64 | myled = !myled; 65 | wait(0.5); 66 | } 67 | } 68 | } 69 | 70 | void initialisePeripherals() { 71 | pc.baud(57600); 72 | //pc2.baud(57600); 73 | myled=!myled; 74 | wait(1); 75 | myled=!myled; 76 | wait(1); 77 | myled=!myled; 78 | ppmPin.rise(&measureChannel); 79 | } 80 | 81 | void initialiseErrInt(float* roll_integ, float* pitch_integ, float* yaw_integ){ 82 | *roll_integ = 0; 83 | *pitch_integ = 0; 84 | *yaw_integ = 0; 85 | roll = 0; 86 | pitch = 0; 87 | yaw = 0; 88 | } 89 | 90 | void initialiseGains(float* roll_kp, float* pitch_kp, float* yaw_kp, float* roll_kd, float* pitch_kd, float* yaw_kd, float* roll_ki, float* pitch_ki, float* yaw_ki){ 91 | *roll_kp = KP_ROLL; 92 | *pitch_kp = KP_PITCH; 93 | *yaw_kp = KP_YAW; 94 | *roll_kd = KD_ROLL; 95 | *pitch_kd = KD_PITCH; 96 | *yaw_kd = KD_YAW; 97 | *roll_ki = KI_ROLL; 98 | *pitch_ki = KI_PITCH; 99 | *yaw_ki = KI_YAW; 100 | } 101 | 102 | void initialiseTimers(){ 103 | t.reset(); 104 | //t1.stop(); 105 | t.start(); 106 | t1.start(); 107 | t3.start(); 108 | } 109 | 110 | void initialiseMagBias(){ 111 | magbias[0] = MAGBIAS_X; 112 | magbias[1] = MAGBIAS_Y; 113 | magbias[2] = MAGBIAS_Z; 114 | } 115 | //add optional magnetometer calibration func. 116 | #endif 117 | -------------------------------------------------------------------------------- /quad/MPU9250.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef MPU9250_H 3 | #define MPU9250_H 4 | 5 | #include "mbed.h" 6 | #include "math.h" 7 | 8 | // See also MPU-9250 Register Map and Descriptions, Revision 4.0, RM-MPU-9250A-00, Rev. 1.4, 9/9/2013 for registers not listed in 9 | // above document; the MPU9250 and MPU9150 are virtually identical but the latter has a different register map 10 | // 11 | //Magnetometer Registers 12 | #define AK8963_ADDRESS 0x0C<<1 13 | #define WHO_AM_I_AK8963 0x00 // should return 0x48 14 | #define INFO 0x01 15 | #define AK8963_ST1 0x02 // data ready status bit 0 16 | #define AK8963_XOUT_L 0x03 // data 17 | #define AK8963_XOUT_H 0x04 18 | #define AK8963_YOUT_L 0x05 19 | #define AK8963_YOUT_H 0x06 20 | #define AK8963_ZOUT_L 0x07 21 | #define AK8963_ZOUT_H 0x08 22 | #define AK8963_ST2 0x09 // Data overflow bit 3 and data read error status bit 2 23 | #define AK8963_CNTL 0x0A // Power down (0000), single-measurement (0001), self-test (1000) and Fuse ROM (1111) modes on bits 3:0 24 | #define AK8963_ASTC 0x0C // Self test control 25 | #define AK8963_I2CDIS 0x0F // I2C disable 26 | #define AK8963_ASAX 0x10 // Fuse ROM x-axis sensitivity adjustment value 27 | #define AK8963_ASAY 0x11 // Fuse ROM y-axis sensitivity adjustment value 28 | #define AK8963_ASAZ 0x12 // Fuse ROM z-axis sensitivity adjustment value 29 | 30 | #define SELF_TEST_X_GYRO 0x00 31 | #define SELF_TEST_Y_GYRO 0x01 32 | #define SELF_TEST_Z_GYRO 0x02 33 | 34 | /*#define X_FINE_GAIN 0x03 // [7:0] fine gain 35 | #define Y_FINE_GAIN 0x04 36 | #define Z_FINE_GAIN 0x05 37 | #define XA_OFFSET_H 0x06 // User-defined trim values for accelerometer 38 | #define XA_OFFSET_L_TC 0x07 39 | #define YA_OFFSET_H 0x08 40 | #define YA_OFFSET_L_TC 0x09 41 | #define ZA_OFFSET_H 0x0A 42 | #define ZA_OFFSET_L_TC 0x0B */ 43 | 44 | #define SELF_TEST_X_ACCEL 0x0D 45 | #define SELF_TEST_Y_ACCEL 0x0E 46 | #define SELF_TEST_Z_ACCEL 0x0F 47 | 48 | #define SELF_TEST_A 0x10 49 | 50 | #define XG_OFFSET_H 0x13 // User-defined trim values for gyroscope 51 | #define XG_OFFSET_L 0x14 52 | #define YG_OFFSET_H 0x15 53 | #define YG_OFFSET_L 0x16 54 | #define ZG_OFFSET_H 0x17 55 | #define ZG_OFFSET_L 0x18 56 | #define SMPLRT_DIV 0x19 57 | #define CONFIG 0x1A 58 | #define GYRO_CONFIG 0x1B 59 | #define ACCEL_CONFIG 0x1C 60 | #define ACCEL_CONFIG2 0x1D 61 | #define LP_ACCEL_ODR 0x1E 62 | #define WOM_THR 0x1F 63 | 64 | #define MOT_DUR 0x20 // Duration counter threshold for motion interrupt generation, 1 kHz rate, LSB = 1 ms 65 | #define ZMOT_THR 0x21 // Zero-motion detection threshold bits [7:0] 66 | #define ZRMOT_DUR 0x22 // Duration counter threshold for zero motion interrupt generation, 16 Hz rate, LSB = 64 ms 67 | 68 | #define FIFO_EN 0x23 69 | #define I2C_MST_CTRL 0x24 70 | #define I2C_SLV0_ADDR 0x25 71 | #define I2C_SLV0_REG 0x26 72 | #define I2C_SLV0_CTRL 0x27 73 | #define I2C_SLV1_ADDR 0x28 74 | #define I2C_SLV1_REG 0x29 75 | #define I2C_SLV1_CTRL 0x2A 76 | #define I2C_SLV2_ADDR 0x2B 77 | #define I2C_SLV2_REG 0x2C 78 | #define I2C_SLV2_CTRL 0x2D 79 | #define I2C_SLV3_ADDR 0x2E 80 | #define I2C_SLV3_REG 0x2F 81 | #define I2C_SLV3_CTRL 0x30 82 | #define I2C_SLV4_ADDR 0x31 83 | #define I2C_SLV4_REG 0x32 84 | #define I2C_SLV4_DO 0x33 85 | #define I2C_SLV4_CTRL 0x34 86 | #define I2C_SLV4_DI 0x35 87 | #define I2C_MST_STATUS 0x36 88 | #define INT_PIN_CFG 0x37 89 | #define INT_ENABLE 0x38 90 | #define DMP_INT_STATUS 0x39 // Check DMP interrupt 91 | #define INT_STATUS 0x3A 92 | #define ACCEL_XOUT_H 0x3B 93 | #define ACCEL_XOUT_L 0x3C 94 | #define ACCEL_YOUT_H 0x3D 95 | #define ACCEL_YOUT_L 0x3E 96 | #define ACCEL_ZOUT_H 0x3F 97 | #define ACCEL_ZOUT_L 0x40 98 | #define TEMP_OUT_H 0x41 99 | #define TEMP_OUT_L 0x42 100 | #define GYRO_XOUT_H 0x43 101 | #define GYRO_XOUT_L 0x44 102 | #define GYRO_YOUT_H 0x45 103 | #define GYRO_YOUT_L 0x46 104 | #define GYRO_ZOUT_H 0x47 105 | #define GYRO_ZOUT_L 0x48 106 | #define EXT_SENS_DATA_00 0x49 107 | #define EXT_SENS_DATA_01 0x4A 108 | #define EXT_SENS_DATA_02 0x4B 109 | #define EXT_SENS_DATA_03 0x4C 110 | #define EXT_SENS_DATA_04 0x4D 111 | #define EXT_SENS_DATA_05 0x4E 112 | #define EXT_SENS_DATA_06 0x4F 113 | #define EXT_SENS_DATA_07 0x50 114 | #define EXT_SENS_DATA_08 0x51 115 | #define EXT_SENS_DATA_09 0x52 116 | #define EXT_SENS_DATA_10 0x53 117 | #define EXT_SENS_DATA_11 0x54 118 | #define EXT_SENS_DATA_12 0x55 119 | #define EXT_SENS_DATA_13 0x56 120 | #define EXT_SENS_DATA_14 0x57 121 | #define EXT_SENS_DATA_15 0x58 122 | #define EXT_SENS_DATA_16 0x59 123 | #define EXT_SENS_DATA_17 0x5A 124 | #define EXT_SENS_DATA_18 0x5B 125 | #define EXT_SENS_DATA_19 0x5C 126 | #define EXT_SENS_DATA_20 0x5D 127 | #define EXT_SENS_DATA_21 0x5E 128 | #define EXT_SENS_DATA_22 0x5F 129 | #define EXT_SENS_DATA_23 0x60 130 | #define MOT_DETECT_STATUS 0x61 131 | #define I2C_SLV0_DO 0x63 132 | #define I2C_SLV1_DO 0x64 133 | #define I2C_SLV2_DO 0x65 134 | #define I2C_SLV3_DO 0x66 135 | #define I2C_MST_DELAY_CTRL 0x67 136 | #define SIGNAL_PATH_RESET 0x68 137 | #define MOT_DETECT_CTRL 0x69 138 | #define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP 139 | #define PWR_MGMT_1 0x6B // Device defaults to the SLEEP mode 140 | #define PWR_MGMT_2 0x6C 141 | #define DMP_BANK 0x6D // Activates a specific bank in the DMP 142 | #define DMP_RW_PNT 0x6E // Set read/write pointer to a specific start address in specified DMP bank 143 | #define DMP_REG 0x6F // Register in DMP from which to read or to which to write 144 | #define DMP_REG_1 0x70 145 | #define DMP_REG_2 0x71 146 | #define FIFO_COUNTH 0x72 147 | #define FIFO_COUNTL 0x73 148 | #define FIFO_R_W 0x74 149 | #define WHO_AM_I_MPU9250 0x75 // Should return 0x71 150 | #define XA_OFFSET_H 0x77 151 | #define XA_OFFSET_L 0x78 152 | #define YA_OFFSET_H 0x7A 153 | #define YA_OFFSET_L 0x7B 154 | #define ZA_OFFSET_H 0x7D 155 | #define ZA_OFFSET_L 0x7E 156 | 157 | #define PI 3.14159265358979323846f 158 | // Using the MSENSR-9250 breakout board, ADO is set to 0 159 | // Seven-bit device address is 110100 for ADO = 0 and 110101 for ADO = 1 160 | //mbed uses the eight-bit device address, so shift seven-bit addresses left by one! 161 | #define ADO 0 162 | #if ADO 163 | #define MPU9250_ADDRESS 0x69<<1 // Device address when ADO = 1 164 | #else 165 | #define MPU9250_ADDRESS 0x68<<1 // Device address when ADO = 0 166 | #endif 167 | 168 | // Set initial input parameters 169 | enum Ascale { 170 | AFS_2G = 0, 171 | AFS_4G, 172 | AFS_8G, 173 | AFS_16G 174 | }; 175 | 176 | enum Gscale { 177 | GFS_250DPS = 0, 178 | GFS_500DPS, 179 | GFS_1000DPS, 180 | GFS_2000DPS 181 | }; 182 | 183 | enum Mscale { 184 | MFS_14BITS = 0, // 0.6 mG per LSB 185 | MFS_16BITS // 0.15 mG per LSB 186 | }; 187 | 188 | class MPU9250 { 189 | 190 | protected: 191 | 192 | public: 193 | 194 | MPU9250(PinName sda, PinName scl); 195 | MPU9250(I2C *i2c); 196 | ~MPU9250(); 197 | 198 | void writeByte(uint8_t address, uint8_t subAddress, uint8_t data); 199 | char readByte(uint8_t address, uint8_t subAddress); 200 | void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest); 201 | void getMres(); 202 | void getGres(); 203 | void getAres(); 204 | void readAccelData(int16_t * destination); 205 | void readGyroData(int16_t * destination); 206 | void readMagData(int16_t * destination); 207 | int16_t readTempData(); 208 | void resetMPU9250(); 209 | void initAK8963(float * destination); 210 | void initMPU9250(); 211 | void calibrateMPU9250(float * dest1, float * dest2); 212 | void MPU9250SelfTest(float * destination); 213 | void MadgwickQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz); 214 | void MahonyQuaternionUpdate(float ax, float ay, float az, float gx, float gy, float gz, float mx, float my, float mz); 215 | 216 | float SelfTest[6]; 217 | float gyroBias[3],accelBias[3]; // Bias corrections for gyro and accelerometer 218 | float magCalibration[3], magbias[3]; // Factory mag calibration and mag bias 219 | 220 | uint8_t Ascale; // AFS_2G, AFS_4G, AFS_8G, AFS_16G 221 | uint8_t Gscale; // GFS_250DPS, GFS_500DPS, GFS_1000DPS, GFS_2000DPS 222 | uint8_t Mscale; // MFS_14BITS or MFS_16BITS, 14-bit or 16-bit magnetometer resolution 223 | uint8_t Mmode; // Either 8 Hz 0x02) or 100 Hz (0x06) magnetometer data ODR 224 | float aRes, gRes, mRes; // scale resolutions per LSB for the sensors 225 | 226 | int16_t accelCount[3]; // Stores the 16-bit signed accelerometer sensor output 227 | int16_t gyroCount[3]; // Stores the 16-bit signed gyro sensor output 228 | int16_t magCount[3]; // Stores the 16-bit signed magnetometer sensor output 229 | float q[4]; // vector to hold quaternion 230 | 231 | float ax, ay, az, gx, gy, gz, mx, my, mz; // variables to hold latest sensor data values 232 | float pitch, yaw, roll; 233 | float deltat; // integration interval for both filter schemes 234 | int lastUpdate, firstUpdate, Now; // used to calculate integration interval 235 | int delt_t; // used to control display output rate 236 | int count; // used to control display output rate 237 | int16_t tempCount; // Stores the real internal chip temperature in degrees Celsius 238 | float temperature; 239 | private: 240 | I2C *i2c_; 241 | }; 242 | 243 | #endif 244 | -------------------------------------------------------------------------------- /quad/PERIPHERALS.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "mbed.h" 4 | #include "MPU9250.h" 5 | 6 | #define ESC_MOTOR3 PA_11 7 | #define ESC_MOTOR1 PC_5 8 | #define ESC_MOTOR2 PC_4 9 | #define ESC_MOTOR4 PB_9 10 | #define PPM_PIN PA_8 11 | #define TX_PIN PB_6 12 | #define RX_PIN PA_10 13 | 14 | Serial pc(TX_PIN, RX_PIN); 15 | //Serial pc(USBTX, USBRX); 16 | Timer t, t1, t3; 17 | MPU9250 imu; 18 | InterruptIn ppmPin(PPM_PIN); 19 | PwmOut esc1(ESC_MOTOR1); 20 | PwmOut esc2(ESC_MOTOR2); 21 | PwmOut esc3(ESC_MOTOR3); 22 | PwmOut esc4(ESC_MOTOR4); 23 | DigitalOut myled(LED1); 24 | 25 | 26 | -------------------------------------------------------------------------------- /quad/PID.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifndef PID_H 4 | #define PID_H 5 | 6 | #include "mbed.h" 7 | #include "PPM.h" 8 | #include "PERIPHERALS.h" 9 | #include "TELEMETRY.h" 10 | 11 | #define setpointScaler 12.575 12 | #define yawSetpointScaler 12.575 13 | #define heightSetpointScaler 200 14 | #define MAX_THRUST_VAL 2000 15 | #define MIN_THRUST_VAL 1000 16 | 17 | float roll_prev, roll_integ, roll_kp, roll_ki, roll_kd; 18 | float pitch_prev, pitch_integ, pitch_kp, pitch_ki, pitch_kd; 19 | float yaw_prev, yaw_integ, yaw_kp, yaw_ki, yaw_kd; 20 | 21 | float* controlQuad(uint8_t print, float roll_prev, float roll_integ, float pitch_prev, float pitch_integ, float yaw_prev, float yaw_integ, float deltat); //set print to 1: thrust vals 2: PID vals 3: proportional errors 4: derivative errors 5: integral errors 0: no print 22 | float setpoints[6]; 23 | float calculateErrInt(float err_int, float err, float errPrev, float deltat); 24 | void convertToSetpoints(); 25 | 26 | float* controlQuad(uint8_t print, float roll_prev, float roll_integ, float pitch_prev, float pitch_integ, float yaw_prev, float yaw_integ, float detat){ 27 | //calculate errors 28 | //first proportional error 29 | 30 | convertToSetpoints(); 31 | float rollErr = setpoints[0] - roll; 32 | float pitchErr = setpoints[1] - pitch; 33 | float yawErr = setpoints[3] - gz*180.0f/PI; 34 | 35 | float rollErrDot = -gx; 36 | float pitchErrDot = -gy; 37 | float yawErrDot = 0; 38 | 39 | //calculate integral error 40 | roll_integ = calculateErrInt(roll_integ, rollErr, roll_prev, deltat); 41 | pitch_integ = calculateErrInt(pitch_integ, pitchErr, pitch_prev, deltat); 42 | yaw_integ = calculateErrInt(yaw_integ, yawErr, yaw_prev, deltat); 43 | 44 | float PID_roll = roll_kp*rollErr + roll_kd*rollErrDot + roll_ki*roll_integ; 45 | float PID_pitch = pitch_kp*pitchErr + pitch_kd*pitchErrDot + pitch_ki*pitch_integ; 46 | float PID_yaw = yaw_kp*yawErr + yaw_kd*yawErrDot + yaw_ki*yaw_integ; 47 | 48 | //convertToMicroseconds(); need to convert PID outputs to esc microseconds 49 | 50 | int thrust1 = channelVal[2] - PID_pitch - PID_roll + PID_yaw; 51 | int thrust2 = channelVal[2] + PID_pitch - PID_roll - PID_yaw; 52 | int thrust3 = channelVal[2] + PID_pitch + PID_roll + PID_yaw; 53 | int thrust4 = channelVal[2] - PID_pitch + PID_roll - PID_yaw; 54 | 55 | if(thrust1 < MIN_THRUST_VAL) thrust1 = MIN_THRUST_VAL; 56 | if(thrust1 > MAX_THRUST_VAL) thrust1 = MAX_THRUST_VAL; 57 | if(thrust2 < MIN_THRUST_VAL) thrust2 = MIN_THRUST_VAL; 58 | if(thrust2 > MAX_THRUST_VAL) thrust2 = MAX_THRUST_VAL; 59 | if(thrust3 < MIN_THRUST_VAL) thrust3 = MIN_THRUST_VAL; 60 | if(thrust3 > MAX_THRUST_VAL) thrust3 = MAX_THRUST_VAL; 61 | if(thrust4 < MIN_THRUST_VAL) thrust4 = MIN_THRUST_VAL; 62 | if(thrust4 > MAX_THRUST_VAL) thrust4 = MAX_THRUST_VAL; 63 | 64 | esc1.pulsewidth_us(thrust1); 65 | esc2.pulsewidth_us(thrust2); 66 | esc3.pulsewidth_us(thrust3); 67 | esc4.pulsewidth_us(thrust4); 68 | 69 | if(t3.read_ms() >= 35){ 70 | if(print == 1) sendThrustData(thrust1,thrust2,thrust3, thrust4); 71 | else if(print == 2) sendPIDData(PID_roll, PID_pitch, PID_yaw); 72 | else if(print == 3) sendAngleData(); 73 | else if(print == 4) sendData(PID_roll, PID_pitch, PID_yaw); 74 | t3.reset(); 75 | } 76 | 77 | static float errorVals[6] = {rollErr, roll_integ, pitchErr, pitch_integ, yawErr, yaw_integ}; 78 | float* pointerToVals; 79 | 80 | pointerToVals = errorVals; 81 | return pointerToVals; 82 | } 83 | 84 | float calculateErrInt(float err_int, float err, float errPrev, float deltat){ //calculate integral of error for all three at omce else timer will fuck up 85 | 86 | //calculate addition to err_int 87 | float deltae = (deltat/2)*(errPrev + err);//reset t1 88 | 89 | //add to err_int 90 | return err_int + deltae; 91 | } 92 | 93 | void convertToSetpoints(){ 94 | for(int i = 0; i < 5; i++){ 95 | if(i != 2 && i != 3){ //height setpoint will come from channel 3 and yaw setpoint to 40 dps 96 | if(channelVal[i] >= 1497 && channelVal[i] <= 1503){ 97 | setpoints[i] = 0; 98 | } 99 | 100 | else{ 101 | setpoints[i] = (channelVal[i] - 1495)/setpointScaler; //to get max setpoint of 20 degrees 102 | } 103 | } 104 | 105 | else if(i == 2) setpoints[i] = (channelVal[i] - 1003)/heightSetpointScaler; //to get max height of 5 meters 106 | 107 | else if(i == 3) setpoints[i] = (channelVal[i] - 1495)/yawSetpointScaler; // to get max yaw rate setpoint of 40 dps 108 | } 109 | } 110 | 111 | 112 | //introduce dead band for yaw 113 | #endif 114 | 115 | //need to view data for yawErrDot and yaw_e.integ 116 | -------------------------------------------------------------------------------- /quad/TELEMETRY.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define CONNECT_TO_BOARD 0XDD 12 | #define CONNECTION_OK 0X7C 13 | 14 | void sendAngleData(); 15 | void sendPIDData(float PID_roll, float PID_pitch, float PID_yaw); 16 | void sendThrustData(int thrust1, int thrust2, int thrust3, int thrust4); 17 | void sendData(float PID_roll, float PID_pitch, float PID_yaw); 18 | uint8_t option; 19 | bool connectToGCS(); 20 | bool connect; 21 | 22 | void sendAngleData(){ 23 | char data[1]; 24 | sprintf(data, "sa%fr%fp%fy\n", roll, pitch, yaw); 25 | pc.printf(data); 26 | } 27 | 28 | void sendThrustData(int thrust1, int thrust2, int thrust3, int thrust4){ 29 | char data[2]; 30 | sprintf(data, "sf%db%dl%dr%de\n", thrust1, thrust2, thrust3, thrust4); 31 | pc.printf(data); 32 | } 33 | 34 | void sendPIDData(float PID_roll, float PID_pitch, float PID_yaw){ 35 | char data[1]; 36 | sprintf(data, "so%fr%fp%fy\n", PID_roll, PID_pitch, PID_yaw); 37 | pc.printf(data); 38 | } 39 | 40 | void sendData(float PID_roll, float PID_pitch, float PID_yaw){ 41 | char data[33]; 42 | sprintf(data, "sf%fr%fp%fy%fq%fw%fe\n", PID_roll, PID_pitch, PID_yaw, roll, pitch, yaw); 43 | pc.printf(data); 44 | } 45 | 46 | bool connecToGCS(){ 47 | bool connect; 48 | connect = false; 49 | char connectionReq[1]; 50 | char recvdMsg[7]; 51 | sprintf(connectionReq, "s%xe", CONNECT_TO_BOARD); 52 | while(!connect){ 53 | myled = 1; 54 | pc.printf(connectionReq); 55 | myled = 0; 56 | myled = 1; 57 | wait(1); 58 | myled = 0; 59 | if(pc.readable()){ 60 | while(pc.readable()){ 61 | pc.gets(recvdMsg, 7); 62 | // pc2.printf 63 | } 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /quad/angledata.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as chart 2 | import numpy as np 3 | 4 | def getAngles(data, angleData): 5 | data = readData() 6 | rMark = data.find('r') 7 | pMark = data.find('p') 8 | yMark = data.find('y') 9 | sMark = data.find('s') 10 | aMark = data.find('a') 11 | 12 | checksum = 0 13 | 14 | if sMark == 0: 15 | if aMark == 1: 16 | if (rMark - aMark) - 1 > 7: 17 | rollAngle = data[aMark+1:rMark] 18 | checksum = 1 19 | 20 | if (pMark - rMark) - 1 > 7: 21 | pitchAngle = data[rMark+1:pMark] 22 | checksum = 2 23 | 24 | if (yMark - pMark) - 1 > 7: 25 | yawAngle = (data[pMark+1:yMark]) 26 | checksum = 3 27 | 28 | if checksum == 3: 29 | angleData.write(rollAngle+','+pitchAngle+','+yawAngle+'\n') 30 | print([rollAngle, pitchAngle, yawAngle]) 31 | 32 | def anglePlot(angleData): 33 | angleData.close() 34 | 35 | roll, pitch, yaw = np.loadtxt(angleData, delimiter=',', unpack=True) 36 | angles = [roll, pitch, yaw] 37 | 38 | anglePlot = chart.figure() 39 | anglePlots = [] 40 | 41 | for i in range(0,3): 42 | anglePlots.append(anglePlot.add_subplot(3, 1, i)) 43 | anglePlots[i].plot(angles[i]) 44 | chart.grid() 45 | 46 | chart.show() 47 | 48 | -------------------------------------------------------------------------------- /quad/baseStaStuff.py: -------------------------------------------------------------------------------- 1 | def resetMenu(option): 2 | 3 | print("Please choose one of the following options to view:") 4 | print("1. Angle Data") 5 | print("2. PID Output") 6 | print("3. PID vs Angles") 7 | print("4. Gains") 8 | print("Press Ctrl + C to exit data streaming") 9 | option = raw_input("Choose your option: ") 10 | 11 | board.write('send') 12 | board.write(option) 13 | 14 | return option 15 | 16 | def readData(): 17 | while board.in_waiting < 1: 18 | pass 19 | data = board.readline() 20 | board.write('send') 21 | board.write(option) 22 | return data 23 | 24 | 25 | -------------------------------------------------------------------------------- /quad/baseStation.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as chart 2 | import serial 3 | import time 4 | import numpy as np 5 | 6 | usb = '/dev/tty.usbmodem1423' 7 | telem = '/dev/tty.usbserial-DN02136Z' 8 | board = serial.Serial(port = usb, baudrate = 57600, timeout = 3) 9 | time.sleep(3) 10 | 11 | angleData = open('anglesData.txt', 'w') 12 | PIDData = open('PIDData.txt', 'w') 13 | thrustData = open('thrustData.txt', 'w') 14 | 15 | def getthrustData(data): 16 | thrustData.write(data) 17 | print(data) 18 | def resetMenu(option): 19 | 20 | print("Please choose one of the following options to view:") 21 | print("1. Angle Data") 22 | print("2. PID Output") 23 | print("3. PID vs Angles") 24 | print("4. Gains") 25 | print("Press Ctrl + C to exit data streaming") 26 | option = raw_input("Choose your option: ") 27 | 28 | board.write('send') 29 | board.write(option) 30 | 31 | return option 32 | 33 | def readData(): 34 | while board.in_waiting < 1: 35 | pass 36 | data = board.readline() 37 | board.reset_input_buffer 38 | return data 39 | 40 | def getPIDOutputs(data, PIDData): 41 | data = readData() 42 | rMark = data.find('r') 43 | pMark = data.find('p') 44 | yMark = data.find('y') 45 | sMark = data.find('s') 46 | oMark = data.find('o') 47 | 48 | checksum = [0, 0, 0] 49 | 50 | if sMark == 0: 51 | if oMark == 1: 52 | if (rMark - oMark) - 1 > 7: 53 | rollPID = data[oMark+1:rMark] 54 | checksum[0] = 1 55 | 56 | if (pMark - rMark) - 1 > 7: 57 | pitchPID = data[rMark+1:pMark] 58 | checksum[1] = 1 59 | 60 | if (yMark - pMark) - 1 > 7: 61 | yawPID = (data[pMark+1:yMark]) 62 | checksum[2] = 1 63 | 64 | if sum(checksum) == 3: 65 | PIDData.write(rollPID + ',' + pitchPID + ',' + yawPID + '\n') 66 | print([rollPID, pitchPID, yawPID]) 67 | 68 | def getData(data, angleData, PIDData): 69 | data = readData() 70 | rMark = data.find('r') 71 | pMark = data.find('p') 72 | yMark = data.find('y') 73 | sMark = data.find('s') 74 | fMark = data.find('f') 75 | qMark = data.find('q') 76 | wMark = data.find('w') 77 | eMark = data.find('e') 78 | 79 | checksum = [0, 0, 0] 80 | checksum2 = [0, 0, 0] 81 | 82 | if sMark == 0: 83 | if fMark == 1: 84 | if (rMark - fMark) - 1 > 7 and (rMark - fMark) - 1 < 15: 85 | rollPID = data[fMark+1:rMark] 86 | checksum[0] = 1 87 | 88 | if (pMark - rMark) - 1 > 7 and (pMark - rMark) - 1 < 15: 89 | pitchPID = data[rMark+1:pMark] 90 | checksum[1] = 1 91 | 92 | if (yMark - pMark) - 1 > 7 and (yMark - pMark) - 1 < 15: 93 | yawPID = (data[pMark+1:yMark]) 94 | checksum[2] = 1 95 | 96 | if (qMark - yMark) - 1 > 7 and (qMark - yMark) - 1 < 15: 97 | rollAngle = data[yMark+1:qMark] 98 | checksum2[0] = 1 99 | 100 | if (wMark - qMark) - 1 > 7 and (wMark - qMark) - 1 < 15: 101 | pitchAngle = data[qMark+1:wMark] 102 | checksum2[1] = 1 103 | 104 | if (eMark - wMark) - 1 > 7 and (eMark - wMark) - 1 < 15: 105 | yawAngle = (data[wMark+1:eMark]) 106 | checksum2[2] = 1 107 | 108 | if sum(checksum2) == 3: 109 | angleData.write(rollAngle + ',' + pitchAngle + ',' + yawAngle + '\n') 110 | print([rollAngle, pitchAngle, yawAngle]) 111 | 112 | if sum(checksum) == 3: 113 | PIDData.write(rollPID + ',' + pitchPID + ',' + yawPID + '\n') 114 | print([rollPID, pitchPID, yawPID]) 115 | 116 | if sum(checksum2) == 3 and sum(checksum) == 3: 117 | print([rollPID, pitchPID, yawPID, rollAngle, pitchAngle, yawAngle]) 118 | 119 | 120 | def getAngles(data, angleData): 121 | data = readData() 122 | rMark = data.find('r') 123 | pMark = data.find('p') 124 | yMark = data.find('y') 125 | sMark = data.find('s') 126 | aMark = data.find('a') 127 | 128 | checksum = [0, 0, 0] 129 | 130 | if sMark == 0: 131 | if aMark == 1: 132 | if (rMark - aMark) - 1 > 7: 133 | rollAngle = data[aMark+1:rMark] 134 | checksum[0] = 1 135 | 136 | if (pMark - rMark) - 1 > 7: 137 | pitchAngle = data[rMark+1:pMark] 138 | checksum[1] = 1 139 | 140 | if (yMark - pMark) - 1 > 7: 141 | yawAngle = (data[pMark+1:yMark]) 142 | checksum[2] = 1 143 | 144 | if sum(checksum) == 3: 145 | angleData.write(rollAngle + ',' + pitchAngle + ',' + yawAngle + '\n') 146 | print([rollAngle, pitchAngle, yawAngle]) 147 | 148 | def PIDPlot(PIDData): 149 | roll, pitch, yaw = np.loadtxt(PIDData, delimiter=',', unpack=True) 150 | outputs = [roll, pitch, yaw] 151 | 152 | PIDPlot = chart.figure() 153 | PIDPlots = [] 154 | 155 | for i in range(0,3): 156 | PIDPlots.append(PIDPlot.add_subplot(3, 1, i+1)) 157 | PIDPlots[i].plot(outputs[i]) 158 | chart.grid() 159 | 160 | chart.show() 161 | 162 | 163 | def anglePlot(angleData): 164 | roll, pitch, yaw = np.loadtxt(angleData, delimiter=',', unpack=True) 165 | angles = [roll, pitch, yaw] 166 | 167 | anglePlot = chart.figure() 168 | anglePlots = [] 169 | 170 | for i in range(0,3): 171 | anglePlots.append(anglePlot.add_subplot(3, 1, i+1)) 172 | anglePlots[i].plot(angles[i]) 173 | chart.grid() 174 | 175 | chart.show() 176 | 177 | option = 0 178 | option = resetMenu(option) 179 | 180 | while(1): 181 | if option == '1': 182 | try: 183 | while(1): 184 | getAngles(readData(), angleData) 185 | except KeyboardInterrupt: 186 | angleData.close() 187 | anglePlot('anglesData.txt') 188 | resetMenu(option) 189 | # board.write('nend') 190 | elif option == '2': 191 | try: 192 | while(1): 193 | getPIDOutputs(readData(), PIDData) 194 | except KeyboardInterrupt: 195 | PIDData.close() 196 | PIDPlot('PIDData.txt') 197 | resetMenu(option) 198 | 199 | elif option == '3': 200 | try: 201 | while(1): 202 | getData(readData(), angleData, PIDData) 203 | except KeyboardInterrupt: 204 | PIDData.close() 205 | angleData.close() 206 | anglePlot('anglesData.txt') 207 | PIDPlot('PIDData.txt') 208 | resetMenu(option) 209 | elif option == '4': 210 | try: 211 | while(1): 212 | getthrustData(readData()) 213 | except KeyboardInterrupt: 214 | thrustData.close() 215 | resetMenu(option) 216 | else: 217 | break 218 | -------------------------------------------------------------------------------- /quad/fort.1: -------------------------------------------------------------------------------- 1 | EXIT OF DOPRI5 AT X= 0.4394E+00 2 | MORE THAN NMAX = 500 STEPS ARE NEEDED 3 | -------------------------------------------------------------------------------- /quad/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "ARMING.h" //Header for SafeGaurds 3 | #include "ATTITUDE.h" //Header for Calculating Attitude 4 | #include "PID.h" //Headere for computing and implementing Corrections 5 | #include "INITIALISE.h" //Definition for Gains and setup functions 6 | #include "MPU9250.h" //IMU Header file 7 | #include "PERIPHERALS.h" //Declaration for ESC output and Initialisation 8 | #include "PPM.h" //Radio Signal setup and reading 9 | #include "TELEMETRY.h" 10 | 11 | int main(){ 12 | initialisePeripherals(); 13 | //pc.printf("HI I AM ALIVE!"); 14 | //wait(3); 15 | initialiseIMU(); 16 | initialiseErrInt(&roll_integ, &pitch_integ, &yaw_integ); 17 | initialiseGains(&roll_kp, &pitch_kp, &yaw_kp, &roll_kd, &pitch_kd, &yaw_kd, &roll_ki, &pitch_ki, &yaw_ki); 18 | initialiseMagBias(); 19 | 20 | armCheck=false; 21 | if(!quadTestMode(false)) armQuad(); //Set this true only when testing in room else arm Quad,pull down channel 2 and 3 for 2 seconds 22 | 23 | //send = true; 24 | 25 | initialiseTimers(); //setup for timer 26 | 27 | float *errorVals; 28 | float deltat; 29 | 30 | while(armCheck){ 31 | 32 | deltat = t1.read_us()/1000000.0f; //functional loop of quad when running 33 | 34 | if(imu.readByte(MPU9250_ADDRESS, INT_STATUS) & 0x01) getAngles(deltat); //Getting attitude Feedback 35 | 36 | //Implementing control, input of function is serial monitor control 37 | errorVals = controlQuad(2, roll_prev, roll_integ, pitch_prev, pitch_integ, yaw_prev, yaw_integ, deltat); 38 | 39 | //assign previous and integral values 40 | roll_prev = errorVals[0]; 41 | roll_integ = errorVals[1]; 42 | pitch_prev = errorVals[2]; 43 | pitch_integ = errorVals[3]; 44 | yaw_prev = errorVals[4]; 45 | yaw_integ = errorVals[5]; 46 | 47 | disarmCheck(); //disarm guard to powerdown the aerial vehicle as precaution 48 | t1.reset(); 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /quad/pitchPrevData.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Thalaivar/python/6702edc229e9938846f9ba757b759d0bb183aa1e/quad/pitchPrevData.png -------------------------------------------------------------------------------- /quad/quickPlot.py: -------------------------------------------------------------------------------- 1 | import serial 2 | import time 3 | usb = '/dev/tty.usbmodem1423' 4 | telem = '/dev/tty.usbserial-DN02136Z' 5 | board = serial.Serial(port = usb, baudrate = 57600, timeout = 3) 6 | time.sleep(3) 7 | 8 | angleData = open('anglesData.txt', 'w') 9 | 10 | print("Starintg") 11 | 12 | while(1): 13 | try: 14 | data = board.readline() 15 | angleData.write(data) 16 | print(data) 17 | board.reset_input_buffer 18 | except KeyboardInterrupt: 19 | angleData.close() 20 | 21 | -------------------------------------------------------------------------------- /quad/test.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as chart 2 | import numpy as np 3 | 4 | roll, pitch, yaw = np.loadtxt('anglesData.txt', delimiter=',', unpack = True) 5 | data = [roll, pitch, yaw] 6 | 7 | chart.style.use('seaborn-dark') 8 | anglesPlot = chart.figure(figsize = (10,6)) 9 | plots = [0, 0, 0] 10 | labels = ['Roll', 'Pitch', 'Yaw'] 11 | 12 | for i in range(0, 3): 13 | plots[i] = anglesPlot.add_subplot(3, 1, i+1) 14 | plots[i].set_ylabel(labels[i]) 15 | plots[i].plot(data[i]) 16 | 17 | chart.grid() 18 | chart.show() 19 | 20 | --------------------------------------------------------------------------------