Search code examples
pythonpython-2.7machine-learningscikit-learnk-means

TypeError: predict() missing 1 required positional argument: 'X' in sklearn KMeans


import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import warnings
import graphviz
warnings.filterwarnings('ignore')
%matplotlib inline
import os
from sklearn.cluster import KMeans

#Importing the dataset
Diabetes2 = pd.read_csv('C:\\Users\\PPP\\Desktop\\Python_Practice\\Datasets\\Diabetes\\diabetic_data.csv', index_col=False)

#Split into input and output features
y = Diabetes2["readmittedFL"]
X = Diabetes2[["time_in_hospital","num_lab_procedures","num_procedures","num_medications",
               "number_outpatient","number_emergency","number_inpatient","number_diagnoses"]]
X.head()
y.head()

#Select the annual income and the spending score columns 
KMeans = KMeans()
X_array=X.iloc[0:8:1].values
y_kmeans = KMeans.predict(X_array)
plt.scatter(X_array[:, 0], X_array[:, 1], c=y_kmeans, s=50, cmap='viridis')

I just started practicing KMeans where I am getting this error. I can not understand what is wrong.

When I run the command y_kmeans = KMeans.predict(X_array), it was showing an error TypeError: predict() missing 1 required positional argument: X

How can I solve this error?


Solution

  • You need yo use fit_predict not predict

    • fit_predict : Train and predict your train point cluster
    • Predict: used for inference when you get new data points that are not present on your train data

    Also your not specifying the number of clusters via n_clusters argument it will take default number which is 8

    for more info check this link