7
7
import matplotlib .pyplot as plt
8
8
import math
9
9
10
- class MeltingPointClusterAnalyzer ():
11
10
11
+ class MeltingPointClusterAnalyzer :
12
12
def _get_clusters (self , points ):
13
13
clustering = AgglomerativeClustering (n_clusters = 2 ).fit (points )
14
14
cluster1 = points [np .argwhere (clustering .labels_ == 1 ).squeeze ()].T
15
15
cluster2 = points [np .argwhere (clustering .labels_ == 0 ).squeeze ()].T
16
16
return cluster1 , cluster2
17
17
18
- def plot_vol_vs_temp (self , ts , vs , plot_title = None ):
18
+ def plot_vol_vs_temp (self , ts , vs , plot_title = None ):
19
19
points = np .array (list (zip (ts , vs )))
20
20
cluster1 , cluster2 = self ._get_clusters (points )
21
21
plt .scatter (* cluster1 )
22
22
plt .scatter (* cluster2 )
23
23
plt .xlabel ("Temperature (K)" )
24
24
plt .ylabel ("Volume (A^3)" )
25
25
Tm = self .estimate_melting_temp (ts , vs )
26
- plt .plot ([Tm , Tm ], [min (vs ), max (vs )], color = 'r' )
27
-
26
+ plt .plot ([Tm , Tm ], [min (vs ), max (vs )], color = "r" )
27
+
28
28
if plot_title is None :
29
29
plt .title ("Volume vs Temperature by Clustering" )
30
30
else :
31
31
plt .title (plot_title )
32
-
32
+
33
33
def estimate_melting_temp (self , temps , vols ):
34
34
points = np .array (list (zip (temps , vols )))
35
35
cluster1 , cluster2 = self ._get_clusters (points )
@@ -42,8 +42,8 @@ def estimate_melting_temp(self, temps, vols):
42
42
43
43
return np .mean ([max (solid_range ), min (liquid_range )])
44
44
45
- class MeltingPointSlopeAnalyzer ():
46
45
46
+ class MeltingPointSlopeAnalyzer :
47
47
def split_dset (self , pts , split_idx ):
48
48
return pts [0 :split_idx ], pts [split_idx :]
49
49
@@ -56,7 +56,7 @@ def assess_splits(self, xs, ys):
56
56
for idx in pt_idxs :
57
57
_ , _ , _ , _ , total_err = self .get_split_fit (xs , ys , idx )
58
58
errs .append (total_err )
59
-
59
+
60
60
return list (zip (pt_idxs , errs ))
61
61
62
62
def get_linear_ys (self , m , b , xs ):
@@ -79,56 +79,61 @@ def plot_split(self, xs, ys, split_idx):
79
79
80
80
plt .scatter (rightxs , rightys )
81
81
plt .plot (rightxs , right_fit_ys )
82
-
82
+
83
83
def get_best_split (self , xs , ys ):
84
84
split_errs = self .assess_splits (xs , ys )
85
85
errs = [pt [1 ] for pt in split_errs ]
86
86
idxs = [pt [0 ] for pt in split_errs ]
87
87
best_split_idx = idxs [np .argmin (errs )]
88
88
return best_split_idx
89
-
89
+
90
90
def plot_vol_vs_temp (self , temps , vols ):
91
91
split_idx = self .get_best_split (temps , vols )
92
92
self .plot_split (temps , vols , split_idx )
93
93
Tm = self .estimate_melting_temp (temps , vols )
94
94
print (Tm )
95
- plt .plot ([Tm , Tm ], [min (vols ), max (vols )], color = 'r' )
96
-
95
+ plt .plot ([Tm , Tm ], [min (vols ), max (vols )], color = "r" )
97
96
98
97
def estimate_melting_temp (self , temps , vols ):
99
98
best_split_idx = self .get_best_split (temps , vols )
100
99
return np .mean ([temps [best_split_idx ], temps [best_split_idx - 1 ]])
101
100
102
- class MeltingPointSlopeRMSEAnalyzer (MeltingPointSlopeAnalyzer ):
103
101
102
+ class MeltingPointSlopeRMSEAnalyzer (MeltingPointSlopeAnalyzer ):
104
103
def get_split_fit (self , xs , ys , split_idx ):
105
104
leftx , rightx = self .split_dset (xs , split_idx )
106
105
lefty , righty = self .split_dset (ys , split_idx )
107
-
106
+
108
107
lslope , lintercept , r_value , p_value , std_err = linregress (leftx , lefty )
109
108
left_y_pred = lintercept + lslope * np .array (leftx )
110
109
lefterr = mean_squared_error (y_true = lefty , y_pred = left_y_pred , squared = False )
111
110
112
111
rslope , rintercept , r_value , p_value , std_err = linregress (rightx , righty )
113
112
right_y_pred = rintercept + rslope * np .array (rightx )
114
113
righterr = mean_squared_error (y_true = righty , y_pred = right_y_pred , squared = False )
115
-
116
- combined_err = math .sqrt (lefterr ** 2 + righterr ** 2 )
114
+
115
+ combined_err = math .sqrt (lefterr ** 2 + righterr ** 2 )
117
116
combined_err = lefterr + righterr
118
117
return lslope , lintercept , rslope , rintercept , combined_err
119
118
120
- class MeltingPointSlopeStdErrAnalyzer (MeltingPointSlopeAnalyzer ):
121
119
120
+ class MeltingPointSlopeStdErrAnalyzer (MeltingPointSlopeAnalyzer ):
122
121
def get_split_fit (self , xs , ys , split_idx ):
123
122
leftx , rightx = self .split_dset (xs , split_idx )
124
123
lefty , righty = self .split_dset (ys , split_idx )
125
-
124
+
126
125
leftfit = linregress (leftx , lefty )
127
126
lefterr = leftfit .stderr
128
-
127
+
129
128
rightfit = linregress (rightx , righty )
130
129
righterr = rightfit .stderr
131
-
132
- combined_err = math .sqrt (lefterr ** 2 + righterr ** 2 )
130
+
131
+ combined_err = math .sqrt (lefterr ** 2 + righterr ** 2 )
133
132
combined_err = lefterr + righterr
134
- return leftfit .slope , leftfit .intercept , rightfit .slope , rightfit .intercept , combined_err
133
+ return (
134
+ leftfit .slope ,
135
+ leftfit .intercept ,
136
+ rightfit .slope ,
137
+ rightfit .intercept ,
138
+ combined_err ,
139
+ )
0 commit comments