У меня есть простой скрипт на 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)
Я пытаюсь скомпилировать его в файл .exe
для моего босса, у которого нет Python, и запустить его наего машина.Я только когда-либо компилировал код C и C ++ перед использованием gcc
, а Java использовал javac
.
. Я пытался использовать модуль 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
) - Обновление setuptools (
python -m pip install --upgrade setuptools
) для этого поста - Перекомпиляция
- Удаление
dist
и повторная попытка
Iсделал другой пример файла,
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"...