import pandas as pd
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
import matplotlib.pyplot as plt
# Path of the file to read
iowa_file_path = '/work/ML kaggle learn/Data_ML_try/Housing Prices/train.csv'
home_data = pd.read_csv(iowa_file_path)
# Create target object and call it y
y = home_data.SalePrice
# Create X
features = ['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd']
X = home_data[features]
# Split into validation and training data
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state=1)
# Specify Model
iowa_model = DecisionTreeRegressor(random_state=1)
# Fit Model
iowa_model.fit(train_X, train_y)
# Make validation predictions and calculate mean absolute error
val_predictions = iowa_model.predict(val_X)
val_mae = mean_absolute_error(val_predictions, val_y)
print("Validation MAE when not specifying max_leaf_nodes: {:,.0f}".format(val_mae))
# Using best value for max_leaf_nodes
iowa_model = DecisionTreeRegressor(max_leaf_nodes=100, random_state=1)
iowa_model.fit(train_X, train_y)
val_predictions = iowa_model.predict(val_X)
val_mae = mean_absolute_error(val_predictions, val_y)
print("Validation MAE for best value of max_leaf_nodes: {:,.0f}".format(val_mae))
# To improve accuracy, create a new Random Forest model which you will train on all training data
rf_model_on_full_data = RandomForestRegressor(n_estimators=100, max_depth=10, random_state=0)
# fit rf_model_on_full_data on all data from the training data
rf_model_on_full_data.fit(X, y)
# path to file you will use for predictions
test_data_path = '/work/ML kaggle learn/Data_ML_try/Housing Prices/test.csv'
# read test data file using pandas
test_data = pd.read_csv(test_data_path)
# create test_X which comes from test_data but includes only the columns you used for prediction.
# The list of columns is stored in a variable called features
test_X = test_data[features]
# make predictions which we will submit.
test_preds = rf_model_on_full_data.predict(test_X)
# Create a DataFrame of predictions
output = pd.DataFrame({'Id': test_data.Id,
'SalePrice': test_preds})
# Save the predictions to a CSV file
output.to_csv('submission.csv', index=False)
import seaborn as sns
# combine the training features and target variable into a single DataFrame
train_data = pd.concat([train_X, train_y], axis=1)
# create a scatter plot matrix for the selected features and target variable
sns.pairplot(train_data, y_vars=['SalePrice'], x_vars=['LotArea', 'YearBuilt', '1stFlrSF', '2ndFlrSF', 'FullBath', 'BedroomAbvGr', 'TotRmsAbvGrd'])
# Create a grid of scatter plots for each feature vs SalePrice
fig, axes = plt.subplots(nrows=2, ncols=4, figsize=(12, 8))
for idx, feature in enumerate(features):
ax = axes[int(idx / 4), idx % 4]
ax.scatter(train_X[feature], train_y, s=6) # set the size of the dots with the s part
ax.set_xlabel(feature)
ax.set_ylabel('SalePrice')
# plt.tight_layout() # adjust spacing between subplots for better readability
plt.show()
import numpy as np
# Create a correlation matrix
corr_matrix = home_data.corr()
# Create a mask for the upper triangle
mask = np.triu(np.ones_like(corr_matrix, dtype=bool))
# Set up the matplotlib figure
fig, ax = plt.subplots(figsize=(12, 10))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(230, 20, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr_matrix, mask=mask, cmap=cmap, vmax=3, center=0,
square=True, linewidths=.9, cbar_kws={"shrink": .9})
plt.title("Correlation matrix plot")
plt.show()
corr_matrix = train_data.corr()
sns.heatmap(corr_matrix, annot=True, cmap='Blues')
plt.show()
sns.violinplot(x=home_data['OverallQual'], y=train_data['SalePrice'])
plt.show()
sns.boxplot(x=home_data['OverallQual'], y=train_data['SalePrice'])
plt.show()
sns.boxplot(x=home_data['SaleCondition'], y=train_data['SalePrice'])
plt.show()
from sklearn.tree import plot_tree
plt.figure(figsize=(30,10))
plot_tree(iowa_model, filled=True, feature_names=X.columns)
plt.show()
plt.hist(train_data['SalePrice'], bins=20)
plt.xlabel('Sale Price')
plt.ylabel('Frequency')
plt.title('Histogram of Sale Prices')
plt.show()
import matplotlib.pyplot as plt
plt.hist(train_X['LotArea'], bins=20)
plt.title('Distribution of Lot Area')
plt.xlabel('Lot Area (sqft)')
plt.ylabel('Count')
plt.show()