def decision(): X, y = make_moons(noise=0.3) X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = tts(X, y, test_size=0.20) oz = DecisionViz(KNeighborsClassifier(3), ax=newfig()) oz.fit(X_train, y_train) oz.draw(X_test, y_test) savefig(oz, "decision_boundaries")
def decision_boundary(model, features, classes, X_train, Y_train, X_test, Y_test): from yellowbrick.contrib.classifier import DecisionViz features = ['name_sim', 'add_sim'] viz = DecisionViz(model, title="random forest", features=features, classes=classes) viz.fit(X_train, Y_train) viz.draw(X_test, Y_test) viz.poof()
def draw_boundaries(): data = datasets.load_iris().data[:, : 2] # we only take the first two features. label = np.array(datasets.load_iris().target, dtype=int) # Take classes names data = StandardScaler().fit_transform(data) # Rescale data viz = DecisionViz( GaussianNB(), title="Gaussian", features=['Sepal Length', 'Sepal Width'], classes=['A', 'B', 'C'] # Determine dimension and no. of classes ) viz.fit(data, label) # Train data to draw viz.draw(data, label) # Draw Data viz.show()
n_clusters_per_class=1) rng = np.random.RandomState(2) X += 2 * rng.uniform(size=X.shape) linearly_separable = (X, y) data_set = make_moons(noise=0.3, random_state=0) X, y = data_set X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.4, random_state=42) viz = DecisionViz(KNeighborsClassifier(3), title="Nearest Neighbors", features=['Feature One', 'Feature Two'], classes=['A', 'B']) viz.fit(X_train, y_train) viz.draw(X_test, y_test) viz.poof(outpath="images/knn_decisionviz.png") viz = DecisionViz(SVC(kernel="linear", C=0.025), title="Linear SVM", features=['Feature One', 'Feature Two'], classes=['A', 'B']) viz.fit(X_train, y_train) viz.draw(X_test, y_test) viz.poof(outpath="images/svc_decisionviz.png")
def execute_classification_code(code, session): global df, model, problem_class, order code_str = urllib.parse.unquote(code) code_arr = code_str.split("\n") print(code_arr) problem_class = code_arr[0] print(problem_class) order = code_arr[1] print(order) exec(code_arr[2]) print(df) exec(code_arr[3], globals()) cmap_pink_green = sns.diverging_palette(352, 136, s=96, l=51, n=7) viz = ClassificationReport(model, cmap=cmap_pink_green) viz.fit(X_train, y_train) viz.score(X_test, y_test) viz.poof(outpath="./plots/classificationmatrix" + session + ".png") image_path_class = "classificationmatrix" plt.clf() plt.cla() plt.close() le = LabelEncoder() dec_viz = DecisionViz(model, title="Decision Boundaries", features=np.where(cols == True)[0].tolist(), classes=list(map(str, y.iloc[:, 0].unique())).sort()) dec_viz.fit(X_train.to_numpy(), le.fit_transform(y_train)) dec_viz.draw(X_test.to_numpy(), le.fit_transform(y_test)) dec_viz.poof(outpath="./plots/decviz" + session + ".png") image_path_dec = "decviz" plt.clf() plt.cla() plt.close() print(list(map(str, y.iloc[:, 0].unique()))) cmap_salmon_dijon = sns.diverging_palette(28, 65, s=98, l=78, n=7) cm = ConfusionMatrix(model, classes=list(map(str, y.iloc[:, 0].unique())).sort(), cmap=cmap_salmon_dijon) cm.fit(X_train, y_train) cm.score(X_test, y_test) plt.tight_layout() cm.poof(outpath="./plots/cm" + session + ".png") image_path_cm = "cm" plt.clf() plt.cla() plt.close() model.fit(X_train, y_train) file = 'pickled_models/trained_model' + session + '.sav' pickle_path = 'trained_model' pickle.dump(model, open(file, 'wb')) return jsonify(image_path_class, image_path_dec, image_path_cm, pickle_path)