【问题标题】:Why do I get FileNotFound in Python even though I have the file saved on my computer?为什么我在 Python 中得到 FileNotFound,即使我已经将文件保存在我的计算机上?
【发布时间】:2019-01-17 17:37:06
【问题描述】:

我需要用 Python 绘制一些数据,但我无法让 spyder 找到包含数据的文件。

import numpy as np    
import matplotlib.pyplot as plt    
import pandas as pd    
from sklearn.linear_model import LinearRegression    

data = pd.read_csv('data(1).csv')    
X = data.iloc[:, 0].values.reshape(-1, 1) # values converts it into a numpy array    
Y = data.iloc[:, 1].values.reshape(-1, 1) # -1 means that calculate the dimension of rows, but have 1 column    
linear_regressor = LinearRegression()    
linear_regressor.fit(X, Y)    
Y_pred = linear_regressor.predict(X)           
plt.scatter(X, Y)    
plt.plot(X, Y_pred, color='red')    
plt.show()

它应该显示一个线性回归,但它只返回这个: FileNotFoundError: 文件 b'data(1).csv' 不存在

【问题讨论】:

  • csv 是否与您的脚本位于同一文件夹中?
  • 尝试使用文件的绝对路径并确保文件名与现有文件名匹配,包括字母(例如大写)、空格等。

标签: python linear-regression file-not-found


【解决方案1】:

我认为您的解释器没有运行您存储它的文件夹中的脚本。

尝试使用绝对路径来引用您的文件。

例如
data = pd.read_csv("C:\\Users\\Owner\\Documents\\file.csv") 用于 Windows
data = pd.read_csv("/home/{username}/data.csv") 用于 Linux

【讨论】:

  • @TorinMay 谢谢!已经忘记了。祝你有美好的一天!
猜你喜欢
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
  • 2015-06-25
  • 2021-01-07
  • 2019-10-01
  • 2021-08-31
相关资源
最近更新 更多