Pandas column of string array to Long tensor - pandas
I have a pandas df that looks like this:
Ln2
[C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, R, R, R]
[C, C, C, C, C, C, G, I, O, P, P, P, R, R, R, R, R, R]
43,000+ rows where each array's length varies from 5 to 20, and this is 1 column fyi, so each row is a single cell.
I want to one hot this column. It was suggested to use torch.nn.functional.one_hot, but it wants it to be a float, and an index tensor. Any suggestions would be greatly appreciated.
Related
Velocity Engine dynamic spaces between generated text
I`m getting from the template some strings that are different in length.Is there any way to generate the text I need with dynamically generated whitespaces? { a, b, c, d , e , f} { a, b, cd, d , e , f} { a, b, cf, d , e , f} { a, b, cggggg, d , e , f} { a, b, c, d , e , f} { a, b, c, d , e , f} { a, b, c, d , e , f} Something like this i want it to be..everything aligned, but I have no clue how shall I set the dynamic whitespaces between my strings
Is this a bug in Idris's type-checker (or unification)?
The following snippet compiles fine in Idris 2, divMod2 : Nat -> (Nat, Bool) divMod2 Z = (Z, False) divMod2 (S Z) = (Z, True) divMod2 (S (S n)) = case divMod2 n of (k, r) => (S k, r) lm : {q:_} -> divMod2 (q+q) = (q, False) lm {q=0} = Refl lm {q=S q} = rewrite sym $ plusSuccRightSucc q q in rewrite lm {q} in Refl but it caused Idris 1.3.4 to fail: | 14 | rewrite lm {q} in Refl | ~~~~~~ When checking right hand side of lm with expected type divMod2 (S q + S q) = (S q, False) When checking an application of function rewrite__impl: Type mismatch between (q1, False) = (q1, False) (Type of Refl) and case divMod2 (plus q q) of (k, r) => (S k, r) = (q1, False) (Expected type) Specifically: Type mismatch between (q1, False) and case divMod2 (plus q q) of (k, r) => (S k, r) I know Idris 1 has become obsolete, but am still curious about the underlying reason behind this observation. Could someone help to explain this? I've done a bit more dig here and found surprisingly that Idris1 failed to compile the following even simpler code snippet, which compiles correctly by Idris2: f : Nat -> (Nat, Bool) f Z = (Z, False) f (S n) = case f n of (k, r) => (S k, r) h2 : {q:_} -> f (S (q+1)) = case f (q+1) of (k, r) => (S k, r) h2 = Refl It spit error message: | 19 | h2 = Refl | ~~~~ When checking right hand side of h2 with expected type f (S (q + 1)) = case f (q + 1) of (k, r) => (S k, r) Type mismatch between case f (plus q 1) of (k, r) => (S k, r) = case f (plus q 1) of (k, r) => (S k, r) (Type of Refl) and case f (plus q 1) of (k, r) => (S k, r) = case f (plus q 1) of (k, r) => (S k, r) (Expected type) Specifically: Type mismatch between case f (plus q 1) of (k, r) => (S k, r) and case f (plus q 1) of (k, r) => (S k, r) Aren't the two mismatching terms identical? Why couldn't be constructed with Refl?
how to write sum of integer variable array in Gurobi Python interface
Desired constraint My code: kume = [(i, t) for i in W for t in T] kume2= [(i, j, s, t) for i in W for j in V for s in W for t in T] a = m.addVars(kume, vtype=GRB.BINARY, name="a") f = m.addVars(kume2, vtype=GRB.CONTINUOUS, name='allocation') It is a optimization problem that using gurobi library.
I wrote an optimization model of a paper with imaginary data in AMPL but there is some problem in running in terms of one of constraints about Years
All you need explain in question title. The mentioned Optimization model belong to a paper with same content but I tried to write the code and analyze what happen exactly through that. I will add the codes to have access to data and find out what is the problem when you are running that. I added the three segment of AMPL code down. Please help me with this. MOD: set O; set J; set Q; set R; #use R instead of K because we have K in line 11 as parameter set T; param D {q in Q, t in T}; param K {o in O, q in Q}; param C {q in Q, j in J}; param S {r in R, q in Q}; param H {r in R, q in Q}; param F {q in Q}; param G {q in Q}; param B {q in Q}; param Delta; param Omega; param I; param A; param M; var X {o in O, q in Q, j in J, t in T} binary; var Z {o in O, q in Q, t in T} binary; var P {q in Q, t in T} binary; var Y {q in Q, t in T} binary; var z {q in Q, t in T} binary; var n {q in Q, t in T} integer >=0; var m {q in Q, t in T} integer >=0; var N {q in Q, t in T} integer >=0; var Teta {q in Q, t in T} integer >=0; var a {r in R, q in Q, t in T} integer >=0; var b {r in R, q in Q, t in T} integer >=0; var u binary; var v binary; var w binary; minimize OF: sum {q in Q, j in J, o in O, t in T} C[q, j] * D[q, t] * X[o, q, j, t] + sum {o in O, q in Q, t in T} K[o, q] * Z[o, q, t] + sum {q in Q , t in T} N[q, t] * Delta + sum {t in T, q in Q} Omega * Teta[q, t] + sum {t in T, r in R, q in Q} S[r, q] * a[r, q, t] + sum {t in T, r in R, q in Q} H[r, q] * b[r, q, t] + sum {t in T, q in Q} F[q] * z[q, t] + sum {t in T, q in Q} G[q] * Y[q, t] + sum {t in T, q in Q} B[q] * P[q, t] + I * v + A * u; subject to c1{q in Q , t in T}: sum {o in O} Z[o, q, t] = D[q, t]; subject to c2{o in O , q in Q , t in T}: sum {j in J} X[o, q, j, t] >= Z[o, q, t]; subject to c3{q in Q , t in T}: n[q, t]= n[q, t-1] + sum {r in R} a[r, q, t-1] + N[q, t] - sum {r in R} a[q, r, t-1]; subject to c4{q in Q , t in T}: m[q, t]= m[q, t-1] + sum {r in R} b[r, q, t-1] + Teta[q, t] - sum {r in R} b[q, r, t-1]; subject to c5{o in O , q in Q , t in T}: Z[o, q, t]<= n[q, t]; subject to c6{q in Q , t in T}: sum {o in O} X[o, q, 1, t]<= m[q, t]; subject to c7{t in T, r in R}: sum {q in Q} a[q, r, t]<= sum{q in Q} n[q, t-1]+ a[r, r, t]; subject to c8{r in R , t in T}: sum {q in Q} b[q, r, t]<= sum{q in Q} m[q, t-1]+ b[r, r, t]; subject to c91{o in O , q in Q , t in T}: X[o, q, 2, t-1]<= X[o, q, 2, t]; subject to c92{o in O , q in Q , t in T}: X[o, q, 3, t-1]<= X[o, q, 3, t]; subject to c10{q in Q , t in T}: sum {o in O} X[o, q, 2, t]- sum {o in O} X[o, q, 2, t-1]<= z[q, t]; subject to c11{q in Q , t in T}: sum {o in O} X[o, q, 3, t]- sum {o in O} X[o, q, 3, t-1]<= P[q, t]; subject to c12{q in Q , t in T , j in J}: X[3, q, j, t]- X[3, q, j, t-1]<= Y[q, t]; subject to c13: sum {t in T, q in Q} Z[4, q, t]<= M * u; subject to c14: sum {t in T, q in Q} Z[5, q, t]<= M * v; subject to c15{o in O , q in Q , t in T}: X[o, q, 1, t]<= M * (w-1); subject to c16{q in Q , t in T}: Z[3, q, t]<= M * w; subject to c17{q in Q , t in T}: Z[2, q, t]<= X[2, q, 1, t]; DATA: set O := Treat1 Treat2 Treat3 Treat4 Treat5; set J := Dewater1 Dewater2 Dewater3; set Q := ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven; set R := ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven; #use R instead of K because we have K in line 15 as parameter set T := Year1 Year2 Year3 Year4 Year5; param D : Year1 Year2 Year3 Year4 Year5 := ArcticBay 0 1 0 1 0 Arviat 1 0 0 1 1 CambridgeBay 1 0 0 1 1 CapeDorse 1 0 1 1 1 ClydeRiver 1 1 0 1 0 GjoaHaven 0 0 1 0 1 ; param K : ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven := Treat1 4051 4221 6477 5205 6443 4088 Treat2 6758 4012 4994 5437 6215 6267 Treat3 5162 4051 6755 6742 6589 4139 Treat4 6556 5094 5963 6043 5745 5428 Treat5 5289 6050 5144 4456 6474 4432 ; param C : Dewater1 Dewater2 Dewater3 := ArcticBay 56299 32825 11894 Arviat 5977 15550 5958 CambridgeBay 20654 28654 19782 CapeDorse 3073 35237 33822 ClydeRiver 19218 48466 34032 GjoaHaven 23650 23304 42540 ; param S : ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven := ArcticBay 0 1359 1480 1458 1728 849 Arviat 2863 0 1229 1873 2353 2731 CambridgeBay 1821 1972 0 1772 1680 2726 CapeDorse 2754 2219 1817 0 1266 2814 ClydeRiver 1216 2632 2527 2782 0 949 GjoaHaven 1229 1238 2605 1060 2673 0; param H : ArcticBay Arviat CambridgeBay CapeDorse ClydeRiver GjoaHaven := ArcticBay 0 2269 1497 1218 2290 2344 Arviat 1424 0 1123 2494 2107 1114 CambridgeBay 2452 2365 0 1924 1292 1722 CapeDorse 1799 2003 2071 0 1216 1150 ClydeRiver 1322 1097 1827 1259 0 1191 GjoaHaven 1163 1444 2085 2398 1308 0; param F := ArcticBay 1981 Arviat 1845 CambridgeBay 1529 CapeDorse 1721 ClydeRiver 1778 GjoaHaven 1802; param G := ArcticBay 8639 Arviat 8829 CambridgeBay 11737 CapeDorse 8167 ClydeRiver 8573 GjoaHaven 10890; param B := ArcticBay 6427 Arviat 9508 CambridgeBay 6661 CapeDorse 8016 ClydeRiver 6196 GjoaHaven 8359; param Delta := 15000; param Omega := 12000; param I := 20000; param A := 17000; param M := 100000000; RUN: reset; option solver CPLEX; model SM.mod; data SM.dat; solve; display X, Z, P, Y, z, n, m, N, Teta, a, b, u, v, w>b.out;
You got an error on constraint collection c3 because you are trying to do aritmetic calculations with values of set T, when in fact set T contains only strings (Year1, Year2, etc). For instance, you wrote n[q, t-1], where t is in T. But what is 'Year1'-1 is not a valid AMPL expression. Other constraint collections have the same problem. You could simply change the data of set T to be 1 2 3 4 5. But then you would run into other problems, because 1-1 is 0, and n[q,0] does not exist. You have to work on your formulation. View project on PIFOP
Multiplying multidimensional array in python
I have two arrays: L, M, N = 6, 31, 500 A = np.random.random((L, M, N)) B = np.random.random((L, L)) I am trying to get an array C such that: C = B * A C has dimension [L, M, N] I tried answer posted at this link but it hasn't given me the desired output. A for loop version of above code is: L, M, N = 6, 31, 500 A = np.random.random((L, M, N)) B = np.random.random((L, L)) z1 = [] for j in range(M): a = np.squeeze(A[:, j, :]) z1.append(np.dot(B, a)) z2 = np.stack(z1)
I think you are looking for numpy.tensordot() where you can specify along which axes to sum: np.tensordot(B,A,axes=(1,0))