Upload model.ipynb
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114{ "cells": [ { "cell_type": "code", "execution_count": 15, "id": "ace57031", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Accuracy: 0.023255813953488372\n", "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" ] } ], "source": [ "from sklearn.feature_extraction.text import TfidfVectorizer\n", "from sklearn.model_selection import train_test_split\n", "from sklearn.linear_model import LogisticRegression\n", "from sklearn.metrics import accuracy_score\n", "\n", "# Step 1: Collect and preprocess data\n", "# Get all the questions from Questions column and responses from Questions column in the dataset data.csv\n", "# questions = data[\"Questions\"].tolist()\n", "# responses = data[\"Responses\"].tolist()\n", "questions = []\n", "responses = []\n", "q_id = []\n", "with open(\"data.csv\", \"r\") as f:\n", " for line in f:\n", " \n", " array = line.split(\",\") \n", " # questions.append(question)\n", " # responses.append(response)\n", " # q_id.append(question_id)\n", " try:\n", " question = array[1]\n", " response = array[2]\n", " question_id = array[0]\n", " questions.append(question)\n", " responses.append(response)\n", " q_id.append(question_id)\n", " except:\n", " pass\n", "\n", "\n", " \n", "\n", "# print(questions)\n", "# print(responses)\n", "\n", "\n", "# questions = [\"What are some symptoms of depression?\",\n", "# \"How can I manage my anxiety?\",\n", "# \"What are the treatments for bipolar disorder?\"]\n", "# responses = [\"Symptoms of depression include sadness, lack of energy, and loss of interest in activities.\",\n", "# \"You can manage your anxiety through techniques such as deep breathing, meditation, and therapy.\",\n", "# \"Treatments for bipolar disorder include medication, therapy, and lifestyle changes.\"]\n", "\n", "vectorizer = TfidfVectorizer()\n", "X = vectorizer.fit_transform(questions)\n", "y = responses\n", "\n", "# Step 2: Split data into training and testing sets\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n", "\n", "# Step 3: Choose a machine learning algorithm\n", "model = LogisticRegression()\n", "\n", "# Step 4: Train the model\n", "model.fit(X_train, y_train)\n", "\n", "# Step 5: Evaluate the model\n", "y_pred = model.predict(X_test)\n", "accuracy = accuracy_score(y_test, y_pred)\n", "print(\"Accuracy:\", accuracy)\n", "\n", "# Step 6: Use the model to make predictions\n", "new_question = \"I feel sad\"\n", "new_question_vector = vectorizer.transform([new_question])\n", "prediction = model.predict(new_question_vector)\n", "print(\"Prediction:\", prediction)\n" ] } ], "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.7" }, "vscode": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } } }, "nbformat": 4, "nbformat_minor": 5}model.ipynbAdded+114