Different Result from MinMaxScaler() with Manual Calculations - python

So i was tinkering with MinMaxScaler and want to see how it works with manual calculations
here's the array i'm trying to scale
array = [[0, -1.73, -1.73, -2.0, -2.0, -2.0, -1.73],
[-1.73, 0, -1.41, -1.73, -1.73, -1.73, -1.41],
[-1.73, -1.41, 0, -1.73, -1.73, -1.73, -0.0],
[-2.0, -1.73, -1.73, 0, -1.41, -1.41, -1.73],
[-2.0, -1.73, -1.73, -1.41, 0, -0.0, -1.73],
[-2.0, -1.73, -1.73, -1.41, -0.0, 0, -1.73],
[-1.73, -1.41, -0.0, -1.73, -1.73, -1.73, 0]]
the lowest value is -2.0 and highest value is 0. When i do my manual calculations it is based on MinMaxScaler() formula stated in sklearn minmax but when i program it, it shows different result as in this code
from sklearn.preprocessing import MinMaxScaler
import numpy as np
X = np.array([
[0, -1.73, -1.73, -2.0, -2.0, -2.0, -1.73],
[-1.73, 0, -1.41, -1.73, -1.73, -1.73, -1.41],
[-1.73, -1.41, 0, -1.73, -1.73, -1.73, -0.0],
[-2.0, -1.73, -1.73, 0, -1.41, -1.41, -1.73],
[-2.0, -1.73, -1.73, -1.41, 0, -0.0, -1.73],
[-2.0, -1.73, -1.73, -1.41, -0.0, 0, -1.73],
[-1.73, -1.41, -0.0, -1.73, -1.73, -1.73, 0]
])
# create an instance of MinMaxScaler
scaler = MinMaxScaler()
# fit the scaler to the data and transform the data
X_scaled = scaler.fit_transform(X)
# print the scaled data
print(X_scaled)
The Result Array is
[[1. 0. 0. 0. 0.
0. 0. ]
[0.135 1. 0.1849711 0.135 0.135
0.135 0.1849711]
[0.135 0.1849711 1. 0.135 0.135
0.135 1. ]
[0. 0. 0. 1. 0.295
0.295 0. ]
[0. 0. 0. 0.295 1.
1. 0. ]
[0. 0. 0. 0.295 1.
1. 0. ]
[0.135 0.1849711 1. 0.135 0.135
0.135 1. ]]
My Calculations
x' = (x-min)/ (max⁡ - min)
x' = (-1.73-(-2)) / (0⁡ -(-2))
x' = 0.135
My question is where did i do my calculations differently than sklearn ? why is -1.73 becomes 0 ?

It is scaling each column of the array. -1.73 becomes 0 only if it is the smallest value in that column. Notice how -1.73 does not become 0 in the first column.
This is intentional and it is due to the axis=0 argument.
If you want to scale each element of the array according to the min and max of the entire array you could do something like this:
from sklearn.preprocessing import MinMaxScaler
import numpy as np
X = np.array(
[
[0, -1.73, -1.73, -2.0, -2.0, -2.0, -1.73],
[-1.73, 0, -1.41, -1.73, -1.73, -1.73, -1.41],
[-1.73, -1.41, 0, -1.73, -1.73, -1.73, -0.0],
[-2.0, -1.73, -1.73, 0, -1.41, -1.41, -1.73],
[-2.0, -1.73, -1.73, -1.41, 0, -0.0, -1.73],
[-2.0, -1.73, -1.73, -1.41, -0.0, 0, -1.73],
[-1.73, -1.41, -0.0, -1.73, -1.73, -1.73, 0],
]
)
# create an instance of MinMaxScaler
scaler = MinMaxScaler()
# fit the scaler to the data and transform the data
X_scaled = scaler.fit_transform(X.reshape(-1, 1)).reshape(*X.shape)
# print the scaled data
print(X_scaled)

Related

How to include data as one group, when plotting separate groups in seaborn

I have a data frame, and I have to compare a column's median that has specific values from my dataframe (filtered), with the same column's median that has all the values in my original dataframe.
Here's the furthest I have reached, I have presented two graphs, which in my opinion should be in the same graph:
My goal is to merge these two graphs together in one graph.
Here's my code that gave that output.
filt_waterfront = df['waterfront'] == 1
fig, axs = plt.subplots(1,2)
sns.boxplot(y='price', data = df[filt_waterfront], ax=axs[0], color= 'red')
sns.boxplot(y='price', data = df, ax=axs[1], color = 'orange')
fig.set_size_inches(9,6)
fig.suptitle('Price plots of properties with waterfront and general properties')
fig.axes[1].set_ylabel("Price")
fig.axes[0].set_ylabel("Price")
fig.axes[1].set_xlabel("General Properties")
fig.axes[0].set_xlabel("Properties with Waterfront") <br>
Where my filter is properties having waterfront, the graph on the right shows general properties, which means the original column, and on the left with the filter, I'm trying to find a way to get both of these graph into one graph (because it would look much cleaner and there's no real reason to present two graphs other than me failing to do it).
Any help is really appreciated, thanks in advance!
The easiest way, which doesn't deal with manually creating an x-axis and assigning different boxplots, is to create a separate DataFrame with all the data labeled in accordance with the desired labels for the x-axis.
If you don't want both original categories, filter out the desired data to use:
filtered = df[['price', 'waterfront']][df.waterfront.eq(1)].copy()
The following example uses the sample DataFrame from the OP, which has many columns. comb is created by selecting the specific columns to be plotted, to prevent creating a potentially large DataFrame with unnecessary information.
If the DataFrame already only has required columns, then use comb = df.assign(waterfront="All")
import pandas as pd
import seaborn as sns
# Using the sample data from the OP, which has many columns
# Create a copy of the columns to plot and all rows, with waterfront as "All"
comb = df[['price', 'waterfront']].assign(waterfront="All")
# Append it to the original columns with the original categories
data = df[['price', 'waterfront']].append(comb).reset_index(drop=True)
# display(data.head())
price waterfront
0 221900.0 0
1 538000.0 1
2 180000.0 0
3 604000.0 1
4 510000.0 0
# display(data.tail())
price waterfront
95 488000.0 All
96 210490.0 All
97 785000.0 All
98 450000.0 All
99 1350000.0 All
# plot
sns.boxplot(data=data, y='price', x='waterfront')
Sample Data
data = {'id': [7129300520, 6414100192, 5631500400, 2487200875, 1954400510, 7237550310, 1321400060, 2008000270, 2414600126, 3793500160, 1736800520, 9212900260, 114101516, 6054650070, 1175000570, 9297300055, 1875500060, 6865200140, 16000397, 7983200060, 6300500875, 2524049179, 7137970340, 8091400200, 3814700200, 1202000200, 1794500383, 3303700376, 5101402488, 1873100390, 8562750320, 2426039314, 461000390, 7589200193, 7955080270, 9547205180, 9435300030, 2768000400, 7895500070, 2078500320, 5547700270, 7766200013, 7203220400, 9270200160, 1432701230, 8035350320, 8945200830, 4178300310, 9215400105, 822039084], 'date': ['20141013T000000', '20141209T000000', '20150225T000000', '20141209T000000', '20150218T000000', '20140512T000000', '20140627T000000', '20150115T000000', '20150415T000000', '20150312T000000', '20150403T000000', '20140527T000000', '20140528T000000', '20141007T000000', '20150312T000000', '20150124T000000', '20140731T000000', '20140529T000000', '20141205T000000', '20150424T000000', '20140514T000000', '20140826T000000', '20140703T000000', '20140516T000000', '20141120T000000', '20141103T000000', '20140626T000000', '20141201T000000', '20140624T000000', '20150302T000000', '20141110T000000', '20141201T000000', '20140624T000000', '20141110T000000', '20141203T000000', '20140613T000000', '20140528T000000', '20141230T000000', '20150213T000000', '20140620T000000', '20140715T000000', '20140811T000000', '20140707T000000', '20141028T000000', '20140729T000000', '20140718T000000', '20150325T000000', '20140716T000000', '20150428T000000', '20150311T000000'], 'price': [221900.0, 538000.0, 180000.0, 604000.0, 510000.0, 1225000.0, 257500.0, 291850.0, 229500.0, 323000.0, 662500.0, 468000.0, 310000.0, 400000.0, 530000.0, 650000.0, 395000.0, 485000.0, 189000.0, 230000.0, 385000.0, 2000000.0, 285000.0, 252700.0, 329000.0, 233000.0, 937000.0, 667000.0, 438000.0, 719000.0, 580500.0, 280000.0, 687500.0, 535000.0, 322500.0, 696000.0, 550000.0, 640000.0, 240000.0, 605000.0, 625000.0, 775000.0, 861990.0, 685000.0, 309000.0, 488000.0, 210490.0, 785000.0, 450000.0, 1350000.0], 'bedrooms': [3, 3, 2, 4, 3, 4, 3, 3, 3, 3, 3, 2, 3, 3, 5, 4, 3, 4, 2, 3, 4, 3, 5, 2, 3, 3, 3, 3, 3, 4, 3, 2, 4, 3, 4, 3, 4, 4, 4, 4, 4, 4, 5, 3, 3, 3, 3, 4, 3, 3], 'bathrooms': [1.0, 2.25, 1.0, 3.0, 2.0, 4.5, 2.25, 1.5, 1.0, 2.5, 2.5, 1.0, 1.0, 1.75, 2.0, 3.0, 2.0, 1.0, 1.0, 1.0, 1.75, 2.75, 2.5, 1.5, 2.25, 2.0, 1.75, 1.0, 1.75, 2.5, 2.5, 1.5, 1.75, 1.0, 2.75, 2.5, 1.0, 2.0, 1.0, 2.5, 2.5, 2.25, 2.75, 1.0, 1.0, 2.5, 1.0, 2.5, 1.75, 2.5], 'sqmeters_living': [109.624675, 238.758826, 71.534745, 182.088443, 156.075808, 503.530286, 159.327388, 98.476403, 165.366035, 175.585284, 330.73207, 107.76663, 132.850242, 127.276106, 168.153103, 274.061687, 175.585284, 148.643627, 111.48272, 116.127834, 150.501672, 283.351914, 210.888146, 99.405425, 227.610554, 158.862876, 227.610554, 130.063174, 141.211446, 238.758826, 215.533259, 110.553698, 216.462282, 101.263471, 191.37867, 213.675214, 154.217763, 219.24935, 113.340766, 243.403939, 238.758826, 392.047566, 333.983649, 145.856559, 118.914902, 293.571163, 91.973244, 212.746191, 116.127834, 255.759941], 'sqmeters_lot': [524.897808, 672.798216, 929.022668, 464.511334, 750.650316, 9469.528056, 633.500557, 902.173913, 693.979933, 609.43887, 910.070606, 557.413601, 1848.848012, 899.293943, 450.575994, 464.511334, 1304.347826, 399.479747, 915.087328, 908.026756, 462.653289, 4168.246005, 585.284281, 895.856559, 603.864734, 436.361947, 250.0, 146.878484, 592.716462, 666.38796, 369.751022, 117.521368, 464.511334, 278.7068, 618.636195, 284.280936, 3237.458194, 557.413601, 750.185805, 701.690821, 512.820513, 2246.934225, 523.875883, 211.817168, 897.064288, 1263.749535, 792.270531, 1246.376812, 553.976217, 6039.111854], 'floors': [1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 1.0, 2.0, 1.0, 1.0, 1.5, 1.0, 1.5, 2.0, 2.0, 1.5, 1.0, 1.0, 1.0, 1.0, 2.0, 1.0, 2.0, 1.5, 2.0, 1.5, 1.0, 2.0, 2.0, 3.0, 1.5, 1.5, 1.0, 1.5, 1.0, 2.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0, 1.0, 2.0, 1.0, 2.0, 1.0, 1.0], 'waterfront': [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1], 'view': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], 'grade': [7, 7, 6, 7, 8, 11, 7, 7, 7, 7, 8, 7, 7, 7, 7, 9, 7, 7, 7, 7, 7, 9, 8, 7, 8, 6, 8, 8, 7, 8, 8, 7, 7, 8, 7, 8, 5, 8, 7, 8, 9, 8, 9, 7, 6, 8, 6, 9, 7, 9], 'sqmeters_above': [109.624675, 201.597919, 71.534745, 97.54738, 156.075808, 361.389818, 159.327388, 98.476403, 97.54738, 175.585284, 172.798216, 79.895949, 132.850242, 127.276106, 168.153103, 183.946488, 175.585284, 148.643627, 111.48272, 116.127834, 79.895949, 216.462282, 210.888146, 99.405425, 227.610554, 158.862876, 162.578967, 130.063174, 73.392791, 238.758826, 215.533259, 110.553698, 140.282423, 101.263471, 118.914902, 140.282423, 86.399108, 219.24935, 82.683017, 243.403939, 238.758826, 241.545894, 333.983649, 145.856559, 85.470085, 293.571163, 91.973244, 212.746191, 116.127834, 201.133408], 'sqmeters_basement': [0.0, 37.160907, 0.0, 84.541063, 0.0, 142.140468, 0.0, 0.0, 67.818655, 0.0, 157.933854, 27.87068, 0.0, 0.0, 0.0, 90.115199, 0.0, 0.0, 0.0, 0.0, 70.605723, 66.889632, 0.0, 0.0, 0.0, 0.0, 65.031587, 0.0, 67.818655, 0.0, 0.0, 0.0, 76.179859, 0.0, 72.463768, 73.392791, 67.818655, 0.0, 30.657748, 0.0, 0.0, 150.501672, 0.0, 0.0, 33.444816, 0.0, 0.0, 0.0, 0.0, 0.0], 'yr_built': [1955.0, 1951.0, 1933.0, 1965.0, 1987.0, 2001.0, 1995.0, 1963.0, 1960.0, 2003.0, 1965.0, 1942.0, 1927.0, 1977.0, 1900.0, 1979.0, 1994.0, 1916.0, 1921.0, 1969.0, 1947.0, 1968.0, 1995.0, 1985.0, 1985.0, 1941.0, 1915.0, 1909.0, 1948.0, 2005.0, 2003.0, 2005.0, 1929.0, 1929.0, 1981.0, 1930.0, 1933.0, 1904.0, 1969.0, 1996.0, 2000.0, 1984.0, 2014.0, 1922.0, 1959.0, 2003.0, 1966.0, 1981.0, 1953.0, 0.0], 'yr_renovated': [0.0, 1991.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2002.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 'zipcode': [98178.0, 98125.0, 98028.0, 98136.0, 98074.0, 98053.0, 98003.0, 98198.0, 98146.0, 98038.0, 98007.0, 98115.0, 98028.0, 98074.0, 98107.0, 98126.0, 98019.0, 98103.0, 98002.0, 98003.0, 98133.0, 98040.0, 98092.0, 98030.0, 98030.0, 98002.0, 98119.0, 98112.0, 98115.0, 98052.0, 98027.0, 98133.0, 98117.0, 98117.0, 98058.0, 98115.0, 98052.0, 98107.0, 98001.0, 98056.0, 98074.0, 98166.0, 98053.0, 98119.0, 98058.0, 98019.0, 98023.0, 98007.0, 98115.0, 0.0], 'lat': [47.5112, 47.721, 47.7379, 47.5208, 47.6168, 47.6561, 47.3097, 47.4095, 47.5123, 47.3684, 47.6007, 47.69, 47.7558, 47.6127, 47.67, 47.5714, 47.7277, 47.6648, 47.3089, 47.3343, 47.7025, 47.5316, 47.3266, 47.3533, 47.3739, 47.3048, 47.6386, 47.6221, 47.695, 47.7073, 47.5391, 47.7274, 47.6823, 47.6889, 47.4276, 47.6827, 47.6621, 47.6702, 47.3341, 47.5301, 47.6145, 47.445, 47.6848, 47.6413, 47.4485, 47.7443, 47.3066, 47.6194, 47.6796, 0.0], 'long': [-122.257, -122.319, -122.233, -122.393, -122.045, -122.005, -122.327, -122.315, -122.337, -122.031, -122.145, -122.292, -122.229, -122.045, -122.394, -122.375, -121.962, -122.343, -122.21, -122.306, -122.341, -122.233, -122.169, -122.166, -122.172, -122.218, -122.36, -122.314, -122.304, -122.11, -122.07, -122.357, -122.368, -122.375, -122.157, -122.31, -122.132, -122.362, -122.282, -122.18, -122.027, -122.347, -122.016, -122.364, -122.175, -121.977, -122.371, -122.151, -122.301, 0.0], 'sqmeters_living15': [124.489038, 157.004831, 252.694166, 126.347083, 167.22408, 442.21479, 207.915273, 153.28874, 165.366035, 222.036418, 205.31401, 123.560015, 165.366035, 127.276106, 126.347083, 198.810851, 175.585284, 149.57265, 98.476403, 118.914902, 130.063174, 381.828317, 208.101078, 113.340766, 204.384987, 95.689335, 163.50799, 172.798216, 141.211446, 244.332962, 239.687848, 129.134151, 135.63731, 145.856559, 187.662579, 147.714604, 200.668896, 160.720922, 119.843924, 243.403939, 229.468599, 223.894463, 336.770717, 146.785582, 124.489038, 283.351914, 114.083984, 248.978075, 90.115199, 0.0], 'sqmeters_lot15': [524.897808, 709.680416, 748.978075, 464.511334, 697.045708, 9469.528056, 633.500557, 902.173913, 753.716091, 703.27016, 829.152731, 557.413601, 1179.580082, 948.34634, 450.575994, 371.609067, 1302.303976, 399.479747, 473.337049, 822.185061, 462.653289, 1889.260498, 650.780379, 779.07841, 637.774062, 437.105165, 331.939799, 358.695652, 579.245634, 559.82906, 369.751022, 163.136381, 464.511334, 471.943515, 810.107767, 303.232999, 1065.310294, 436.640654, 724.637681, 1104.050539, 526.662951, 2844.388703, 523.875883, 245.261984, 818.283166, 857.673727, 821.256039, 1271.367521, 473.801561, 0.0]}
df = pd.DataFrame(data)
# display(df[['price', 'waterfront']].head())
price waterfront
0 221900.0 0
1 538000.0 1
2 180000.0 0
3 604000.0 1
4 510000.0 0
Use the hue property of the boxplot:
sns.boxplot(y='price', data = meters_df, ax=axs[1], color = 'orange', hue=filt_waterfront)

Double inequality constraint in Gekko

I have an optimization problem in which some inequalities constraints can either be 0 or greater than a certain value. For example, in the code below, qtde and c1 are lists and pp is a 2d numpy array.
import numpy as np
from gekko import GEKKO
qtde = [7, 2, 2, 12, 2, 7, 1.5, 8, 4, 16, 2, 1, 3, 0.2, 3, 1, 1, 10, 8, 5, 3, 2.5, 5, 2.5, 10, 3, 1, 6, 12, 2, 6, 1, 4, 1, 2, 10, 1, 1, 1, 1]
c1 = [26.0, 150.0, 300.0, 110.0, 400.0, 500.0, 200.0, 200.0, 27.0, 150.0, 50.0, 200.0, 75.0, 0.0, 250.0, 22.8, 300.0, 22.8, 22.8, 150.0, 300.0, 150.0, 100.0, 100.0, 1000.0, 150.0, 150.0, 200.0, 31.2, 100.0, 100.0, 50.0, 23.0, 300.0, 200.0, 300.0, 0.0, 300.0, 30.0, 26.0, 300.0, 300.0, 250.0, 100.0, 100.0, 200.0, 400.0, 21.2, 200.0, 500.0, 0.0]
mm = [[4,0,0,0,0,0,0,0,0,0,9,0,0,0,0,0,5,0,2,0,0,0,7,0,0,0,6,0,0,0,8,0,0,0,0,0,0,0,0,0,3,0,1,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,13,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,14,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,10,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,0,17,15,0,0,16,0,0,18,0,0,0,0,0,0,0,0,0,0],
[26,0,0,0,0,0,0,0,0,0,27,0,0,0,0,0,0,0,21,0,0,0,25,0,0,0,23,0,0,0,22,0,0,0,0,0,0,0,0,0,24,0,20,0,0,0,0,0,0,0,0],
[29,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,34,0,0,0,0,0,0,0,30,0,0,31,0,0,0,0,0,0,0,32,0,0,33,0,28,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,40,0,0,0,0,0,0,0,37,0,0,0,36,0,0,0,38,0,0,0,39,0,0,0,0,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,0],
[42,0,0,0,0,0,0,0,0,0,48,0,0,0,0,0,44,0,43,0,0,0,49,0,0,0,46,0,0,0,47,0,0,0,0,0,0,0,0,0,45,0,41,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,54,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,53,0,0,0,52,0,0,0,0,0,0,0,0,0,51,0,50,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,60,0,0,0,0,0,0,0,56,0,0,0,59,0,0,0,57,0,0,0,58,0,0,0,0,0,0,0,0,0,0,0,55,0,0,0,0,0,0,0,0],
[69,0,0,0,0,0,0,0,0,0,68,0,0,0,0,0,61,0,0,0,0,0,64,0,0,0,63,0,0,0,65,0,0,0,0,0,0,67,0,0,62,0,66,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,71,0,70,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,78,0,0,0,0,0,77,0,0,0,0,0,73,0,0,0,76,0,0,0,75,0,0,0,0,0,0,0,0,0,74,0,72,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,80,0,0,0,79,0,0,0,82,0,0,0,0,0,0,0,0,0,83,0,81,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,86,0,0,0,84,0,0,0,0,0,0,0,0,0,85,0,87,0,0,0,0,0,0,0,0],
[93,0,0,0,0,0,0,0,0,0,95,0,0,0,0,0,94,0,92,0,0,0,90,0,0,0,91,0,0,0,96,0,0,0,0,0,0,0,0,0,89,0,88,0,0,0,0,0,0,0,0],
[104,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,99,0,98,0,0,0,103,0,0,0,101,0,0,0,102,0,0,0,0,0,0,0,0,0,0,0,97,0,0,0,0,0,0,0,0],
[112,0,0,0,0,0,0,0,0,0,108,0,0,0,0,0,110,0,107,0,0,0,111,0,0,0,109,0,0,0,113,0,0,0,0,0,0,0,0,0,106,0,105,0,0,0,0,0,0,0,0],
[114,0,0,0,0,0,0,0,0,0,116,0,0,0,0,0,117,0,119,0,0,0,115,0,0,0,118,0,0,0,120,0,0,0,0,0,0,0,0,0,121,0,122,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,123,0,0,0,0,0,0,0,0],
[0,129,0,0,0,0,126,0,0,0,0,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,127,125,0,0,0,0,0,0,0,0,0,0,130,0,0,0,0,0,124,0,131,0,0,0],
[0,133,0,0,0,0,136,0,0,0,0,0,0,135,0,0,0,0,0,0,0,0,0,0,0,0,0,132,0,0,0,0,0,0,0,0,0,0,134,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,138,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,137,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,139,0,0,0,0,0,0,0,0,0,0,0,0,140,0,0,0,0,0,0,0,0,0,0,0,0,0,141],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,142,0,143,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,144,0,0,0,150,0,146,0,149,0,0,0,0,0,0,152,0,0,0,145,0,0,0,0,147,0,0,151,0,0,0,0,0,148],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,154,0,0,0,0,0,153,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,155,0,0,0,157,0,0,156,0,0,0,158,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,160,0,0,0,0,0,0,0,0,0,0,0,0,0,159,0],
[0,0,0,0,0,0,0,0,0,0,0,161,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,164,0,0,163,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,162,0],
[0,0,165,0,0,0,0,0,0,166,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,167,169,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,170,0,0,0,0,0,0,0,0,0,0,168,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,173,0,0,0,0,0,0,175,177,0,0,171,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,176,0,0,0,0,0,0,0,0,0,0,0,0,174,172,0],
[0,0,0,0,0,0,0,0,0,0,0,0,180,0,0,178,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,179,0],
[0,0,0,0,182,184,0,186,0,0,0,183,185,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,181,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,190,191,0,0,187,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,189,0,0,0,0,0,0,0,0,0,0,0,0,0,188,0],
[0,0,193,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,192,0,0,0,0],
[0,0,197,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,196,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,195,0,0,194,0,0,0,0],
[0,0,0,0,0,0,0,0,0,199,0,0,0,0,201,0,0,0,0,0,0,0,200,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,198,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,203,0,0,0,0,204,0,0,0,0,0,0,0,0,0,0,0,0,0,0,202,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,205,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
mm = np.array(mm)
#
pp = [[5.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,7.90,0.00,0.00,0.00,0.00,0.00,5.49,0.00,2.89,0.00,0.00,0.00,5.98,0.00,0.00,0.00,5.94,0.00,0.00,0.00,6.21,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,3.55,0.00,2.89,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,5.70,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,3.61,0.00,0.00,0.00,5.80,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,3.15,0.00,3.15,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,15.95,0.00,0.00,0.00,0.00,0.00,0.00,14.00,11.95,0.00,0.00,12.36,0.00,0.00,14.18,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[3.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,3.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,2.20,0.00,0.00,0.00,2.80,0.00,0.00,0.00,2.29,0.00,0.00,0.00,2.27,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,2.61,0.00,2.20,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[3.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,9.76,0.00,0.00,0.00,0.00,0.00,0.00,0.00,5.70,0.00,0.00,6.47,0.00,0.00,0.00,0.00,0.00,0.00,0.00,7.47,0.00,0.00,8.51,0.00,3.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,10.50,0.00,0.00,0.00,0.00,0.00,0.00,0.00,9.52,0.00,0.00,0.00,9.10,0.00,0.00,0.00,9.57,0.00,0.00,0.00,9.62,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,9.10,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[6.75,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,9.50,0.00,0.00,0.00,0.00,0.00,7.98,0.00,6.99,0.00,0.00,0.00,11.05,0.00,0.00,0.00,8.55,0.00,0.00,0.00,8.88,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,8.27,0.00,6.75,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,11.20,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,10.95,0.00,0.00,0.00,9.75,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,9.63,0.00,9.16,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,3.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,1.69,0.00,0.00,0.00,1.98,0.00,0.00,0.00,1.77,0.00,0.00,0.00,1.96,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,1.69,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[10.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,7.10,0.00,0.00,0.00,0.00,0.00,1.59,0.00,0.00,0.00,0.00,0.00,1.95,0.00,0.00,0.00,1.74,0.00,0.00,0.00,2.09,0.00,0.00,0.00,0.00,0.00,0.00,6.43,0.00,0.00,1.70,0.00,2.83,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,9.93,0.00,9.93,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,18.40,0.00,0.00,0.00,0.00,0.00,14.49,0.00,0.00,0.00,0.00,0.00,12.89,0.00,0.00,0.00,14.36,0.00,0.00,0.00,13.76,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,13.48,0.00,11.91,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,9.39,0.00,0.00,0.00,7.97,0.00,0.00,0.00,9.57,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,10.24,0.00,9.49,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,33.35,0.00,0.00,0.00,14.80,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,18.00,0.00,72.90,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[5.70,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,6.00,0.00,0.00,0.00,0.00,0.00,5.78,0.00,4.50,0.00,0.00,0.00,3.90,0.00,0.00,0.00,4.06,0.00,0.00,0.00,6.46,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,3.55,0.00,3.55,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[4.50,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,3.60,0.00,0.00,0.00,0.00,0.00,3.19,0.00,2.69,0.00,0.00,0.00,4.12,0.00,0.00,0.00,3.75,0.00,0.00,0.00,4.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,2.69,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[5.70,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,3.80,0.00,0.00,0.00,0.00,0.00,4.65,0.00,3.69,0.00,0.00,0.00,5.42,0.00,0.00,0.00,4.50,0.00,0.00,0.00,6.40,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,3.55,0.00,3.55,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[4.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,5.40,0.00,0.00,0.00,0.00,0.00,5.49,0.00,6.60,0.00,0.00,0.00,4.33,0.00,0.00,0.00,6.38,0.00,0.00,0.00,6.92,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,7.09,0.00,8.68,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,8.68,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,18.99,0.00,0.00,0.00,0.00,16.98,0.00,0.00,0.00,0.00,0.00,0.00,17.80,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,17.20,16.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,28.58,0.00,0.00,0.00,0.00,0.00,13.99,0.00,30.45,0.00,0.00,0.00],
[0.00,9.49,0.00,0.00,0.00,0.00,34.98,0.00,0.00,0.00,0.00,0.00,0.00,18.90,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,8.77,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,15.90,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,47.90,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,38.39,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,89.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,91.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,92.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,66.89,0.00,79.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,27.30,0.00,0.00,0.00,36.90,0.00,29.50,0.00,36.00,0.00,0.00,0.00,0.00,0.00,0.00,49.90,0.00,0.00,0.00,28.90,0.00,0.00,0.00,0.00,31.99,0.00,0.00,42.00,0.00,0.00,0.00,0.00,0.00,33.50],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,65.00,0.00,0.00,0.00,0.00,0.00,23.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,12.89,0.00,0.00,0.00,13.99,0.00,0.00,13.90,0.00,0.00,0.00,14.32,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,16.50,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,15.57,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,36.75,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,58.73,0.00,0.00,53.43,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,51.85,0.00],
[0.00,0.00,5.39,0.00,0.00,0.00,0.00,0.00,0.00,6.90,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,12.36,14.63,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,18.76,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,12.90,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,86.00,0.00,0.00,0.00,0.00,0.00,0.00,89.90,97.30,0.00,0.00,81.60,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,96.70,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,89.00,83.77,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,64.28,0.00,0.00,49.46,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,52.34,0.00],
[0.00,0.00,0.00,0.00,79.90,89.00,0.00,124.00,0.00,0.00,0.00,85.00,104.47,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,67.20,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,91.00,91.11,0.00,0.00,73.61,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,81.50,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,80.60,0.00],
[0.00,0.00,2.47,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,2.44,0.00,0.00,0.00,0.00],
[0.00,0.00,28.44,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,15.90,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,15.10,0.00,0.00,13.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,22.00,0.00,0.00,0.00,0.00,31.92,0.00,0.00,0.00,0.00,0.00,0.00,0.00,28.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,22.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,8.55,0.00,0.00,0.00,0.00,62.70,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,8.30,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00],
[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,62.70,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00]]
pp = np.array(pp)
#c1 = [26.0, 150.0, 300.0, 110.0, 400.0, 500.0, 200.0, 200.0, 27.0, 150.0, 50.0, 200.0, 75.0, 0.0, 250.0, 22.8, 300.0, 22.8, 22.8, 150.0, 300.0, 150.0, 100.0, 100.0, 1000.0, 150.0, 150.0, 200.0, 31.2, 100.0, 100.0, 50.0, 23.0, 300.0, 200.0, 300.0, 0.0, 300.0, 30.0, 26.0, 300.0, 300.0, 250.0, 100.0, 100.0, 200.0, 400.0, 21.2, 200.0, 500.0, 0.0]
m = GEKKO()
ni = 40
nj = 51
x = [[m.Var(lb=0,integer=True) for j in range(nj)] for i in range(ni)]
s = 0
expr = []
for i in range(ni):
for j in range(nj):
s += x[i][j]
for i in range(ni):
expr.append(sum(x[i]))
for i in range(ni):
for j in range(nj):
if mm[i][j] == 0:
m.Equation(x[i][j] == 0)
for i in range(ni):
m.Equation(sum([x[i][j] for j in range(nj)]) >= qtde[i])
b = m.Array(m.Var,nj,integer=True,lb=0,ub=1)
iv = [None]*nj
for j in range(nj):
iv[j] = m.sum([pp[i][j]*x[i][j] for i in range(ni)])
m.Equation(iv[j] >= b[j]*c1[j])
m.Equation((1 - b[j])*iv[j] == 0)
m.Obj(m.sum(expr))
m.options.SOLVER=1 # switch to APOPT
m.solver_options = ['minlp_gap_tol 1.0e-1',\
'minlp_maximum_iterations 10000',\
'minlp_max_iter_with_int_sol 1000',\
'minlp_branch_method 1',\
'minlp_integer_leaves 2']
m.solve()
Edit: I have changed the writing of the last constraint as suggested by John Hedengren (bellow). However, with the insertion of the binary variable, the code now returns an error before starting any iterations. How can this be prevented?
You can use a binary variable (0=equipment off, 1=equipment on and above threshold) and equation as:
b = m.Array(m.Var,nj,integer=True,lb=0,ub=1)
iv = [None]*nj
for j in range(nj):
iv[j] = m.sum([pp[i][j]*x[i][j] for i in range(ni)])
m.Equation(iv[j] >= b[j]*c1[j])
m.Equation((1-b[j])*iv[j] <= 0)
m.options.SOLVER = 1 # Change to MINLP solver
You can split out the summation into an intermediate variable iv because it is used in two equations. Another recommendation is to use m.sum() instead of sum. Using the Gekko summation is typically faster. There are also other ways to pose the problem but this may be the most reliable. I can't verify this solution because your script is missing some inputs. It helps on future posts to reduce the problem to a Minimal and Reproducible example so that solutions can be verified. There is additional information on logical conditions in optimization problems.
Response to Edit
The MINLP does not converge quickly because there are nj x ni = 2040 binary variables. That is 2^2040 potential solutions. You can adjust solver settings to help it find at least one feasible solution.
m.options.SOLVER=3
m.solve() # sometimes it helps to solve with IPOPT first
m.options.SOLVER=1 # switch to APOPT
m.solver_options = ['minlp_gap_tol 1.0e-2',\
'minlp_maximum_iterations 10000',\
'minlp_max_iter_with_int_sol 500',\
'minlp_branch_method 1',\
'minlp_integer_leaves 2']
m.solve()
There is additional description on the solver options on the APOPT website.
Response to Edit
The error on the first MINLP iteration is because the problem is not feasible. If you switch to solver option minlp_as_nlp 1 then you can see the first NLP problem fail to converge. You can also see this with the IPOPT solver if you switch to m.options.SOLVER=3.
EXIT: Converged to a point of local infeasibility.
Problem may be infeasible.
If you solve locally with m=GEKKO(remote=False) and open the run folder before the solve command with m.open_folder() then you can see the infeasibilities.txt file that will help you identify the infeasible equation. I suspect that the infeasibility is because of the equations m.Equation(m.sum([x[i][j] for j in range(nj)]) >= qtde[i]) and m.Equation(x[i][j] == 0). You can also try to identify an infeasible problem with m.options.COLDSTART=2. There is additional help on troubleshooting applications in exercise 18 in the Gekko tutorials.

Build a dataframe from a filtered list of tuple

I have a two lists
kuid -> ['LOdRTFfn', 'Lckq4LkU', 'LcsYHodm']
And NN that is a list of tuple where the first element is a list of indexs of kuid and the other element is an array of values
NN -> [([0, 1, 2], [0.0, 1.2, 1.4]), ([1, 0, 2], [0.0, 1.4, 1.4]), ([2, 0, 1], [0.0, 1.1, 1.4])]
I'd like to keep only the indexes where the values are less then 1.3 for example:
[([0, 1], [0.0, 1.2]), ([1], [0.0]), ([2, 0], [0.0, 1.1])]
and then get the correct value of kuid and build a dataframe:
kuid la lametric
0 LOdRTFfn [LOdRTFfn, Lckq4LkU] [0.0, 1.2]
1 Lckq4LkU [Lckq4LkU] [0.0]
2 LcsYHodm [LcsYHodm, LOdRTFfn] [0.0, 1.1]
Is it possible to do this with list of comprehensions (or other fast solution) avoiding looping? The array could be very large....
Thank you
Here's one way
In [1789]: df = pd.DataFrame(dict(kuid=kuid, NN=[np.array(x) for x in NN]))
In [1790]: df['lametric'] = df.NN.apply(lambda x: x[1][x[1] < 1.3])
In [1791]: df['la'] = df.NN.apply(lambda x: [kuid[int(i)] for i in x[0][x[1] < 1.3]])
In [1792]: df
Out[1792]:
NN kuid lametric \
0 [[0.0, 1.0, 2.0], [0.0, 1.2, 1.4]] LOdRTFfn [0.0, 1.2]
1 [[1.0, 0.0, 2.0], [0.0, 1.4, 1.4]] Lckq4LkU [0.0]
2 [[2.0, 0.0, 1.0], [0.0, 1.1, 1.4]] LcsYHodm [0.0, 1.1]
la
0 [LOdRTFfn, Lckq4LkU]
1 [Lckq4LkU]
2 [LcsYHodm, LOdRTFfn]

How to create a 2d quadrilateral mesh out of a pointcloud and plot it using matplotlib

I want to create a 2D quadrilateral mesh out of pointcloud and plot it using the matplotlib library
my points look like:
points = [
[2.9600000000000004, 6.3999999999999995],
[2.72, 4.8],
[2.48, 3.1999999999999997],
[2.2399999999999998, 1.5999999999999996],
[4.32, 6.3999999999999995],
[4.24, 4.8],
[4.16, 3.1999999999999997],
[4.08, 1.5999999999999996],
[5.68, 6.3999999999999995],
[5.759999999999999, 4.8],
[5.84, 3.1999999999999997],
[5.92, 1.5999999999999996],
[7.04, 6.3999999999999995],
[7.28, 4.8],
[7.5200000000000005, 3.1999999999999997],
[7.76, 1.5999999999999996],
[0, 0],
[2.0, 0.0],
[4.0, 0.0],
[6.0, 0.0],
[8.0, 0.0],
[10, 0],
[2, 8],
[3.2, 8.0],
[4.4, 8.0],
[5.6, 8.0],
[6.8, 8.0],
[8, 8],
[2, 8],
[1.6, 6.4],
[1.2, 4.8],
[0.7999999999999998, 3.1999999999999993],
[0.3999999999999999, 1.5999999999999996],
[0, 0],
[8, 8],
[8.4, 6.4],
[8.8, 4.8],
[9.2, 3.1999999999999993],
[9.6, 1.5999999999999996],
[10, 0]
]
I have tried numpy's meshgrid tool but I couldn't manage to get it to work. I would appreciate some help.
Thanks in advance,
Sebastian

Scipy Minimize SLSQP simply returns x0

I am really new to programming and I am a bit in over my head here with this whole minimize business, so it might just be a simple mistake, but when I try to run my code below, it simply returns the x0 values that I put in to start.
What I'm trying to do:
I have two "functions" that are made up of points, f(x) and h(x). f(X) can be thought of a measured curve, and h(x) is a reference curve. I am trying to use the least squares to find the horizontal shift, x scale, and y scale terms that will best fit the reference curve to the measured results.
I am using the interpolate function to fit a spline to the reference data so the spline can be used to find intermediate values along the curve.
Here is my code:
import numpy
from scipy import optimize
from scipy import interpolate
def f(x):
vals = {1: 0.35, 17: 0.45, 33: 0.67, 49: 0.8, 65: 0.73, 81: 0.65, 97: 0.51, 113: 0.27, 129: 0.01, 145: -0.1,
161: -0.19, 177: -0.21, 193: -0.2, 209: -0.23, 225: -0.24, 241: -0.25, 257: -0.23, 273: -0.26, 289: -0.28,
305: -0.22, 321: -0.24, 337: -0.12, 353: 0.14}
return vals[x]
def h(x):
vals = {1: -0.2, 17: -0.2, 33: -0.2, 49: -0.2, 65: -0.2, 81: -0.2, 97: -0.2, 113: -0.2, 129: -0.1, 145: 0.1,
161: 0.32, 177: 0.4, 193: 0.7, 209: 0.81, 225: 0.7, 241: 0.6, 257: 0.5, 273: 0.3, 289: 0, 305: -0.1,
321: -0.2, 337: -0.2, 353: -0.2}
return vals[x]
x1 = []
y1 = []
for i in range(1, 365, 16):
x1.append(i)
y1.append(h(i))
tck = interpolate.splrep(x1, y1)
fun = lambda x: ((1 / 22.8125 * numpy.sum(
(f(i) - (x[0] * interpolate.splev((x[1] * (i + x[2]) + 0.5), tck)) - 0.5) ** 2 for i in range(1, 365, 16))) ** (
1 / 2))
bnds = ((0.3, 1.5), (0.3, 1.5), (0, 150))
res = optimize.minimize(fun, (1, 1, 0), method='SLSQP', bounds=bnds)
print res.x
Again, when I run this I simply get [1.0, 1.0, 0.0] for res.x. Any thoughts?
Thank you!
1 / 2 in Python2 without from __future__ import division is equal to 0, and this seems to be what's causing your problem. After replacement with 0.5 or 1./2, I get
[ 3.00000000e-01 1.14967789e+00 7.48854782e-04]
for res.x.

Categories

Resources