I developed the web application with asp.net webform. And I have decision tree model python 3.8.5 script created using Jupyter Notebook in Anaconda Navigator. I tried to more ways to input some value to textbox and after click send button, pass the input value to python method and return the output result to this Output Value Label. Still I cant find solution. Please anyone can help me. This is one of the functionality of my Final Project. I hope your asp.net c# and python code.
What I have tried:
protected void Button1_Click(object sender, EventArgs e)
{
float mpg = 15.2f;
float cylinders=8f;
float cubicinches=400f;
float hp=165f;
float weightlbs=4525f;
float timeto60=12f;
float year=1998f;
CallPython python = new CallPython(@"C:\python38\python.exe");
python.Run_Script(@"C:\SQL Dataset with Decision tree Prediction.py", mpg, cylinders, cubicinches, hp, weightlbs, timeto60, year);
}
public class CallPython
{
string pythonLocationString;
public CallPython(string pythonLocation)
{
pythonLocationString = pythonLocation;
}
public void Run_Script(string scriptPathName, float mpg, float cylinders, float cubicinches, float hp, float weightlbs, float timeto60, float year)
{
int ExitCode;
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = pythonLocationString;
processStartInfo.Arguments = scriptPathName;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.CreateNoWindow = true;
Process updateProcess = Process.Start(processStartInfo);
updateProcess.WaitForExit();
ExitCode = updateProcess.ExitCode;
updateProcess.Close();
updateProcess.ErrorDataReceived += Process_OutputDataReceived;
process.OutputDataReceived += Process_OutputDataReceived;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
Console.Read();
}
}
My Python Script:
# Load libraries
import pandas as pd
from sklearn.tree import DecisionTreeClassifier # Import Decision Tree Classifier
from sklearn.model_selection import train_test_split # Import train_test_split function
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation
from sqlalchemy import create_engine
mpg = 15.2;
cylinders=8;
cubicinches=400;
hp=165;
weightlbs=4525;
timeto60=12;
year=1998;
SERVER = 'DESKTOP-75BEPIE\\NEO'
DATABASE = 'FFAS'
DRIVER = 'SQL Server Native Client 11.0'
USERNAME = 'sa'
PASSWORD = 'sa'
DATABASE_CONNECTION = f'mssql://{USERNAME}:{PASSWORD}@{SERVER}/{DATABASE}?driver={DRIVER}'
engine = create_engine(DATABASE_CONNECTION)
connection = engine.connect()
data = pd.read_sql_query("select * from Dataset", connection)
data.head()
#split dataset in features and target variable
feature_cols = ['mpg', 'cylinders', 'cubicinches', 'hp','weightlbs','timeto60','year']
X = data[feature_cols] # Features
y = data.Decision # Target variable
ft = pd.get_dummies(data[ ['mpg', 'cylinders', 'cubicinches', 'hp', 'weightlbs', 'timeto60', 'year'] ])
ft
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1) # 70% training and 30% test
# Create Decision Tree classifer object
clf = DecisionTreeClassifier()
# Train Decision Tree Classifer
clf = clf.fit(X_train,y_train)
#Predict the response for test dataset
y_pred = clf.predict(X_test)
y_pred
# Model Accuracy, how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(y_test, y_pred))
# Using random forest to make sure my model doesn't overfit
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators = 20) #n_esitmators value can be changed according to need
clf = clf.fit(ft,data['Decision'])
#input parameters to Predict
clf.predict([[15.0,8,400,165,4525,12,1998]]))
I need to pass textbox value to this method clf.predict([[15.0,8,400,165,4525,12,1998]])) And output prediction view on output value label in asp.net web form