Upload model.ipynb
Browse files- model.ipynb +114 -0
model.ipynb
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 15,
|
| 6 |
+
"id": "ace57031",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stdout",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"Accuracy: 0.023255813953488372\n",
|
| 14 |
+
"Prediction: [' is a member of the BC Partners for Mental Health and Addictions Information. The institute is dedicated to the study of substance use in support of community-wide efforts aimed at providing all people with access to healthier lives']\n"
|
| 15 |
+
]
|
| 16 |
+
}
|
| 17 |
+
],
|
| 18 |
+
"source": [
|
| 19 |
+
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
|
| 20 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 21 |
+
"from sklearn.linear_model import LogisticRegression\n",
|
| 22 |
+
"from sklearn.metrics import accuracy_score\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"# Step 1: Collect and preprocess data\n",
|
| 25 |
+
"# Get all the questions from Questions column and responses from Questions column in the dataset data.csv\n",
|
| 26 |
+
"# questions = data[\"Questions\"].tolist()\n",
|
| 27 |
+
"# responses = data[\"Responses\"].tolist()\n",
|
| 28 |
+
"questions = []\n",
|
| 29 |
+
"responses = []\n",
|
| 30 |
+
"q_id = []\n",
|
| 31 |
+
"with open(\"data.csv\", \"r\") as f:\n",
|
| 32 |
+
" for line in f:\n",
|
| 33 |
+
" \n",
|
| 34 |
+
" array = line.split(\",\") \n",
|
| 35 |
+
" # questions.append(question)\n",
|
| 36 |
+
" # responses.append(response)\n",
|
| 37 |
+
" # q_id.append(question_id)\n",
|
| 38 |
+
" try:\n",
|
| 39 |
+
" question = array[1]\n",
|
| 40 |
+
" response = array[2]\n",
|
| 41 |
+
" question_id = array[0]\n",
|
| 42 |
+
" questions.append(question)\n",
|
| 43 |
+
" responses.append(response)\n",
|
| 44 |
+
" q_id.append(question_id)\n",
|
| 45 |
+
" except:\n",
|
| 46 |
+
" pass\n",
|
| 47 |
+
"\n",
|
| 48 |
+
"\n",
|
| 49 |
+
" \n",
|
| 50 |
+
"\n",
|
| 51 |
+
"# print(questions)\n",
|
| 52 |
+
"# print(responses)\n",
|
| 53 |
+
"\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"# questions = [\"What are some symptoms of depression?\",\n",
|
| 56 |
+
"# \"How can I manage my anxiety?\",\n",
|
| 57 |
+
"# \"What are the treatments for bipolar disorder?\"]\n",
|
| 58 |
+
"# responses = [\"Symptoms of depression include sadness, lack of energy, and loss of interest in activities.\",\n",
|
| 59 |
+
"# \"You can manage your anxiety through techniques such as deep breathing, meditation, and therapy.\",\n",
|
| 60 |
+
"# \"Treatments for bipolar disorder include medication, therapy, and lifestyle changes.\"]\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"vectorizer = TfidfVectorizer()\n",
|
| 63 |
+
"X = vectorizer.fit_transform(questions)\n",
|
| 64 |
+
"y = responses\n",
|
| 65 |
+
"\n",
|
| 66 |
+
"# Step 2: Split data into training and testing sets\n",
|
| 67 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n",
|
| 68 |
+
"\n",
|
| 69 |
+
"# Step 3: Choose a machine learning algorithm\n",
|
| 70 |
+
"model = LogisticRegression()\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"# Step 4: Train the model\n",
|
| 73 |
+
"model.fit(X_train, y_train)\n",
|
| 74 |
+
"\n",
|
| 75 |
+
"# Step 5: Evaluate the model\n",
|
| 76 |
+
"y_pred = model.predict(X_test)\n",
|
| 77 |
+
"accuracy = accuracy_score(y_test, y_pred)\n",
|
| 78 |
+
"print(\"Accuracy:\", accuracy)\n",
|
| 79 |
+
"\n",
|
| 80 |
+
"# Step 6: Use the model to make predictions\n",
|
| 81 |
+
"new_question = \"I feel sad\"\n",
|
| 82 |
+
"new_question_vector = vectorizer.transform([new_question])\n",
|
| 83 |
+
"prediction = model.predict(new_question_vector)\n",
|
| 84 |
+
"print(\"Prediction:\", prediction)\n"
|
| 85 |
+
]
|
| 86 |
+
}
|
| 87 |
+
],
|
| 88 |
+
"metadata": {
|
| 89 |
+
"kernelspec": {
|
| 90 |
+
"display_name": "Python 3",
|
| 91 |
+
"language": "python",
|
| 92 |
+
"name": "python3"
|
| 93 |
+
},
|
| 94 |
+
"language_info": {
|
| 95 |
+
"codemirror_mode": {
|
| 96 |
+
"name": "ipython",
|
| 97 |
+
"version": 3
|
| 98 |
+
},
|
| 99 |
+
"file_extension": ".py",
|
| 100 |
+
"mimetype": "text/x-python",
|
| 101 |
+
"name": "python",
|
| 102 |
+
"nbconvert_exporter": "python",
|
| 103 |
+
"pygments_lexer": "ipython3",
|
| 104 |
+
"version": "3.10.7"
|
| 105 |
+
},
|
| 106 |
+
"vscode": {
|
| 107 |
+
"interpreter": {
|
| 108 |
+
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
|
| 109 |
+
}
|
| 110 |
+
}
|
| 111 |
+
},
|
| 112 |
+
"nbformat": 4,
|
| 113 |
+
"nbformat_minor": 5
|
| 114 |
+
}
|