Using machine learning to predict music genres with PyScript 🐍
(ここでは性別・年齢を選択して好みの音楽のジャンルを予測します)
Choose Test Split:
(分割するテスト用データの割合)
Answer between 0 to 1.
Choose gender to predict:
(予測する人の性別)
Male(男性)
Female(女性)
Choose age to predict:
(予測する人の年齢)
Answer age to predict.
Import Data
Train Model
Predict
Calculate Accuracy
# import python libraries import pandas as pd from pyodide.http import open_url from sklearn.tree import DecisionTreeClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score import joblib from sklearn import tree import warnings warnings.simplefilter('ignore') # define global variables music_data = pd.DataFrame() model = DecisionTreeClassifier() def get_music(): columns = ['age', 'gender', 'genre'] data = [] data.append([20,1,'HipHop']) data.append([23,1,'HipHop']) data.append([25,1,'HipHop']) data.append([26,1,'Jazz']) data.append([29,1,'Jazz']) data.append([30,1,'Jazz']) data.append([31,1,'Classical']) data.append([33,1,'Classical']) data.append([37,1,'Classical']) data.append([20,0,'Dance']) data.append([21,0,'Dance']) data.append([25,0,'Dance']) data.append([26,0,'Acoustic']) data.append([27,0,'Acoustic']) data.append([30,0,'Acoustic']) data.append([31,0,'Classical']) data.append([34,0,'Classical']) data.append([35,0,'Classical']) df = pd.DataFrame(data, columns=columns) return df def import_data(*args, **kwargs): # Import the data global music_data df = get_music() if df.shape[0] == 18: document.getElementById("train").classList.remove("disabled"); document.getElementById("train").disabled = False; document.getElementById("predict").classList.remove("disabled"); document.getElementById("predict").disabled = False; document.getElementById("score").classList.remove("disabled"); document.getElementById("score").disabled = False; console.warn(f'df.shape: {df.shape}') music_data = df pyscript.write('output', f'import_data(): df.shape {df.shape}') else: console.error(f'pd.read_csv() error: df.shape: {df.shape}') pyscript.write('output', f'import_data(): pd.read_csv() network error df.shape {df.shape}') def train_model(*args, **kwargs): # Prepare the data df = music_data x = df.drop(columns=['genre']) # age, gender y = df['genre'] # genre # Train the Model model.fit(x, y) console.warn('model.fit(x, y) done!') pyscript.write('output', f'train_model(): done!') def predict(*args, **kwargs): # Make Predictions gender = int(document.querySelector('input[name="genderSelection"]:checked').value); # 1-Male, 0-Female console.warn(f'gender= {gender}') age = int(document.getElementById("age").value) console.warn(f'age= {age}') predections = model.predict([[age, gender]]) # [age, gender] console.warn(f'predections: {predections[0]}') pyscript.write('output', f'predict(): {predections[0]}') def calculate_accuracy(*args, **kwargs): # Calculating the Accuracy test_split = float(document.getElementById("test_split").value) # 0.2 console.warn(f'test_split= {test_split:.2f}') df = music_data x = df.drop(columns=['genre']) y = df['genre'] x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=test_split) console.warn(f'len(x_train): {len(x_train)}, len(y_train): {len(y_train)}') console.warn(f'len(x_test): {len(x_test)}, len(y_test): {len(y_test)}') model.fit(x_train, y_train) predections = model.predict(x_test) score = accuracy_score(y_test, predections) console.warn(f'score: {score}') pyscript.write('output', f'calculate_accuracy(): score= {score}')