Search code examples
machine-learningspyderdecision-treeanalysis

x and y must have same first dimension, but have shapes (4536, 32) and (1944, 24)


I am a beginner in the machine learning world and I am stuck in somewhere. I really need some help. I have a dataset consisting of State names,Month, temperature and rainfall. My code is:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data=pd.read_csv('cropdata.csv')
x=data.iloc[:, :-1].values
y=data.iloc[:, 4].values


district = pd.get_dummies(data['District'],drop_first = False)
month = pd.get_dummies(data['Month'],drop_first = False)
crop = pd.get_dummies(data['Crop'],drop_first = False)
data= pd.concat([data,district],axis=1)
data.drop('District', axis=1,inplace=True)
data= pd.concat([data,month],axis=1)
data.drop('Month', axis=1,inplace=True)
data= pd.concat([data,crop],axis=1)
data.drop('Crop', axis=1,inplace=True)

print(data.head(1))

train=data.iloc[:, 0:44].values
test=data.iloc[: ,44:].values

from sklearn.preprocessing import Imputer
imputer1 = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer1 = imputer1.fit(train[:, 0:44])
train[:, 0:44] = imputer1.transform(train[:, 0:44])


from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(train,test,test_size=0.3)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)


from sklearn.tree import DecisionTreeRegressor
from sklearn.datasets import load_iris
clf=DecisionTreeRegressor(max_depth = 19,random_state = None)

#Fitting the classifier into training set
clf.fit(X_train,y_train)
pred=clf.predict(X_test)

print(pred)
predx=pred.round()


from sklearn.metrics import accuracy_score
# Finding the accuracy of the model
a=accuracy_score(y_test,pred.round())
print("The accuracy of this model is: ", a*100)


from sklearn import tree
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
plt.figure(figsize=(10,10))
tree.plot_tree(clf);

The accuracy of the model is 70% but with the error :

ValueError: x and y must have same first dimension, but have shapes (4536, 44) and (1944, 12)

Now I don't understand what can I do to remove the error and how can I plot the graph from this question?


Solution

  • Acc to you error your X_train data contain 4536 row for training dataset then it implies that each row having it's own target value (label) so label value should be 4536(y_train)

    But your y_train contain only 1944 label which is not matching with X_train required label

    each X_train required corresponding label you provide some of label and rest are unlabel