-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (45 loc) · 2.29 KB
/
main.py
File metadata and controls
56 lines (45 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Import necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.pyplot import title
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.linear_model import LinearRegression
# Load the housing dataset
housing = pd.read_csv('housing.csv')
# Step 1: Create an 'income category' column for stratified sampling
housing['income_cat'] = pd.cut(housing['median_income'],
bins=[0., 1.5, 3.0, 4.5, 6., np.inf],
labels=[1, 2, 3, 4, 5])
# Step 2: Stratified split into training and testing sets
split = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=42)
for train_index, test_index in split.split(housing, housing["income_cat"]):
strat_train_set = housing.loc[train_index]
strat_test_set = housing.loc[test_index]
# Remove the 'income_cat' column from training and test sets
for set_ in (strat_train_set, strat_test_set):
set_.drop('income_cat', axis=1, inplace=True)
# Step 3: Visualization of data distribution (scatter plot)
housing = strat_train_set.copy()
housing.plot(kind='scatter', x='longitude', y='latitude',title = 'heatmap California', alpha=0.4,
s=housing['population'] / 100, label='Population', figsize=(12, 8),
c='median_house_value', cmap=plt.get_cmap('jet'), colorbar=True)
plt.legend()
plt.show()
# Step 4: Prepare data for Linear Regression
housing = strat_train_set.drop("median_house_value", axis=1) # Features
housing_labels = strat_train_set["median_house_value"].copy() # Target
# One-hot encode 'ocean_proximity' and drop the first category to avoid collinearity
housing = pd.get_dummies(housing, columns=['ocean_proximity'], drop_first=True)
# Step 5: Handle missing values in 'total_bedrooms' by filling with the median
housing.fillna({'total_bedrooms': housing['total_bedrooms'].median()}, inplace=True)
# Step 6: Train the Linear Regression model
lin_reg = LinearRegression()
lin_reg.fit(housing, housing_labels)
# Step 7: Make predictions on a sample of the training set
sample_data = housing.iloc[:5]
sample_labels = housing_labels.iloc[:5]
predictions = lin_reg.predict(sample_data)
# Output the predictions and actual values for comparison
print("Predictions:", predictions)
print("Actual values:", sample_labels.values)