tutorial 3

.pdf

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

144

Subject

Mechanical Engineering

Date

Oct 30, 2023

Type

pdf

Pages

13

Uploaded by BarristerJay3669

Report
+ DMA'23 - Tutorial 3: Decision Trees After this tutorial, you will be able to: 1. Use lambda functions 2. Use itertools and counters 3. Use classifiers (estimators) from sklearn import itertools from collections import Counter import pandas as pd import numpy as np from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy score v Lambda Function Lambdas are one line functions that are not named. # Define a lambda function that adds two numbers add = lambda x, y: X +y # Use the lambda function result = add(5, 3) print(result) 8 def add(x,y): return x+y <function <lambday> at 0x7e9049dbd750> v Write a function to find the reciprocal of a number. Let's create a dummy df with 100 rows and 5 columns: dummy data = pd.DataFrame(np.random.randint(1,100,size=(100, 5)), columns=1ist('ABCDE')) print(dummy data.shape) dummy_data.head()
(100, 5) A B C D E 99 77 40 41 24 29 80 31 53 43 63 77 2 a/R 74 7 79 QK We can create new columns by applying functions on existing columns dummy_data['E_1"'] dummy_data['E_2"'] pow(dummy data['E'],2) 1/dummy_data['E'] dummy_data.head() E_2 1681 0.024390 961 0.032258 5929 0.012987 7225 0.011765 4 68 97 38 97 9409 0.010309 def get reciprocal num(x): return 1 / x dummy_data['E_R'] = dummy data.head() dummy_data['E'].apply(get_reciprocal_num) 55 97 38 85 97 1681 961 5929 7225 9409 E_2 0.024390 0.032258 0.012987 0.011765 0.010309 E_R 0.024390 0.032258 0.012987 0.011765 0.010309 The above function can be written as a lambda function: dummy_data['E_R'] = dummy_data['E'].apply(lambda x: 1/x) dummy data.head()
A B C D E E1 E_2 E_R 0 8 99 77 40 41 1681 0.024390 0.024390 1 87 24 29 80 31 961 0.032258 0.032258 2 63 53 43 63 77 5929 0.012987 0.012987 3 96 79 78 78 85 7225 0.011765 0.011765 Functions can be applied: 1. Column wise (all rows of a single column go as inputs). Use axis=0 2. Row wise (all columns of a single row go as inputs). Use axis=1 axis : {@ or 'index', 1 or ‘'columns'}, default © Axis along which the function is applied: * @ or 'index': apply function to each column. * 1 or 'columns': apply function to each row. dummy_data_new = dummy_data.apply(lambda x:np.mean(x), axis = 0) print(type(dummy_data new)) dummy data_ new <class 'pandas.core.series.Series'> A 50.610000 B 51.040000 C 53.950000 D 48.840000 E 47.110000 E_1 3029.870000 E_2 ©.051245 E_R 0.051245 dtype: float64 dummy data new = dummy data.apply(lambda x:np.mean(x), axis=1) print(type(dummy_data_new)) dummy data_new <class 'pandas.core.series.Series'> %) 252.381098 1 151.508065 2 778.503247 3 955.127941 4 1220.502577 95 972.002907 96 30.285714 97 144.758333 98 98.635417 99 342.630000 Length: 100, dtype: float64
v ltertools and counters e jterable e chain e combinations i = iter(range(7)) #iterator(iterable - anything that would work in a for loop: list, dict print(type(i)) <class 'range_iterator'> print(next(i)) 0 print(next(i)) 1 print(next(i)) 2 print(i) <range_iterator object at 0x7e9049d8bde0d> 1 = list(i) print(1l) print(l.pop()) print(1l) [3.’ 4.’ 5.’ 6] 6 [3, 4, 5] v Reminder A combination is a mathematical technique that determines the number of possible arrangements in a collection of items where the order of the selection does not matter. e (7)o print("-- Combinations of 2--")
for combo in itertools.combinations(iter(range(5)), 2): print(combo) print("\n-- Combinations of 3--") for combo in itertools.combinations(iter(range(5)), 3): print(combo) -- Combinations of 2-- (0, 1) (0, 2) (0, 3) (0, 4) (1, 2) (1, 3) (1, 4) (2, 3) (2, 4) (3, 4) -- Combinations of 3-- (e, 1, 2) (e, 1, 3) (0, 1, 4) (e, 2, 3) (e, 2, 4) (0, 3, 4) (1, 2, 3) (1, 2, 4) (1, 3, 4) (2, 3, 4) zero_5 = range(0,6) six_10 = range(6,11) chained_iterator = itertools.chain(zero_5, six_10) for item in chained_iterator: print(item) OVooNOTUVhA, WNERO = o Power set: Contains all possible subsets of a set (plus the empty set and the original set). Example: If S is the set {X, y, z}, then all the subsets of S are:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help

Browse Popular Homework Q&A

Q: If f(x) = x2 + 10 sin x, show that there is a number c such that f(c) = 1000.
Q: 3. Computer chips often contain surface imperfections. For a certain type of computer chips, the…
Q: How do ecological/functionalist models compare with political economy/conflict models of urban…
Q: ) if fcx) f (x) = = f 2 [ x² + x² +5 sin (+) dt
Q: Let P = [P] N. m) Determine the magnitude FA of the reaction at pin A. n) Determine the magnitude FB…
Q: Complete the following cash budget for the company.
Q: Find the critical value(s) of f(x)=8√x−x 2
Q: 2 Find the exact area of the region bounded by the curves y = 4x² - 8x+12 and y=3x²-3x+12. Answer in…
Q: Tharaldson Corporation makes a product with the following standard costs: Standard Quantity or Hours…
Q: Given set B = (-2, 0, 2, 4,..., 28), choose all numbers below that are elements of B. Review…
Q: Evaluate the following integral using integration by parts. Ste A. -221 dt Use the integration by…
Q: Define a function distance that consumes a point (i.e. 2 integers say x1, y1) and displays the…
Q: To investigate whether one-pound bags of potato chips actually contain a full pound of chips, a…
Q: Which among the following principles is not a principle of SOLID class design? SRP, OCP, LSP, ADP,…
Q: The P-value for a hypothesis test is shown. Use the P-value to decide whether to reject. Ho when the…
Q: If y = tan ¹(x³ + 14x), then 2 Əx
Q: "he total sales of S (in thousands of DVD's) of a certain movie are given by the following formula…
Q: A sample of blood pressure measurements is taken for a group of adults, and those values (mm Hg) are…
Q: Fill in the missing product, or reactant in the reactions below. If no reaction will happen, write…
Q: How do I determine the speed of cylinder A in the picture based on the upward speed of cylinder B?
Q: Reaction 1: Both reactants are 2M NaOH (aq) + HCl (aq) → H₂O (1) + NaCl (aq) Reaction 2: Both…
Q: How does the tension in a "real" rope (that is attached to the ceiling holding up a mass) vary from…