Issue
When i run my code i am facing an error
AttributeError: 'DataFrame' object has no attribute '_validate_params'
Here is my code:
import seaborn as sb
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression as lr
from sklearn import metrics
lol = {"exp":[0,2,5,6,10,7,9,3,5,4,6,1,2,0,0,4,5,8,7,6],
"sales":[10,20,15,12,14,18,19,20,25,10,5,7,8,20,15,17,18,25,14,17],
"manage":[5,2,9,8,3,6,7,5,4,2,1,5,8,7,9,4,2,0,1,5],
"get":[0,1,1,1,0,0,1,1,0,1,0,1,0,1,0,1,1,0,0,1]}
data = pd.DataFrame(lol,columns=["exp","sales","manage","get"])
x = data[["exp", "sales", "manage"]]
y = data["get"]
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=0)
lr.fit(x_train,y_train)
x_pred = lr.predict(x_test)
conf_mat = pd.crosstab(y_test,x_pred,rownames="True?",colnames="Pred")
sb.heatmap(conf_mat,annot=True)
print("Accuracy:" ,metrics.accuracy_score(y_test,x_pred))
plt.show()
Solution
The issues were with how you were importing LogisticRegression
, and the syntax for pd.crosstab
. See below for fixes.
import seaborn as sb
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
data_dict = {"exp":[0,2,5,6,10,7,9,3,5,4,6,1,2,0,0,4,5,8,7,6],
"sales":[10,20,15,12,14,18,19,20,25,10,5,7,8,20,15,17,18,25,14,17],
"manage":[5,2,9,8,3,6,7,5,4,2,1,5,8,7,9,4,2,0,1,5],
"get":[0,1,1,1,0,0,1,1,0,1,0,1,0,1,0,1,1,0,0,1]}
data = pd.DataFrame(data_dict)
x = data[["exp", "sales", "manage"]]
y = data["get"]
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=0)
lr = LogisticRegression() #Make a new LogisticRegression instance
lr.fit(x_train,y_train) #Fit the new instance
x_pred = lr.predict(x_test)
conf_mat = pd.crosstab(index=y_test, columns=x_pred) #rows=y, columns=predictions
ax = sb.heatmap(conf_mat,annot=True, )
ax.set_xlabel('prediction')
ax.set_ylabel('label')
print("Accuracy:" ,metrics.accuracy_score(y_test,x_pred))
plt.show()
Answered By - some3128
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.