我有一个简单的Python脚本,粘贴在下面:
import sklearn
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics
def get_average(arr):
return sum(arr) / len(arr)
def get_median(arr):
arr = sorted(arr)
listlength = len(arr)
num = listlength//2
if listlength%2==0:
middlenum = (arr[num]+arr[num-1])/2
else:
middlenum = arr[num]
return middlenum
iris = load_iris()
sklearn.utils.Bunch
# Data
X = iris.data
# Classes
y= iris.target
# Split data
X_train, X_test, y_train, y_test = train_test_split (X,y,test_size = 0.2, random_state = 4)
scores = []
best_k = 0
best_score = 0
for k in range (1,30):
knn = KNeighborsClassifier(n_neighbors = k)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
score = metrics.accuracy_score(y_test, y_pred)
if score > best_score:
best_k = k
scores.append(score)
# print(scores)
print("Your scores were: ", scores)
print("The best k value was: ", k)
print("The average score was: ", get_average(scores) * 100)
print("The median score was: ", get_median(scores) * 100)
我正尝试将其编译为没有Python的老板的.exe
文件,然后在他的计算机上运行它。在使用gcc
和Java使用javac
之前,我只编译过C和C ++代码。
我尝试使用pyinstaller
模块来编译.exe
。但是,当我运行.exe
> Traceback (most recent call last): File "knn_example_iris.py", line
> 38, in <module>
> from sklearn.neighbors import KNeighborsClassifier File "c:\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
> line 627, in exec_module
> exec(bytecode, module.__dict__) File "site-packages\sklearn\neighbors\__init__.py", line 6, in <module>
> File "sklearn\neighbors\dist_metrics.pxd", line 48, in init
> sklearn.neighbors.ball_tree File
> "sklearn\neighbors\dist_metrics.pyx", line 52, in init
> sklearn.neighbors.dist_metrics ImportError: No module named 'typedefs'
> [12564] Failed to execute script knn_example_iris
我尝试了以下步骤:
python -m pip install --upgrade pip
)python -m pip install --upgrade setuptools
)dist
,然后重试我确实制作了另一个示例文件,
print("hello world").py
编译并运行正常。
我也尝试过重新编译文件,并在我第一次错过的编译器中发现了许多WARNING
消息。
> temporary directory at C:\Users\J39304\AppData\Local\Temp\tmpoxunow1d
> "C:/Users/jerry/Desktop/knn_example_iris.py" 54322 INFO: PyInstaller:
> 3.4 54322 INFO: Python: 3.5.0 54324 INFO: Platform: Windows-10.0.16299 54331 INFO: wrote
> C:\Users\jerry\AppData\Local\Temp\tmpoxunow1d\knn_example_iris.spec
> 54336 INFO: UPX is not available. 54355 INFO: Extending PYTHONPATH
> with paths ['C:\\Users\\J39304\\Desktop',
> 'C:\\Users\\jerry\\AppData\\Local\\Temp\\tmpoxunow1d'] 54355 INFO:
> checking Analysis 54356 INFO: Building Analysis because
> Analysis-00.toc is non existent 54356 INFO: Initializing module
> dependency graph... 54359 INFO: Initializing module graph hooks...
> 54362 INFO: Analyzing base_library.zip ... 58531 INFO: running
> Analysis Analysis-00.toc 58760 WARNING: lib not found:
> api-ms-win-crt-stdio-l1-1-0.dll dependency of
> c:\python35-32\python.exe 58976 WARNING: lib not found:
> api-ms-win-crt-heap-l1-1-0.dll dependency of c:\python35-32\python.exe
> 59161 WARNING: lib not found: api-ms-win-crt-math-l1-1-0.dll
> dependency of c:\python35-32\python.exe 59347 WARNING: lib not found:
> api-ms-win-crt-runtime-l1-1-0.dll dependency of
> c:\python35-32\python.exe 59554 WARNING: lib not found:
> api-ms-win-crt-locale-l1-1-0.dll dependency of
> c:\python35-32\python.exe 59745 WARNING: lib not found:
> api-ms-win-crt-stdio-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 59941 WARNING: lib not found:
> api-ms-win-crt-string-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 60129 WARNING: lib not found:
> api-ms-win-crt-convert-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 60316 WARNING: lib not found:
> api-ms-win-crt-heap-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 60499 WARNING: lib not found:
> api-ms-win-crt-runtime-l1-1-0.dll dependency of
> c:\python35-32\VCRUNTIME140.dll 60702 WARNING: lib not found:
> api-ms-win-crt-process-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 60883 WARNING: lib not found:
> api-ms-win-crt-stdio-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61074 WARNING: lib not found:
> api-ms-win-crt-string-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61255 WARNING: lib not found:
> api-ms-win-crt-convert-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61436 WARNING: lib not found:
> api-ms-win-crt-math-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61624 WARNING: lib not found:
> api-ms-win-crt-heap-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61804 WARNING: lib not found:
> api-ms-win-crt-runtime-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 61986 WARNING: lib not found:
> api-ms-win-crt-filesystem-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62185 WARNING: lib not found:
> api-ms-win-crt-conio-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62369 WARNING: lib not found:
> api-ms-win-crt-time-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62547 WARNING: lib not found:
> api-ms-win-crt-locale-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62734 WARNING: lib not found:
> api-ms-win-crt-environment-l1-1-0.dll dependency of
> c:\python35-32\python35.dll 62741 INFO: Caching module hooks... 62748
> INFO: Analyzing C:\Users\J39304\Desktop\knn_example_iris.py 63449
> INFO: Processing pre-find module path hook distutils 65132 INFO:
> Processing pre-find module path hook site 65133 INFO: site:
> retargeting to fake-dir
> 'c:\\python35-32\\lib\\site-packages\\PyInstaller\\fake-modules' 67350
> INFO: Processing pre-safe import module hook
> setuptools.extern.six.moves 78457 INFO: Loading module hooks... 78457
> INFO: Loading module hook "hook-setuptools.py"...
答案 0 :(得分:2)
实际上pyinstaller不支持sklearn的导入,请参阅Pyinstaller Supported Packages,经过多次研究,合适的答案似乎是hook
,但是我没有花足够的时间来解释它正确地
相反,对我有用的是,手动导入所需的lib /方法,它应该像这样:
编辑
将此添加到导入列表:
import sklearn.neighbors.typedefs
运行pyinstaller yourfile.py
即使有警告和错误消息,也要等到它结束。
然后我们需要在\\dist\\test\\sklearn\\datasets
中创建两个目录:
data
,您需要在其中复制并粘贴iris.csv
中的C:\Users\{your_name}\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\sklearn\datasets\data
descr
,您需要在其中复制并粘贴iris.rst
中的C:\Users\{your_name}\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\sklearn\datasets\descr
请注意,这是我的python版本(Python37-32)
希望有帮助