fork download
  1. # your code goes here
Success #stdin #stdout 0.05s 9628KB
stdin
# Importing necessary libraries
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score

# Sample dataset (replace with your dataset)
data = pd.DataFrame({
    'text': ["I love this movie!", "This movie is terrible.", "Neutral tweet about something."],
    'sentiment': ['positive', 'negative', 'neutral']
})

# Split data into features (X) and target (y)
X = data['text']
y = data['sentiment']

# Vectorize text data using TF-IDF representation
vectorizer = TfidfVectorizer()
X_vect = vectorizer.fit_transform(X)

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_vect, y, test_size=0.2, random_state=42)

# Build and train Logistic Regression model
model = LogisticRegression()
model.fit(X_train, y_train)

# Evaluate model
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)
stdout
Standard output is empty