Skip to content

Instantly share code, notes, and snippets.

View saimadhu-polamuri's full-sized avatar
💭
For the love of data.

saimadhu saimadhu-polamuri

💭
For the love of data.
View GitHub Profile
import spacy
nlp = spacy.load('en_core_web_sm')
# Define the text to be processed
text = "This is a sample sentence! It contains some punctuation marks."
# Create a Doc object
doc = nlp(text)
def generate_text(seed_text, num_words):
for _ in range(num_words):
token_list = tokenizer.texts_to_sequences([seed_text])[0]
token_list = tf.keras.preprocessing.sequence.pad_sequences([token_list], maxlen=max_length)
predicted_index = model.predict(token_list)[0].argmax()
predicted_word = tokenizer.index_word[predicted_index]
seed_text += " " + predicted_word
return seed_text
# Generate a story
# Tokenize text and create word-to-index mapping
tokenizer = tf.keras.preprocessing.text.Tokenizer()
tokenizer.fit_on_texts(data["preprocessed_text"])
sequences = tokenizer.texts_to_sequences(data["preprocessed_text"])
padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=max_length)
# Create training data
X_train, X_val, y_train, y_val = train_test_split(padded_sequences, padded_sequences, test_size=0.2, random_state=42)
# Train the model
import tensorflow as tf
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout
from tensorflow.keras.models import Sequential
# Define model architecture
model = Sequential([
Embedding(input_dim=vocab_size, output_dim=embedding_dim),
LSTM(units=256, return_sequences=True),
Dropout(0.2),
# Preprocess text
def preprocess_text(text):
# Remove stop words stop_words = set(stopwords.words("english"))
words = word_tokenize(text.lower())
filtered_words = [word for word in words if word not in stop_words]
return " ".join(filtered_words)
data["preprocessed_text"] = data["text"].apply(preprocess_text)
# Load data (assuming a CSV file)
data = pd.read_csv("stories.csv")
import pandas as pd
import nltk
import tensorflow as tf
@saimadhu-polamuri
saimadhu-polamuri / dataaspirant-xgboost-iris-classification.ipynb
Created November 15, 2020 15:00
Dataaspirant-XGBoost-Iris-classification.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@saimadhu-polamuri
saimadhu-polamuri / dataaspirant-xgboost-boston-housing-price-prediction.ipynb
Created November 15, 2020 15:21
Dataaspirant-XGBoost-Boston-Housing-Price-Prediction.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import xgboost as xgb
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
# load the Boston housing dataset
boston = load_boston()
X, y = boston.data, boston.target
# split the data into training and testing sets