简体   繁体   English

Python 脚本无法在我的计算机上运行特定文件

[英]Python script not working on my computer for a specific file

I'm using Python 3.7 and learning on a published code.我正在使用 Python 3.7 并学习已发布的代码。 In my first attempt to write the code, it ran without errors which means all of the necessary libraries (NumPy, Pandas, scikit-learn) should have been installed properly by me.在我第一次尝试编写代码时,它运行没有错误,这意味着我应该正确安装所有必要的库(NumPy、Pandas、scikit-learn)。 When I changed the source file in order to work on different data, it stopped working.当我更改源文件以处理不同的数据时,它停止工作。 However, it works on another computer.但是,它可以在另一台计算机上运行。 I updated the system with all of the apps and tried to shorten the imported file, but it didn't change anything.我用所有应用程序更新了系统并尝试缩短导入的文件,但它没有改变任何东西。 The input files are identical on both computers.两台计算机上的输入文件相同。

This is the error with warnings:这是带有警告的错误:

/usr/lib64/python3.7/site-packages/sklearn/externals/six.py:7: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
/usr/lib64/python3.7/site-packages/sklearn/utils/__init__.py:4: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
  from collections import Sequence
/usr/lib64/python3.7/site-packages/sklearn/model_selection/_split.py:18: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
  from collections import Iterable
/usr/lib64/python3.7/site-packages/sklearn/model_selection/_search.py:16: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
  from collections import Mapping, namedtuple, defaultdict, Sequence
/usr/lib64/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.
  from numpy.core.umath_tests import inner1d
Traceback (most recent call last):
  File "algorithm2.py", line 26, in <module>
    classifier.fit(X_train, y_train)
  File "/usr/lib64/python3.7/site-packages/sklearn/ensemble/forest.py", line 248, in fit
    y = check_array(y, accept_sparse='csc', ensure_2d=False, dtype=None)
  File "/usr/lib64/python3.7/site-packages/sklearn/utils/validation.py", line 453, in check_array
    _assert_all_finite(array)
  File "/usr/lib64/python3.7/site-packages/sklearn/utils/validation.py", line 44, in _assert_all_finite
    " or a value too large for %r." % X.dtype)
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

This is the code:这是代码:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

data=open("./data3.csv")

headernames=data.readline()
dataset=pd.read_csv("data3.csv")
dataset.head()

X = dataset.iloc[:, :-2].values
y = dataset.iloc[:, -1].values

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30)

from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 50)
classifier.fit(X_train, y_train)

y_pred = classifier.predict(X_test)

from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
result = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
print(result)
result1 = classification_report(y_test, y_pred)
print("Classification Report:",)
print (result1)
result2 = accuracy_score(y_test,y_pred)
print("Accuracy:", result2)

Are you using the imp module in your code?您在代码中使用imp模块吗? Like import imp ?喜欢import imp
If so, can you try using import importlib .如果是这样,您可以尝试使用import importlib

Also, I see warnings related to collections .另外,我看到与collections相关的警告。
Try to check the answer provided in this SO answer尝试检查此 SO 答案中提供的答案

Apart from these, you have an error of除了这些,你还有一个错误
ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
You might have to take a look at your input file once again.您可能需要再次查看您的输入文件。 It seems there are issues with the input.输入法好像有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此python脚本使我的计算机崩溃 - Why is this python script crashing my computer 在整个计算机中搜索文件的python脚本 - A python script that searchs for a file in the entire computer 无法在我电脑中的所有位置运行的exe的Python文件仅在python安装中的dist文件夹中有效 - Python file to exe not working in all locations in my computer only works in dist folder in python installation 使用Python在计算机上打开文件 - Open file on my computer using Python Python:为什么我的SMTP脚本会冻结计算机? - Python: Why does my SMTP script freeze my computer? 使用我自己的python脚本向我的计算机进行身份验证 - Using my own python script to authenticate into my computer Python:将我的脚本与其他计算机上的所有依赖项一起使用 - Python: Using my Script with all dependencies on a different computer 如何使用python脚本关闭计算机? - How can I shut down my computer using python script? 在线监控python脚本的进度,或在我的手机上,或另一台计算机上 - Monitoring the progress of a python script online, or on my phone, or another computer 在另一台计算机上将 python 库的特定行作为 exe 文件执行时出错 - Error executing specific lines of a python library on another computer as exe file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM