-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProdigy_ML_01
44 lines (35 loc) · 1.44 KB
/
Prodigy_ML_01
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
# Import necessary libraries for the task
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
# Generate some sample data
np.random.seed(42)
num_samples = 100
square_footage = np.random.randint(800, 2500, num_samples)
num_bedrooms = np.random.randint(1, 5, num_samples)
num_bathrooms = np.random.randint(1, 4, num_samples)
prices = 150000 + 100 * square_footage + 50000 * num_bedrooms + 30000 * num_bathrooms + np.random.normal(0, 20000, num_samples)
# Create a DataFrame
data = pd.DataFrame({'SquareFootage': square_footage, 'Bedrooms': num_bedrooms, 'Bathrooms': num_bathrooms, 'Price': prices})
# Split the data into training and testing sets
X = data[['SquareFootage', 'Bedrooms', 'Bathrooms']]
y = data['Price']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a linear regression model
model = LinearRegression()
# Training the model
model.fit(X_train, y_train)
# Making predictions on the test set
y_pred = model.predict(X_test)
# Evaluation of the model
MSE = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error: {MSE}')
# Plot predictions vs actual values
plt.scatter(y_test, y_pred)
plt.xlabel('Actual Price')
plt.ylabel('Predicted Price')
plt.title('Linear Regression graph for Actual Prices vs Predicted Prices')
plt.show()