'How can I predict a result from new data with a trained existing model in python?
'''
#Defining Model Function
def models(X_train, Y_train):
log = LogisticRegression(random_state=0)
log.fit(X_train, Y_train)
tree = DecisionTreeClassifier(criterion='entropy', random_state=0)
tree.fit(X_train, Y_train)
forest = RandomForestClassifier(n_estimators=10, criterion='entropy', random_state=0)
forest.fit(X_train, Y_train)
return log, tree, forest
#Main Code
df = pd.read_csv("data.csv")
X = df.iloc[:, 2:31].values #Selecting Data from 3rd column to 32th column
Y = df.iloc[:, 1].values #Selecting Data from 2nd column
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25, random_state=0)
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.fit_transform(X_test)
model = models(X_train, Y_train)
newdata = pd.read_csv("new.csv") #This is new data
X_new = newdata.iloc[:, 2:31].values #These data I want to check with trained data
predictnewdata1=model[0].predict(X_test)
print("new data1") #Printing test data from model[0] i.e. log model
print(predictnewdata1)
predictnewdata2=model[1].predict((X_test))
print("new data2") #Printing test data from model[1] i.e. tree model
print(predictnewdata2)
predictnewdata3=model[2].predict(X_test)
print("new data3") #Printing test data from model[2] i.e. forest model
print(predictnewdata3)
predictnewdata4 = model[2].predict(X_new) #This code is anyhow wrong
print(predictnewdata4) #Printing wrong values
I want to test any model with new data, but somehow my algorithm/code isnt working. I searched a lot but couldnt find any solution. So please help in this regard.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
