ium_151636/script5_1.py
2023-05-14 21:19:23 +02:00

50 lines
1.6 KiB
Python

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MultiLabelBinarizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
# Load the dataset from the CSV file
data = pd.read_csv('data.csv')
# Prepare the data
X = data[['movie title', 'User Rating', 'Director', 'Top 5 Casts', 'Writer']]
y = data['Rating']
# Preprocess the data
# Convert the categorical columns into numerical representations
mlb_genres = MultiLabelBinarizer()
X_genres = pd.DataFrame(mlb_genres.fit_transform(data['Generes']), columns=mlb_genres.classes_)
mlb_keywords = MultiLabelBinarizer()
X_keywords = pd.DataFrame(mlb_keywords.fit_transform(data['Plot Kyeword']), columns=mlb_keywords.classes_)
mlb_casts = MultiLabelBinarizer()
X_casts = pd.DataFrame(mlb_casts.fit_transform(data['Top 5 Casts'].astype(str)), columns=mlb_casts.classes_)
# Concatenate the transformed columns with the remaining columns
X = pd.concat([X, X_genres, X_keywords, X_casts], axis=1)
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create the neural network model
model = Sequential()
model.add(Dense(32, activation='relu', input_dim=X.shape[1]))
model.add(Dense(16, activation='relu'))
model.add(Dense(1))
# Compile the model
model.compile(optimizer=Adam(), loss='mse')
# Train the model
model.fit(X_train, y_train, batch_size=64, epochs=10, validation_data=(X_test, y_test))
# Evaluate the model
mse = model.evaluate(X_test, y_test)
print("Mean Squared Error:", mse)