model = pd.DataFrame(zip(features, coefficients)) print model X_train[0,:] # first training observation lr.predict_proba(X_train[0,:]) # what is the predicted probability? lr.predict(X_train[0,:]) # what is the predicted class? X_train[13,:] # 13th training observation lr.predict_proba(X_train[13,:]) # what is the predicted probability? lr.predict(X_train[13,:]) # what is the predicted class? from sklearn.metrics import accuracy_score from sklearn.metrics import confusion_matrix y_pred = lr.predict(X_test) # apply the model to the test set accuracy_score(y_test, y_pred) # calculate the accuracy score y_pred2 = lr.predict(X_train) accuracy_score(y_train, y_pred2) # plot import sys sys.path.append("C:/Users/Craig/Documents/GitHub/Python-Machine-Learning") from functions_module import plot_decision_regions plot_decision_regions(X_train, y_train, lr) plt.figure(figsize=(20,15))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0) from sklearn.ensemble import RandomForestClassifier forest = RandomForestClassifier(criterion='entropy', n_estimators=10, random_state=1, n_jobs=2) forest.fit(X_train, y_train) # Plot decision regions of model import numpy as np import matplotlib.pyplot as plt X_combined = np.vstack((X_train, X_test)) y_combined = np.hstack((y_train, y_test)) import sys sys.path.append("~/Documents/Practice/Python/Repos/Python-Machine-Learning") from functions_module import plot_decision_regions plot_decision_regions(X_combined, y_combined, classifier=forest, test_idx=range(105, 150)) plt.xlabel('petal length') plt.ylabel('petal width') plt.legend(loc='upper left') plt.show()
# train SVM from sklearn.svm import SVC svm = SVC(kernel = 'linear', C = 1.0, random_state = 0) svm.fit(X_train_std, y_train) # plot decision regions of SVM import sys import numpy as np sys.path.append("~/Documents/Practice/Repos/Python-Machine-Learning") from functions_module import plot_decision_regions X_combined_std = np.vstack((X_train_std, X_test_std)) y_combined = np.hstack((y_train, y_test)) plot_decision_regions(X_combined_std, y_combined, classifier = svm, test_idx = range(105, 150)) # kernel SVM # create XOR gate sample data import matplotlib.pyplot as plt np.random.seed(0) X_xor = np.random.randn(200, 2) y_xor = np.logical_xor(X_xor[:, 0] > 0, X_xor[:, 1] > 0) y_xor = np.where(y_xor, 1, -1) plt.scatter(X_xor[y_xor == 1, 0], X_xor[y_xor == 1, 1],
# create tree object from sklearn.tree import DecisionTreeClassifier tree = DecisionTreeClassifier(criterion = 'entropy', max_depth = 3, random_state = 0) # fit tree on unscaled training data tree.fit(X_train, y_train) X_combined = np.vstack((X_train, X_test)) y_combined = np.hstack((y_train, y_test)) import sys sys.path.append("C:/Users/Craig/Documents/GitHub/Python-Machine-Learning") from functions_module import plot_decision_regions plot_decision_regions(X_combined, y_combined, classifier = tree, test_idx = range(105, 150)) plt.xlabel('petal length [cm]') plt.ylabel('petal width [cm]') plt.legend(loc = 'upper left') plt.show() # create tree diagram for graphviz from sklearn.tree import export_graphviz export_graphviz(tree, out_file = '~/Documents/Practice/Python/Python-Machine-Learning/tree.dot', feature_names = ['petal length', 'petal width'])
svm = SVC(kernel='linear', C=1.0, random_state=0) svm.fit(X_train_std, y_train) # plot decision regions of SVM import sys import numpy as np sys.path.append("~/Documents/Practice/Repos/Python-Machine-Learning") from functions_module import plot_decision_regions X_combined_std = np.vstack((X_train_std, X_test_std)) y_combined = np.hstack((y_train, y_test)) plot_decision_regions(X_combined_std, y_combined, classifier=svm, test_idx=range(105, 150)) # kernel SVM # create XOR gate sample data import matplotlib.pyplot as plt np.random.seed(0) X_xor = np.random.randn(200, 2) y_xor = np.logical_xor(X_xor[:, 0] > 0, X_xor[:, 1] > 0) y_xor = np.where(y_xor, 1, -1) plt.scatter(X_xor[y_xor == 1, 0], X_xor[y_xor == 1, 1], c='b',