Skip to content

Commit

Permalink
add console output
Browse files Browse the repository at this point in the history
  • Loading branch information
lpm0073 committed Jun 29, 2023
1 parent fd9d869 commit e68a54e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion jupyter-notebook/SVM_Notebook.html
Original file line number Diff line number Diff line change
Expand Up @@ -17794,7 +17794,7 @@ <h4 id="5.1:-Build-a-Support-Vector-Machine-model-using-a-linear-kernel"><strong
<div class="jp-InputPrompt jp-InputArea-prompt">In&nbsp;[42]:</div>
<div class="jp-CodeMirrorEditor jp-Editor jp-InputArea-editor" data-type="inline">
<div class="CodeMirror cm-s-jupyter">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">svm</span> <span class="o">=</span> <span class="n">SVC</span><span class="p">(</span><span class="n">kernel</span><span class="o">=</span><span class="s1">'linear'</span><span class="p">,</span> <span class="n">probability</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="c1"># Linear kernal or linear decision boundary</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">svm</span> <span class="o">=</span> <span class="n">SVC</span><span class="p">(</span><span class="n">kernel</span><span class="o">=</span><span class="s1">'linear'</span><span class="p">,</span> <span class="n">probability</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span> <span class="c1"># Linear Kernel or linear decision boundary</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">svm</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="o">=</span> <span class="n">X_train_scaled</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">y_train</span><span class="p">)</span>
</pre></div>

Expand Down
2 changes: 1 addition & 1 deletion jupyter-notebook/SVM_Notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2820,7 +2820,7 @@
},
"outputs": [],
"source": [
"svm = SVC(kernel='linear', probability=True) # Linear kernal or linear decision boundary\n",
"svm = SVC(kernel='linear', probability=True) # Linear Kernel or linear decision boundary\n",
"model = svm.fit(X= X_train_scaled, y = y_train)\n"
]
},
Expand Down
53 changes: 36 additions & 17 deletions support-vector-machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.preprocessing import MinMaxScaler

# module initializations
sns.set()
HERE = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -59,6 +60,7 @@ def prepare_data():
- split dependent / independent variables
- split training / test data sets
"""
print("Preparing data sets")
original_db = pd.read_csv(os.path.join(HERE, "data", "reservations-db.csv"))

# need to be careful to only work with a **COPY** of the original
Expand Down Expand Up @@ -86,71 +88,88 @@ def prepare_data():
return train_test_split(x, y, test_size=0.30, stratify=y, random_state=1)


def linear_kernal():
def linear_Kernel():
"""
- create training and test data sets
- create a Logistic Regression model
- train the model
- generate confusion matrix and f-score for the training set
- generate confusion matrix and f-score for the test set
"""
print("Linear Kernel")
x_train, x_test, y_train, y_test = prepare_data()

scaling = MinMaxScaler(feature_range=(-1,1)).fit(x_train)
print("- scaling")
scaling = MinMaxScaler(feature_range=(-1, 1)).fit(x_train)
x_train_scaled = scaling.transform(x_train)
x_test_scaled = scaling.transform(x_test)

# Linear kernal or linear decision boundary
svm = SVC(kernel='linear', probability=True)
model = svm.fit(X= x_train_scaled, y = y_train)
# Linear Kernel or linear decision boundary
print("- training")
svm = SVC(kernel="linear", probability=True)
model = svm.fit(X=x_train_scaled, y=y_train)

print("- modeling on training data")
y_pred_train_svm = model.predict(x_train_scaled)
metrics_score(y_train, y_pred_train_svm)

print("- modeling on test data")
y_pred_test_svm = model.predict(x_test_scaled)
metrics_score(y_test, y_pred_test_svm)

# Set the optimal threshold (refer to the Jupyter Notebook to see how we arrived at 42)
optimal_threshold_svm=0.40
optimal_threshold_svm = 0.40

print("- remodeling on training data")
y_pred_train_svm = model.predict_proba(x_train_scaled)
metrics_score(y_train, y_pred_train_svm[:,1]>optimal_threshold_svm)
metrics_score(y_train, y_pred_train_svm[:, 1] > optimal_threshold_svm)

print("- remodeling on test data")
y_pred_test = model.predict_proba(x_test_scaled)
metrics_score(y_test, y_pred_test[:,1]>optimal_threshold_svm)
metrics_score(y_test, y_pred_test[:, 1] > optimal_threshold_svm)


def rbf_kernal():
def rbf_Kernel():
"""
- create training and test data sets
- create a Logistic Regression model
- train the model
- generate confusion matrix and f-score for the training set
- generate confusion matrix and f-score for the test set
"""
print("RBF Kernel")
x_train, x_test, y_train, y_test = prepare_data()

scaling = MinMaxScaler(feature_range=(-1,1)).fit(x_train)
print("- scaling")
scaling = MinMaxScaler(feature_range=(-1, 1)).fit(x_train)
x_train_scaled = scaling.transform(x_train)
x_test_scaled = scaling.transform(x_test)

# Linear kernal or linear decision boundary
svm_rbf=SVC(kernel='rbf',probability=True)
model = svm_rbf.fit(x_train_scaled,y_train)
# Linear Kernel or linear decision boundary
print("- training")
svm_rbf = SVC(kernel="rbf", probability=True)
model = svm_rbf.fit(x_train_scaled, y_train)

print("- modeling on training data")
y_pred_train_svm = model.predict(x_train_scaled)
metrics_score(y_train, y_pred_train_svm)

print("- modeling on test data")
y_pred_test_svm = model.predict(x_test_scaled)
metrics_score(y_test, y_pred_test_svm)

# Set the optimal threshold (refer to the Jupyter Notebook to see how we arrived at 42)
optimal_threshold_svm=0.41
optimal_threshold_svm = 0.41

print("- remodeling on training data")
y_pred_train_svm = model.predict_proba(x_train_scaled)
metrics_score(y_train, y_pred_train_svm[:,1]>optimal_threshold_svm)
metrics_score(y_train, y_pred_train_svm[:, 1] > optimal_threshold_svm)

print("- remodeling on test data")
y_pred_test = model.predict_proba(x_test_scaled)
metrics_score(y_test, y_pred_test[:,1]>optimal_threshold_svm)
metrics_score(y_test, y_pred_test[:, 1] > optimal_threshold_svm)


if __name__ == "__main__":
linear_kernal()
linear_Kernel()
rbf_Kernel()

0 comments on commit e68a54e

Please sign in to comment.