In PYTHON, I am trying to debug this error for a while now. Can someone help with debugging this? I got this code from this
In PYTHON, I am trying to debug this error for a while now. Can someone help with debugging this? I got this code from this website http://www.semspirit.com/artificial-intelligence/machine-learning/classification/classifier-evaluation/classifier-evaluation-with-cap-curve-in-python/
import matplotlib.pyplot as plt from scipy import integrate #calculus integration
def capcurve(y_values, y_preds_proba): num_pos_obs = np.sum(y_values) num_count = len(y_values) rate_pos_obs = float(num_pos_obs) / float(num_count) ideal = pd.DataFrame({'x':[0,rate_pos_obs,1],'y':[0,1,1]}) xx = np.arange(num_count) / float(num_count - 1)
y_cap = np.c_[y_values,y_preds_proba] y_cap_df_s = pd.DataFrame(data=y_cap) y_cap_df_s = y_cap_df_s.sort_values([1], ascending=False).reset_index('index', drop=True) print(y_cap_df_s.head(20))
yy = np.cumsum(y_cap_df_s[0]) / float(num_pos_obs) yy = np.append([0], yy[0:num_count-1]) #add the first curve point (0,0) : for xx=0 we have yy=0
percent = 0.5 row_index = np.trunc(num_count * percent)
val_y1 = yy[row_index] val_y2 = yy[row_index+1] if val_y1 == val_y2: val = val_y1*1.0 else: val_x1 = xx[row_index] val_x2 = xx[row_index+1] val = val_y1 + ((val_x2 - percent)/(val_x2 - val_x1))*(val_y2 - val_y1)
sigma_ideal = 1 * xx[num_pos_obs - 1 ] / 2 + (xx[num_count - 1] - xx[num_pos_obs]) * 1 sigma_model = integrate.simps(yy,xx) sigma_random = integrate.simps(xx,xx)
ar_value = (sigma_model - sigma_random) / (sigma_ideal - sigma_random) #ar_label = 'ar value = %s' % ar_value
fig, ax = plt.subplots(nrows = 1, ncols = 1) ax.plot(ideal['x'],ideal['y'], color='grey', label='Perfect Model') ax.plot(xx,yy, color='red', label='User Model') #ax.scatter(xx,yy, color='red') ax.plot(xx,xx, color='blue', label='Random Model') ax.plot([percent, percent], [0.0, val], color='green', linestyle='--', linewidth=1) ax.plot([0, percent], [val, val], color='green', linestyle='--', linewidth=1, label=str(val*100)+'% of positive obs at '+str(percent*100)+'%')
plt.xlim(0, 1.02) plt.ylim(0, 1.25) plt.title("CAP Curve - a_r value ="+str(ar_value)) plt.xlabel('% of the data') plt.ylabel('% of positive obs') plt.legend() plt.show()
--------------------------------------------------------------------------------
y_probablity =classifier.predict_proba(X_test) capcurve(y_test, y_probablity[:,1])
----------------------------------------------------------------------------------
KeyError Traceback (most recent call last)in () 1 y_probablity =classifier.predict_proba(X_test) ----> 2 capcurve(y_test, y_probablity[:,1]) in capcurve(y_values, y_preds_proba) 11 y_cap = np.c_[y_values,y_preds_proba] 12 y_cap_df_s = pd.DataFrame(data=y_cap) ---> 13 y_cap_df_s = y_cap_df_s.sort_values([1], ascending=False).reset_index('index', drop=True) 14 15 print(y_cap_df_s.head(20)) C:\ANACONDA\lib\site-packages\pandas\core\frame.py in reset_index(self, level, drop, inplace, col_level, col_fill) 4096 if not isinstance(level, (tuple, list)): 4097 level = [level] -> 4098 level = [self.index._get_level_number(lev) for lev in level] 4099 if isinstance(self.index, MultiIndex): 4100 if len(level) < self.index.nlevels: C:\ANACONDA\lib\site-packages\pandas\core\frame.py in (.0) 4096 if not isinstance(level, (tuple, list)): 4097 level = [level] -> 4098 level = [self.index._get_level_number(lev) for lev in level] 4099 if isinstance(self.index, MultiIndex): 4100 if len(level) < self.index.nlevels: C:\ANACONDA\lib\site-packages\pandas\core\indexes\base.py in _get_level_number(self, level) 1944 1945 def _get_level_number(self, level): -> 1946 self._validate_index_level(level) 1947 return 0 1948 C:\ANACONDA\lib\site-packages\pandas\core\indexes\base.py in _validate_index_level(self, level) 1941 elif level != self.name: 1942 raise KeyError('Level %s must be same as name (%s)' % -> 1943 (level, self.name)) 1944 1945 def _get_level_number(self, level): KeyError: 'Level index must be same as name (None)'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started