Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

python - 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...