Run ruff
This commit is contained in:
@@ -24,27 +24,50 @@
|
||||
"data = load_breast_cancer()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"feature_names = [n for n in data.feature_names if 'radius' in n or 'area' in n]\n",
|
||||
"feature_names = [n for n in data.feature_names if \"radius\" in n or \"area\" in n]\n",
|
||||
"\n",
|
||||
"X = data.data[:, [data.feature_names.tolist().index(n) for n in feature_names]]\n",
|
||||
"X = StandardScaler().fit_transform(X)\n",
|
||||
"y = data.data[:, [data.feature_names.tolist().index(n) for n in data.feature_names if n not in feature_names]]\n",
|
||||
"y = data.data[\n",
|
||||
" :,\n",
|
||||
" [\n",
|
||||
" data.feature_names.tolist().index(n)\n",
|
||||
" for n in data.feature_names\n",
|
||||
" if n not in feature_names\n",
|
||||
" ],\n",
|
||||
"]\n",
|
||||
"y = StandardScaler().fit_transform(y)\n",
|
||||
"feature_dim = X.shape[1]\n",
|
||||
"target_dim = y.shape[1]\n",
|
||||
"\n",
|
||||
"def get_regression_model(n_hidden_layers: int, n_neurons: int, activation: type=LeakyReLU, regularization_strength: float = 1e-3) -> list[Layer]:\n",
|
||||
"\n",
|
||||
"def get_regression_model(\n",
|
||||
" n_hidden_layers: int,\n",
|
||||
" n_neurons: int,\n",
|
||||
" activation: type = LeakyReLU,\n",
|
||||
" regularization_strength: float = 1e-3,\n",
|
||||
") -> list[Layer]:\n",
|
||||
" layers = []\n",
|
||||
" layers.append(Layer(feature_dim, n_neurons, activation_function=activation()))\n",
|
||||
" for _ in range(n_hidden_layers - 1):\n",
|
||||
" layers.append(Layer(n_neurons, n_neurons, activation_function=activation()))\n",
|
||||
" layers.append(Layer(n_neurons, target_dim, activation_function=Linear()))\n",
|
||||
" for layer in layers:\n",
|
||||
" layer.regularization = Regularization(regularization_strength, 'l2')\n",
|
||||
" layer.regularization = Regularization(regularization_strength, \"l2\")\n",
|
||||
" return layers\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_model():\n",
|
||||
" return FFNN(get_regression_model(n_hidden_layers=2, n_neurons=32, activation=LeakyReLU, regularization_strength=1e-3), AdamScheduler(epochs=10000, learning_rate=1e-2), MSELoss())"
|
||||
" return FFNN(\n",
|
||||
" get_regression_model(\n",
|
||||
" n_hidden_layers=2,\n",
|
||||
" n_neurons=32,\n",
|
||||
" activation=LeakyReLU,\n",
|
||||
" regularization_strength=1e-3,\n",
|
||||
" ),\n",
|
||||
" AdamScheduler(epochs=10000, learning_rate=1e-2),\n",
|
||||
" MSELoss(),\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -57,8 +80,12 @@
|
||||
"crossvalidation_groups = 5\n",
|
||||
"group_length = X.shape[0] // crossvalidation_groups\n",
|
||||
"predictions = np.zeros_like(y)\n",
|
||||
"for g in range(crossvalidation_groups + 1): # One final smaller group to catch the rest\n",
|
||||
" indices = [i for i in range(X.shape[0]) if i < g*group_length or i > (g+1)*group_length]\n",
|
||||
"for g in range(crossvalidation_groups + 1): # One final smaller group to catch the rest\n",
|
||||
" indices = [\n",
|
||||
" i\n",
|
||||
" for i in range(X.shape[0])\n",
|
||||
" if i < g * group_length or i > (g + 1) * group_length\n",
|
||||
" ]\n",
|
||||
" prediction_indices = [i for i in range(X.shape[0]) if i not in indices]\n",
|
||||
" X_train = X[indices]\n",
|
||||
" X_pred = X[prediction_indices]\n",
|
||||
@@ -66,8 +93,7 @@
|
||||
"\n",
|
||||
" model = get_model()\n",
|
||||
" model.fit(X_train, y_train)\n",
|
||||
" predictions[prediction_indices] = model.predict(X_pred)\n",
|
||||
"\n"
|
||||
" predictions[prediction_indices] = model.predict(X_pred)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -122,15 +148,21 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"combined_data = np.zeros_like(data.data)\n",
|
||||
"combined_data[:,:6] = X\n",
|
||||
"combined_data[:, :6] = X\n",
|
||||
"combined_data[:, 6:] = predictions\n",
|
||||
"\n",
|
||||
"combined_df = pd.DataFrame(combined_data, columns=[*feature_names, *[n for n in data.feature_names if n not in feature_names]])\n",
|
||||
"combined_df['target'] = data.target\n",
|
||||
"combined_df = pd.DataFrame(\n",
|
||||
" combined_data,\n",
|
||||
" columns=[\n",
|
||||
" *feature_names,\n",
|
||||
" *[n for n in data.feature_names if n not in feature_names],\n",
|
||||
" ],\n",
|
||||
")\n",
|
||||
"combined_df[\"target\"] = data.target\n",
|
||||
"combined_df.to_csv(\"breast_cancer_regression_results.csv\", index=False)\n",
|
||||
"\n",
|
||||
"original_df = pd.DataFrame(data.data, columns=data.feature_names)\n",
|
||||
"original_df['target'] = data.target\n",
|
||||
"original_df[\"target\"] = data.target\n",
|
||||
"original_df.to_csv(\"breast_cancer_original_data.csv\", index=False)"
|
||||
]
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user