generated from nighthawkcoders/student
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
534f0fa
commit cf5c8ef
Showing
8 changed files
with
1,732 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sklearn.tree import DecisionTreeClassifier, export_text\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", | ||
"import numpy as np\n", | ||
"import pdb\n", | ||
"import ast\n", | ||
"\n", | ||
"# Define the TitanicRegression global variable\n", | ||
"inflation_regression = None\n", | ||
"\n", | ||
"# Define the TitanicRegression class\n", | ||
"class InflationRegression:\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", | ||
" self.initInflation() # Call initTitanic in the constructor\n", | ||
"\n", | ||
"\n", | ||
" def initInflation(self):\n", | ||
" cpi_data = pd.read_csv('files/us_cpi.csv')\n", | ||
" self.td = cpi_data\n", | ||
" self.td.dropna(inplace=True) # drop rows with at least one missing value, after dropping unuseful columns\n", | ||
"\n", | ||
" # Encode categorical variables\n", | ||
" self.encoder = OneHotEncoder(handle_unknown='ignore')\n", | ||
" self.encoder.fit(self.td[['embarked']])\n", | ||
" self.onehot = self.encoder.transform(self.td[['embarked']]).toarray()\n", | ||
" cols = ['embarked_' + val for val in self.encoder.categories_[0]]\n", | ||
" self.td[cols] = pd.DataFrame(self.onehot)\n", | ||
" self.td.dropna(inplace=True)\n", | ||
" print(self.td)\n", | ||
" # clean data\n", | ||
" \n", | ||
"\n", | ||
"\n", | ||
"\n", | ||
" def runDecisionTree(self):\n", | ||
" X = self.td.drop('survived', axis=1) # all except 'survived'\n", | ||
" y = self.td['survived'] # only 'survived'\n", | ||
" self.X_train, X_test, self.y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n", | ||
" dt = DecisionTreeClassifier()\n", | ||
" dt.fit(self.X_train, self.y_train)\n", | ||
" self.dt = dt\n", | ||
" print(export_text(dt, feature_names=X.columns.tolist()))\n", | ||
"\n", | ||
" # more code here\n", | ||
"\n", | ||
" def runLogisticRegression(self, X, y):\n", | ||
" # more code here\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", | ||
" self.logreg = LogisticRegression()\n", | ||
" self.logreg.fit(self.X_train, self.y_train)\n", | ||
" \n", | ||
" def predictSurvival(self, passenger):\n", | ||
" X = self.td.drop('survived', axis=1) # all except 'survived'\n", | ||
" y = self.td['survived'] # only 'survived'\n", | ||
" self.X_train, X_test, self.y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n", | ||
" \n", | ||
" self.logreg = LogisticRegression()\n", | ||
" self.logreg.fit(self.X_train, self.y_train)\n", | ||
" \n", | ||
" passenger = list(passenger.values())\n", | ||
" \n", | ||
" passenger = pd.DataFrame({\n", | ||
" 'name': [passenger[0]],\n", | ||
" 'pclass': [passenger[1]],\n", | ||
" 'sex': [passenger[2]],\n", | ||
" 'age': [passenger[3]],\n", | ||
" 'sibsp': [passenger[4]],\n", | ||
" 'parch': [passenger[5]],\n", | ||
" 'fare': [passenger[6]],\n", | ||
" 'embarked': [passenger[7]],\n", | ||
" 'alone': [passenger[8]]\n", | ||
" })\n", | ||
" \n", | ||
" passenger['sex'] = passenger['sex'].apply(lambda x: 1 if x == 'male' else 0)\n", | ||
" passenger['alone'] = passenger['alone'].apply(lambda x: 1 if x == True else 0)\n", | ||
" onehot = self.encoder.transform(passenger[['embarked']])\n", | ||
" cols = ['embarked_' + val for val in self.encoder.categories_[0]]\n", | ||
" print(passenger)\n", | ||
" passenger[cols] = pd.DataFrame(onehot.toarray(), index=passenger.index)\n", | ||
" passenger.drop(['name'], axis=1, inplace=True)\n", | ||
" passenger.drop(['embarked'], axis=1, inplace=True)\n", | ||
" \n", | ||
" print(passenger)\n", | ||
" # passenger_list = list(passenger[\"passenger\"].values())\n", | ||
"\n", | ||
" # passenger = np.asarray(passenger_list).reshape(1, -1)\n", | ||
" # #preprocessing\n", | ||
" \n", | ||
" aliveProb = np.squeeze(self.logreg.predict_proba(passenger))\n", | ||
" print(aliveProb)\n", | ||
" aliveProb.tolist()\n", | ||
" deathProb = aliveProb[0]\n", | ||
" aliveProb = aliveProb[1]\n", | ||
" \n", | ||
" return 'Survival probability: {:.2%}'.format(aliveProb),('Death probability: {:.2%}'.format(deathProb)) \n", | ||
"\n", | ||
"\n", | ||
"def initInflation():\n", | ||
" global titanic_regression\n", | ||
" titanic_regression = TitanicRegression()\n", | ||
" titanic_regression.initTitanic()\n", | ||
" X = titanic_regression.td.drop('survived', axis=1)\n", | ||
" y = titanic_regression.td['survived']\n", | ||
" titanic_regression.runLogisticRegression(X, y) #s # Pass X and y to runLogisticRegression\n", | ||
"\n", | ||
"\n", | ||
"# From API\n", | ||
"\n", | ||
"# Sample usage without API" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.