-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_Regression.py
More file actions
168 lines (128 loc) · 4.8 KB
/
Copy pathLinear_Regression.py
File metadata and controls
168 lines (128 loc) · 4.8 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import numpy as np
import pandas as pd
def normalize_features(features):
"""
Normalize features by subtracting mean and dividing by standard deviation.
Parameters:
- features (pandas.DataFrame): Input features DataFrame.
Returns:
- pandas.DataFrame: Normalized features DataFrame.
"""
mean = features.mean()
std_dev = features.std()
normalized_features = (features - mean) / std_dev
return normalized_features
def train_test_split_data(df, test_size=0.2,random_seed=42):
"""
Split the dataset into training and testing sets.
Parameters:
- data (pandas.DataFrame): The input DataFrame.
- test_size (float): how much part of the dataset is to include in the test split.
Returns:
(pandas.DataFrame, pandas.DataFrame, pandas.Series, pandas.Series):
- X_train: The training features.
- X_test: The testing features.
- y_train: The training labels.
- y_test: The testing labels.
"""
if random_seed is not None:
np.random.seed(random_seed)
shuffled_indexes = np.random.permutation(len(df))
test_dataset_length = int(len(df) * test_size)
test_indexes = shuffled_indexes[:test_dataset_length]
train_indexes = shuffled_indexes[test_dataset_length:]
return df.iloc[train_indexes, :-1], df.iloc[test_indexes, :-1], df.iloc[train_indexes, -1], df.iloc[test_indexes, -1]
def mean_squared_error(predictions, targets):
"""
Compute the mean squared error between predictions and target values.
Parameters:
- predictions (numpy.ndarray): Predicted values.
- targets (numpy.ndarray): True target values.
Returns:
- float: Mean squared error.
"""
return np.mean((predictions - targets) ** 2)
def gradient_descent(X, y, learning_rate, epochs):
"""
Train the linear regression model using gradient descent.
Parameters:
- X (pandas.DataFrame): Input features DataFrame.
- y (numpy.ndarray): True target values.
- learning_rate (float): Learning rate for gradient descent.
- epochs (int): Number of training epochs.
Returns:
- numpy.ndarray: Trained weights.
"""
num_samples, num_features = X.shape
weights = np.zeros(num_features)
#print(weights)
for epoch in range(epochs):
predictions = np.dot(X, weights)
#print(predictions)
errors = predictions - y
gradient = 2 * np.dot(X.transpose(), errors) / num_samples
weights -= learning_rate * gradient
return weights
def linear_regression_train(X_train,y_train, learning_rate=0.01, epochs=1000):
"""
Train the linear regression model using the given training data.
Parameters:
- X_train (pandas.DataFrame): feature values.
- y_train (numpy.ndarray): Target values for the training dataset.
- learning_rate (float, optional): Learning rate for gradient descent. Default is 0.01.
- epochs (int, optional): Number of training epochs. Default is 1000.
Returns:
- numpy.ndarray: Trained weights.
"""
# Extract features and target variable
X = X_train
y = y_train
# Normalize features and add bias feature
X_normalized = normalize_features(X)
y_normalized = normalize_features(y)
# Train the model using gradient descent
weights = gradient_descent(X_normalized ,y_normalized, learning_rate, epochs)
return weights
def linear_regression_predict(weights, X_test):
"""
Make predictions on a test sample using the trained weights.
Parameters:
- weights (numpy.ndarray): Trained weights.
- X_test (pandas.Series): Test sample for prediction.
Returns:
- numpy.ndarray : Predicted outputs for the test sample.
"""
normalized_test_sample = normalize_features(X_test)
predictions = np.dot(normalized_test_sample, weights)
return predictions
#load data into data frame
df = pd.read_csv('house_price.csv')
#print(df.head())
#print(df.info())
#deleting feature that aren't int or float
columns = df.select_dtypes(include=['int', 'float']).columns
df=df[columns]
df.drop(["Id"],axis=1,inplace=True)
#print(df.head())
#print(df.info())
#print(df['LotFrontage'][0:10])
#find columns with NAN values
df = df.dropna()
#print(df['LotFrontage'][0:10])
#print(df.info())
df = df.astype({"LotFrontage": 'int64', "MasVnrArea": 'int64',"GarageYrBlt": 'int64'})
#print(df.info())
#split the dataset into training and testing sets
X_train, X_test, y_train, y_test=train_test_split_data(df, test_size=0.2,random_seed=38)
#print(y_train)
# Train the linear regression model
trained_weights = linear_regression_train(X_train,y_train,0.05)
# Make a prediction on the test sample
predicted_output = linear_regression_predict(trained_weights, X_test)
#print(predicted_output)
#print(y_test)
y=normalize_features(y_test)
#print(y)
#mean square error
mse = (sum((y-predicted_output)**2))/len(y)
print(mse)