text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
import matplotlib.pyplot as plt

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

df = pd.read_csv('../input/prediction-of-music-genre/music_genre.csv')

import seaborn as sns
import matplotlib.pyplot as plt

df_mr = df.drop(columns=[i for i in df.columns if i not in ['popularity', 'tempo', 'instrumentalness']])

df_mr.tempo = df_mr.tempo.apply(lambda row: str(row).replace('?', '0') if row == '?' else str(row))
df_mr.tempo = df_mr.tempo.apply(pd.to_numeric, errors='coerce')
df_mr = df_mr.dropna()

x = df_mr[['instrumentalness', 'tempo']]
y = df_mr['popularity']

def fit(x, a):
    f = a[2] * x * x + a[1] * x + a[0]
    return f
def grad(x, a):
    g = 2 * a[2] * x + a[1]
    return g
x = df_mr[['tempo', 'instrumentalness']]
y = df_mr['popularity']
f = fit(x, y)

x = df_mr[['tempo']]
y = df_mr['popularity']
def find_theta(X, y):
    m = X.shape[0]
    X = np.append(X, np.ones((m, 1)), axis=1)
    theta = np.dot(np.linalg.inv(np.dot(X.T, X)), np.dot(X.T, y))
    return theta
def predict(X):
    X = np.append(X, np.ones((X.shape[0], 1)), axis=1)
    preds = np.dot(X, theta)
    return preds
theta = find_theta(x, y)
print(theta)
preds = predict(x)
fig = plt.figure(figsize=(8, 6))
plt.plot(x, y, 'y.')
plt.plot(x, preds, 'c-')
plt.xlabel('Input')
plt.ylabel('target')