1
+ import numpy as np
2
+
3
+ english_score = np .array ([55 ,89 ,76 ,65 ,48 ,70 ])
4
+ math_score = np .array ([60 ,85 ,60 ,68 ,np .nan ,60 ])
5
+ chinese_score = np .array ([65 ,90 ,82 ,72 ,66 ,77 ])
6
+
7
+ #上 3 列共六位同學的英文、數學、國文成績,第一個元素代表第一位同學,舉例第一位同學英文 55 分
8
+ # 、數學 60 分、國文 65 分,今天第五位同學因某原因沒來考試,導致數學成績缺值
9
+ def Tavg ():
10
+ EngAvg = np .average (english_score )
11
+ MathAvg = np .nanmean (math_score )
12
+ ChtAvg = np .average (chinese_score )
13
+ print ('各科成績平均 %s' % EngAvg ,MathAvg ,ChtAvg )
14
+
15
+ def Tmax ():
16
+ EngMax = np .nanmax (english_score )
17
+ MathMax = np .nanmax (math_score )
18
+ ChtMax = np .nanmax (chinese_score )
19
+ print ('各科成績最大值 %s' % EngMax ,MathMax , ChtMax )
20
+
21
+ def Tmin ():
22
+ EngMin = np .nanmin (english_score )
23
+ MathMin = np .nanmin (math_score )
24
+ ChtMin = np .nanmin (chinese_score )
25
+ print ('各科成績最小值 %s' % EngMin ,MathMin , ChtMin )
26
+
27
+ def Tstd ():
28
+ EngStd = np .nanstd (english_score )
29
+ MathStd = np .nanstd (math_score )
30
+ ChtStd = np .nanstd (chinese_score )
31
+ print ('各科成績標準差 %s' % EngStd ,MathStd , ChtStd )
32
+
33
+ print (" English Math Chinese" )
34
+ #1. 請計算各科成績平均、最大值、最小值、標準差,其中數學缺一筆資料可忽略?
35
+ Tavg ()
36
+ Tmax ()
37
+ Tmin ()
38
+ Tstd ()
39
+
40
+ #2. 第五位同學補考數學後成績為 55,請計算補考後數學成績平均、最大值、最小值、標準差?
41
+ math_score [4 ]= 55
42
+ Tavg ()
43
+ Tmax ()
44
+ Tmin ()
45
+ Tstd ()
46
+
47
+ #3. 用補考後資料找出與國文成績相關係數最高的學科?
48
+ E = np .corrcoef (english_score , chinese_score )
49
+ M = np .corrcoef (math_score , chinese_score )
50
+ if np .maximum (E ,M ).all () :
51
+ print ("與國文成績相關係數最高的學科 is English" )
52
+ else :
53
+ print ("與國文成績相關係數最高的學科 is Math" )
0 commit comments