Spaces:
Sleeping
Sleeping
File size: 4,929 Bytes
462fea8 |
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"id": 1,
"source": [
"# Data Science Analysis Notebook\n",
"\n",
"This notebook contains some example Python code for data analysis."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"id": 2,
"source": [
"# Import libraries\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"\n",
"# Set visualization style\n",
"sns.set(style='whitegrid')\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"id": 3,
"source": [
"# Load the dataset\n",
"df = pd.read_csv('housing_data.csv')\n",
"\n",
"# Display basic information\n",
"print(f\"Dataset shape: {df.shape}\")\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"id": 4,
"source": [
"# Perform data cleaning\n",
"# Fill missing values with median\n",
"for column in df.columns:\n",
" if df[column].dtype in ['float64', 'int64']:\n",
" df[column].fillna(df[column].median(), inplace=True)\n",
" else:\n",
" df[column].fillna(df[column].mode()[0], inplace=True)\n",
"\n",
"# Check for remaining missing values\n",
"print(\"Missing values after cleaning:\")\n",
"print(df.isnull().sum())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"id": 5,
"source": [
"# Exploratory data analysis\n",
"# Create correlation matrix\n",
"numeric_columns = df.select_dtypes(include=['float64', 'int64']).columns\n",
"correlation_matrix = df[numeric_columns].corr()\n",
"\n",
"# Plot heatmap\n",
"plt.figure(figsize=(12, 10))\n",
"sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', linewidths=0.5)\n",
"plt.title('Correlation Matrix of Numeric Features', fontsize=18)\n",
"plt.xticks(rotation=45, ha='right')\n",
"plt.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"id": 6,
"source": [
"# Feature engineering\n",
"# Create new features\n",
"if 'bedrooms' in df.columns and 'total_rooms' in df.columns:\n",
" df['bedrooms_ratio'] = df['bedrooms'] / df['total_rooms']\n",
"\n",
"if 'total_rooms' in df.columns and 'households' in df.columns:\n",
" df['rooms_per_household'] = df['total_rooms'] / df['households']\n",
"\n",
"# Scale numeric features\n",
"from sklearn.preprocessing import StandardScaler\n",
"scaler = StandardScaler()\n",
"df[numeric_columns] = scaler.fit_transform(df[numeric_columns])\n",
"\n",
"# Display transformed data\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"id": 7,
"source": [
"# Build a simple prediction model\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import mean_squared_error, r2_score\n",
"\n",
"# Assume we're predicting median_house_value\n",
"if 'median_house_value' in df.columns:\n",
" # Prepare features and target\n",
" X = df.drop('median_house_value', axis=1)\n",
" y = df['median_house_value']\n",
" \n",
" # Split the data\n",
" X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
" \n",
" # Train the model\n",
" model = LinearRegression()\n",
" model.fit(X_train, y_train)\n",
" \n",
" # Make predictions\n",
" y_pred = model.predict(X_test)\n",
" \n",
" # Evaluate the model\n",
" mse = mean_squared_error(y_test, y_pred)\n",
" r2 = r2_score(y_test, y_pred)\n",
" \n",
" print(f\"Mean Squared Error: {mse:.2f}\")\n",
" print(f\"R² Score: {r2:.2f}\")\n",
" \n",
" # Plot actual vs predicted values\n",
" plt.figure(figsize=(10, 6))\n",
" plt.scatter(y_test, y_pred, alpha=0.5)\n",
" plt.plot([y.min(), y.max()], [y.min(), y.max()], 'r--')\n",
" plt.xlabel('Actual Values')\n",
" plt.ylabel('Predicted Values')\n",
" plt.title('Actual vs Predicted Values')\n",
" plt.tight_layout()\n",
" plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|