あり、これを行うために、よりニシキヘビの方法は、おそらくですが、ここで私が最後に思い付くことができたものです。
(データはこの場合には十分に分離されていない、覚えておいてください、その曲線はシグモイド曲線上0.50ポイントで区切られたクラスを持つ伝統的な外観を持っているdoen't。)
#############################################################################
#### Draws a sigmoid probability plot from prediction results ###############
#############################################################################
import matplotlib.pyplot as plt
import numpy as np
print ('-'*40)
# make the predictions (class) and also get the prediction probabilities
y_train_predict = clf.predict(X_train)
y_train_predictProbas = clf.predict_proba(X_train)
y_train_predictProbas = y_train_predictProbas[:, 1]
y_test_predict = clf.predict(X_test)
y_test_predictProbas = clf.predict_proba(X_test)
y_test_predictProbas = y_test_predictProbas[:, 1]
#Get the thetas from the model
thetas = clf.coef_[0]
intercept = clf.intercept_[0]
print 'thetas='
print thetas
print 'intercept='
print intercept
#Display the predictors and their associated Thetas
for idx, x in enumerate(thetas):
print "Predictor: " + str(labels[idx+1]) + "=" + str(x)
#append intercept to thetas (because scikitlearn doesn't normally output theta0
interceptAndThetas = np.append([intercept],thetas)
X_testWithThetaZero = []
for row in X_test:
X_testWithThetaZero.append(np.append([1],row))
#Calculate the dot product for plotting the sigmoid
dotProductResult = []
for idx, x in enumerate(X_testWithThetaZero):
dotProductResult.append(np.dot(x, interceptAndThetas))
fig, ax1 = plt.subplots()
wrongDotProducts = []
rightDotProducts = []
#Build the plot
for idx in range(0,len(dotProductResult)):
#plot the predicted value on the sigmoid curve
if y_test[idx] == 1:
ax1.scatter(dotProductResult[idx], y_test_predictProbas[idx], c=['green'],linewidths=0.0)
else:
ax1.scatter(dotProductResult[idx], y_test_predictProbas[idx], c=['black'],linewidths=0.0)
#plot the actual
if y_test[idx] == 1:
ax1.scatter(dotProductResult[idx], y_test[idx], c=['green'],linewidths=0.0)
#determine which ones are "wrong" so we can make a histogram
if y_test_predictProbas[idx] < 0.5:
wrongDotProducts.append(dotProductResult[idx])
else:
rightDotProducts.append(dotProductResult[idx])
else:
ax1.scatter(dotProductResult[idx], y_test[idx], c=['black'],linewidths=0.0)
#determine which ones are "wrong" so we can make a histogram
if y_test_predictProbas[idx] > 0.5:
wrongDotProducts.append(dotProductResult[idx])
else:
rightDotProducts.append(dotProductResult[idx])
#plt.xlim([-0.05, numInstances + 0.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('x')
plt.grid(which="major", axis='both',markevery=0.10) # which='major',
plt.ylabel('Prediction Probability')
plt.title('Sigmoid Curve & Histogram of Predictions')
## Add a histogram to show where the correct/incorrect prediction distributions
ax2 = ax1.twinx()
ax2.hist(wrongDotProducts, bins=[-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7], hatch="/", color="black", alpha=0.2)
ax2.hist(rightDotProducts, bins=[-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7], hatch="\\", color="green", alpha=0.2)
ax2.set_ylabel('Histogram Count of Actual Class\n1=Green 0=Black')
ax2.set_xlabel('')
ax2.set_title('')
plt.show()