-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheda.py
734 lines (549 loc) · 21.1 KB
/
eda.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# -*- coding: utf-8 -*-
"""EDA.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1eqt0kTVihqnhrqwuq3GiXsOymXUvL7ng
"""
from google.colab import drive
# Unmount the drive first
drive.flush_and_unmount()
print('Drive unmounted')
# Remove existing files from the mountpoint if it exists
import os
if os.path.exists('/content/drive'):
!rm -rf '/content/drive' # Use with caution! This permanently deletes all files in the directory.
print('Files removed from mountpoint')
# Remount the drive
drive.mount('/content/drive')
print('Drive mounted')
"""Exploring Classical Song:"""
# Commented out IPython magic to ensure Python compatibility.
# Usual Libraries
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline
import sklearn
# Librosa
import librosa
import librosa.display
import IPython.display as ipd
import warnings
warnings.filterwarnings('ignore')
import os
general_path = "/content/drive/Shareddrives/Machine_Learning_Project_Drive/Youtube_Data"
# print(list(os.listdir(f'{general_path}')))
# Importing 1 file
y, sr = librosa.load(f'{general_path}/Classical/Bach - Air - Best-of Classical Music.wav')
print('y:', y, '\n')
print('y shape:', np.shape(y), '\n')
print('Sample Rate (KHz):', sr, '\n')
# Verify length of the audio
print('Check Len of Audio:', 7202561/22050)
"""Sound Wave Shape:"""
# Trim leading and trailing silence from an audio signal (silence before and after the actual audio)
audio_file, _ = librosa.effects.trim(y)
# the result is an numpy ndarray
print('Audio File:', audio_file, '\n')
print('Audio File shape:', np.shape(audio_file))
# Create the waveform plot with matplotlib
plt.figure(figsize=(16, 6))
plt.plot(np.linspace(0, len(audio_file) / sr, num=len(audio_file)), audio_file, color="#E6E6FA")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.title("Sound Waves in Classical (first song)", fontsize=23)
plt.show()
"""Short-Time Fourier Transform:"""
# Default FFT window size
n_fft = 2048 # FFT window size
hop_length = 512 # number audio of frames between STFT columns (looks like a good default)
# Short-time Fourier transform (STFT)
D = np.abs(librosa.stft(audio_file, n_fft = n_fft, hop_length = hop_length))
print('Shape of D object:', np.shape(D))
plt.figure(figsize = (16, 6))
plt.plot(D);
"""Spectogram:"""
# Convert an amplitude spectrogram to Decibels-scaled spectrogram.
DB = librosa.amplitude_to_db(D, ref = np.max)
# Creating the Spectogram
plt.figure(figsize = (16, 6))
librosa.display.specshow(DB, sr = sr, hop_length = hop_length, x_axis = 'time', y_axis = 'log',
cmap = 'cool')
plt.colorbar();
"""Mel Spectogram:"""
import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
# Load and trim the audio file
y, sr = librosa.load(f'{general_path}/Classical/Bach - Air - Best-of Classical Music.wav')
y, _ = librosa.effects.trim(y)
# Generate the Mel spectrogram
S = librosa.feature.melspectrogram(y=y, sr=sr)
S_DB = librosa.amplitude_to_db(S, ref=np.max)
# Plot the Mel spectrogram
plt.figure(figsize=(16, 6))
librosa.display.specshow(S_DB, sr=sr, hop_length=512, x_axis='time', y_axis='log', cmap='cool')
plt.colorbar(format='%+2.0f dB')
plt.title("Classical Mel Spectrogram", fontsize=23)
plt.xlabel("Time")
plt.ylabel("Frequency (Hz)")
plt.show()
"""Harmonics and Perceptrual:"""
#Harmonics and Perceptrual
y, sr = librosa.load(f'{general_path}/Classical/Bach - Air - Best-of Classical Music.wav')
y, _ = librosa.effects.trim(y)
y_harm, y_perc = librosa.effects.hpss(audio_file)
plt.figure(figsize = (16, 6))
plt.plot(y_harm, color = '#A300F9');
plt.plot(y_perc, color = '#FFB100');
"""Spectral Centroid along the waveform:"""
import sklearn.preprocessing
import matplotlib.pyplot as plt
import librosa
import librosa.display
def normalize(x, axis=0):
return sklearn.preprocessing.minmax_scale(x, axis=axis)
# Plotting the Spectral Centroid along the waveform
spectral_centroids = librosa.feature.spectral_centroid(y=audio_file, sr=sr)[0]
frames = range(len(spectral_centroids))
# Converts frame counts to time (seconds)
t = librosa.frames_to_time(frames)
plt.figure(figsize=(16, 6))
librosa.display.waveshow(audio_file, sr=sr, alpha=0.4, color='#A300F9')
plt.plot(t, normalize(spectral_centroids), color='#FFB100')
plt.title('Spectral Centroid along the Waveform')
plt.xlabel('Time (s)')
plt.ylabel('Normalized Spectral Centroid')
plt.show()
"""Spectral Rolloff along the Waveform:"""
import sklearn.preprocessing
import matplotlib.pyplot as plt
import librosa
import librosa.display
def normalize(x, axis=0):
return sklearn.preprocessing.minmax_scale(x, axis=axis)
# Spectral RollOff Vector
spectral_rolloff = librosa.feature.spectral_rolloff(y=audio_file, sr=sr)[0]
# Converts frame counts to time (seconds)
frames = range(len(spectral_rolloff))
t = librosa.frames_to_time(frames)
# The plot
plt.figure(figsize=(16, 6))
librosa.display.waveshow(audio_file, sr=sr, alpha=0.4, color='#A300F9')
plt.plot(t, normalize(spectral_rolloff), color='#FFB100')
plt.title('Spectral Rolloff along the Waveform')
plt.xlabel('Time (s)')
plt.ylabel('Normalized Spectral Rolloff')
plt.show()
"""MFCCs:"""
import sklearn.preprocessing
import matplotlib.pyplot as plt
import librosa
import librosa.display
# Load the audio file
audio_file, sr = librosa.load(f'{general_path}/Classical/Bach - Air - Best-of Classical Music.wav')
audio_file, _ = librosa.effects.trim(audio_file)
# Mel-Frequency Cepstral Coefficients
mfccs = librosa.feature.mfcc(y=audio_file, sr=sr)
print('mfccs shape:', mfccs.shape)
# Displaying the MFCCs
plt.figure(figsize=(16, 6))
librosa.display.specshow(mfccs, sr=sr, x_axis='time', cmap='cool')
plt.colorbar(format='%+2.0f dB')
plt.title('MFCC')
plt.xlabel('Time (s)')
plt.ylabel('MFCC Coefficients')
plt.show()
"""Scaled MFCCs:"""
# Perform Feature Scaling
mfccs = sklearn.preprocessing.scale(mfccs, axis=1)
print('Mean:', mfccs.mean(), '\n')
print('Var:', mfccs.var())
plt.figure(figsize = (16, 6))
librosa.display.specshow(mfccs, sr=sr, x_axis='time', cmap = 'cool');
"""Chromogram:"""
import sklearn.preprocessing
import matplotlib.pyplot as plt
import librosa
import librosa.display
# Increase or decrease hop_length to change how granular you want your data to be
hop_length = 5000
# Chromogram
chromagram = librosa.feature.chroma_stft(y=audio_file, sr=sr, hop_length=hop_length)
print('Chromogram shape:', chromagram.shape)
plt.figure(figsize=(16, 6))
librosa.display.specshow(chromagram, x_axis='time', y_axis='chroma', hop_length=hop_length, cmap='coolwarm')
plt.colorbar()
plt.title('Chromagram')
plt.xlabel('Time (s)')
plt.ylabel('Chroma')
plt.show()
"""Analysis of Rock vs Classical:
Checking Data:
"""
# Commented out IPython magic to ensure Python compatibility.
# Usual Libraries
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline
import sklearn
# Librosa
import librosa
import librosa.display
import IPython.display as ipd
import warnings
warnings.filterwarnings('ignore')
import os
general_path = "/content/drive/Shareddrives/Machine_Learning_Project_Drive/Youtube_Data"
# print(list(os.listdir(f'{general_path}')))
# Importing 2 files
y, sr = librosa.load(f'{general_path}/Classical/Bach - Air - Best-of Classical Music.wav')
z, sr2 = librosa.load(f'{general_path}/Rock/AC⧸DC - Highway to Hell.wav')
print('y:', y, '\n')
print('y shape:', np.shape(y), '\n')
print('Sample Rate (KHz):', sr, '\n')
# Verify length of the audio
print('Check Len of Audio:', 7904768/22050)
print('z:', z, '\n')
print('z shape:', np.shape(z), '\n')
print('Sample Rate (KHz):', sr2, '\n')
# Verify length of the audio
print('Check Len of Audio:', 4601857/22050)
"""Classical Sound Wave:"""
# Trim leading and trailing silence from an audio signal (silence before and after the actual audio)
audio_file, _ = librosa.effects.trim(y)
# the result is an numpy ndarray
print('Audio File:', audio_file, '\n')
print('Audio File shape:', np.shape(audio_file))
# Create the waveform plot with matplotlib
plt.figure(figsize=(16, 6))
plt.plot(np.linspace(0, len(audio_file) / sr, num=len(audio_file)), audio_file, color="#7A5C91")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.title("Sound Waves in Bach - Air - Best-of Classical Music.wav", fontsize=23)
plt.show()
"""Rock Sound Wave:"""
audio_file_2, _ = librosa.effects.trim(z)
# the result is an numpy ndarray
print('Audio File:', audio_file_2, '\n')
print('Audio File shape:', np.shape(audio_file_2))
# Create the waveform plot with matplotlib
plt.figure(figsize=(16, 6))
plt.plot(np.linspace(0, len(audio_file_2) / sr2, num=len(audio_file_2)), audio_file_2, color="#8B0000")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.title("Sound Waves in AC⧸DC - Highway to Hell.wav", fontsize=23)
plt.show()
"""Checking Shape:"""
# Default FFT window size
n_fft = 2048 # FFT window size
hop_length = 512 # number audio of frames between STFT columns
# Short-time Fourier transform (STFT)
D = np.abs(librosa.stft(audio_file, n_fft = n_fft, hop_length = hop_length))
print('Shape of D object 1:', np.shape(D))
#for audio 2:
D_2 = np.abs(librosa.stft(audio_file_2, n_fft = n_fft, hop_length = hop_length))
print('Shape of D object 2:', np.shape(D_2))
Classical Fourier Transform:
plt.figure(figsize = (16, 6))
plt.plot(D);
"""Rock Fourier Transform:"""
plt.figure(figsize = (16, 6))
plt.plot(D_2);
"""Classical Spectrogram:"""
# Convert an amplitude spectrogram to Decibels-scaled spectrogram.
DB1 = librosa.amplitude_to_db(D, ref = np.max)
# Creating the Spectogram
plt.figure(figsize = (16, 6))
librosa.display.specshow(DB1, sr = sr, hop_length = hop_length, x_axis = 'time', y_axis = 'log',
cmap = 'cool')
plt.colorbar();
"""Rock Spectrogram:"""
# Convert an amplitude spectrogram to Decibels-scaled spectrogram.
DB2 = librosa.amplitude_to_db(D_2, ref = np.max)
# Creating the Spectogram
plt.figure(figsize = (16, 6))
librosa.display.specshow(DB2, sr = sr2, hop_length = hop_length, x_axis = 'time', y_axis = 'log',
cmap = 'cool')
plt.colorbar();
"""Classical Mel Spectrogram:"""
import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
# Load and trim the audio file
y, sr = librosa.load(f'{general_path}/Classical/Bach - Air - Best-of Classical Music.wav')
y, _ = librosa.effects.trim(y)
# Generate the Mel spectrogram
S = librosa.feature.melspectrogram(y=y, sr=sr)
S_DB = librosa.amplitude_to_db(S, ref=np.max)
# Plot the Mel spectrogram
plt.figure(figsize=(16, 6))
librosa.display.specshow(S_DB, sr=sr, hop_length=512, x_axis='time', y_axis='log', cmap='cool')
plt.colorbar(format='%+2.0f dB')
plt.title("Classical Mel Spectrogram", fontsize=23)
plt.xlabel("Time")
plt.ylabel("Frequency (Hz)")
plt.show()
"""Rock Mel Spectrogram:"""
import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
# Generate the Mel spectrogram
S = librosa.feature.melspectrogram(y=z, sr=sr)
S_DB = librosa.amplitude_to_db(S, ref=np.max)
# Plot the Mel spectrogram
plt.figure(figsize=(16, 6))
librosa.display.specshow(S_DB, sr=sr2, hop_length=512, x_axis='time', y_axis='log', cmap='cool')
plt.colorbar(format='%+2.0f dB')
plt.title("Rock Mel Spectrogram", fontsize=23)
plt.xlabel("Time")
plt.ylabel("Frequency (Hz)")
plt.show()
"""Zero-Crossings:"""
zero_crossings_1 = librosa.zero_crossings(audio_file, pad=False)
print("Classical: ")
print(sum(zero_crossings_1))
print("Rock: ")
zero_crossings_2 = librosa.zero_crossings(audio_file_2, pad=False)
print(sum(zero_crossings_2))
"""Classical Harmonics and Perceptrual:"""
#Harmonics and Perceptrual
y_harm, y_perc = librosa.effects.hpss(audio_file)
plt.figure(figsize = (16, 6))
plt.plot(y_harm, color = '#A300F9');
plt.plot(y_perc, color = '#FFB100');
"""Rock Harmonics and Perceptual"""
#Harmonics and Perceptrual
y_harm, y_perc = librosa.effects.hpss(audio_file_2)
plt.figure(figsize = (16, 6))
plt.plot(y_harm, color = '#A300F9');
plt.plot(y_perc, color = '#FFB100');
"""Classical Chromogram:"""
import librosa
import librosa.display
import matplotlib.pyplot as plt
# Compute the chromagram
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
# Plot the chromagram
plt.figure(figsize=(12, 6))
librosa.display.specshow(chroma, x_axis='time', y_axis='chroma', cmap='coolwarm')
plt.colorbar()
plt.title('Chromogram: Classical')
plt.xlabel('Time (s)')
plt.ylabel('Pitch Class')
plt.show()
"""Rock Chromogram:"""
import librosa
import librosa.display
import matplotlib.pyplot as plt
# Compute the chromagram
chroma = librosa.feature.chroma_stft(y=z, sr=sr2)
# Plot the chromagram
plt.figure(figsize=(12, 6))
librosa.display.specshow(chroma, x_axis='time', y_axis='chroma', cmap='coolwarm')
plt.colorbar()
plt.title('Chromogram: Rock')
plt.xlabel('Time (s)')
plt.ylabel('Pitch Class')
plt.show()
"""Sound Wave of Songs From Each Genre:"""
# Commented out IPython magic to ensure Python compatibility.
# Usual Libraries
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
# %matplotlib inline
import sklearn
# Librosa
import librosa
import librosa.display
import IPython.display as ipd
import warnings
warnings.filterwarnings('ignore')
import os
general_path = "/content/drive/Shareddrives/Machine_Learning_Project_Drive/Youtube_Data"
for genre in list(os.listdir(f'{general_path}')):
if genre == ".ipynb_checkpoints":
continue
song = list(os.listdir(f'{general_path}/{genre}'))[11]
y, sr = librosa.load(f'{general_path}/{genre}/{song}')
audio_file, _ = librosa.effects.trim(y)
plt.figure(figsize=(16, 6))
plt.plot(np.linspace(0, len(audio_file) / sr, num=len(audio_file)), audio_file, color="#E6E6FA")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")
plt.title(f'{genre}: {song} Sound Wave', fontsize=23)
plt.show()
"""Fourier Transform of Songs From Each Genre:"""
# Default FFT window size
n_fft = 2048 # FFT window size
hop_length = 512 # number audio of frames between STFT columns (looks like a good default)
for genre in list(os.listdir(f'{general_path}')):
if genre == ".ipynb_checkpoints":
continue
song = list(os.listdir(f'{general_path}/{genre}'))[11]
y, sr = librosa.load(f'{general_path}/{genre}/{song}')
audio_file, _ = librosa.effects.trim(y)
# Short-time Fourier transform (STFT)
D = np.abs(librosa.stft(audio_file, n_fft = n_fft, hop_length = hop_length))
plt.title(f'{genre}: {song} Fourier Transform', fontsize=23)
plt.figure(figsize = (16, 6))
plt.plot(D);
"""Spectrogram of Songs From Each Genre:"""
import os
general_path = "/content/drive/Shareddrives/Machine_Learning_Project_Drive/Youtube_Data"
for genre in list(os.listdir(f'{general_path}')):
if genre == ".ipynb_checkpoints":
continue
song = list(os.listdir(f'{general_path}/{genre}'))[11]
y, sr = librosa.load(f'{general_path}/{genre}/{song}')
audio_file, _ = librosa.effects.trim(y)
# Default FFT window size
n_fft = 2048 # FFT window size
hop_length = 512 # number audio of frames between STFT columns (looks like a good default)
# Short-time Fourier transform (STFT)
D = np.abs(librosa.stft(audio_file, n_fft = n_fft, hop_length = hop_length))
# Convert an amplitude spectrogram to Decibels-scaled spectrogram.
DB = librosa.amplitude_to_db(D, ref = np.max)
# Creating the Spectogram
plt.figure(figsize = (16, 6))
plt.title(f'{genre}: {song} Spectrogram', fontsize=23)
librosa.display.specshow(DB, sr = sr, hop_length = hop_length, x_axis = 'time', y_axis = 'log',
cmap = 'cool')
plt.colorbar();
"""Mel Spectrogram of Songs From Each Genre:"""
import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
for genre in list(os.listdir(f'{general_path}')):
if genre == ".ipynb_checkpoints":
continue
print(f'{genre}:')
song = list(os.listdir(f'{general_path}/{genre}'))[11]
y, sr = librosa.load(f'{general_path}/{genre}/{song}')
y, _ = librosa.effects.trim(y)
# Generate the Mel spectrogram
S = librosa.feature.melspectrogram(y=y, sr=sr)
S_DB = librosa.amplitude_to_db(S, ref=np.max)
# Plot the Mel spectrogram
plt.figure(figsize=(16, 6))
librosa.display.specshow(S_DB, sr=sr, hop_length=512, x_axis='time', y_axis='log', cmap='cool')
plt.colorbar(format='%+2.0f dB')
plt.title(f'{genre}: {song} Mel Spectrogram', fontsize=23)
plt.xlabel("Time")
plt.ylabel("Frequency (Hz)")
plt.show()
"""Exploring all the features:
Correlation Matrix of all Features:
"""
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
import pandas as pd
from sklearn.preprocessing import LabelEncoder
data = pd.read_csv('/content/drive/Shareddrives/Machine_Learning_Project_Drive/Audio_Features/all_audio_features_modified.csv')
le = LabelEncoder()
data['genre'] = le.fit_transform(data['genre'])
matrix = data.drop('song_name', axis = 1).corr()
mask = np.triu(np.ones_like(matrix, dtype=bool))
# plotting correlation matrix
sns.heatmap(matrix, cmap="Blues", annot=True, mask=mask)
"""Correlation Matrix for Mean Metrics:"""
import seaborn as sns
# Computing the Correlation Matrix
spike_cols = [col for col in data.columns if 'mean' in col]
corr = data[spike_cols].corr()
# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=bool))
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(16, 11));
# Generate a custom diverging colormap
cmap = sns.diverging_palette(230, 260, as_cmap=True, s = 90, l = 45, n = 5)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0,
square=True, linewidths=.5, cbar_kws={"shrink": .5})
plt.title('Correlation Heatmap (for the MEAN variables)', fontsize = 25)
plt.xticks(fontsize = 10)
plt.yticks(fontsize = 10);
plt.savefig("Corr Heatmap.jpg")
"""Columns:"""
data.columns
"""PCA with 4 Components:"""
from sklearn import preprocessing
import pandas as pd
import numpy as np
data = pd.read_csv('/content/drive/Shareddrives/Machine_Learning_Project_Drive/Audio_Features/all_audio_features_modified.csv')
# Dropping the first column (assumed to be non-feature column) and setting up X and y
data = data.iloc[:, 1:]
y = data['genre']
X = data.drop(columns=['genre'])
# Check for non-numeric columns in X
non_numeric_cols = X.select_dtypes(exclude=[np.number]).columns
if len(non_numeric_cols) > 0:
print("Non-numeric columns found and removed:", non_numeric_cols)
X = X.drop(columns=non_numeric_cols) # Drop non-numeric columns
#### NORMALIZE X ####
cols = X.columns
min_max_scaler = preprocessing.MinMaxScaler()
np_scaled = min_max_scaler.fit_transform(X)
X = pd.DataFrame(np_scaled, columns=cols)
#### PCA 2 COMPONENTS ####
from sklearn.decomposition import PCA
pca = PCA(n_components=4)
principalComponents = pca.fit_transform(X)
principalDf = pd.DataFrame(data=principalComponents, columns=['principal component 1', 'principal component 2', 'principal component 3', 'principal component 4'])
# Concatenate with target label
finalDf = pd.concat([principalDf, y.reset_index(drop=True)], axis=1)
# Explained variance
explained_variance = pca.explained_variance_ratio_
print(f"PCA explained variance ratio: {explained_variance}")
print(f"Total explained variance by 4 components: {explained_variance.sum() * 100:.2f}%")
"""PCA with 2 Components:"""
from sklearn import preprocessing
import pandas as pd
import numpy as np
data = pd.read_csv('/content/drive/Shareddrives/Machine_Learning_Project_Drive/Audio_Features/all_audio_features_modified.csv')
# Dropping the first column (assumed to be non-feature column) and setting up X and y
data = data.iloc[:, 1:] # Adjust this line if needed based on your actual data structure
y = data['genre']
X = data.drop(columns=['genre'])
# Check for non-numeric columns in X
non_numeric_cols = X.select_dtypes(exclude=[np.number]).columns
if len(non_numeric_cols) > 0:
print("Non-numeric columns found and removed:", non_numeric_cols)
X = X.drop(columns=non_numeric_cols) # Drop non-numeric columns
#### NORMALIZE X ####
cols = X.columns
min_max_scaler = preprocessing.MinMaxScaler()
np_scaled = min_max_scaler.fit_transform(X)
X = pd.DataFrame(np_scaled, columns=cols)
#### PCA 2 COMPONENTS ####
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(X)
principalDf = pd.DataFrame(data=principalComponents, columns=['principal component 1', 'principal component 2'])
# Concatenate with target label
finalDf = pd.concat([principalDf, y.reset_index(drop=True)], axis=1)
# Explained variance
explained_variance = pca.explained_variance_ratio_
print(f"PCA explained variance ratio: {explained_variance}")
print(f"Total explained variance by 2 components: {explained_variance.sum() * 100:.2f}%")
"""PCA visualization"""
data = pd.read_csv('/content/drive/Shareddrives/Machine_Learning_Project_Drive/Audio_Features/all_audio_features_modified.csv')
plt.figure(figsize = (16, 9))
sns.scatterplot(x = "principal component 1", y = "principal component 2", data = finalDf, hue = "genre", alpha = 0.7,
s = 100);
plt.title('PCA on Genres', fontsize = 25)
plt.xticks(fontsize = 14)
plt.yticks(fontsize = 10);
plt.xlabel("Principal Component 1", fontsize = 15)
plt.ylabel("Principal Component 2", fontsize = 15)
plt.savefig("PCA Scattert.jpg")