8/14/24, 10:20 PM tmphwow90l0.
html
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
"""**Note:** We will create a dictionary to keep track of the types of each feature (x) used in our assignment"""
dtype_dict = {'bathrooms':float, 'waterfront':int, 'sqft_above':int, 'sqft_living15':float, 'grade':int, 'yr_renovated':int,
"""**Task 1:** Define a **lambda function** that returns a feature raised to a specific power"""
# Write your code below:
x_power = lambda feature, power: feature**power
"""**Task 2:** Generate an array filled with 100 random integers. Each integer takes values from 0 to 10"""
# Write your code below:
x = np.random.randint(0, 10, (100,))
print('Maximum Value = ', x.max())
print('Minimum Value = ', x.min())
print('Average Value = ', x.mean())
print(f'Standard Deviation (sigma) = {x.std():.3f}')
"""**Task 3:** Define an empty dataframe"""
# Write your code below:
df = pd.DataFrame()
df.head()
"""**Task 4:** Add feature x to the empty dataframe"""
# Write your code below:
df['x'] = x
df.head()
"""**Task 5:** Add feature x^2 to the dataframe"""
# Write your code below:
df['x**2'] = x_power(x, 2)
df.head()
"""**Task 6:** Write your own function called **polynomial_frame()** which accepts an array **feature** and a maximal **degree
"""
# Write your code below:
def polynomial_frame(feature, degree = 3):
df = pd.DataFrame(index = range(len(feature)))
if degree < 1:
return df
df['x'] = feature
for deg in np.arange(2, degree + 1):
df['x**' + str(deg)] = feature ** deg
return df
"""**Task 7:** Use your **polynomial_frame()** function to create a dataframe filled with feature **x** and its power-raised v
# Write your code below:
df = polynomial_frame(x, degree = 5)
df.head()
"""**Task 8:** For the remainder of the assignment we will be working with the **house Sales** data as in the previous noteboo
**Step 1:** Load in the data and also sort the sales by **sqft_living**.
**Note:** When we plot the fitted values we want to join them up in a line and this works best if the variable on the X-axis (
"""
# Write your code below:
df_file = '/content/drive/MyDrive/Colab Notebooks/summer_2024_ai_datasets/Module 4/kc_house_data.csv'
sales = pd.read_csv(df_file, dtype = dtype_dict)
sales = sales.sort_values(['sqft_living', 'price'])
"""**Task 9:** Make a 1 degree polynomial SFrame with **sales['sqft_living']** as the the feature. Call it **poly1_data**."""
# Write your code below:
poly1_data = pd.DataFrame()
poly1_data['power_1'] = polynomial_frame(sales['sqft_living'], degree = 1)
# Hint try: poly1_data['power_1'] = polynomial_frame(sales['sqft_living'], degree = 2)
poly1_data.head()
"""**Task 10:** Add **sales['price']** to **poly1_data** as this will be our output variable."""
# Write your code below:
poly1_data['price'] = sales['price']
"""**Task 11:** 6. Use **LinearRegression** class from **sklearn** to compute the regression weights for predicting **sales[
**Note:** The result should be an intercept ($ \mathbb{w}_{0} $ and $ \mathbb{w}_{1} $) and the model should look like:
$$
\hat{\mathbb{y}} = \mathbb{w}_{0} + \mathbb{w}_{1} \cdot \mathbb{x}
$$
file:///C:/Users/lghou/AppData/Local/Temp/tmphwow90l0.html 1/3
8/14/24, 10:20 PM tmphwow90l0.html
where $ \mathbb{x} $ represents the degree-1 polynomial feature **'sqft_living'**.
**Important Note:** Make sure to import the necessary packages!
"""
# Write your code below
from sklearn.linear_model import LinearRegression
poly1_model = LinearRegression(fit_intercept = False)
print(poly1_model)
"""**Task 12:** Fit the created linear regression model using the training data **poly1_data['power_1'], poly1_data['price']**
# Write your code below:
x = poly1_data['power_1'].values.reshape(-1, 1)
y = poly1_data['price']
poly1_model.fit(x, y)
"""**Task 12:** Predict the output corresponing to the trainind data **poly1_data['power_1']**"""
# Write your code below:
y_hat = poly1_model.predict(poly1_data['power_1'].values.reshape(-1, 1))
"""**Task 13:** Create a scatter plot of the training data (just square feet vs price) and add the fitted model with **matplot
"""
# Write your code below:
x_ax = poly1_data['power_1']
y_ax = poly1_data['price']
plt.scatter(x_ax, y_ax, s = 4, color = 'red', label = 'Training Data')
plt.plot(x_ax, y_hat, color = 'blue', label = 'Prediction')
plt.xlabel('Feature $\mathbf{sqft\_living}$')
plt.ylabel('Price (US $)')
plt.title('Simple Linear Regression Model $ \hat{y} = w_{0} + w_{1} \cdot x $')
plt.legend()
plt.show()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# Create some sample data
x1 = np.array([1, 2, 3, 4, 5])
x2 = np.array([2, 4, 6, 8, 10])
y = np.array([3, 5, 7, 9, 11])
z2 = np.array([4, 8, 12, 16, 20])
# Create a meshgrid for the z2 data
X1, X2 = np.meshgrid(x1, x2)
Z2 = np.zeros_like(X1)
for i in range(len(x1)):
for j in range(len(x2)):
Z2[j, i] = z2[i]
# Create the 3D plot
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Plot the y data as a scatter plot
ax.scatter(x1, x2, y, c='b', label='Y')
# Plot the z2 data as a surface plot
ax.plot_surface(X1, X2, Z2, alpha=0.5, color='r', label='Z2')
# Add labels and title
ax.set_xlabel('X1')
ax.set_ylabel('X2')
ax.set_zlabel('Y/Z2')
ax.set_title('3D Plot of Y vs X1, X2 and Z2 vs X1, X2')
plt.show()
"""**Task 14:**
Now that you have plotted the results using a 1st degree polynomial, try it again using a 2nd degree and 3rd degree polynomial
"""
# Write your code below:
poly2_data = pd.DataFrame()
poly2_data[['power_1', 'power_2']] = polynomial_frame(sales['sqft_living'], 2)
poly2_model = LinearRegression()
x = poly2_data[['power_1', 'power_2']]
y = poly1_data['price']
file:///C:/Users/lghou/AppData/Local/Temp/tmphwow90l0.html 2/3
8/14/24, 10:20 PM tmphwow90l0.html
poly2_model.fit(x, y)
y_hat = poly2_model.predict(poly2_data[['power_1', 'power_2']])
y_hat.shape
plt.plot(poly1_data['price'])
plt.plot(y_hat, color = 'red')
# Write your code below:
x_ax = poly2_data['power_2']
y_ax = poly1_data['price']
plt.scatter(x_ax, y_ax, s = 4, color = 'red', label = 'Training Data')
plt.plot(x_ax, y_hat, color = 'blue', label = 'Prediction')
plt.xlabel('Feature $\mathbf{sqft\_living}$')
plt.ylabel('Price (US $)')
plt.title('Simple Linear Regression Model $ \hat{y} = w_{0} + w_{1} \cdot x $')
plt.legend()
plt.show()
from mpl_toolkits.mplot3d import Axes3D
zz2 = poly1_data['price'].to_numpy()
x1 = poly2_data['power_1'].to_numpy()
x2 = poly2_data['power_2'].to_numpy()
zz2 = zz2[0:100]
x1 = x1[0:100]
x2 = x2[0:100]
y = y_ax[0:100]
# Create a meshgrid for the poly2_data['power_1', 'power_2'] data
X1, X2 = np.meshgrid(x1, x2)
Z2 = np.zeros_like(X1)
for i in range(len(x1)):
for j in range(len(x2)):
Z2[j, i] = zz2[i]
# Create the 3D plot
fig = plt.figure(figsize = (10, 8))
ax = fig.add_subplot(111, projection = '3d')
ax.scatter(x1, x2, y, c = 'b', label = 'Training Data')
ax.plot_surface(X1, X2, Z2, alpha = 0.5, color = 'r', label = 'Prediction')
ax.set_xlabel('$ \mathbf{sqft\_living} $')
ax.set_ylabel('$ \mathbf{sqft\_living}^{2} $')
ax.set_zlabel('House Price')
ax.set_title('Polynomial Regression')
plt.show()
"""**Task 15:**
Now try a 15th degree polynomial. Print out the coefficients and look at the resulted fitted line. Do you think this degree is
"""
# Write your code below:
poly15_data = pd.DataFrame()
power_list = [f"power_{i}" for i in range(1, 16)]
poly15_data[power_list] = polynomial_frame(sales['sqft_living'], 15)
poly15_model = LinearRegression()
x = poly15_data[power_list]
y = poly1_data['price']
poly15_model.fit(x, y)
y_hat = poly15_model.predict(poly15_data[power_list])
file:///C:/Users/lghou/AppData/Local/Temp/tmphwow90l0.html 3/3