analyzingonlinedata / tracingonlinedating_159.py
antitheft159's picture
Upload tracingonlinedating_159.py
5ff5b0c verified
raw
history blame contribute delete
No virus
2.18 kB
# -*- coding: utf-8 -*-
"""Tracingonlinedating.159
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1OkkJMge8YJRdezVwRU92t1timr9gJw9M
"""
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
df = pd.read_csv("/content/Online_Dating_Behavior_Dataset.csv")
print(df.head())
print(df.describe())
print(df.isnull().sum())
plt.figure(figsize=(10, 6))
sns.histplot(df['Matches'], bins=30, kde=True)
plt.title('Distribution of Matches')
plt.xlabel('Number of Matches')
plt.ylabel('Frequency')
plt.show()
sns.pairplot(df)
plt.show()
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
numerical_features = ['Income', 'Age', 'Attractiveness', 'Children']
df[numerical_features] = scaler.fit_transform(df[numerical_features])
X = df.drop('Matches', axis=1)
y = df['Matches']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print("Training set shape:", X_train.shape)
print("Testing set shape:", X_test.shape)
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error, r2_score
lr_model = LinearRegression()
rf_model = RandomForestRegressor(random_state=42)
lr_model.fit(X_train, y_train)
y_pred_lr = lr_model.predict(X_test)
print("Linear Regression - RMSE:", mean_squared_error(y_test, y_pred_lr, squared=False))
print("Linear Regression - R^2 Score:", r2_score(y_test, y_pred_lr))
rf_model.fit(X_train, y_train)
y_pred_rf = rf_model.predict(X_test)
print("Random Forest - RMSE:", mean_squared_error(y_test, y_pred_rf, squared=False))
print("Random Forest - R^2 Score:", r2_score(y_test, y_pred_rf))
importance = rf_model.feature_importances_
features = X.columns
indices = np.argsort(importance)[::-1]
plt.figure(figsize=(12, 6))
plt.title("Feature Importances")
plt.bar(range(X.shape[1]), importance[indices], align="center")
plt.xticks(range(X.shape[1]), features[indices], rotation=90)
plt.xlim([-1, X.shape[1]])
plt.show()