text_html_output_1.png
from sklearn.model_selection import KFold

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import LabelEncoder

from tqdm import tqdm

import joblib

import lightgbm as lgb

import nltk

import numpy as np # linear algebra

import os

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
import lightgbm as lgb
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
import joblib
from spacy.util import minibatch, compounding
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
from nltk.tokenize import sent_tokenize
from nltk.sentiment import SentimentIntensityAnalyzer
import multiprocessing
from tqdm import tqdm
from sklearn.model_selection import KFold
le = LabelEncoder()
import os

df = pd.read_csv('/kaggle/input/WELFakeAdditional/final (1).csv')

def get_pos_counts(text):
    tokens = nltk.word_tokenize(text)
    pos_tags = nltk.pos_tag(tokens)
    pos_counts = {'NN': 0, 'VB': 0, 'JJ': 0}
    for word, tag in pos_tags:
        if tag.startswith('NN'):
            pos_counts['NN'] += 1
        elif tag.startswith('VB'):
            pos_counts['VB'] += 1
        elif tag.startswith('JJ'):
            pos_counts['JJ'] += 1
    return (pos_counts['NN'], pos_counts['VB'], pos_counts['JJ'])

df = pd.read_csv('/kaggle/input/WELFakeAdditional/final (1).csv')
df.drop('sentiment', axis=1, inplace=True)
df.drop('subject', axis=1, inplace=True)
df.drop('date', axis=1, inplace=True)
df.drop('subject.1', axis=1, inplace=True)
df.drop('date.1', axis=1, inplace=True)
df.dropna(inplace=True)
tqdm.pandas(desc='Processing title column')
df[['title_nouns', 'title_verbs', 'title_adjectives']] = df['title'].progress_apply(get_pos_counts).apply(pd.Series)
tqdm.pandas(desc='Processing text column')
df[['text_nouns', 'text_verbs', 'text_adjectives']] = df['text'].progress_apply(get_pos_counts).apply(pd.Series)

df.drop('text', axis=1, inplace=True)
df.drop('title', axis=1, inplace=True)
train_data = df.sample(frac=0.8, random_state=42)
val_data = df.drop(train_data.index)
X = df.drop('is_fake', axis=1)
y = df['is_fake']
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)
num_models = 10
params = {'objective': 'binary', 'metric': 'binary_logloss', 'num_leaves': 31, 'learning_rate': 0.05, 'min_child_samples': 20, 'feature_fraction': 0.8}
kf = KFold(n_splits=num_models, shuffle=True, random_state=42)
models = []
for i, (train_idx, val_idx) in enumerate(kf.split(X)):
    X_train, X_val = (X.iloc[train_idx], X.iloc[val_idx])
    y_train, y_val = (y.iloc[train_idx], y.iloc[val_idx])
    train_set = lgb.Dataset(X_train, y_train)
    val_set = lgb.Dataset(X_val, y_val)
    model = lgb.train(params, train_set, num_boost_round=1000, early_stopping_rounds=100, valid_sets=[val_set])
    models.append(model)
y_pred = np.zeros((len(X_val), num_models))
for i, model in enumerate(models):
    y_pred[:, i] = model.predict(X_val)
y_pred_avg = np.mean(y_pred, axis=1)
y_pred_binary = [1 if pred >= 0.5 else 0 for pred in y_pred_avg]
accuracy = sum(y_pred_binary == y_val) / len(y_val)

joblib.dump(models, 'models.joblib')
joblib.dump(le, 'label_encoder.joblib')