-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path02_03_housing_csv_dataset.py
49 lines (36 loc) · 1.11 KB
/
02_03_housing_csv_dataset.py
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
# Python code to illustrate
# regression using data set
import matplotlib.pyplot as plt
from sklearn import linear_model
import pandas as pd
import numpy as np
# Load CSV and columns
df = pd.read_csv("csv/Housing.csv")
Y = df['price']
X = df['lotsize']
X = X.values.reshape(len(X), 1)
Y = Y.values.reshape(len(Y), 1)
# Split the data into training/testing sets
X_train = X[:-250]
X_test = X[-250:]
# Split the targets into training/testing sets
Y_train = Y[:-250]
Y_test = Y[-250:]
# Plot outputs
plt.scatter(X_test, Y_test, color='black')
plt.title('Test Data')
plt.xlabel('Size')
plt.ylabel('Price')
plt.xticks(())
plt.yticks(())
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit(X_train, Y_train)
# Plot outputs
plt.plot(X_test, regr.predict(X_test), color='red', linewidth=3)
plt.show()
new_house_lotsize = np.array([5000]).reshape(1, 1) # Reshape for prediction
predicted_price = regr.predict(new_house_lotsize)
predicted_price_value = predicted_price[0][0] # Extract actual price
print("Predicted Price for New House:", predicted_price_value)