illuminaite-academy / illuminaite-academy.github.io

0 stars 8 forks source link

Third Blog #5

Open isaiahoh opened 2 weeks ago

isaiahoh commented 2 weeks ago
Napwillcode8848 commented 3 days ago

READING A CSV FILE WITH MATPLOTLIB

Content:

Introduction

How to open a CSV file in Python

How to read a file

Matplotlib Introduction

A Step by Step Guide

Load the CSV file

data = pd.read_csv('filename.csv')

Display the first few rows of the dataframe

print(filename.head())

   * Visualise it with some basic methods:  

Plot the data

plt.plot(data['Date'], data['Sales'])

Add labels and title

plt.xlabel('Date') plt.ylabel('Sales') plt.title('Sales Over Time')

Show the plot

plt.xticks(rotation=45) # Rotate date labels for better readability plt.show()

Application of the CSV file with a house sale example!

With the given example CSV file, we will explore how to “select columns” and “predict the price of a house based on the data!”

How to Select a Column?

 House_Size (sqft)   Price ($)

0 1000 200000 1 1500 300000 2 2000 400000 3 2500 500000 4 3000 600000

Now, let’s try to predict how much it would cost if I had this size of a house.

Here is a step breakdown:

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

Step 1: Load the CSV file into a Pandas DataFrame

data = pd.read_csv('house_data.csv')

Step 2: Extract the 'House_Size' and 'Price' columns

house_size = data['House_Size (sqft)'].values.reshape(-1, 1) # Reshaping for sklearn price = data['Price ($)'].values

Step 3: Visualize the data using a scatter plot

plt.scatter(house_size, price, color='blue') plt.xlabel('House Size (sqft)') plt.ylabel('Price ($)') plt.title('House Size vs Price') plt.show()

Step 4: Fit a linear regression model to predict the price of a house based on its size

model = LinearRegression() model.fit(house_size, price)

Predict the price of a house for a given size, e.g., 2200 sqft

house_size_to_predict = np.array([[2200]]) predicted_price = model.predict(house_size_to_predict)

print(f'The predicted price for a house of size 2200 sqft is: ${predicted_price[0]:,.2f}')

  • This is what you should get from the following code: The predicted price for a house of size 2200 sqft is: $440,000.00