-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathgenre_classification.py
More file actions
59 lines (43 loc) · 1.48 KB
/
genre_classification.py
File metadata and controls
59 lines (43 loc) · 1.48 KB
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
#!/usr/bin/env python3
# Imports
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
import keras
from keras import models
from keras import layers
# Path to the CSV containing the Features
path = input("Enter the Full Path of the CSV file.")
# Reading the CSV files
data = pd.read_csv(path)
data.head()
data = data.drop(["filename"], axis=1)
data.head()
# Extracting the Label
genre_list = data.iloc[:, -1]
encoder = LabelEncoder()
y = encoder.fit_transform(genre_list)
print(y)
# Model Preprocessing
scaler = StandardScaler()
X = scaler.fit_transform(np.array(data.iloc[:, :-1], dtype=float))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Loading a Sequential Model using Keras
model = models.Sequential()
model.add(layers.Dense(256, activation="relu", input_shape=(X_train.shape[1],)))
model.add(layers.Dense(128, activation="relu"))
model.add(layers.Dense(64, activation="relu"))
model.add(layers.Dense(10, activation="softmax"))
# Using Adam Optimization for smoothing
model.compile(
optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
)
# Determining the number of Epoch, and Batch Sizes
history = model.fit(X_train, y_train, epochs=20, batch_size=128)
# Deliverables
test_loss, test_acc = model.evaluate(X_test, y_test)
print("test_acc: ", test_acc)
predictions = model.predict(X_test)
# Saving the Model
model.save("name.h5")