0% found this document useful (0 votes)
92 views3 pages

Machine Learning Project PDF

The document discusses training a machine learning model to recommend music genres based on a user's age and gender. It imports necessary libraries, loads a music dataset from a CSV file, defines the input and target variables, trains a decision tree classifier model on the data, exports the trained model to a file, and generates a visualization of the decision tree structure.

Uploaded by

Kenny Ralph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views3 pages

Machine Learning Project PDF

The document discusses training a machine learning model to recommend music genres based on a user's age and gender. It imports necessary libraries, loads a music dataset from a CSV file, defines the input and target variables, trains a decision tree classifier model on the data, exports the trained model to a file, and generates a visualization of the decision tree structure.

Uploaded by

Kenny Ralph
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

05/02/2023 22:44 Machine Learning Project

In [4]: import pandas as pd

from sklearn.tree import DecisionTreeClassifier

from joblib import dump, load

# music_data = pd.read_csv('music.csv')

# X = music_data.drop(columns=['genre'])

# y = music_data['genre']

# model = DecisionTreeClassifier()

# model.fit(X, y)

model = joblib.dump(model, 'music-recommender.joblib')

predictions = model.predict([[21, 1],[22, 0]])

predictions

---------------------------------------------------------------------------

NameError Traceback (most recent call last)

~\AppData\Local\Temp\ipykernel_65448\591308401.py in <module>

10 # model.fit(X, y)

11

---> 12 model = joblib.dump(model, 'music-recommender.joblib')

13 predictions = model.predict([[21, 1],[22, 0]])

14 predictions

NameError: name 'joblib' is not defined

In [18]: import pandas as pd

from sklearn.tree import DecisionTreeClassifier

from joblib import dump, load

music_data = pd.read_csv('music.csv')

X = music_data.drop(columns=['genre'])

y = music_data['genre']

model = DecisionTreeClassifier()

model.fit(X, y)

dump(model, 'music-recommender.joblib')

model = load('music-recommender.joblib')

predictions = model.predict([[21, 1]])

print(predictions)

file:///C:/Users/kenny.ralph/Downloads/Machine Learning Project (2).html 1/3


05/02/2023 22:44 Machine Learning Project

['HipHop']

C:\Users\kenny.ralph\Anaconda3\lib\site-packages\sklearn\base.py:450: UserWarning: X does not have valid feature names, but Deci
sionTreeClassifier was fitted with feature names

warnings.warn(

In [34]: # import pandas as pd

from sklearn.tree import DecisionTreeClassifier

from sklearn import tree

music_data = pd.read_csv('music.csv')

X = music_data.drop(columns=['genre'])

y = music_data['genre']

model = DecisionTreeClassifier()

model.fit(X, y)

tree.export_graphviz(model, out_file='music-recommender.dot',

feature_names=['age', 'gender'],

class_names=sorted(y.unique()),

label='all',

rounded=True,

filled=True)

music-recommender.dot:
digraph Tree {
node [shape=box, style="filled, rounded", color="black", fontname="helvetica"] ;
edge [fontname="helvetica"] ;
0 [label="age <= 30.5\ngini =
0.778\nsamples = 18\nvalue = [3, 6, 3, 3, 3]\nclass = Classical", fillcolor="#e5fad7"] ;
1 [label="gender <= 0.5\ngini = 0.75\nsamples = 12\nvalue = [3, 0, 3, 3,
3]\nclass = Acoustic", fillcolor="#ffffff"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="age <= 25.5\ngini = 0.5\nsamples = 6\nvalue = [3,
0, 3, 0, 0]\nclass = Acoustic", fillcolor="#ffffff"] ;
1 -> 2 ;
3 [label="gini = 0.0\nsamples = 3\nvalue = [0, 0, 3, 0, 0]\nclass = Dance", fillcolor="#39e5c5"] ;
2 -> 3 ;
4
[label="gini = 0.0\nsamples = 3\nvalue = [3, 0, 0, 0, 0]\nclass = Acoustic", fillcolor="#e58139"] ;
2 -> 4 ;
5 [label="age <= 25.5\ngini = 0.5\nsamples = 6\nvalue =
[0, 0, 0, 3, 3]\nclass = HipHop", fillcolor="#ffffff"] ;
1 -> 5 ;
6 [label="gini = 0.0\nsamples = 3\nvalue = [0, 0, 0, 3, 0]\nclass = HipHop", fillcolor="#3c39e5"] ;
5 -> 6 ;
7
[label="gini = 0.0\nsamples = 3\nvalue = [0, 0, 0, 0, 3]\nclass = Jazz", fillcolor="#e539c0"] ;
5 -> 7 ;
8 [label="gini = 0.0\nsamples = 6\nvalue = [0, 6, 0, 0, 0]\nclass =
Classical", fillcolor="#7be539"] ;
0 -> 8 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;

file:///C:/Users/kenny.ralph/Downloads/Machine Learning Project (2).html 2/3


05/02/2023 22:44 Machine Learning Project

The script uses a decision tree classifier algorithm to train the music recommendation model, using the 'age' and 'gender' columns as input
features to predict the music genre.
Import the required libraries: 'pandas' and 'sklearn'.
Read a music data set from a .csv file into a pandas
DataFrame.
Define the input features (X) and target variable (y) for the model. X is the data in the DataFrame except the 'genre' column, and y is
the 'genre' column.
Create an instance of the DecisionTreeClassifier class, fitting the model to the data.
Export the model to a .dot file, which can
be visualized with graphviz to show the structure of the decision tree.

file:///C:/Users/kenny.ralph/Downloads/Machine Learning Project (2).html 3/3

You might also like