使用Shell脚本运行python脚本时如何解决此错误

我正在尝试运行ML算法-KNN回归函数。 我已经在jupyter笔记本和vs代码上成功运行了此代码

然后我使用Shell脚本运行python脚本。但是,生成了下面的错误代码。我不确定是什么问题。

我在安装了依赖项的虚拟环境中运行。另外,通过shell脚本读取的requirements.txt文件生成了requirements.txt。

有人能协助我吗?

def knn_regressor(preprocess_lr,x_train,x_test,y_train,y_test):
    #combine pre-processing with ML algorithm - KNNRegression
    pipeline = make_pipeline(preprocess_lr,KNeighborsRegressor())
    params = {
        'kneighborsregressor__n_neighbors': range(2,21),'kneighborsregressor__weights': ['uniform','distance']
        }
    model_3 = gridsearchcv(pipeline,params,cv=5,scoring='neg_mean_squared_error')
    #train the pipeline
    model_3.fit(x_train,y_train)
    #fit the model on the test data
    pred_test = model_3.predict(x_test)
    #display the results of the metrics
    rmse_model = np.sqrt(mean_squared_error(y_test,pred_test))
    r2_model = r2_score(y_test,pred_test)
    print("..........")
    print("Results on Test Data for KNN Regressor")
    print("RMSE - KNN Regressor: {:.2f}".format(rmse_model))
    print("R2 score - KNN Regressor: {:.5f}".format(r2_model))
    if(int(sys.argv[1]) == 1):
        print("Applying KNN Regressor algorithm for prediction...")
        knn_regressor(preprocess_lr,y_test)
shell script:

#!/usr/bin/env bash

pip install -r requirements.txt

echo "Please select your algorithm: "
echo "1 - KNN Regressor"

python src/module1.py $user_algo

错误消息:

 knn_regressor(preprocess_lr,y_test)
  File "src/module1.py",line 62,in knn_regressor
    model_3.fit(x_train,y_train)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\utils\validation.py",line 72,in inner_f
    return f(**kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\model_selection\_search.py",line 736,in fit
    self._run_search(evaluate_candidates)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\model_selection\_search.py",line 1188,in _run_search
    evaluate_candidates(ParameterGrid(self.param_grid))
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\model_selection\_search.py",line 708,in evaluate_candidates
    out = parallel(delayed(_fit_and_score)(clone(base_estimator),File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\joblib\parallel.py",line 1029,in __call__
    if self.dispatch_one_batch(iterator):
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\joblib\parallel.py",line 847,in dispatch_one_batch
    self._dispatch(tasks)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\joblib\parallel.py",line 765,in _dispatch
    job = self._backend.apply_async(batch,callback=cb)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\joblib\_parallel_backends.py",line 208,in apply_async
    result = ImmediateResult(func)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\joblib\_parallel_backends.py",line 572,in __init__
    self.results = batch()
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\joblib\parallel.py",line 252,in __call__
    return [func(*args,**kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\joblib\parallel.py",in <listcomp>
    return [func(*args,**kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\model_selection\_validation.py",line 560,in _fit_and_score
    test_scores = _score(estimator,X_test,y_test,scorer)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\model_selection\_validation.py",line 607,in _score
    scores = scorer(estimator,y_test)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\metrics\_scorer.py",line 87,in __call__
    score = scorer._score(cached_call,estimator,File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\metrics\_scorer.py",line 206,in _score
    y_pred = method_caller(estimator,"predict",X)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\metrics\_scorer.py",line 53,in _cached_call
    return getattr(estimator,method)(*args,**kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\utils\Metaestimators.py",line 119,in <lambda>
    out = lambda *args,**kwargs: self.fn(obj,*args,**kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\pipeline.py",line 408,in predict
    return self.steps[-1][-1].predict(Xt,**predict_params)
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\sklearn\neighbors\_regression.py",line 185,in predict
    y_pred = np.mean(_y[neigh_ind],axis=1)
  File "<__array_function__ internals>",line 5,in mean
  File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\numpy\core\fromnumeric.py",line 3372,in mean
    return _methods._mean(a,axis=axis,dtype=dtype,File "C:\Users\user\AppData\Local\Programs\Python\python38\lib\site-packages\numpy\core\_methods.py",line 162,in _mean
    ret = um.true_divide(
TypeError: unsupported operand type(s) for /: 'str' and 'int'

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...