-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.py
99 lines (71 loc) · 2.97 KB
/
Main.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
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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense
data = pd.read_csv("realest.csv")
data.replace("NA", np.nan, inplace=True)
data = data.apply(pd.to_numeric, errors='ignore')
def fill_missing_values_and_analyze(df):
filled_df = df.copy()
for column in filled_df.columns:
if filled_df[column].isnull().any():
train_data = filled_df.dropna(subset=[column])
X_train = train_data.drop(column, axis=1)
y_train = train_data[column]
model = Sequential()
model.add(Dense(10, input_dim=X_train.shape[1], activation='relu'))
model.add(Dense(5, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(X_train, y_train, epochs=100, batch_size=10, verbose=0)
for index, row in filled_df[filled_df[column].isnull()].iterrows():
X_pred = row.drop(column).values.reshape(1, -1)
filled_df.at[index, column] = model.predict(X_pred)
return filled_df
def visualize_data_before(data):
fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(15, 10))
axes = axes.flatten()
for i, col in enumerate(data.columns):
ax = axes[i]
ax.hist(data[col].dropna(), bins=20, color='skyblue', edgecolor='black')
ax.set_title(col)
ax.set_xlabel('Value')
ax.set_ylabel('Frequency')
ax.grid(True)
missing_count = data[col].isnull().sum()
if missing_count > 0:
ax.annotate(f'Missing: {missing_count}', xy=(0.7, 0.9), xycoords='axes fraction', fontsize=10, color='red')
for i in range(len(data.columns), len(axes)):
fig.delaxes(axes[i])
plt.tight_layout()
plt.show()
visualize_data_before(data)
filled_data = fill_missing_values_and_analyze(data)
print(f"\t\t\tHead\n\n\n{filled_data.head()}\n\n")
print(f"\t\t\tDescription\n\n\n{filled_data.describe()}\n\n")
print(f"\t\t\tinfo\n\n\n{filled_data.info()}\n\n")
visualize_data_before(filled_data)
corr_matrix = data.corr()
plt.figure(figsize=(12, 8))
plt.imshow(corr_matrix, cmap='coolwarm', interpolation='nearest')
plt.title('Correlation Matrix')
plt.colorbar()
for i in range(len(corr_matrix)):
for j in range(len(corr_matrix)):
plt.text(j, i, f'{corr_matrix.iloc[i, j]:.2f}', ha='center', va='center', color='black')
plt.xticks(range(len(corr_matrix)), corr_matrix.columns, rotation=45)
plt.yticks(range(len(corr_matrix)), corr_matrix.columns)
plt.show()
plt.figure(figsize=(12, 6))
plt.scatter(data['Space'], data['Price'], alpha=0.7, color='blue')
plt.title('Scatter Plot of Price vs Space')
plt.xlabel('Space')
plt.ylabel('Price')
plt.grid(True)
plt.show()
plt.figure(figsize=(12, 6))
data.boxplot(column=['Price', 'Space', 'Lot'])
plt.title('Box Plot of Price, Space, and Lot')
plt.ylabel('Value')
plt.show()