Spaces:
Sleeping
Sleeping
File size: 2,855 Bytes
54b5359 ea492b0 54b5359 ea492b0 548c423 b587838 548c423 ea492b0 54b5359 738d8c0 54b5359 888a131 54b5359 888a131 ea492b0 54b5359 |
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 |
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.ensemble import RandomForestRegressor
import pandas as pd
from tqdm.auto import tqdm
import streamlit as st
from huggingface_hub import Repository, HfApi
import os
tqdm.pandas()
# Authentifizierung (stellen Sie sicher, dass Ihr Hugging Face Token gesetzt ist)
api = HfApi(
endpoint="https://huggingface.co/spaces/Add1E/SpotifyHitPrediction",
token=os.getenv("token")
)
# Erstellen oder Klonen Sie ein Repository (wenn noch nicht vorhanden)
repo = Repository(local_dir="SpotifyHitPrediction", clone_from="Add1E/SpotifyHitPrediction")
def predict_popularity(features, trainset):
predictions = [None] * 2
predictions[0], predictions[1] = rf_model.predict([features]), model.predict([features])
addToCsvAndTrain(trainset)
return predictions
def addToCsvAndTrain(trainset):
trainset = [
[trainset[0],trainset[1],trainset[2],trainset[3],trainset[4],trainset[5],trainset[6],trainset[7],
trainset[8],trainset[9],trainset[10],trainset[11],trainset[12],trainset[13]
]
]
neues_df = pd.DataFrame(trainset, columns= data.columns)
df = pd.concat([data, neues_df], ignore_index=True)
df.to_csv('SpotifyHitPrediction/top50.csv', index=False)
repo.git_add('top50.csv')
repo.git_commit("Add top50.csv")
repo.git_push()
data = pd.read_csv('top50.csv', encoding='ISO-8859-1')
print(data.head())
# Let's also describe the data to get a sense of the distributions
print(data.describe())
# Selecting the features and the target variable
X = data.drop(['Unnamed: 0', 'Track.Name', 'Artist.Name', 'Genre', 'Popularity'], axis=1)
y = data['Popularity']
# Splitting 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)
# Initializing the Linear Regression model
model = LinearRegression()
# Fitting the model
model.fit(X_train, y_train)
# Making predictions
y_pred = model.predict(X_test)
# Calculating the performance metrics
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
# Initialize the Random Forest Regressor
rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
# Fitting the model
rf_model.fit(X_train, y_train)
# Making predictions
rf_pred = rf_model.predict(X_test)
# Calculating the performance metrics
rf_mse = mean_squared_error(y_test, rf_pred)
rf_r2 = r2_score(y_test, rf_pred)
# Feature importances
feature_importances = rf_model.feature_importances_
# Create a pandas series with feature importances
importances = pd.Series(feature_importances, index=X.columns)
# Sort the feature importances in descending order
sorted_importances = importances.sort_values(ascending=False)
|