<jupyter_start><jupyter_text>Importing Required Libraries<jupyter_code># data analysis and wrangling
import pandas as pd
import numpy as np

# visualization
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
sns.set(style='dark')

# machine learning
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
from sklearn.cluster import KMeans
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.feature_selection import SelectKBest, f_classif<jupyter_output><empty_output><jupyter_text>Load the Dataset<jupyter_code>data = pd.read_csv('C:/Users/S_The/Downloads/top50.csv')
data.head()<jupyter_output><empty_output><jupyter_text>Drop "Unnamed: 0" column<jupyter_code>data.drop('Unnamed: 0', inplace=True, axis=1)
data.shape
data.describe()
data.isnull().sum()<jupyter_output><empty_output><jupyter_text>**Calculating the number of songs by genre**<jupyter_code>data.Genre.value_counts()
values = data.Genre.value_counts()
indexes = values.index

fig = plt.figure(figsize=(35, 15))
sns.barplot(indexes, values)

plt.ylabel('Number of values')
plt.xlabel('Genre')<jupyter_output>c:\python\python39\lib\site-packages\seaborn\_decorators.py:36: FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid positional argument will be `data`, and passing other arguments without an explicit keyword will result in an error or misinterpretation.
  warnings.warn(<jupyter_text>**Liveness distribution**<jupyter_code>values = data.Liveness.value_counts()
indexes = values.index

fig = plt.figure(figsize=(15, 10))
sns.barplot(indexes, values)

plt.ylabel('Number of values')
plt.xlabel('Liveness')
data.rename(columns={'Track.Name':'Track_Name', 'Artist.Name':'Artist_Name',
                      'Beats.Per.Minute':'Beats_Per_Minute', 'Loudness..dB..':'Loudness',
                      'Valence.':'Valence', 'Length.':'Length', 'Acousticness..':'Acousticness', 'Speechiness.':'Speechiness'}, inplace=True)
data.drop('Track_Name', axis=1, inplace=True)
data.head()<jupyter_output><empty_output><jupyter_text>**BPM distribution.used a function to split "Beats_Per_Minute" column by groupes**<jupyter_code>data.Beats_Per_Minute.describe()
def transform(x):
  if x <= 100:
    return '85-100'
  elif x <= 120:
    return '101-120'
  elif x <= 140:
    return '121-140'
  else:
    return '141-190'

groups_of_bpm = data.Beats_Per_Minute.apply(transform)

values = groups_of_bpm.value_counts()
labels = values.index
colors = ['grey', 'blue', 'green', 'purple']
fig = plt.figure(figsize=(15, 10))
plt.pie(values, colors=colors, autopct='%1.1f%%', startangle=90, textprops={ 'color': 'w' })
plt.title('BPM distribution', fontsize=15)
plt.legend(labels)
plt.show()<jupyter_output><empty_output><jupyter_text>Correlation heatmap<jupyter_code>correlations = data.corr()

fig = plt.figure(figsize=(12, 10))
sns.heatmap(correlations, annot=True, cmap='afmhot_r', center=1)<jupyter_output><empty_output><jupyter_text>Relationship between energy and loudness (quite good correlation)<jupyter_code>fig = plt.figure(figsize=(15, 10))
sns.regplot(x='Energy', y='Loudness', data=data)<jupyter_output><empty_output><jupyter_text>Data preprocessing *Encoding categorical features*<jupyter_code>le = LabelEncoder()

for col in data.columns.values:
  if data[col].dtypes == 'object':
    le.fit(data[col].values)
    data[col] = le.transform(data[col])

data.head()<jupyter_output><empty_output><jupyter_text>Let's find the best features (according to the heatmap too)<jupyter_code>X = data.drop('Loudness', axis=1)
y = data.Loudness

selector = SelectKBest(score_func=f_classif, k=5)
fitted = selector.fit(X, y)
features_scores = pd.DataFrame(fitted.scores_)
features_columns = pd.DataFrame(X.columns)

best_features = pd.concat([features_columns, features_scores], axis=1)
best_features.columns = ['Feature', 'Score']
best_features.sort_values(by='Score', ascending=False, inplace=True)
best_features<jupyter_output><empty_output><jupyter_text>Model Training Split data. Actually we ain't drop columns as we have only 10 (except Artist_Name since there are too many artists and it becomes useless).<jupyter_code>X.drop('Artist_Name', axis=1, inplace=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
X_train<jupyter_output><empty_output><jupyter_text>**Scaling the data**<jupyter_code>sc = StandardScaler()

X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)

type(X_train), type(X_test)<jupyter_output><empty_output><jupyter_text>**Apply Linear Regression on the data**<jupyter_code>regressor = LinearRegression()
regressor.fit(X_train, y_train)

y_pred = regressor.predict(X_test)

print(regressor.intercept_, regressor.coef_)
print(mean_squared_error(y_test, y_pred))<jupyter_output>-5.878787878787879 [-0.08081003  0.31432331  1.54601124 -0.09561211  0.14983013 -0.09416585
  0.3376415   0.18716505 -0.70038778 -0.13967672]
3.5210114507517<jupyter_text>**Apply Support Machine Regression**<jupyter_code>regressor = SVR(C=0.5)
regressor.fit(X_train, y_train)

y_pred = regressor.predict(X_test)
print(mean_squared_error(y_test, y_pred))<jupyter_output>2.751195308540081<jupyter_text>**Apply K-Means Clustering. Actually, I think MSE is excessively high since our data is too small**<jupyter_code>clustering = KMeans(n_clusters=2)
clustering.fit(X_train, y_train)

y_pred = clustering.predict(X_test)
print(mean_squared_error(y_test, y_pred))<jupyter_output>38.35294117647059
{'fraction_non_alphanumeric': 0.12987012987012986, 'fraction_numerical': 0.04216331613591887, 'mean_word_length': 7.378539493293592, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '141274', 'license': 'MIT', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2031, 'n_words': 382, 'path': 'Top 50 Spotify Songs/Top 50 Spotify Songs.ipynb', 'repo_name': 'bhavya278/Hacktober-CP-contributions', 'size': '5621'}