Spaces:
Runtime error
Runtime error
Regino commited on
Commit ·
045e883
1
Parent(s): 3f37371
jlsdndfnds
Browse files8 unmodified lines91011121213141517 unmodified lines33343536363738394041383940414442434447454647484950535152535492 unmodified lines1471481491521501511521531561541551561578 unmodified lines1661671681711691701711721731741781751761771789 unmodified lines1881891901941951911921931941951961972011981992002014 unmodified lines20620720821220921021121221 unmodified lines2342352362402412372382392402442412422432448 unmodified lines }, { "cell_type": "code", "execution_count": 5, "execution_count": null, "metadata": {}, "outputs": [ {17 unmodified lines } ], "source": [ "import pandas as pd \n", "import pandas as pd \n", "\n", "# Define column names manually\n", "column_names = ['id',\"place\",\"label\", \"text\"] # Change this based on your dataset\n", "\n", "# Load training dataset\n", "column_names = ['id',\"place\",\"label\", \"text\"]\n", "#Train Dataset\n", "train_df = pd.read_csv(\"twitter_training.csv\", names=column_names, header=None)\n", "\n", "# Load test dataset\n", "#Test Dataset\n", "test_df = pd.read_csv(\"twitter_validation.csv\", names=column_names, header=None)\n", "\n", "# Display first few rows\n", "\n", "print(train_df.head())\n" ] }, { "cell_type": "code", "execution_count": 10, "execution_count": null, "metadata": {}, "outputs": [ {92 unmodified lines "import nltk\n", "from nltk.corpus import stopwords\n", "\n", "# Download stopwords if not already downloaded\n", "# Stopwords\n", "nltk.download(\"stopwords\")\n", "stop_words = set(stopwords.words(\"english\"))\n", "\n", "# Function to clean text\n", "# Clean Text\n", "def preprocess_text(text):\n", " if isinstance(text, float): # Handle missing values\n", " return \"\"\n",8 unmodified lines "train_df[\"clean_text\"] = train_df[\"text\"].apply(preprocess_text)\n", "test_df[\"clean_text\"] = test_df[\"text\"].apply(preprocess_text)\n", "\n", "# Display a sample of the cleaned text\n", "print(\"Sample cleaned text:\")\n", "display(train_df[[\"text\", \"clean_text\"]].head())\n" ] }, { "cell_type": "code", "execution_count": 11, "execution_count": null, "metadata": {}, "outputs": [ {9 unmodified lines "source": [ "from sklearn.feature_extraction.text import TfidfVectorizer\n", "\n", "# Initialize TF-IDF Vectorizer\n", "vectorizer = TfidfVectorizer(max_features=5000) # Limit to 5000 most important words\n", "# TF-IDF Vectorizer\n", "vectorizer = TfidfVectorizer(max_features=5000)\n", "\n", "# Fit and transform training data, then transform test data\n", "X_train = vectorizer.fit_transform(train_df[\"clean_text\"])\n", "X_test = vectorizer.transform(test_df[\"clean_text\"])\n", "\n", "# Extract labels (assuming the sentiment column is named \"label\")\n", "# Extract labels\n", "y_train = train_df[\"label\"]\n", "y_test = test_df[\"label\"]\n", "\n",4 unmodified lines }, { "cell_type": "code", "execution_count": 12, "execution_count": null, "metadata": {}, "outputs": [ {21 unmodified lines "from sklearn.linear_model import LogisticRegression\n", "from sklearn.metrics import accuracy_score, classification_report\n", "\n", "# Initialize and train the model\n", "model = LogisticRegression(max_iter=1000) # Increase iterations to ensure convergence\n", "# Train the model\n", "model = LogisticRegression(max_iter=1000)\n", "model.fit(X_train, y_train)\n", "\n", "# Make predictions on the test set\n", "# Make predictions\n", "y_pred = model.predict(X_test)\n", "\n", "# Evaluate the model\n",Train Model.ipynb+17-20