Numpy - Arrays - Loading a text file data using NumPy's loadtxt() function


#1

I am correctly getting the output but on submitting answer it says “path for housing.csv (HOUSING_PATH) not correct”

defining housing.csv file path

HOUSING_PATH = ‘/home/dmellorenal3274/cloudxlab_jupyter_notebooks/ml/machine_learning/datasets/housing/’

reading the large housing.csv file using pandas

housing_raw = pd.read_csv(os.path.join(HOUSING_PATH, “housing.csv”))

extracting only a few rows (5 rows) of data from the pandas dataframe ‘my_df’

my_df = housing_raw.iloc[ : 5]

creating a new small csv file - ‘housing_short.csv’ - containing the above extracted 5 rows of data

my_df.to_csv(‘housing_short.csv’, index=False)

FILE = ‘housing_short.csv’

def load_housing_data(file = FILE ):
return np.loadtxt(file, dtype={‘names’: (‘longitude’,‘latitude’,‘housing_median_age’,‘total_rooms’,‘total_bedrooms’,‘population’,‘households’,‘median_income’,‘median_house_value’,‘ocean_proximity’),‘formats’: (‘f8’, ‘f8’, ‘f8’, ‘f8’, ‘f8’, ‘f8’, ‘f8’, ‘f8’, ‘f8’, ‘|S15’)}, delimiter=’,’, skiprows=1, unpack=True)

longitude_arr,latitude_arr,housing_median_age_arr,total_rooms_arr,total_bedrooms_arr,population_arr,households_arr,median_income_arr,median_house_value_arr,ocean_proximity_arr = load_housing_data()

print(median_house_value_arr)