0% found this document useful (0 votes)
7 views21 pages

DAlab Python Code

The LAB syllabus outlines a series of experiments focused on data preprocessing, machine learning models (including Linear Regression, Logistic Regression, Decision Trees, and Random Forest), time series analysis with ARIMA, object segmentation, and various visualization techniques. Each experiment includes code examples in Python for implementation, covering data handling, model fitting, and visualization. Additionally, there are instructions for performing descriptive and predictive analytics on healthcare and sales data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views21 pages

DAlab Python Code

The LAB syllabus outlines a series of experiments focused on data preprocessing, machine learning models (including Linear Regression, Logistic Regression, Decision Trees, and Random Forest), time series analysis with ARIMA, object segmentation, and various visualization techniques. Each experiment includes code examples in Python for implementation, covering data handling, model fitting, and visualization. Additionally, there are instructions for performing descriptive and predictive analytics on healthcare and sales data.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

LAB Syllabus

List of Experiments:

1. Data Preprocessing a. Handling missing values b. Noise detection removal c. Identifying


data redundancy and elimination

2. Implement any one imputation model

3. Implement Linear Regression

4. Implement Logistic Regression

5. Implement Decision Tree Induction for classification

6. Implement Random Forest Classifier

7. Implement ARIMA on Time Series data

8. Object segmentation using hierarchical based methods

9. Perform Visualization techniques (types of maps - Bar, Colum, Line, Scatter, 3D Cubes etc)

10. Perform Descriptive analytics on healthcare data

11. Perform Predictive analytics on Product Sales data

12. Apply Predictive analytics for Weather forecasting

EXP :1

Data Preprocessing

a. Handling missing values

b. Noise detection removal

c. Identifying data redundancy and elimination

import pandas as pd
import numpy as np
from [Link] import StandardScaler
from sklearn.feature_selection import VarianceThreshold

# Assume 'data' is your DataFrame

# 1. Remove rows with missing values


data = [Link]()

# 2. Impute missing values in a specific column with the mean (if needed)
column = 'column_with_missing'
data[column] = data[column].fillna(data[column].mean())

# 3. Remove outliers based on z-score


z_scores = [Link](StandardScaler().fit_transform(data[['numeric_column']]))
outliers = [Link](z_scores > 3)[0]
data = [Link](index=outliers)

# 4. Remove duplicate rows


data = data.drop_duplicates()

# 5. Remove highly correlated variables


cor_matrix = [Link]().abs()
upper_triangle = cor_matrix.where([Link]([Link](cor_matrix.shape), k=1).astype(bool))
high_correlation = [column for column in upper_triangle.columns if any(upper_triangle[column] >
0.9)]
data = [Link](columns=high_correlation)

Exp 2: Write a program to Implement any one imputation model

import pandas as pd
import numpy as np

# Set seed for reproducibility


[Link](123)

# Generate sample data with missing values


data = [Link]({
'id': range(1, 11),
'age': [Link](list(range(20, 61)) + [[Link]], size=10, replace=True),
'height': [Link](list(range(150, 201)) + [[Link]], size=10, replace=True),
'weight': [Link](list(range(50, 101)) + [[Link]], size=10, replace=True)
})

# Print original data


print("Original data:\n")
print(data)

# Function to impute missing values using mean


def mean_imputation(column):
if [Link].is_numeric_dtype(column):
return [Link]([Link]())
return column

# Apply mean imputation to numeric columns


data_imputed = [Link](mean_imputation)

# Print imputed data


print("\nImputed data using mean imputation:\n")
print(data_imputed)

EXP3 :Write a program to Implement Linear Regression using R

import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns
from sklearn.linear_model import LinearRegression

# Set random seed for reproducibility


[Link](123)

# Generate sample data


n = 100
x = [Link](1, 10, n)
y = 3 * x + [Link](loc=0, scale=2, size=n) # Simulated linear relationship with noise

# Create DataFrame
data = [Link]({'x': x, 'y': y})

# Visualize the data


[Link](figsize=(8, 5))
[Link](data=data, x='x', y='y')
[Link]("Sample Data for Linear Regression")
[Link]("X")
[Link]("Y")
[Link]()

# Fit linear regression model


model = LinearRegression()
[Link](data[['x']], data['y'])

# Print model summary (slope and intercept)


print("\nLinear Regression Coefficients:")
print(f"Intercept: {model.intercept_}")
print(f"Slope: {model.coef_[0]}")

# Plot regression line


[Link](figsize=(8, 5))
[Link](data=data, x='x', y='y')
[Link](data['x'], [Link](data[['x']]), color='red') # Regression line
[Link]("Linear Regression")
[Link]("X")
[Link]("Y")
[Link]()

# Predict using the model


new_x = 11
predicted_y = [Link]([[new_x]])
print(f"\nPredicted value for x = {new_x} : {predicted_y[0]}")

EXP:4:Write a Program to Implement Logistic Regression using R

import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression

# Set seed for reproducibility


[Link](123)

# Generate sample data


n = 100
x = [Link](-5, 5, n)
linear_combination = -2 + 0.5 * x
probabilities = 1 / (1 + [Link](-linear_combination))
y = [Link](1, probabilities)

# Create DataFrame
data = [Link]({'x': x, 'y': y})

# Visualize the data


[Link](figsize=(8, 5))
[Link](x='x', y='y', data=data)
[Link]("Sample Data for Logistic Regression")
[Link]("X")
[Link]("Y")
[Link]([0, 1])
[Link]()

# Fit logistic regression model


model = LogisticRegression()
[Link](data[['x']], data['y'])

# Print model coefficients


print("\nLogistic Regression Coefficients:")
print(f"Intercept: {model.intercept_[0]}")
print(f"Coefficient: {model.coef_[0][0]}")

# Plot logistic regression curve


x_range = [Link](data['x'].min(), data['x'].max(), 300)
logit_line = model.predict_proba(x_range.reshape(-1, 1))[:, 1]

[Link](figsize=(8, 5))
[Link](x='x', y='y', data=data)
[Link](x_range, logit_line, color='red', linewidth=2)
[Link]("Logistic Regression Curve")
[Link]("X")
[Link]("Predicted Probability")
[Link]()

# Predict for a new value


new_x = 1
predicted_probability = model.predict_proba([[new_x]])[0][1]
print(f"\nPredicted probability for x = {new_x} : {predicted_probability}")

EXP5:Write a program to Implement Decision Tree Induction for

classification using R

import numpy as np
import pandas as pd
import [Link] as plt
from [Link] import DecisionTreeClassifier, plot_tree

# Set seed for reproducibility


[Link](123)

# Generate sample data


n = 100
x1 = [Link](0, 10, n)
x2 = [Link](0, 10, n)
y = [Link]((x1 + x2) > 10, "A", "B") # Classification outcome

# Create DataFrame
data = [Link]({'x1': x1, 'x2': x2, 'y': y})

# Visualize the data


[Link](figsize=(8, 5))
colors = ['red' if label == 'A' else 'blue' for label in data['y']]
[Link](data['x1'], data['x2'], c=colors, s=50)
[Link]("X1")
[Link]("X2")
[Link]("Sample Data for Decision Tree Classification")
[Link]()

# Fit decision tree model


model = DecisionTreeClassifier()
[Link](data[['x1', 'x2']], data['y'])

# Visualize the decision tree


[Link](figsize=(10, 6))
plot_tree(model, feature_names=['x1', 'x2'], class_names=model.classes_, filled=True)
[Link]("Decision Tree for Classification")
[Link]()

# Predict using the model


new_data = [Link]({'x1': [3, 7], 'x2': [8, 2]})
predicted_classes = [Link](new_data)

print("Predicted classes for new data:", predicted_classes)

EXP: 6 Write a program to Implement Random Forest Classifier using R

import numpy as np
import pandas as pd
from [Link] import RandomForestClassifier
import [Link] as plt

# Set seed for reproducibility


[Link](123)

# Generate sample data


n = 100
x1 = [Link](0, 10, n)
x2 = [Link](0, 10, n)
y = [Link]((x1 + x2) > 10, "A", "B") # Simulated classification outcome

# Create DataFrame
data = [Link]({'x1': x1, 'x2': x2, 'y': y})

# Fit Random Forest model


rf_model = RandomForestClassifier(n_estimators=100, random_state=123)
rf_model.fit(data[['x1', 'x2']], data['y'])

# Print model details


print("\nRandom Forest Classifier")
print(f"Number of trees: {len(rf_model.estimators_)}")
print(f"Training accuracy: {rf_model.score(data[['x1', 'x2']], data['y'])}")

# Plot variable importance


importances = rf_model.feature_importances_
features = ['x1', 'x2']

[Link](figsize=(6, 4))
[Link](features, importances, color='skyblue')
[Link]("Variable Importance Plot")
[Link]("Importance")
[Link]()

# Predict using the model


new_data = [Link]({'x1': [3, 7], 'x2': [8, 2]})
predicted_classes = rf_model.predict(new_data)

print("\nPredicted classes for new data:", predicted_classes)

EXP 7:Write a program to Implement ARIMA on Time Series data

import numpy as np
import [Link] as plt
import pandas as pd
import pmdarima as pm
from [Link] import ARIMA

# Set seed for reproducibility


[Link](123)

# Generate sample time series data


n = 100
ts_data = [Link](loc=0, scale=1, size=n)

# Convert to pandas Series with time index


ts_series = [Link](ts_data, index=[Link](start=1, stop=n+1, step=1))

# Plot the sample time series data


[Link](figsize=(10, 5))
[Link](ts_series)
[Link]("Sample Time Series Data")
[Link]("Time")
[Link]("Value")
[Link](True)
[Link]()

# Fit ARIMA model using auto_arima


arima_model = pm.auto_arima(ts_series, seasonal=False, stepwise=True,
suppress_warnings=True)

# Print model summary


print("\nARIMA Model Summary:")
print(arima_model.summary())

# Forecast the next 10 steps


forecast, conf_int = arima_model.predict(n_periods=10, return_conf_int=True)

# Plot the forecast


[Link](figsize=(10, 5))
[Link](ts_series, label="Observed")
forecast_index = [Link](len(ts_series)+1, len(ts_series)+11)
[Link](forecast_index, forecast, label="Forecast", color="green")
plt.fill_between(forecast_index, conf_int[:, 0], conf_int[:, 1], color='green', alpha=0.2)
[Link]("Forecast using ARIMA")
[Link]("Time")
[Link]("Value")
[Link]()
[Link](True)
[Link]()

Exp:8 Write a program to implement Object segmentation using

hierarchical based methods


● Pillow or imageio to read images,

● skimage for image processing,

● scipy for hierarchical clustering,

● matplotlib for displaying results.

import numpy as np
import [Link] as plt
from skimage import io, color, img_as_float
from [Link] import linkage, fcluster
from [Link] import pdist

# Load the image


# Replace '[Link]' with the actual image path
image = [Link]('[Link]')
gray_image = color.rgb2gray(img_as_float(image)) # Convert to grayscale and normalize

# Flatten the image to a 1D vector


flattened = gray_image.flatten().reshape(-1, 1)

# Perform hierarchical clustering


Z = linkage(pdist(flattened), method='ward') # Compute linkage matrix
num_segments = 4 # Number of clusters
clusters = fcluster(Z, t=num_segments, criterion='maxclust')

# Reconstruct segmented image


segmented = [Link](gray_image.shape)

# Plot original and segmented images side-by-side


[Link](figsize=(12, 5))

[Link](1, 2, 1)
[Link](gray_image, cmap='gray')
[Link]("Original Grayscale Image")
[Link]('off')

[Link](1, 2, 2)
[Link](segmented, cmap='tab10')
[Link](f"Segmented Image ({num_segments} segments)")
[Link]('off')

plt.tight_layout()
[Link]()

Requirements:
Make sure to install necessary libraries:

pip install scikit-image scipy matplotlib imageio

Notes:
● This approach is slow for large images due to hierarchical clustering's complexity;
downsampling may help.

● Replace '[Link]' with the path to your actual image file.

EXP:9 Write a program to Perform Visualization techniques (types of maps

- Bar, Colum, Line, Scatter, 3D Cubes etc)

import numpy as np
import pandas as pd
import [Link] as plt
import seaborn as sns
import plotly.graph_objects as go

# Set random seed for reproducibility


[Link](123)

# Create sample data


data = [Link]({
'x': [Link](1, 11),
'y1': [Link](size=10),
'y2': [Link](size=10),
'y3': [Link](size=10),
'y4': [Link](size=10),
'y5': [Link](size=10),
})

# ---------------- Bar Plot ----------------


[Link](figsize=(6, 4))
[Link](x='x', y='y1', data=data, color='skyblue')
[Link]("Bar Plot")
[Link]("X")
[Link]("Y")
[Link]()

# ---------------- Column Plot (same as bar, vertical) ----------------


[Link](figsize=(6, 4))
[Link](data['x'], data['y2'], color='lightgreen')
[Link]("Column Plot")
[Link]("X")
[Link]("Y")
[Link]()

# ---------------- Line Plot ----------------


[Link](figsize=(6, 4))
[Link](data['x'], data['y3'], color='orange', marker='o')
[Link]("Line Plot")
[Link]("X")
[Link]("Y")
[Link](True)
[Link]()

# ---------------- Scatter Plot ----------------


[Link](figsize=(6, 4))
[Link](data['y4'], data['y5'], color='red')
[Link]("Scatter Plot")
[Link]("Y4")
[Link]("Y5")
[Link](True)
[Link]()

# ---------------- 3D Scatter Plot ----------------


fig = [Link](data=[go.Scatter3d(
x=data['y4'],
y=data['y5'],
z=data['x'],
mode='markers',
marker=dict(size=5, color='blue')
)])
fig.update_layout(
title="3D Scatter Plot",
scene=dict(
xaxis_title='Y4',
yaxis_title='Y5',
zaxis_title='X'
)
)
[Link]()

Bar vs Column Plot: In Python, both are typically done with [Link]();
the distinction is mostly stylistic.

3D Scatter: Plotly's Scatter3d is used to replicate the interactive plot_ly behavior from R.

Make sure to install the required libraries:

pip install numpy pandas matplotlib seaborn plotly

EXP10:To perform descriptive analytics on healthcare data using Python, you can follow these
steps. Below is an example of a Python code that uses the popular libraries like pandas,
matplotlib, and seaborn to perform descriptive analytics:

1. Load the healthcare dataset (assumed to be in CSV format).

2. Check for missing values and clean the data.

3. Summarize the data (statistics like mean, median, mode, etc.).

4. Visualize the data (distribution, correlation, etc.).

Here's an example Python code that performs these tasks:

# Import necessary libraries


import pandas as pd
import [Link] as plt
import seaborn as sns

# Step 1: Load the healthcare dataset


# For this example, we assume 'healthcare_data.csv' contains relevant healthcare information
data = pd.read_csv('healthcare_data.csv')

# Step 2: Inspect the first few rows of the dataset to understand its structure
print("First 5 rows of the dataset:")
print([Link]())
# Step 3: Get a summary of the data - general information about the columns, data types, and
non-null values
print("\nDataset info:")
print([Link]())

# Step 4: Check for missing values in the dataset


print("\nMissing values in the dataset:")
print([Link]().sum())

# Step 5: Clean the data (e.g., drop rows with missing values or fill them with median/mean
values)
# Let's fill missing numeric columns with the median value
[Link]([Link](), inplace=True)

# Step 6: Perform Descriptive Statistics


print("\nDescriptive Statistics:")
print([Link]())

# Step 7: Data Visualization

# 1. Distribution of numerical variables (e.g., Age, Blood Pressure, etc.)


numerical_columns = data.select_dtypes(include=['float64', 'int64']).columns

# Plot histogram for each numerical column


for column in numerical_columns:
[Link](figsize=(10, 6))
[Link](data[column], kde=True, bins=20)
[Link](f'Distribution of {column}')
[Link](column)
[Link]('Frequency')
[Link]()

# 2. Pairplot to see the relationships between numerical variables


[Link](data[numerical_columns])
[Link]('Pairplot of Numerical Variables', y=1.02)
[Link]()

# 3. Correlation Heatmap (Correlation between numerical variables)


[Link](figsize=(12, 8))
[Link](data[numerical_columns].corr(), annot=True, cmap='coolwarm', fmt='.2f',
linewidths=0.5)
[Link]('Correlation Heatmap')
[Link]()
# 4. Boxplots to identify outliers in numerical variables
for column in numerical_columns:
[Link](figsize=(10, 6))
[Link](x=data[column])
[Link](f'Boxplot of {column}')
[Link]()

# Step 8: Analyze categorical variables (e.g., gender, disease category, etc.)


categorical_columns = data.select_dtypes(include=['object']).columns

for column in categorical_columns:


# Count plot for each categorical variable
[Link](figsize=(10, 6))
[Link](x=data[column])
[Link](f'Count plot of {column}')
[Link](rotation=45)
[Link]()

# Step 9: Analyze the relationship between categorical and numerical variables


# Example: Comparing Age across Gender (assuming Gender is a categorical variable)
if 'gender' in [Link] and 'age' in [Link]:
[Link](figsize=(10, 6))
[Link](x='gender', y='age', data=data)
[Link]('Age distribution across Gender')
[Link]()

# Conclusion:
# Now we have performed basic descriptive analytics on healthcare data.
# This includes summarizing the data, cleaning it, and visualizing relationships between
variables.

Explanation of the Code:


1. Loading and Inspecting the Data:

○ The data is loaded using pd.read_csv().

○ We print the first few rows and inspect the data using info() to check data
types and missing values.

2. Cleaning the Data:


○ We handle missing values by filling them with the median of the corresponding
column using fillna().

3. Descriptive Statistics:

○ The describe() function provides an overview of the dataset, including count,


mean, standard deviation, minimum, and maximum values for numeric columns.

4. Data Visualization:

○ Histograms: These are used to understand the distribution of numeric variables.

○ Pairplot: It helps in understanding relationships between numeric columns.

○ Correlation Heatmap: It shows the correlation between numerical variables.

○ Boxplots: These are used to detect outliers in the numerical data.

○ Countplots: These are used for categorical variables to visualize the distribution
of different categories.

○ Boxplots (categorical vs. numerical): Used to examine the relationship


between a categorical variable (e.g., gender) and a numerical variable (e.g.,
age).

Customization:
● Depending on the structure of your healthcare dataset, you might need to adjust the
column names (e.g., age, gender, etc.) and data handling methods.

● This code assumes your dataset has both categorical and numerical variables; for a
different structure, you might need to adapt the code accordingly.

Here’s a Python script to perform predictive analytics on product sales data using machine
learning. We'll use Linear Regression as a basic example, assuming you want to predict
future sales based on features like marketing spend, store type, season, etc.

🔧 Steps in the Predictive Analytics Pipeline


1. Load and explore the data

2. Preprocess the data (handle missing values, encode categorical variables, etc.)

3. Split the data into training and testing sets


4. Train a machine learning model (Linear Regression in this case)

5. Evaluate the model's performance

6. (Optional) Visualize actual vs. predicted sales

Python Code for Predictive Analytics on Product Sales


# Import necessary libraries
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

from sklearn.model_selection import train_test_split


from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
from [Link] import StandardScaler, OneHotEncoder
from [Link] import ColumnTransformer
from [Link] import Pipeline

# Step 1: Load the dataset


data = pd.read_csv('product_sales_data.csv') # Replace with your actual CSV file path

# Step 2: Basic data exploration


print([Link]())
print([Link]())
print([Link]())

# Step 3: Handle missing values (fill numeric columns with median and drop or encode
categorical)
[Link]([Link](numeric_only=True), inplace=True)

# Step 4: Define features and target


# Assume 'Sales' is the target column we want to predict
X = [Link]('Sales', axis=1)
y = data['Sales']

# Step 5: Identify categorical and numerical columns


categorical_cols = X.select_dtypes(include=['object']).[Link]()
numeric_cols = X.select_dtypes(include=['int64', 'float64']).[Link]()

# Step 6: Preprocessing pipeline


preprocessor = ColumnTransformer([
('num', StandardScaler(), numeric_cols),
('cat', OneHotEncoder(handle_unknown='ignore'), categorical_cols)
])

# Step 7: Build the pipeline with a model


model_pipeline = Pipeline([
('preprocessor', preprocessor),
('regressor', LinearRegression())
])

# Step 8: Split the dataset


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 9: Train the model


model_pipeline.fit(X_train, y_train)

# Step 10: Make predictions


y_pred = model_pipeline.predict(X_test)

# Step 11: Evaluate the model


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("\nModel Evaluation:")
print(f"Mean Squared Error (MSE): {mse:.2f}")
print(f"R-squared (R2 Score): {r2:.2f}")

# Step 12: Plot actual vs predicted


[Link](figsize=(10,6))
[Link](y_test, y_pred, alpha=0.7)
[Link]("Actual Sales")
[Link]("Predicted Sales")
[Link]("Actual vs Predicted Sales")
[Link]([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'r--')
[Link](True)
[Link]()

🧠 Assumptions:

● Your dataset includes a Sales column as the target.


● Input features may include things like:

○ Marketing_Spend, Store_Type, Holiday_Season, Product_Category,


etc.

● Categorical columns are encoded using OneHotEncoding.

● Missing values in numeric columns are filled with the median.

🔁 Optional Enhancements:

● Use other models like RandomForestRegressor, XGBoost, or


GradientBoostingRegressor.

● Add time-based features if data has a temporal component.

● Use grid search (GridSearchCV) for hyperparameter tuning.

Here's a full Python program that applies predictive analytics to weather forecasting using
machine learning. This approach assumes you have a historical weather dataset (CSV
format), and the goal is to predict temperature (or another weather metric) based on features
like humidity, wind speed, pressure, etc.

Weather Forecasting Using Machine Learning (Regression)


We'll use Random Forest Regressor for better performance on potentially non-linear weather
data.

🔧 Steps:
1. Load and explore weather data

2. Preprocess (handle missing values, encode categorical variables, scale features)

3. Split into train/test

4. Train a model (Random Forest)

5. Predict and evaluate


6. Visualize predictions

📌 Python Code:
# Import libraries
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

from sklearn.model_selection import train_test_split


from [Link] import RandomForestRegressor
from [Link] import mean_absolute_error, mean_squared_error, r2_score
from [Link] import StandardScaler, OneHotEncoder
from [Link] import ColumnTransformer
from [Link] import Pipeline

# Step 1: Load the dataset


data = pd.read_csv('weather_data.csv') # Replace with actual CSV file path

# Step 2: Initial Exploration


print([Link]())
print([Link]())
print([Link]())

# Step 3: Handle missing values


[Link]([Link](numeric_only=True), inplace=True)

# Optional: Convert date column if exists


if 'date' in [Link]:
data['date'] = pd.to_datetime(data['date'])

# Step 4: Define target and features


# Assume we are predicting temperature
target = 'temperature' # change to actual target column
features = [Link](columns=[target])

X = features
y = data[target]

# Step 5: Identify numerical and categorical features


categorical_cols = X.select_dtypes(include=['object']).[Link]()
numerical_cols = X.select_dtypes(include=['float64', 'int64']).[Link]()

# Step 6: Preprocessing
preprocessor = ColumnTransformer([
('num', StandardScaler(), numerical_cols),
('cat', OneHotEncoder(handle_unknown='ignore'), categorical_cols)
])

# Step 7: Define the model pipeline


model_pipeline = Pipeline([
('preprocessor', preprocessor),
('regressor', RandomForestRegressor(n_estimators=100, random_state=42))
])

# Step 8: Split data


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 9: Train model


model_pipeline.fit(X_train, y_train)

# Step 10: Predict


y_pred = model_pipeline.predict(X_test)

# Step 11: Evaluate


mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("\nModel Evaluation:")
print(f"Mean Absolute Error (MAE): {mae:.2f}")
print(f"Mean Squared Error (MSE): {mse:.2f}")
print(f"R-squared (R2): {r2:.2f}")

# Step 12: Visualize Actual vs Predicted


[Link](figsize=(10, 6))
[Link](y_test.values[:100], label='Actual', marker='o')
[Link](y_pred[:100], label='Predicted', marker='x')
[Link]('Sample Index')
[Link]('Temperature')
[Link]('Actual vs Predicted Temperature')
[Link]()
[Link](True)
plt.tight_layout()
[Link]()
📈 Dataset Assumptions:

The dataset (weather_data.csv) should include columns like:

● temperature (target)

● humidity, pressure, wind_speed, weather_condition, etc.

If you have a time-series weather dataset and want to forecast future temperatures over time, I
can provide a Time Series Forecasting model (like ARIMA, Prophet, or LSTM).

Would you like a time series-based version as well?

You might also like