-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
108 lines (81 loc) · 3.15 KB
/
Copy pathproject.py
File metadata and controls
108 lines (81 loc) · 3.15 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error , mean_absolute_error , r2_score
from sklearn.model_selection import train_test_split
df = pd.read_csv(r"/content/sample_data/california_housing_test.csv")
X = df["longitude"]
Y = df["latitude"]
plt.scatter(X,Y)
plt.xlabel("longitude")
plt.ylabel("latitude")
plt.title("california housing")
plt.show()
lr_model = LinearRegression()
a = df[["median_income","population","households","housing_median_age","longitude",
"latitude","total_rooms","total_bedrooms"]]
b = df["median_house_value"]
a_trainval,a_test,b_trainval,b_test = train_test_split(a,b,test_size=0.25,random_state = 42)
a_train,a_val,b_train,b_val = train_test_split(a_trainval,b_trainval,test_size = 0.2,random_state = 42)
Scaler = StandardScaler()
a_train = Scaler.fit_transform(a_train)
a_val = Scaler.transform(a_val)
a_test = Scaler.transform(a_test)
lr_model.fit(a_train,b_train)
b_pred = lr_model.predict(a_test)
mae = mean_absolute_error(b_test,b_pred)
mse = mean_squared_error(b_test,b_pred)
rmse = np.sqrt(mse)
r2 = r2_score(b_test,b_pred)
print("Evaluation for Linear Regression")
print("Mean Absolute Error:", mae)
print("Mean Squared Error:", mse)
print("Root Mean Squared Error:", rmse)
print("R2 score:", r2)
plt.scatter(b_test,b_pred,s =10)
plt.xlabel("Actual value")
plt.ylabel("predicted value")
plt.title("california housing prediction (linear regression)")
plt.show()
rf_model = RandomForestRegressor(n_estimators = 200,n_jobs = -1,random_state = 42)
rf_model.fit(a_train, b_train)
y_pred = rf_model.predict(a_test)
mae = mean_absolute_error(b_test,y_pred)
mse = mean_squared_error(b_test,y_pred)
rmse = np.sqrt(mse)
print("Evaluation for Randoom Forest Regresssor")
print("Mean Absolute Error:", mae)
print("Mean Squared Error:", mse)
print("Root Mean Squared Error:", rmse)
print("R2:", r2_score(b_test, y_pred))
plt.scatter(b_test,y_pred,s =10)
plt.xlabel("Actual value")
plt.ylabel("predicted value")
plt.title("california housing prediction (random forest regressor)")
plt.show()
print("Prediction on new values")
features = ["median_income","population","households","housing_median_age","longitude",
"latitude","total_rooms","total_bedrooms"]
def get_input():
val ={}
for i in features:
while True:
try:
val[i] = float(input(f"{i}: "))
break
except ValueError:
print("Enter a valid number.")
return val
def model_activation(user_input,a,scaler,lr_model,rf_model):
input_df = pd.DataFrame([user_input],columns = features)
scaled_df = scaler.transform(input_df)
lr_pred = lr_model.predict(scaled_df)[0]
rf_pred = rf_model.predict(scaled_df)[0]
print("Linear Regression Prediction:", lr_pred)
print("Random Forest Prediction:", rf_pred)
return lr_pred,rf_pred
user_input = get_input()
model_activation(user_input,a,Scaler,lr_model,rf_model)