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
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
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:
{} (also denoted as &, the empty set or the null set) {x} {y} {z} {x, y} {x, z} {y. z} {xv 2} and hence the power set of Sis {{}, {x}, {y}, {z}, {x, y}. {x, z}, {y. z}. {x, . Z}}. Taken from Wikipedia. Get the power set of a list. You can assume that it will fit in memory. s = np.arange(1,4) #1, 2, 3 power_list = [] for r in range(len(s)+1): #0 , 1, 2, 3 sub_set = list(itertools.combinations(s, r)) power_list += sub_set power_list [()) (1))) (2))1 (3J)J (1J 2).’ (1, 3)J (ZJ 3)) (1J 2) 3)] powerset = [] for r in range(5): subset_r = itertools.combinations("abcd", r) powerset += subset r powerset [O), ("a’,), ('b',), ("c'y), ('d",), ("a’, 'b"), (‘a', 'c'), (‘a', 'd'), ('b*, 'c'), ('b, 'd"), ("c’, 'd'), ('a'J Ibl: lcl), ('a'J Ibl, ldl)) ('a'J Icl, 'dl)} ('b'J Icl, ldl)) (‘a', 'b', 'c', 'd")] powerset = list(itertools.chain.from iterable(itertools.combinations("abcd", r) for r in r powerset
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
NN TN NN TN NN TN NN NN NN - O T W 9N TOTOC W OV W - - - . - - - o - - d’)] v Basic classification using sklearn dummy data["target"] = np.random dummy_data.head() X = dummy_data[["A","B 55 97 38 y = dummy_data[ 'target'] ) = -+ Il clf clf.Fit(X, y) from sklearn import tree tree.plot _tree(clf,class names=True,fontsize=7) 1681 961 5929 7225 9409 .randint(9,2,size=(100,)) E_2 0.024390 0.032258 0.012987 0.011765 0.010309 ","C","D"]] DecisionTreeClassifier(max_depth=3) E_R target 0.024390 0 0.032258 0 0.012987 1 0.011765 1 0.010309 1
[Text(©.375, 0.875, Text(0.25, 0.625, Text(0.5, 0.625, Text(0.25, 0.375, 'x[@] <= 10.5\ngini 'X[0] <= 4.5\ngini 'X[3] <= 6.5\ngini = 0.497\nsamples = 100\nvalue = ‘gini = 0.0\nsamples = 6\nvalue = [6, = 0.489\nsamples = 94\nvalue = = 0.198\nsamples = 9\nvalue = [1, 8]\nclass = ) [46, 54]\ncla: @]\nclass = y[@]"), [409, 54]\nclass Text(©.125, 0.125, 'gini = @.0\nsamples = 1\nvalue = [1, ©@]\nclass = y[@]"), Text(0.375, 0.125, 'gini = @.0\nsamples = 8\nvalue = [0, 8]\nclass = y[1]"'), Text(0.75, 0.375, 'x[2] <= 16.5\ngini = 0.497\nsamples = 85\nvalue = [39, 46]\nclas: Text(0.625, 0.125, 'gini = @.32\nsamples = 10\nvalue = [8, 2]\nclass = y[@]"'), Text(0.875, 0.125, 'gini = ©.485\nsamples = 75\nvalue = [31, 44]\nclass = y[1]')] x[3] <=65 gini = 0.497 samples = 100 value = [46, 54] class = y[1] T x[0] <= 105 sagrlr?;l)leso-=06 gini = 0.489 value = [6, 0] e TAD. 4] R value = [40, class = y[0] class = y[1] x[0] <= 4.5 x[2] <= 165 gini = 0.198 gini = 0.497 samples =9 samples = 85 value = [1, 8] value = [39, 46] class = yI1] class = yill y pred = clf.predict(X) print(y_pred) [111111111111019©01111111111111110111001 119010106061 111111111111111001111011111 1111010111111111111110111] print(accuracy score(y, y_pred)) 0.67 from sklearn.datasets import load iris from sklearn import tree iris = load _iris() print(type(iris)) df = pd.DataFrame(data=np.c_[iris['data’], iris['target’']], columns=iris['feature_names’] df.head() #Goal is to predict which class of iris the flower belongs to: # 0: Iris Setosa # 1: Iris Versicolour # 2: Iris Virginica
<class 'sklearn.utils. bunch.Bunch'> sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target lwget -0 iris.jpg https://upload.wikimedia.org/wikipedia/commons/4/49/Iris germanica_ %28Pu from IPython.display import Image Image('iris.jpg', height=600) --2023-09-10 08:07:58-- https://upload.wikimedia.org/wikipedia/commons/4/49/Iris_gei Resolving upload.wikimedia.org (upload.wikimedia.org)... 208.80.154.240, 2620:0:861:¢ Connecting to upload.wikimedia.org (upload.wikimedia.org)|208.80.154.240]|:443... cont HTTP request sent, awaiting response... 200 OK Length: 4481846 (4.3M) [image/jpeg] Saving to: ‘iris.jpg’ iris.jpg 100%[ ===================>] 4,27M 22.5MB/s in 0.2s 2023-09-10 08:07:58 (22.5 MB/s) - ‘iris.jpg’ saved [4481846/4481846] lwget -0 iris _len wid.jpg https://www.integratedots.com/wp-content/uploads/2019/06/iris pe from IPython.display import Image
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
Image('iris_len_wid.jpg', height=400) --2023-09-10 08:08:12-- https://www.integratedots.com/wp-content/uploads/2019/06/ir: Resolving www.integratedots.com (www.integratedots.com)... 50.87.253.62 Connecting to www.integratedots.com (www.integratedots.com)|50.87.253.62]|:443... coni HTTP request sent, awaiting response... 200 OK Length: 286232 (280K) [image/png] Saving to: ‘iris_len_wid.jpg’ iris_len_wid.jpg 100%[ ===================>] 279.52K 1.22MB/s in 0.2s 2023-09-10 ©8:08:12 (1.22 MB/s) - ‘iris len wid.jpg’ saved [286232/286232] lwget -0 iris class.jpg https://i.imgur.com/PQqYGaW.png from IPython.display import Image Image('iris_class.jpg', height=400)
--2023-09-10 08:08:34-- https://i.imgur.com/PQgYGaW.png Resolving i.imgur.com (i.imgur.com)... 199.232.32.193 Connecting to i.imgur.com (i.imgur.com)|199.232.32.193|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 627819 (613K) [image/png] Saving to: €‘iris class.jpg’ iris _class.jpg 100%[ ===================>] 613.10K --.-KB/s in 0.04s 2023-09-10 08:08:34 (13.7 MB/s) - ‘iris_class.jpg’ saved [627819/627819] iris setosa import matplotlib.pyplot as plt clf = tree.DecisionTreeClassifier() clf = clf.fit(iris.data, iris.target) fig, ax = plt.subplots(figsize=(14, 14)) tree.plot_tree(clf, fontsize=10, feature names=iris['feature names'],class names=True) plt.show()
petal width (cm) <= 0.8 gini = 0.667 samples = 150 value = [50, 50, 50] class = y[0] gini=0,0 ||Petalwidth en samples = 50 sar?wlglle; : value = [50, 0, 0] value = [0 class = y[0] e petal length (cm) <= 4.95 aini = 0.168 print(tree.export_text(clf, feature_names=iris['feature_names'])) --- petal width (cm) <= 0.80 |--- class: © --- petal width (cm) > ©.80 | --- petal width (cm) <= 1.75 | | --- petal length (cm) <= 4.95 | | | --- petal width (cm) <= 1.65 | | | |--- class: 1 | | | --- petal width (cm) > 1.65 | | | |--- class: 2 | | --- petal length (cm) > 4.95 | | | --- petal width (cm) <= 1.55 | | | |--- class: 2 | | | --- petal width (cm) > 1.55 | | | | --- petal length (cm) <= 5.45 | | | | |--- class: 1 | | | | --- petal length (cm) > 5.45 | | | | |--- class: 2 | --- petal width (ecm) > 1.75 | | --- petal length (cm) <= 4.85 | | | --- sepal length (cm) <= 5.95 | | | |--- class: 1 | | | --- sepal length (cm) > 5.95 | | | |--- class: 2 | | --- petal length (cm) > 4.85 | | |--- class: 2 gini = 0.0 C samples = 2 sa value = [0, 2, 0] valu clace = vill cl
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…