Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary files and dependencies #136

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3,921 changes: 2,608 additions & 1,313 deletions AI_NLP_CHATBOT.ipynb

Large diffs are not rendered by default.

110 changes: 77 additions & 33 deletions Blockchain-ML medical health .ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"from sklearn.model_selection import train_test_split\n",
"from sklearn.ensemble import RandomForestClassifier, VotingClassifier\n",
"from xgboost import XGBClassifier\n",
"\n",
"os.getcwd()"
]
},
Expand All @@ -61,7 +62,7 @@
"metadata": {},
"outputs": [],
"source": [
"os.chdir('/Users/tahirabbas/Desktop/imp')"
"os.chdir(\"/Users/tahirabbas/Desktop/imp\")"
]
},
{
Expand Down Expand Up @@ -102,70 +103,98 @@
}
],
"source": [
"import time \n",
"import time\n",
"\n",
"\n",
"class Blockchain:\n",
" def __init__(self):\n",
" self.chain = []\n",
" self.current_reports = []\n",
" self.create_block(proof=1, previous_hash='0')\n",
" self.create_block(proof=1, previous_hash=\"0\")\n",
" self.reports = {}\n",
"\n",
" def create_block(self, proof, previous_hash):\n",
" block = {\n",
" 'index': len(self.chain) + 1,\n",
" 'timestamp': time.time(), \n",
" 'proof': proof,\n",
" 'previous_hash': previous_hash,\n",
" 'reports': self.current_reports\n",
" \"index\": len(self.chain) + 1,\n",
" \"timestamp\": time.time(),\n",
" \"proof\": proof,\n",
" \"previous_hash\": previous_hash,\n",
" \"reports\": self.current_reports,\n",
" }\n",
" self.chain.append(block)\n",
" self.current_reports = []\n",
" return block\n",
"\n",
"\n",
" def add_report(self, stress_value, diagnosis, key):\n",
" self.reports.setdefault(key, []).append({\n",
" 'stress_value': stress_value,\n",
" 'diagnosis': diagnosis\n",
" })\n",
" self.current_reports.append({\n",
" 'stress_value': stress_value,\n",
" 'diagnosis': diagnosis\n",
" })\n",
" self.reports.setdefault(key, []).append(\n",
" {\"stress_value\": stress_value, \"diagnosis\": diagnosis}\n",
" )\n",
" self.current_reports.append(\n",
" {\"stress_value\": stress_value, \"diagnosis\": diagnosis}\n",
" )\n",
"\n",
" def get_last_block(self):\n",
" return self.chain[-1]\n",
"\n",
" def get_reports_by_key(self, key):\n",
" return self.reports.get(key, [])\n",
"\n",
"\n",
"def load_and_prepare_data():\n",
" df = pd.read_csv('heart.csv')\n",
" df = pd.read_csv(\"heart.csv\")\n",
" label_encoder = LabelEncoder()\n",
" df['Sex'] = label_encoder.fit_transform(df['Sex'])\n",
" df['Blood Pressure'] = df['Blood Pressure'].str.replace('/', '').astype(float)\n",
" df.drop(['Patient ID', 'Diabetes', 'Family History', \"Smoking\", 'Alcohol Consumption', 'Diet', 'Previous Heart Problems', 'Medication Use', 'Sedentary Hours Per Day', 'Income', 'BMI', 'Triglycerides', 'Country', 'Continent', 'Hemisphere', 'Heart Attack Risk', 'Obesity'], axis=1, inplace=True)\n",
" y = df['Stress Level']\n",
" df[\"Sex\"] = label_encoder.fit_transform(df[\"Sex\"])\n",
" df[\"Blood Pressure\"] = df[\"Blood Pressure\"].str.replace(\"/\", \"\").astype(float)\n",
" df.drop(\n",
" [\n",
" \"Patient ID\",\n",
" \"Diabetes\",\n",
" \"Family History\",\n",
" \"Smoking\",\n",
" \"Alcohol Consumption\",\n",
" \"Diet\",\n",
" \"Previous Heart Problems\",\n",
" \"Medication Use\",\n",
" \"Sedentary Hours Per Day\",\n",
" \"Income\",\n",
" \"BMI\",\n",
" \"Triglycerides\",\n",
" \"Country\",\n",
" \"Continent\",\n",
" \"Hemisphere\",\n",
" \"Heart Attack Risk\",\n",
" \"Obesity\",\n",
" ],\n",
" axis=1,\n",
" inplace=True,\n",
" )\n",
" y = df[\"Stress Level\"]\n",
" X = df.drop([\"Stress Level\"], axis=1)\n",
" return X, y\n",
"\n",
"\n",
"def train_model(X, y):\n",
" X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
" X_train, X_test, y_train, y_test = train_test_split(\n",
" X, y, test_size=0.2, random_state=42\n",
" )\n",
" scaler = StandardScaler()\n",
" X_train_scaled = scaler.fit_transform(X_train)\n",
" X_test_scaled = scaler.transform(X_test)\n",
" rf_clf = RandomForestClassifier(random_state=42)\n",
" xgb_clf = XGBClassifier(random_state=42)\n",
" voting_clf = VotingClassifier(estimators=[('rf', rf_clf), ('xgb', xgb_clf)], voting='soft')\n",
" voting_clf = VotingClassifier(\n",
" estimators=[(\"rf\", rf_clf), (\"xgb\", xgb_clf)], voting=\"soft\"\n",
" )\n",
" voting_clf.fit(X_train_scaled, y_train)\n",
" return voting_clf, scaler\n",
"\n",
"\n",
"def predict_stress_level(model, scaler, user_input):\n",
" user_input_scaled = scaler.transform([user_input])\n",
" predicted_stress_level = model.predict(user_input_scaled)[0]\n",
" return predicted_stress_level\n",
"\n",
"\n",
"def get_diagnosis(stress_level):\n",
" if stress_level <= 3:\n",
" return \"Stress level is in control.\"\n",
Expand All @@ -174,21 +203,34 @@
" else:\n",
" return \"Consult a doctor, your stress level is very high.\"\n",
"\n",
"\n",
"def get_user_input():\n",
" age = float(input(\"Enter Age: \"))\n",
" sex = input(\"Enter Sex (Male/Female): \")\n",
" sex = 1 if sex.lower() == 'male' else 0\n",
" sex = 1 if sex.lower() == \"male\" else 0\n",
" cholesterol = float(input(\"Enter Cholesterol level: \"))\n",
" blood_pressure = float(input(\"Enter Blood Pressure: \"))\n",
" heart_rate = float(input(\"Enter Heart Rate: \"))\n",
" exercise_hours_per_week = float(input(\"Enter Exercise Hours Per Week: \"))\n",
" physical_activity_days_per_week = float(input(\"Enter Physical Activity Days Per Week: \"))\n",
" physical_activity_days_per_week = float(\n",
" input(\"Enter Physical Activity Days Per Week: \")\n",
" )\n",
" sleep_hours_per_day = float(input(\"Enter Sleep Hours Per Day: \"))\n",
" \n",
" user_input = [age, sex, cholesterol, blood_pressure, heart_rate, exercise_hours_per_week, physical_activity_days_per_week, sleep_hours_per_day]\n",
"\n",
" user_input = [\n",
" age,\n",
" sex,\n",
" cholesterol,\n",
" blood_pressure,\n",
" heart_rate,\n",
" exercise_hours_per_week,\n",
" physical_activity_days_per_week,\n",
" sleep_hours_per_day,\n",
" ]\n",
" return user_input\n",
"\n",
"X, y = load_and_prepare_data() \n",
"\n",
"X, y = load_and_prepare_data()\n",
"model, scaler = train_model(X, y)\n",
"user_input = get_user_input()\n",
"predicted_stress_level = predict_stress_level(model, scaler, user_input)\n",
Expand All @@ -197,8 +239,7 @@
"blockchain = Blockchain()\n",
"key = input(\"Enter a key to save the report: \")\n",
"blockchain.add_report(predicted_stress_level, diagnosis, key)\n",
"print(\"Medical report created and saved successfully!\")\n",
"\n"
"print(\"Medical report created and saved successfully!\")"
]
},
{
Expand Down Expand Up @@ -240,13 +281,16 @@
" else:\n",
" return reports\n",
"\n",
"\n",
"key_to_retrieve = input(\"Enter your key to retrieve the reports: \")\n",
"reports = retrieve_reports(blockchain, key_to_retrieve)\n",
"\n",
"if isinstance(reports, list):\n",
" print(\"All medical reports associated with the key:\")\n",
" for i, report in enumerate(reports, start=1):\n",
" print(f\"Report {i}: Stress Value: {report['stress_value']}, Diagnosis: {report['diagnosis']}\")\n",
" print(\n",
" f\"Report {i}: Stress Value: {report['stress_value']}, Diagnosis: {report['diagnosis']}\"\n",
" )\n",
"else:\n",
" print(reports)"
]
Expand All @@ -269,7 +313,7 @@
}
],
"source": [
"df = pd.read_csv('heart.csv')\n",
"df = pd.read_csv(\"heart.csv\")\n",
"df.shape"
]
},
Expand Down
Loading