text
stringlengths
0
4.99k
print(row.title, \":\", row.genres)
print(\"----\" * 8)
print(\"Top 10 movie recommendations\")
print(\"----\" * 8)
recommended_movies = movie_df[movie_df[\"movieId\"].isin(recommended_movie_ids)]
for row in recommended_movies.itertuples():
print(row.title, \":\", row.genres)
Showing recommendations for user: 474
====================================
Movies with high ratings from user
--------------------------------
Fugitive, The (1993) : Thriller
Remains of the Day, The (1993) : Drama|Romance
West Side Story (1961) : Drama|Musical|Romance
X2: X-Men United (2003) : Action|Adventure|Sci-Fi|Thriller
Spider-Man 2 (2004) : Action|Adventure|Sci-Fi|IMAX
--------------------------------
Top 10 movie recommendations
--------------------------------
Dazed and Confused (1993) : Comedy
Ghost in the Shell (Kôkaku kidôtai) (1995) : Animation|Sci-Fi
Drugstore Cowboy (1989) : Crime|Drama
Road Warrior, The (Mad Max 2) (1981) : Action|Adventure|Sci-Fi|Thriller
Dark Knight, The (2008) : Action|Crime|Drama|IMAX
Inglourious Basterds (2009) : Action|Drama|War
Up (2009) : Adventure|Animation|Children|Drama
Dark Knight Rises, The (2012) : Action|Adventure|Crime|IMAX
Star Wars: Episode VII - The Force Awakens (2015) : Action|Adventure|Fantasy|Sci-Fi|IMAX
Thor: Ragnarok (2017) : Action|Adventure|Sci-Fi
Demonstration of how to handle highly imbalanced classification problems.
Introduction
This example looks at the Kaggle Credit Card Fraud Detection dataset to demonstrate how to train a classification model on data with highly imbalanced classes.
First, vectorize the CSV data
import csv
import numpy as np
# Get the real data from https://www.kaggle.com/mlg-ulb/creditcardfraud/
fname = \"/Users/fchollet/Downloads/creditcard.csv\"
all_features = []
all_targets = []
with open(fname) as f:
for i, line in enumerate(f):
if i == 0:
print(\"HEADER:\", line.strip())
continue # Skip header
fields = line.strip().split(\",\")
all_features.append([float(v.replace('\"', \"\")) for v in fields[:-1]])
all_targets.append([int(fields[-1].replace('\"', \"\"))])
if i == 1:
print(\"EXAMPLE FEATURES:\", all_features[-1])
features = np.array(all_features, dtype=\"float32\")
targets = np.array(all_targets, dtype=\"uint8\")
print(\"features.shape:\", features.shape)
print(\"targets.shape:\", targets.shape)
HEADER: \"Time\",\"V1\",\"V2\",\"V3\",\"V4\",\"V5\",\"V6\",\"V7\",\"V8\",\"V9\",\"V10\",\"V11\",\"V12\",\"V13\",\"V14\",\"V15\",\"V16\",\"V17\",\"V18\",\"V19\",\"V20\",\"V21\",\"V22\",\"V23\",\"V24\",\"V25\",\"V26\",\"V27\",\"V28\",\"Amount\",\"Class\"
EXAMPLE FEATURES: [0.0, -1.3598071336738, -0.0727811733098497, 2.53634673796914, 1.37815522427443, -0.338320769942518, 0.462387777762292, 0.239598554061257, 0.0986979012610507, 0.363786969611213, 0.0907941719789316, -0.551599533260813, -0.617800855762348, -0.991389847235408, -0.311169353699879, 1.46817697209427, -0.470400525259478, 0.207971241929242, 0.0257905801985591, 0.403992960255733, 0.251412098239705, -0.018306777944153, 0.277837575558899, -0.110473910188767, 0.0669280749146731, 0.128539358273528, -0.189114843888824, 0.133558376740387, -0.0210530534538215, 149.62]
features.shape: (284807, 30)
targets.shape: (284807, 1)
Prepare a validation set
num_val_samples = int(len(features) * 0.2)
train_features = features[:-num_val_samples]
train_targets = targets[:-num_val_samples]
val_features = features[-num_val_samples:]
val_targets = targets[-num_val_samples:]
print(\"Number of training samples:\", len(train_features))
print(\"Number of validation samples:\", len(val_features))
Number of training samples: 227846
Number of validation samples: 56961
Analyze class imbalance in the targets
counts = np.bincount(train_targets[:, 0])
print(
\"Number of positive samples in training data: {} ({:.2f}% of total)\".format(
counts[1], 100 * float(counts[1]) / len(train_targets)
)
)
weight_for_0 = 1.0 / counts[0]
weight_for_1 = 1.0 / counts[1]
Number of positive samples in training data: 417 (0.18% of total)
Normalize the data using training set statistics
mean = np.mean(train_features, axis=0)
train_features -= mean
val_features -= mean
std = np.std(train_features, axis=0)
train_features /= std
val_features /= std
Build a binary classification model
from tensorflow import keras
model = keras.Sequential(
[
keras.layers.Dense(
256, activation=\"relu\", input_shape=(train_features.shape[-1],)