Home > Net >  FileNotFoundError: [Errno 2] No such file or directory: './iris.csv'
FileNotFoundError: [Errno 2] No such file or directory: './iris.csv'

Time:12-03

I'm getting this error for my Python code using the IDLE Shell and I'm not sure how to resolve it. I've tried downloading and adding the iris.csv file into the same place as the following .py file but it just gives me another set of errors like shown. If someone could help me it would be greatly appreciated!

-------------------------------------------------------------------------------------------------------------------------------

Traceback (most recent call last):
  File "C:\Users\kyle_\OneDrive\Documents\COIS 4400H\Lab 5.py", line 17, in <module>
    iris_df = pd.read_csv('./iris.csv')
  File "C:\Users\kyle_\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\kyle_\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\util\_decorators.py", line 317, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\kyle_\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\io\parsers\readers.py", line 950, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\kyle_\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\io\parsers\readers.py", line 605, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\kyle_\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\io\parsers\readers.py", line 1442, in __init__
    self._engine = self._make_engine(f, self.engine)
  File "C:\Users\kyle_\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\io\parsers\readers.py", line 1729, in _make_engine
    self.handles = get_handle(
  File "C:\Users\kyle_\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\io\common.py", line 857, in get_handle
    handle = open(
FileNotFoundError: [Errno 2] No such file or directory: './iris.csv'

This is the code that I'm using:

-------------------------------------------------------------------------------------------------------------------------------

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

import datetime as dt

import sklearn
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score

from scipy.cluster.hierarchy import linkage
from scipy.cluster.hierarchy import dendrogram
from scipy.cluster.hierarchy import cut_tree

iris_df = pd.read_csv('./iris.csv')

iris_df.head()

iris_df['iris'].drop_duplicates()

iris_df = iris_df.drop('iris',axis=1)

scaler = StandardScaler()

iris_df_scaled = scaler.fit_transform(iris_df)
iris_df_scaled.shape

sse = []
range_n_clusters = [2, 3, 4, 5, 6, 7, 8 , 9 , 10 ]
for num_clusters in range_n_clusters:
    kmeans = KMeans(n_clusters=num_clusters, max_iter=50)
kmeans.fit(iris_df_scaled)

sse.append(kmeans.inertia_)

plt.plot(sse)

# 1. Kmeans with k=3
kmeans = KMeans(n_clusters=3, max_iter=50)
y = kmeans.fit_predict(iris_df_scaled)

y

iris_df['Label'] = kmeans.labels_
iris_df.head()

plt.scatter(iris_df_scaled[y == 0, 0], iris_df_scaled[y == 0, 1], s = 100, c = 'purple', label = 'Iris-setosa')
plt.scatter(iris_df_scaled[y == 1, 0], iris_df_scaled[y == 1, 1], s = 100, c = 'orange', label = 'Iris-versicolour')
plt.scatter(iris_df_scaled[y == 2, 0], iris_df_scaled[y == 2, 1], s = 100, c = 'green', label = 'Iris-virginica')

# 2. Hierarchical clustering
plt.figure(figsize=(15, 5))
mergings = linkage(iris_df_scaled,method='complete',metric='euclidean')
dendrogram(mergings)
plt.show()

cluster_hier = cut_tree(mergings,n_clusters=3).reshape(-1)

iris_df['Label'] = cluster_hier
iris_df.head()

plt.scatter(iris_df_scaled[cluster_hier == 0, 0], iris_df_scaled[cluster_hier == 0, 1], s = 100, c = 'purple', label = 'Iris-setosa')
plt.scatter(iris_df_scaled[cluster_hier == 1, 0], iris_df_scaled[cluster_hier == 1, 1], s = 100, c = 'orange', label = 'Iris-versicolour')
plt.scatter(iris_df_scaled[cluster_hier == 2, 0], iris_df_scaled[cluster_hier == 2, 1], s = 100, c = 'green', label = 'Iris-virginica')

CodePudding user response:

Looks like you are looking in the file path that ends with \Lab 5 .py, which will just contain your python script. So you need to look one layer "above" your python script, which is the directory containing the script and the iris.csv-file.

Try: iris_df = pd.read_csv('../iris.csv')

CodePudding user response:

Does using the full path fix the issue:

iris_df = pd.read_csv('C:\Users\kyle_\OneDrive\Documents\COIS 4400H\iris.csv')

Alternatively, you could try changing the current working directory to the directory where the file is located, and then use a relative path to the file, like this:

import os
os.chdir('C:\Users\kyle_\OneDrive\Documents\COIS 4400H')
iris_df = pd.read_csv('./iris.csv')
  • Related