0
$\begingroup$

Today I am trying build ensemble model. Where I am working with iris dataset. In my model I am using LogisticRegression, KNeighborsClassifier, RandomForestClassifier. But when I am going run the program I get ValueError: Found input variables with inconsistent numbers of samples: [10, 150] error. Below I am giving my code:

df = pd.read_csv('/kaggle/input/iriscsv/Iris.csv')
df.head()
df

output --->

enter image description here

Then I am deleting id collumn from this dataset

df = df.iloc[:, 1:]

After this I am used LabelEncoder on Species column

df['Species'] = encoder.fit_transform(df['Species'])
import seaborn as sns 
sns.pairplot(df, hue = 'Species')

Output

from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score


clf1 = LogisticRegression()
clf2 = RandomForestClassifier()
clf3 = KNeighborsClassifier()

estimators = [('lr',clf1),('rf',clf2),('knn',clf3)]


for estimator in estimators:
    x = cross_val_score(estimator[1],x,y,cv=10,scoring='accuracy')
    print(estimator[0],np.round(np.mean(x),2))

After running the last estimator I am getting these error.

$\endgroup$

1 Answer 1

1
$\begingroup$

In your 2nd last line, you are overwriting the variable x, which previously held your input X data.

$\endgroup$
4
  • $\begingroup$ This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review $\endgroup$ Commented Sep 7, 2022 at 11:44
  • $\begingroup$ @Icrmorin Did you read the question, and subsequently read my answer - or did you just see a single sentence and assume? The question is: "Why am I getting ValueError". My answer is that the 2nd last line, is being overwirtten, causing ValueError. A very direct answer. If you have a better one go ahead and answer it. $\endgroup$
    – GooJ
    Commented Sep 7, 2022 at 20:21
  • $\begingroup$ I will conceed there is merit to asking if this should be on stack overflow rather than data science. $\endgroup$
    – GooJ
    Commented Sep 7, 2022 at 20:22
  • 1
    $\begingroup$ Yeah the automatic wording is a bit bad. For this kind of question I would usually vote to close and provide an answer in comment. I am sorry if this gave you the impression you are at fault here. $\endgroup$ Commented Sep 8, 2022 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.