简体   繁体   中英

Syntax error in python machine learning sklearn code mosh Python course. Id appreciate if someone could hep me

I am doing a Python course with Mosh programming and am getting an error with some machine learning code

The code is this ->

import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

music_data = pd.read_csv('music.csv')
X = music_data.drop(columns=['genre'])
y = music_data['genre']

model = DecisionTreeClassifier
X_train, X_test, y_train, y_train = train_test_split(X, y, test_size=0.2)
model.fit(X_train, y_train)
predictions = model.predict(X_test)

score = accuracy_score(y_test, predictions)
score

I am getting an error that says ->

TypeError                                 Traceback (most recent call last)
<ipython-input-28-0880d58e9ac4> in <module>
 10 model = DecisionTreeClassifier
 11 X_train, X_test, y_train, y_train = train_test_split(X, y, test_size=0.2)
---> 12 model.fit(X_train, y_train)
 13 predictions = model.predict(X_test)
 14 

TypeError: fit() missing 1 required positional argument: 'y'

I'm new to these libraries and am able to resolve the issue Id grateful if you could help me.

The mistake comes from the fact you are not generating the instance of the model.

model = DecisionTreeClassifier

This is incorrect, because DecisionTreeClassifier is a function, not a method. To create the model, you need to replace the line of code above with:

model = DecisionTreeClassifier()

This will create model and now you can pass data to fit it and perform all further operations (predict, score, etc...).

Also, as Gavin Wong pointed, there's a mistake when using train_test_split() because you defined y_train twice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM