[英]Python is stating my file is not in the directory even though it is saved on my computer. what is wrong?
[英]Why do I get FileNotFound in Python even though I have the file saved on my computer?
我需要使用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'不存在
我認為您的解釋器未在存儲腳本的文件夾中運行該腳本。
嘗試使用絕對路徑引用文件。
例如
data = pd.read_csv("C:\\\\Users\\\\Owner\\\\Documents\\\\file.csv")
對於Windows
data = pd.read_csv("/home/{username}/data.csv")
對於Linux
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.