Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jm1021 committed Mar 8, 2024
1 parent 667eee6 commit 675ed23
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 34 deletions.
64 changes: 54 additions & 10 deletions _notebooks/2024-03-05-DS-java-data_types.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"execution_count": 1,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Main method before changeInt(n): n = 5\n",
"In changeInt method\n",
"\tBefore n += 10: n = 5\n",
"\tAfter n += 10: n = 15\n",
"Main method after changeInt(n): n = 5\n"
]
}
],
"source": [
"public class IntByValue {\n",
" \n",
Expand Down Expand Up @@ -180,7 +196,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"public class IntegerByValueOrReference {\n",
Expand Down Expand Up @@ -230,7 +250,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"import java.util.concurrent.atomic.AtomicInteger;\n",
Expand Down Expand Up @@ -280,9 +304,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"execution_count": 2,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Before: 21 16\n",
"After: 16 21\n",
"\n",
"Before: 16 21\n",
"After: 16 21\n",
"\n",
"Before: 16 -1\n",
"After: -1 16\n",
"\n"
]
}
],
"source": [
"public class IntByReference { // this is a class that wraps an int value\n",
" private int value; // this is the wrapped value\n",
Expand Down Expand Up @@ -348,9 +392,9 @@
"codemirror_mode": "java",
"file_extension": ".jshell",
"mimetype": "text/x-java-source",
"name": "java",
"name": "Java",
"pygments_lexer": "java",
"version": "17.0.5+0"
"version": "17.0.9+0"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
149 changes: 149 additions & 0 deletions _notebooks/2024-03-05-DS-python-pandas-df_titanic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,155 @@
"for feature, importance in zip(new_passenger.columns, importances):\n",
" print(f'The importance of {feature} is: {importance}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The code above needs to be reformed into Model (Python Class), View (JavaScript), Control (Python API)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Decision Tree Classifier Accuracy: 100.00%\n",
"Logistic Regression Accuracy: 100.00%\n"
]
}
],
"source": [
"## Python Titanic Model\n",
"\n",
"# Import the required libraries\n",
"from sklearn.tree import DecisionTreeClassifier\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.metrics import accuracy_score\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import OneHotEncoder\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"\n",
"# Define the TitanicRegression global variable\n",
"titanic_regression = None\n",
"\n",
"# Define the TitanicRegression class\n",
"class TitanicRegression:\n",
" def __init__(self):\n",
" self.dt = None\n",
" self.logreg = None\n",
" self.X_train = None\n",
" self.X_test = None\n",
" self.y_train = None\n",
" self.y_test = None\n",
" self.encoder = None\n",
"\n",
" def initTitanic(self):\n",
" titanic_data = sns.load_dataset('titanic')\n",
" X = titanic_data.drop('survived', axis=1)\n",
" y = titanic_data['survived']\n",
" self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
"\n",
" # Initialize the encoder\n",
" self.encoder = OneHotEncoder(handle_unknown='ignore')\n",
" self.X_train = self.encoder.fit_transform(self.X_train)\n",
" self.X_test = self.encoder.transform(self.X_test)\n",
"\n",
" self.dt = DecisionTreeClassifier()\n",
" self.dt.fit(self.X_train, self.y_train)\n",
"\n",
" self.logreg = LogisticRegression()\n",
" self.logreg.fit(self.X_train, self.y_train)\n",
"\n",
" def runDecisionTree(self):\n",
" if self.dt is None:\n",
" print(\"Decision Tree model is not initialized. Please run initTitanic() first.\")\n",
" return\n",
" y_pred_dt = self.dt.predict(self.X_test)\n",
" accuracy_dt = accuracy_score(self.y_test, y_pred_dt)\n",
" print('Decision Tree Classifier Accuracy: {:.2%}'.format(accuracy_dt))\n",
"\n",
" def runLogisticRegression(self):\n",
" if self.logreg is None:\n",
" print(\"Logistic Regression model is not initialized. Please run initTitanic() first.\")\n",
" return\n",
" y_pred_logreg = self.logreg.predict(self.X_test)\n",
" accuracy_logreg = accuracy_score(self.y_test, y_pred_logreg)\n",
" print('Logistic Regression Accuracy: {:.2%}'.format(accuracy_logreg))\n",
"\n",
"def initTitanic():\n",
" global titanic_regression\n",
" titanic_regression = TitanicRegression()\n",
" titanic_regression.initTitanic()\n",
" titanic_regression.runDecisionTree()\n",
" titanic_regression.runLogisticRegression()\n",
"\n",
"def predictSurvival(passenger):\n",
" passenger_df = pd.DataFrame(passenger, index=[0]) \n",
" passenger_df.drop(['name'], axis=1, inplace=True)\n",
" passenger = passenger_df.copy()\n",
"\n",
" # Add missing columns and fill them with default values\n",
" missing_cols = set(titanic_regression.X_train.columns) - set(passenger.columns)\n",
" for col in missing_cols:\n",
" passenger[col] = 0\n",
"\n",
" # Ensure the order of column in the passenger matches the order in the training data\n",
" passenger = passenger[titanic_regression.X_train.columns]\n",
"\n",
" # Preprocess the passenger data\n",
" passenger = titanic_regression.encoder.transform(passenger)\n",
"\n",
" predict = titanic_regression.logreg.predict(passenger)\n",
" return predict\n",
"\n",
"\n",
"# Sample usage\n",
"if __name__ == \"__main__\":\n",
" # Initialize the Titanic model\n",
" initTitanic()\n",
"\n",
" # Predict the survival of a passenger\n",
" passenger = {\n",
" 'name': ['John Mortensen'],\n",
" 'pclass': [2],\n",
" 'sex': ['male'],\n",
" 'age': [64],\n",
" 'sibsp': [1],\n",
" 'parch': [1],\n",
" 'fare': [16.00],\n",
" 'embarked': ['S'],\n",
" 'alone': [False]\n",
" }\n",
" print(predictSurvival(passenger))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Python Titanic API endpoint\n",
"from flask import request, jsonify\n",
"\n",
"# Define the API endpoint for prediction\n",
"@app.route('/api/predict', methods=['POST'])\n",
"def predict():\n",
" # Get the passenger data from the request\n",
" passenger = request.get_json()\n",
"\n",
" response = predictSurvival(passenger)\n",
"\n",
" # Return the response as JSON\n",
" return jsonify(response)\n",
"\n"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 675ed23

Please sign in to comment.