numpy inner product and value error
I have problem with inner product of two vector. I define subtract and distance in these shape:
subtract = np.zeros((3,1), dtype=int)
distance = np.zeros((7,))
then when I want to do this operation:
subtract = np.subtract(pix[i,j],cluster[k])
distance[k] = np.inner(subtract,np.transpose(subtract))
I get this error:
distance[k] = np.inner(subtract,np.transpose(subtract))
ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)
there is no problem with subtract I can print it and its transpose.
See also questions close to this topic

python3: Can I get cartesian product of 'one' 3d list?
I have a list looks like below
[['a'], ['b'], [[1], [2], [3]], ['d']]
and I need to get
[['a'], ['b'], [1], ['d]]
,[['a'], ['b'], [2], ['d]]
,[['a'], ['b'], [3], ['d]]
I saw few posts and found itertools.product, but I think this needs multiple lists in same dimension as argument.
Is there any way to get the result?
Thank you.

applying int function to a pandas data frame which is read from an excel file returns unexpected results
When I run the following code I get an unexpected result
xl = pd.ExcelFile(path+'/'+file_name) df = xl.parse(sheet_name) df = pd.DataFrame(df, columns={"column1"}) df["column1"] = df["column"].apply(int)
When I print values of
df["column1"]
before applying functionint
I get101.0, 211.0, 351.0
. After I print it I get values100, 210, 350
. It is supposed to return values101, 211, 351
. 
How to match string from another column
I have to check title column contains color value from color column.
dataframe1['title'].str.contains(dataframe1['color'])
I am getting an error as
TypeError: 'Series' objects are mutable, thus they cannot be hashed

When I copy and multiply constant, original array is also affected
b = np.array([1, 2, 3, 4, 5]) c = b c[0:2] = 2*c[0:2]
In this case, I expected [2, 4, 3, 4, 5] for c and no change about b. However, I have no idea why array b is also affected and changed. The value of b is also [2, 4, 3, 4, 5]. I wanted to create another array based on the previous array. What is my problem? and Is there any suggestion?

Numpy convet column dtype and replace orignal column
I want change the numpy column data type, but when I to replace the original numpy column, the dtype will not change succesfully.
import numpy as np arraylist =[(1526869384.273246, 0, 'a0'), (1526869385.273246, 1, 'a1'), (1526869386.273246, 2, 'a2'), (1526869387.273246, 3, 'a3'), (1526869388.273246, 4, 'a4'), (1526869389.273246, 5, 'a5'), (1526869390.273246, 6, 'a6'), (1526869391.273246, 7, 'a7'), (1526869392.273246, 8, 'a8'), (1526869393.273246, 9, 'a9'), (1526869384.273246, 0, 'a0'), (1526869385.273246, 1, 'a1'), (1526869386.273246, 2, 'a2'), (1526869387.273246, 3, 'a3'), (1526869388.273246, 4, 'a4'), (1526869389.273246, 5, 'a5'), (1526869390.273246, 6, 'a6'), (1526869391.273246, 7, 'a7'), (1526869392.273246, 8, 'a8'), (1526869393.273246, 9, 'a9')] array = np.array(arraylist) array.dtype dtype('<U32') array[:,0]=array[:,0].astype("float64") array[:,0].dtype >>> dtype('<U32')
Event through I changed the dtype of the column, but why I want to replace the orignal column it's still
u32
? 
Error in handling shape and fit of training set while using Multinomial Naive Bayes for Text classification
I have done all the preprocessing task like removing stopwords,HTML tags etc. I am trying to classify IMDB movie dataset(Large Movie Review Datasets of Stanford Unoversity) using Multinomial Naive Bayes. I am getting error on varibable X. I have made into 2D array but don't know how to handle error?
This is part of code of Multinomial Naive Bayes, which I have made(or build).
categories = ['pos','neg'] doc_to_train = sklearn.datasets.load_files("/home/satyam/aclImdb_v1/aclImdb/train", description = None, categories = categories ,load_content=True,enco ding='utf8',shuffle=True,random_state=42) X=np.array([[doc_to_train.data], [doc_to_train.shape(25000, 12500)]]) y = doc_to_train.target X_train, X_test, y_train,y_test= train_test_split(X,y, test_size=0.3) print(len(X_train), len(X_test), len(y_train), len(y_test)) print ('X dimensionality', np.shape(X_train)) print ('Y dimensionality', np.shape(y)) mnb=MultinomialNB() print (X) print(mnb.fit(X, y)) print(mnb.predict(X_test)) accuracy = mnb.score(X_test, y_test) print (accuracy)
Errors that were encountered:
Traceback (most recent call last): File "/usr/lib/python3.6/sitepackages/sklearn/utils/__init__.py", line 61, in __getattr__ return self[key] KeyError: 'shape' During handling of the above exception, another exception occurred: Traceback (most recent call last): X=np.array([[doc_to_train.data], [doc_to_train.shape(25000, 12500)]]) File "/usr/lib/python3.6/sitepackages/sklearn/utils/__init__.py", line 63, in __getattr__ raise AttributeError(key) AttributeError: shape

How to printout a transposed 3Darray
I have the following 3D array, which I need to print out as transposed one. It has 10 columns, 7 rows, adressed by (x,y) or (col, row)
int[][][] map = { {{1, 1, 3, 3, 4, 4, 4, 4}, {}, {}, {2, 2}, {}, {}, {}}, //first column {{}, {}, {}, {}, {}, {}, {}}, {{}, {}, {}, {}, {}, {}, {}}, {{}, {}, {}, {}, {}, {}, {}}, {{}, {}, {}, {}, {}, {}, {0}}, {{}, {}, {}, {}, {}, {}, {}}, {{}, {}, {}, {3, 4}, {}, {}, {}}, {{}, {}, {}, {}, {}, {}, {}}, {{}, {}, {}, {}, {}, {}, {}}, {{}, {}, {}, {}, {}, {}, {}} };
I tried switching the conditions, but it always crashes.
for (int threeD = 0; threeD < map.length; threeD++){ for(int twoD = 0; twoD < map[threeD].length; twoD++){ for (int oneD = 0; oneD < map[threeD][twoD].length; oneD++){ System.out.print(map[threeD][twoD][oneD]); } System.out.print(" "); } System.out.println(); }
Executing the code above gives this output:
{1, 1, 3, 3, 4, 4, 4, 4} {} {} {2 2} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {0} {} {} {} {} {} {} {} {} {} {} {3 4} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {}
The output i want should look as the following:
{1 1 3 3 4 4 4 4} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {2 2} {} {} {} {} {} {3 4} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {} {0} {} {} {} {} {}
(I left the brackets to visualise it better )

excel macros convert multiple rows up to same cell value to columns
as in the picture, convert rows ou to same cell value into column

Can't figure out why int(" ") is acting weird
So i'm making a code that takes a name and number from a user and then counts to it by ones. I'm using 'while' and 'try' to stop any non letters from going through, but it doesn't seem to work with just blank. I've used idle to test if "int("")" and it comes up with a value error, but for some reason when its in a function it is acting up. Any ideas? I left out the rest of the code to just this.
Here's my code.
testNumber="" def numQualify(x): while x: try: x=int(x) except ValueError: print("That was not a proper number.") return False else: print("Great number!") break testNumber=input("Write a number") while numQualify(testNumber) == False: testNumber=input("Please try another number") print("Done")

ValueError: negative dimensions are not allowed while generating samples from multivariate normal distribution
I want to generate samples from multivariate normal distribution in numpy. Here is my code:
import numpy as np import random from random import uniform import math from sklearn.datasets import make_spd_matrix k = 5 # how many classes N = 20 # how many samples x_min = 20 x_max = 20 y_min = 20 y_max = 20 def main(): w = np.random.random(k) # generate k random numbers w /= w.sum() # divide by their sum # now these sum to 1 #print(w) # actual weight vector number_of_divisions_x = number_of_divisions_y = math.ceil(math.sqrt(k)) x_div = (x_max  x_min)/number_of_divisions_x y_div = (y_max  y_min)/number_of_divisions_y # divide a grid # x = x_min to x_max # y = y_min to y_max # now form the grid and put the means points = [] # the list of actual means # now generate means for i in range(number_of_divisions_x): for j in range(number_of_divisions_y): if len(points) == k: # after generating k points, break the loop break point = [x_min + i * x_div, y_min + j * y_div] # array (not tuple) is needed for the function points.append(point) if len(points) == k: # after generating k points, break the loop break samples = [] # list containing all the samples actual_sample = [None] * k # how many samples in each class actually. Stored for later weight correction covs = [] # actual covariances for i in range(k): number_of_samples = math.ceil(N * w[i]) # number of samples from class i, generate at least one sample actual_sample[i] = number_of_samples # for later weight correction if i == k1: number_of_samples = N  sum(actual_sample[:k1]) # for the last class, take all the remaining samples actual_sample[i] = number_of_samples mean = points[i] # generate covariance matrix cov = make_spd_matrix(2) # Generate a random symmetric, positivedefinite matrix, whose size is 2x2 covs.append(cov) file = open("class " + str(i+1) + ".txt", "w") # class 1, 2, 3 etc file.write(str(mean) + "\n\n") file.write(str(cov)) file.close() # save the actual mean and covariance to file s = np.random.multivariate_normal(mean, cov, number_of_samples) # list of lists for element in s: e = list(element) samples.append(e) # make list and insert to the main list
If I run the code for around 10 times, it executes without any error 9 times, but shows an error during one run.
s = np.random.multivariate_normal(mean, cov, number_of_samples) # list of lists File "mtrand.pyx", line 4508, in mtrand.RandomState.multivariate_normal File "mtrand.pyx", line 1550, in mtrand.RandomState.standard_normal File "mtrand.pyx", line 167, in mtrand.cont0_array ValueError: negative dimensions are not allowed
So the error comes from the random number generation from multivariate normal distribution. I have generate the covariance matrix to be positive definite using
make_spd_matrix()
, so the determinant of the covariance matrix should always be positive. Then why am I getting the error sometimes? Here the list of means is:[[20.0, 20.0], [20.0, 6.666666666666666], [20.0, 6.666666666666668], [6.666666666666666, 20.0], [6.666666666666666, 6.666666666666666]]

ValueError:  IF Statements, WHILE Loop, Syntax Error, Literal Error
I need some help, i have some code where i am trying to iterate through years of sea ice concentration data, the field i am referring to in the code have some fields that have 'no data', i need to write a line of code, possibly an IF statement that can either IGNORE the 'no data' fields or change them to 0 so that the code can iterate without errors.
My first error was this: if c > 90: TypeError: '>' not supported between instances of 'str' and 'int'
That caused me to change the code because it is telling me i assume that i cannot read a string as an integer. After changing the code line to read this:
c = ((int (row[4]))) a = row [1] if c > 90: total_area = total_area + float(a)
I received a syntax error that reads:
a = row [1] SyntaxError: invalid syntax (arrow points to the a)
So i changed the code again to this and this is what i have so far? I am a bit frustrated, Now I am getting the same literal for int() with base 10: " error again
Any help would be appreciated. Thanks
import matplotlib.pyplot as plt import numpy import shapefile as shp import math import tarfile import glob import pandas as pd # UNZIP FILES AND EXTRACT ALL def unzip(fname): if (fname.endswith("tar.gz")): tar = tarfile.open(fname, "r:gz") tar.extractall() tar.close() elif(fname.endswith("tar")): tar = tarfile.open(fname, "r:") tar.extractall() tar.close() for name in glob.glob('/Users/keneshia/Desktop/SEA_ICE_CON/*.tar'): unzip(name) # FINDING THE TOTAL AREA FOR EACH YEAR yearly_area = [] year = 2007 #def findNulls(fieldValue): # if fieldValue is None: # return "null values" # elif fieldValue is not None: # return "no nulls here" for name in glob.glob('/Users/keneshia/Desktop/SEA_ICE_CON/*.shp'): r = shp.Reader(name) total_area = 0 for row in r.records(): c = ((int (row[4]))) a = row[1] if row in r.records() == (''): row1 == 0 #print ('No Data Available') elif c > 90: total_area = total_area + float(a) yearly_area.append(total_area) year += 1 plt.plot(yearly_area) plt.show(block = True)

¿How to compute the Frobenius inner space between two matrices in Python 2 and 3?
Is there any way to compute the frobenius inner product in Python?

Dot Product of large arrays in C#
In the Unity app i'm developing in C#, I have two large arrays stored in two different .csv files. One of them is a 144x144 array, and the other is a 144x1 column. I am able to Read the files columnwise using StreamReader. How can I firstly read the 144x144 into my application. And after that, how do I apply a dot product between the two arrays to get a new array (result should be 144x1 in size).