11: NumPy

11-7: Dot Method

** ถ้าใช้งานบนมือถือหรือ tablet แนะนำให้ใช้ Chrome หรือ Safari เท่านั้น **

แบบฝึกหัด 11-7 ข้อที่ 1

จงเขียนฟังก์ชัน orthogonal_to_u(vectors, u) ที่รับ vectors เป็นอาเรย์สองมิติ แต่ละแถวแทนเวกเตอร์หนึ่งเวกเตอร์ ส่วน u เป็นอาเรย์มิติเดียวแทนเวกเตอร์ที่มีขนาดเท่ากับจำนวนคอลัมน์ของ vectors ฟังก์ชันนี้คืนอาเรย์สองมิติ แต่ละแถวเก็บเวกเตอร์จาก vectors ที่ตั้งฉากกับ u (ในลำดับก่อนหลังเหมือนที่ปรากฏใน vectors) เช่น orthogonal_to_u(np.array([[1,0,0], [0,1,0], [1,1,1]]), np.array([0,0,-2])) จะได้ np.array([[1,0,0], [0,1,0]]) (ไม่ต้องใช้คำสั่งวงวน)

import numpy as np def orthogonal_to_u(vectors, u): import numpy as np def orthogonal_to_u(vectors, u): dotp = np.dot(vectors, u) return vectors[dotp == 0] import numpy as np q = "`" fname = "orthogonal_to_u" arg1 = 'np.array([[4,2,-2],[-2,-4,2.5],[1,2,3],[16,-2,-3],[2,-1,3]])' arg2 = 'np.array([1,2,4])' func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') func.check_not(has_code(r"(for|while|set|dict)"), msg="ไม่ใช้ for หรือ while นะ") fcall = fname + "(" + arg1 + "," + arg2 + ")" func.check_call(fcall).has_equal_value(incorrect_msg=(q+fname+q) + " ให้ผลผิด")
เวกเตอร์ a กับ b จะตั้งฉากกัน ก็เมื่อ a ⋅ b มีค่าเท่ากับ 0
แบบฝึกหัด 11-7 ข้อที่ 2

จงเขียนฟังก์ชัน to_points(grades) รับ grades เป็นอาเรย์ของเกรดที่นักเรียนคนหนึ่งได้รับในวิชาต่าง ๆ ที่เรียนมา (เกรดเป็นตัวอักษร A, B+, B, C+, C, D+, D และ F) ฟังก์ชันนี้แปลงเกรดเป็นจำนวนที่เทียบเท่ากับเกรดที่ได้ (คืนกลับเป็นผลลัพธ์) โดยเกรดทั้ง 8 ตัวมีค่าเท่ากับ 4, 3.5, 3, 2.5, 2, 1.5, 1 และ 0 ตามลำดับ เช่น to_points(np.array(["A", "B+", "D", "F"])) จะได้ผลเป็น np.array([4.0, 3.5, 1.0, 0.0]) (ไม่ต้องใช้คำสั่งวงวน)
และเขียนอีกฟังก์ชัน gpa(grades, credits) ที่รับ grades (เหมือนกับของ to_points) และ credits ซึ่งเป็นอาเรย์หนึ่งมิติขนาดเดียวกับ grades เก็บจำนวนหน่วยกิตของแต่ละวิชาที่เรียน (โดยวิชาที่มี credits ช่องที่ k ได้รับเกรดตามที่เก็บใน grades ช่องที่ k) ฟังก์ชันนี้คืนค่าเกรดเฉลี่ยคำนวณจากผลการเรียนทุกวิชาที่ให้มา เช่น gpa(np.array(["B+", "B", "D", "C+"]), np.array([3, 3, 3, 2])) ได้ผลเป็น (3.5x3 + 3x3 + 1x3 + 2.5x2)/(3+3+3+2) = 2.5 (ไม่ต้องใช้คำสั่งวงวน)

import numpy as np def to_points(grades): def gpa(grades, credits): import numpy as np def to_points(grades): g = np.array([["A","B+","B","C+","C","D+","D"]]) p = np.array( [ 4, 3.5, 3, 2.5, 2, 1.5, 1 ] ) return p.dot(grades==g.T) # เขียนแบบข้างล่างนี้ก็ได้ # p = (grades == 'A' )*4.0 # p += (grades == 'B+')*3.5 # p += (grades == 'B' )*3.0 # p += (grades == 'C+')*2.5 # p += (grades == 'C' )*2.0 # p += (grades == 'D+')*1.5 # p += (grades == 'D' )*1.0 # return p def gpa(grades, credits): points = to_points(grades) return np.dot(points, credits)/np.sum(credits) import numpy as np q = "`" fname = "to_points" arg1 = "np.array(['F', 'D', 'D+', 'C', 'C+', 'B', 'B+', 'A', 'A', 'D'])" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์หนึ่งตัว') func.check_not(has_code(r"(for|while)"), msg="ไม่ใช้วงวน while หรือ for นะ") fcall = fname + "(" + arg1 + ")" func.check_call(fcall).has_equal_value(incorrect_msg=(q+fname+q) + " ให้ผลผิด") fname = "gpa" arg2 = "np.array([3, 2, 2, 1, 3, 4, 2, 2, 3, 2])" func = Ex().check_function_def(fname, missing_msg="ไม่พบฟังก์ชัน " + (q+fname+q)) func.has_equal_part_len('args', 'ฟังก์ชันนี้รับพารามิเตอร์สองตัว') func.check_not(has_code(r"(for|while)"), msg="ไม่ใช้วงวน while หรือ for นะ") fcall = fname + "(" + arg1 + ", " + arg2 + ")" func.check_call(fcall).has_equal_value(incorrect_msg=(q+fname+q) + " ให้ผลผิด")