You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am using VB.Net. Below is a sample code that produces the same error.
Imports System
Imports System.Reflection
Imports Python.Runtime
Public Class Form1
Private Shared m_Scope As PyModule
Private Sub btnInitPython_Click(sender As Object, e As EventArgs) Handles btnInitPython.Click
Dim aPythonPath(5) As String
aPythonPath(0) = "C:\Users\NICE531\anaconda3\envs\pyflow_env\python39.dll"
aPythonPath(1) = "C:\Users\NICE531\anaconda3\envs\pyflow_env"
aPythonPath(2) = "C:\Users\NICE531\anaconda3\envs\pyflow_env"
aPythonPath(3) = "N"
aPythonPath(4) = ""
Dim sConntionScript As String = PyEngine.Init(aPythonPath(0), aPythonPath(1), aPythonPath(2), aPythonPath(3), aPythonPath(4))
MsgBox(sConntionScript)
End Sub
Private Sub btnExecScript_Click(sender As Object, e As EventArgs) Handles btnExecScript.Click
Dim sScript As String = "
import numpy as np, pandas as pd
import json
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_regression
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
X, y = make_regression(n_samples=1000, n_features=10, n_informative=5, n_targets=3, random_state=1, noise=0.5)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=4)
rfr_model, rfr_pred = rfr_model_traing(X_train, y_train, X_test, y_test)
"
Dim sResult As String = PyEngine.PyExec(sScript)
MsgBox(sResult)
End Sub
Public Shared Function Init(ByVal aPythonDLL As String, ByVal aPythonHome As String, ByVal aPythonInstallPath As String, ByVal aisPythnVenv As String, ByVal aPythonPath As String) As String
Dim sConntionVersion As String = String.Empty
Try
Runtime.PythonDLL = aPythonDLL
If aisPythnVenv <> "N" Then
Environment.SetEnvironmentVariable("VIRTUAL_ENV", aPythonInstallPath, EnvironmentVariableTarget.Process)
Else
Environment.SetEnvironmentVariable("PYTHONHOME", aPythonHome, EnvironmentVariableTarget.Process)
End If
Environment.SetEnvironmentVariable("PYTHONPATH", aPythonPath, EnvironmentVariableTarget.Process)
PythonEngine.Initialize()
PythonEngine.BeginAllowThreads()
Using Py.GIL()
m_Scope = Py.CreateScope
End Using
sConntionVersion = "Python " & PythonEngine.Version
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
Return sConntionVersion
End Function
Public Shared Function PyExec(ByVal aScript As String) As String
Try
Using Py.GIL()
m_Scope.Exec(aScript)
End Using
Return "finish"
Catch ex As Exception
Return ex.Message
End Try
End Function
End Class
Python Initialization
Screen that appears when running Python code
Message that appears when exiting a generated screen
If there was a crash, please include the traceback here.
This discussion was converted from issue #2448 on September 12, 2024 18:47.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Environment
Details
I want to train a machine learning model using PythonNet.
I use GridSearchCV to train the model.
Minimal, Complete, and Verifiable example
this will help us understand the issue.
I am using VB.Net. Below is a sample code that produces the same error.
Imports System
Imports System.Reflection
Imports Python.Runtime
Public Class Form1
Private Shared m_Scope As PyModule
import numpy as np, pandas as pd
import json
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_regression
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV
def model_best_parameter(model, params, x, y):
grs = GridSearchCV(estimator=model, param_grid=params, cv=2, n_jobs=None ,scoring='neg_mean_squared_error')
grs.fit(x, y)
best_model = grs.best_estimator_
def rfr_model_traing(x, y, x_test, y_test):
rfr_model = RandomForestRegressor(random_state=8)
params = {'max_depth':[12, 14, 16, 18, 20, 22, 24]}
rfr_model = model_best_parameter(rfr_model, params, x, y)
rfr_model.fit(x, y)
pred_data = rfr_model.predict(x_test)
X, y = make_regression(n_samples=1000, n_features=10, n_informative=5, n_targets=3, random_state=1, noise=0.5)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=4)
rfr_model, rfr_pred = rfr_model_traing(X_train, y_train, X_test, y_test)
"
Dim sResult As String = PyEngine.PyExec(sScript)
MsgBox(sResult)
End Sub
End Class
Python Initialization
Screen that appears when running Python code
Message that appears when exiting a generated screen
No error log is produced
All reactions