ium_151636/script5.py

42 lines
1.4 KiB
Python
Raw Normal View History

2023-05-14 19:55:04 +02:00
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, Embedding, Flatten
from tensorflow.keras.optimizers import Adam
# Load the dataset from the CSV file
2023-05-14 20:11:31 +02:00
data = pd.read_csv('data.csv', on_bad_lines='skip', engine='python')
2023-05-14 19:55:04 +02:00
2023-05-14 19:59:54 +02:00
2023-05-14 19:55:04 +02:00
# Prepare the data
2023-05-14 20:20:49 +02:00
X = data[['movie title', 'User Rating', 'Generes', 'Plot Kyeword', 'Director', 'Top 5 Casts', 'Writer', 'year']]
2023-05-14 19:55:04 +02:00
y = data['Rating']
# Preprocess the data
# Convert the categorical columns into numerical representations
mlb = MultiLabelBinarizer()
2023-05-14 20:25:20 +02:00
X['Generes'] = mlb.fit_transform(X['Generes'])
2023-05-14 20:36:09 +02:00
X['Plot Kyeword'] = mlb.fit_transform(X['Plot Kyeword'])
2023-05-14 20:34:27 +02:00
X['Top 5 Casts'] = mlb.fit_transform(X['Top 5 Casts'].astype(str))
2023-05-14 19:55:04 +02:00
# 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()
2023-05-14 20:27:17 +02:00
model.add(Embedding(input_dim=len(mlb.classes_), output_dim=10, input_length=X.shape[1]))
2023-05-14 19:55:04 +02:00
model.add(Flatten())
model.add(Dense(32, 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)