# LDA Topic Modeling
from sklearn.decomposition import LatentDirichletAllocation
import numpy as np
lda = LatentDirichletAllocation(
n_components=10,
max_iter=50,
learning_method='online',
random_state=42
)
lda.fit(X_train_tfidf)
topics = lda.transform(X_test)
top_words = np.argsort(lda.components_)
# LDA Topic Modeling
from sklearn.decomposition import LatentDirichletAllocation
import numpy as np
lda = LatentDirichletAllocation(
n_components=10,
max_iter=50,
learning_method='online',
random_state=42
)
lda.fit(X_train_tfidf)
topics = lda.transform(X_test)
top_words = np.argsort(lda.components_)
# LDA Topic Modeling
from sklearn.decomposition import LatentDirichletAllocation
import numpy as np
lda = LatentDirichletAllocation(
n_components=10,
max_iter=50,
learning_method='online',
random_state=42
)
lda.fit(X_train_tfidf)
topics = lda.transform(X_test)
top_words = np.argsort(lda.components_)
# LDA Topic Modeling
from sklearn.decomposition import LatentDirichletAllocation
import numpy as np
lda = LatentDirichletAllocation(
n_components=10,
max_iter=50,
learning_method='online',
random_state=42
)
lda.fit(X_train_tfidf)
topics = lda.transform(X_test)
top_words = np.argsort(lda.components_)
# LSTM Time Series
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.LSTM(
128, return_sequences=True,
input_shape=(timesteps, features)
),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1)
])
model.compile(
optimizer='adam',
loss='mse',
metrics=['mae']
)
# LSTM Time Series
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.LSTM(
128, return_sequences=True,
input_shape=(timesteps, features)
),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1)
])
model.compile(
optimizer='adam',
loss='mse',
metrics=['mae']
)
# LSTM Time Series
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.LSTM(
128, return_sequences=True,
input_shape=(timesteps, features)
),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1)
])
model.compile(
optimizer='adam',
loss='mse',
metrics=['mae']
)
# LSTM Time Series
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.LSTM(
128, return_sequences=True,
input_shape=(timesteps, features)
),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1)
])
model.compile(
optimizer='adam',
loss='mse',
metrics=['mae']
)
# K-Means Clustering
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
kmeans = KMeans(
n_clusters=8,
init='k-means++',
n_init=10,
max_iter=300
)
labels = kmeans.fit_predict(X_scaled)
centroids = kmeans.cluster_centers_
# K-Means Clustering
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
kmeans = KMeans(
n_clusters=8,
init='k-means++',
n_init=10,
max_iter=300
)
labels = kmeans.fit_predict(X_scaled)
centroids = kmeans.cluster_centers_
# K-Means Clustering
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
kmeans = KMeans(
n_clusters=8,
init='k-means++',
n_init=10,
max_iter=300
)
labels = kmeans.fit_predict(X_scaled)
centroids = kmeans.cluster_centers_
# K-Means Clustering
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
kmeans = KMeans(
n_clusters=8,
init='k-means++',
n_init=10,
max_iter=300
)
labels = kmeans.fit_predict(X_scaled)
centroids = kmeans.cluster_centers_
# Transformer Attention
import torch.nn.functional as F
def scaled_dot_product(q, k, v, mask=None):
d_k = q.size(-1)
scores = torch.matmul(q, k.transpose(-2,-1))
scores = scores / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask==0, -1e9)
weights = F.softmax(scores, dim=-1)
return torch.matmul(weights, v), weights
# Transformer Attention
import torch.nn.functional as F
def scaled_dot_product(q, k, v, mask=None):
d_k = q.size(-1)
scores = torch.matmul(q, k.transpose(-2,-1))
scores = scores / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask==0, -1e9)
weights = F.softmax(scores, dim=-1)
return torch.matmul(weights, v), weights
# Transformer Attention
import torch.nn.functional as F
def scaled_dot_product(q, k, v, mask=None):
d_k = q.size(-1)
scores = torch.matmul(q, k.transpose(-2,-1))
scores = scores / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask==0, -1e9)
weights = F.softmax(scores, dim=-1)
return torch.matmul(weights, v), weights
# Transformer Attention
import torch.nn.functional as F
def scaled_dot_product(q, k, v, mask=None):
d_k = q.size(-1)
scores = torch.matmul(q, k.transpose(-2,-1))
scores = scores / math.sqrt(d_k)
if mask is not None:
scores = scores.masked_fill(mask==0, -1e9)
weights = F.softmax(scores, dim=-1)
return torch.matmul(weights, v), weights
# Neural Network — PyTorch
import torch
import torch.nn as nn
class DeepNet(nn.Module):
def __init__(self):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
def forward(self, x):
return self.layers(x)
model = DeepNet().cuda()
optimizer = torch.optim.Adam(
model.parameters(), lr=1e-3
)
# Neural Network — PyTorch
import torch
import torch.nn as nn
class DeepNet(nn.Module):
def __init__(self):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
def forward(self, x):
return self.layers(x)
model = DeepNet().cuda()
optimizer = torch.optim.Adam(
model.parameters(), lr=1e-3
)
# Neural Network — PyTorch
import torch
import torch.nn as nn
class DeepNet(nn.Module):
def __init__(self):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
def forward(self, x):
return self.layers(x)
model = DeepNet().cuda()
optimizer = torch.optim.Adam(
model.parameters(), lr=1e-3
)
# Neural Network — PyTorch
import torch
import torch.nn as nn
class DeepNet(nn.Module):
def __init__(self):
super().__init__()
self.layers = nn.Sequential(
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
def forward(self, x):
return self.layers(x)
model = DeepNet().cuda()
optimizer = torch.optim.Adam(
model.parameters(), lr=1e-3
)
# Big Data — PySpark Pipeline
from pyspark.ml import Pipeline
from pyspark.ml.feature import (
VectorAssembler, StandardScaler
)
from pyspark.ml.classification import GBTClassifier
assembler = VectorAssembler(
inputCols=feature_cols,
outputCol='features'
)
scaler = StandardScaler(
inputCol='features',
outputCol='scaled_features'
)
gbt = GBTClassifier(
maxIter=100, maxDepth=5
)
pipeline = Pipeline(
stages=[assembler, scaler, gbt]
)
# Big Data — PySpark Pipeline
from pyspark.ml import Pipeline
from pyspark.ml.feature import (
VectorAssembler, StandardScaler
)
from pyspark.ml.classification import GBTClassifier
assembler = VectorAssembler(
inputCols=feature_cols,
outputCol='features'
)
scaler = StandardScaler(
inputCol='features',
outputCol='scaled_features'
)
gbt = GBTClassifier(
maxIter=100, maxDepth=5
)
pipeline = Pipeline(
stages=[assembler, scaler, gbt]
)
# Big Data — PySpark Pipeline
from pyspark.ml import Pipeline
from pyspark.ml.feature import (
VectorAssembler, StandardScaler
)
from pyspark.ml.classification import GBTClassifier
assembler = VectorAssembler(
inputCols=feature_cols,
outputCol='features'
)
scaler = StandardScaler(
inputCol='features',
outputCol='scaled_features'
)
gbt = GBTClassifier(
maxIter=100, maxDepth=5
)
pipeline = Pipeline(
stages=[assembler, scaler, gbt]
)
# Big Data — PySpark Pipeline
from pyspark.ml import Pipeline
from pyspark.ml.feature import (
VectorAssembler, StandardScaler
)
from pyspark.ml.classification import GBTClassifier
assembler = VectorAssembler(
inputCols=feature_cols,
outputCol='features'
)
scaler = StandardScaler(
inputCol='features',
outputCol='scaled_features'
)
gbt = GBTClassifier(
maxIter=100, maxDepth=5
)
pipeline = Pipeline(
stages=[assembler, scaler, gbt]
)