Commit eb6bfca2 authored by Chetan Sharma's avatar Chetan Sharma
Browse files

Updated thesis draft, updated data checking script

parent 6b9ebc6e
......@@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
......@@ -20,10 +20,14 @@
"import os\n",
"\n",
"from matplotlib import pyplot as plt\n",
"from sklearn.linear_model import LinearRegression, Lasso\n",
"from sklearn.preprocessing import StandardScaler\n",
"\n",
"from objects import Conditions, Data, Prediction, MachineChar, EndMill\n",
"from ml import LinearModel, UnifiedLinearModel, mean_no_outliers\n",
"from ml import UnifiedLinearModel, mean_no_outliers\n",
"from optimize import Optimizer\n",
"from collections import defaultdict\n",
"from itertools import combinations\n",
"\n",
"from IPython.core.display import display, HTML\n",
"def niceprint(*args):\n",
......@@ -39,6 +43,11 @@
"for i in range(len(tableau20)): \n",
" r, g, b = tableau20[i] \n",
" tableau20[i] = (r / 255., g / 255., b / 255.) \n",
"\n",
"def get_params(desired_data):\n",
" model = UnifiedLinearModel()\n",
" model.ingest_data(desired_data)\n",
" return model.params\n",
" \n",
"# sweep graphs'\n",
"def sweep_graphs(desired_data):\n",
......@@ -198,7 +207,7 @@
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": false
"scrolled": true
},
"outputs": [
{
......@@ -34784,6 +34793,204 @@
" ammp_graphs(data, data_name)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<p>Best properties to use when only using 2 of them: elastic_modulus, solidus with score 0.4687849981607055</p>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<p>Best properties to use when only using 3 of them: elastic_modulus, elongation_at_break, specific_heat_capacity with score 0.4149268190719831</p>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<p>Best properties to use when only using 4 of them: elastic_modulus, solidus, specific_heat_capacity, thermal_conductivity with score 0.5471455473909048</p>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<p>Best properties to use when only using 5 of them: elastic_modulus, solidus, specific_heat_capacity, tensile_yield_strength, thermal_conductivity with score 0.622745950357826</p>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<p>Best properties to use when only using 6 of them: elastic_modulus, elongation_at_break, solidus, specific_heat_capacity, thermal_conductivity, ultimate_tensile_strength with score 0.8515664681715516</p>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# mini experiment to see how well a linear regressor can predict the cutting force coefficients\n",
"blacklist = ['ammp-alu-1_8_coolant', 'ammp-ss-3_8-no_deflection_limit', 'ammp-alu-1_8', 'sweep-alu-1_4']\n",
"with shelve.open(os.path.join(\"saved_cuts\", \"shelve\")) as db:\n",
" materials = defaultdict(list)\n",
" for key in db.keys():\n",
" if key in blacklist: continue\n",
" _, material, _ = key.split(\"-\", 2)\n",
" data = db[key]\n",
" materials[material] += data\n",
" \n",
" \n",
"material_params = {material: get_params(data) for material, data in materials.items()} \n",
"# all material properties are in SI units with no prefixes\n",
"material_props = {\n",
" 'alu': {\n",
" 'ultimate_tensile_strength': 310e6,\n",
" 'tensile_yield_strength': 275e6,\n",
" 'elastic_modulus': 68.9e9,\n",
" 'elongation_at_break': 0.17,\n",
" 'specific_heat_capacity': 896,\n",
" 'thermal_conductivity': 167,\n",
" 'solidus': 582\n",
" },\n",
" 'lcs': {\n",
" 'ultimate_tensile_strength': 440e6,\n",
" 'tensile_yield_strength': 370e6,\n",
" 'elastic_modulus': 200e9,\n",
" 'elongation_at_break': 0.15,\n",
" 'specific_heat_capacity': 486,\n",
" 'thermal_conductivity': 51.9,\n",
" 'solidus': 1420,\n",
" },\n",
" 'brass': {\n",
" 'ultimate_tensile_strength': 400e6,\n",
" 'tensile_yield_strength': 310e6,\n",
" 'elastic_modulus': 97e9,\n",
" 'elongation_at_break': 0.25,\n",
" 'specific_heat_capacity': 380,\n",
" 'thermal_conductivity': 115,\n",
" 'solidus': 885,\n",
" },\n",
" '4140': {\n",
" 'ultimate_tensile_strength': 655e6,\n",
" 'tensile_yield_strength': 415e6,\n",
" 'elastic_modulus': 205e9,\n",
" 'elongation_at_break': 0.25,\n",
" 'specific_heat_capacity': 473,\n",
" 'thermal_conductivity': 42.6,\n",
" 'solidus': 1420,\n",
" },\n",
" 'pc': {\n",
" 'ultimate_tensile_strength': 66.2e6,\n",
" 'tensile_yield_strength': 63.3e6,\n",
" 'elastic_modulus': 2.36e9,\n",
" 'elongation_at_break': 0.87,\n",
" 'specific_heat_capacity': 1540,\n",
" 'thermal_conductivity': 0.196,\n",
" 'solidus': 146,\n",
" },\n",
" 'ss': {\n",
" 'ultimate_tensile_strength': 515e6,\n",
" 'tensile_yield_strength': 205e6,\n",
" 'elastic_modulus': 193e9,\n",
" 'elongation_at_break': 0.40,\n",
" 'specific_heat_capacity': 500,\n",
" 'thermal_conductivity': 16.2,\n",
" 'solidus': 1400,\n",
" },\n",
" 'uhmw': {\n",
" 'ultimate_tensile_strength': 44.1e6,\n",
" 'tensile_yield_strength': 23.4e6,\n",
" 'elastic_modulus': 1.21e9,\n",
" 'elongation_at_break': 3.50,\n",
" 'specific_heat_capacity': 1900,\n",
" 'thermal_conductivity': 0.410,\n",
" 'solidus': 136,\n",
" }\n",
"}\n",
"\n",
"# arrange into vectors\n",
"materials = sorted(list(material_params.keys()))\n",
"properties = sorted(list(material_props['alu'].keys()))\n",
"\n",
"props_to_vector = lambda props: [i[1] for i in sorted([(key, item) for key, item in props.items()])]\n",
"\n",
"def average_error(y_ests, y_acts):\n",
" errors = list()\n",
" for i in range(len(y_ests)):\n",
" y_est, y_act = y_ests[i], y_acts[i]\n",
" y_diff = [abs((a - b) / a) for a, b in zip(y_est, y_act)]\n",
" errors.append(np.mean(y_diff))\n",
" return np.mean(errors)\n",
"\n",
"for num in range(2, 7):\n",
" best_score, best_combination = float('inf'), None\n",
" property_combinations = combinations(properties, num)\n",
" for property_combination in property_combinations:\n",
" x_vector = np.array([[material_props[material][prop] for prop in property_combination] for material in materials])\n",
" y_vector = np.array([material_params[material] for material in materials])\n",
" input_scaler, output_scaler = StandardScaler(), StandardScaler()\n",
" input_scaler.fit(x_vector)\n",
" output_scaler.fit(y_vector)\n",
"\n",
" x_vector_scaled = input_scaler.transform(x_vector)\n",
" y_vector_scaled = output_scaler.transform(y_vector)\n",
" \n",
" scores = list()\n",
" \n",
" # aggressive leave-one-out testing\n",
" \n",
" for i in range(len(x_vector_scaled)):\n",
" x_missing_one = [x_vector_scaled[j] for j in range(len(x_vector_scaled)) if j != i]\n",
" y_missing_one = [y_vector_scaled[j] for j in range(len(y_vector_scaled)) if j != i]\n",
" model = LinearRegression()\n",
" model.fit(x_missing_one, y_missing_one)\n",
" y_pred = output_scaler.inverse_transform(model.predict([x_vector_scaled[i]]))\n",
" y_act = output_scaler.inverse_transform([y_vector_scaled[i]])\n",
" score = average_error(y_pred, y_act)\n",
" scores.append(score)\n",
" \n",
" score = np.mean(scores)\n",
" \n",
" if score < best_score:\n",
" best_score = score\n",
" best_combination = property_combination\n",
" \n",
" niceprint(\"Best properties to use when only using\", num, \"of them:\", \", \".join(best_combination), \"with score\", best_score)"
]
},
{
"cell_type": "code",
"execution_count": null,
......@@ -34826,7 +35033,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
"version": "3.7.3"
}
},
"nbformat": 4,
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment