-
Notifications
You must be signed in to change notification settings - Fork 0
/
Movie_genre_classification.py
215 lines (104 loc) · 3.5 KB
/
Movie_genre_classification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
# coding: utf-8
# # *Problem Statement*
# Create a machine learning model that can predict the genre of a
# movie based on its plot summary or other textual information. You
# can use techniques like TF-IDF or word embeddings with classifiers
# such as Naive Bayes, Logistic Regression, or Support Vector
# Machines.
# # Step #1: Importing Data
# In[31]:
with open(r"C:\Users\pruth\Downloads\Codsoft\Task1\Genre Classification Dataset\train_data.txt",'r',errors='ignore') as file:
text_data=file.read()
# In[32]:
text_data
# In[33]:
# Split text data into rows
lines = text_data.split('\n')
# Split each row into individual values
data = [line.split(':::') for line in lines]
# In[34]:
data
# #### Converting the text file to CSV
# In[35]:
import csv
# Specify the path for the output CSV file
output_file = "output.csv"
# Open the CSV file in write mode and write the data
with open(output_file, 'w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
# Write the header row if your data has one
# For example, if the first row of your text file is the header:
# csv_writer.writerow(data[0])
# Write the remaining rows
csv_writer.writerows(data)
# # Step #2: Data Cleaning
# In[36]:
import pandas as pd
df=pd.read_csv('outputp.csv',encoding='ISO-8859-1')
# In[37]:
df
# In[38]:
X=df['Description']
# In[39]:
X
# In[40]:
y=df['Genre']
# In[41]:
y
# # Step #3: Vectorization
# In[42]:
from sklearn.feature_extraction.text import TfidfVectorizer
# In[43]:
tfidf_vectorizer = TfidfVectorizer(
max_features=10000, # Limit the number of features (words) to 5000
stop_words='english' # Remove common English stop words
)
# In[44]:
X_tfidf = tfidf_vectorizer.fit_transform(X)
# In[45]:
X_tfidf
# # Step #4: Model Training
# In[46]:
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report, precision_recall_fscore_support
# In[47]:
# 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)
# In[48]:
# Create a TF-IDF vectorizer
tfidf_vectorizer = TfidfVectorizer(max_features=5000, stop_words='english')
# In[49]:
# Transform the textual data into TF-IDF vectors
X_train_tfidf = tfidf_vectorizer.fit_transform(X_train)
X_test_tfidf = tfidf_vectorizer.transform(X_test)
# In[50]:
# Define a Logistic Regression classifier
# Manually choose hyperparameters here
clf = LogisticRegression(max_iter=1000, C=1.0, penalty='l2')
# In[51]:
# Fit the classifier on the training data
clf.fit(X_train_tfidf, y_train)
# # Step #5: Model Prediction
# In[52]:
# Make predictions on the test data
y_pred = clf.predict(X_test_tfidf)
# Evaluate the model
accuracy = accuracy_score(y_test, y_pred)
# Calculate precision, recall, and F1-score for all classes
precision, recall, f1, support = precision_recall_fscore_support(y_test, y_pred, zero_division=0)
report = classification_report(y_test, y_pred, zero_division=0)
print(f"Accuracy: {accuracy}")
print(report)
# Print precision, recall, and F1-score for each class
for i, genre in enumerate(clf.classes_):
print(f"Genre: {genre}")
print(f"Precision: {precision[i]}")
print(f"Recall: {recall[i]}")
print(f"F1-Score: {f1[i]}")
print(f"Support: {support[i]}")
print()
# In[ ]: