all input arrays must have the same dimensions - python using append
import numpy as np
x = [1,2,3,4] y = [[5,6,7,8],[9,0,1,2]]
j = np.append(x,y,axis=0)
Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 5147, in append return concatenate((arr, values), axis=axis) ValueError: all the input arrays must have same number of dimensions
See also questions close to this topic
Adding a row and column from a list to a csv in python
I am new to python and I want to run a pairwise similarity algorithm over a bunch of vectors(vectors.csv). Each vector is a representation of a node. I have vectors.csv file that contains:
1,2,3 4,5,6 7,8,9
and I have a list that has y = [56,76,87] depicting the nodes.
I want to get a .csv file that will contain:
null,56,76,87 56,1,2,3 76,4,5,6 87,7,8,9
What is the best way to do this in python3?
The matrix in the csv is a numpy array.
Any help would be greatly appreciated.
Thanks in advance!
Is there a head and tail method for Numpy array?
I loaded a csv file into 'dataset' and tried to execute dataset.head(), but it reports an error. How to check the head or tail of a numpy array? without specifying specific lines?
Failed to import numpy python module in PyCharm
I am learning Python in my free time. I am pretty new. I use PyCharm for IDE. When I:
import numpy as np
ModuleNotFoundError: No module named 'numpy'
pip3 install numpy
Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (1.15.4)
When I go to PyCharm - base interpreter is set to:
The only suspicious thing I find is that Location of environment in PyCharm is set
/Users/XXX/PycharmProjects/MachineLearning/venvI read a little bit about environments and it makes logic if I have installed numpy in one env and use another in the IDE, but not sure how to check this. Help is appreciated. Thanks.
Time complexity of 2 different append functions to a linked list?
Suppose you have the following Python functions and assume the normal structure for a linked list:
def append(self, element): # Line 1 new_node = Node(element, None) #2nd arg. is the next_node # Line 2 ptr = self.head # Line 3 if self.size == 0: # No elements case # Line 4 self.head = new_node # Line 5 self.tail = new_node # Line 6 self.size += 1 # Line 7 else: # 1 or more elements # Line 8 while ptr.next_node != None: # Line 9 ptr = ptr.next_node # Line 10 ptr.next_node = new_node # Line 11 self.tail = new_node # Line 12 self.size += 1 # Line 13
The function attempts to append a node to the end of a linked list, while updating relevant values. I want to know the time complexity of the function - ideally, it would be O(c), where c is a constant, but I believe it is O(n).
Analyzing line by line, I can see lines 1-7 and 10-13 are executed once per function call and run in constant time. Lines 8-10, however, would run in linear time since the while loop has to execute n times for n nodes. Moreover, line 10 is also executed n number of times.
Am I correct that the function runs in O(n) time?
Next, consider an alternative function.
def append(self, element): # Line 1 new_node = Node(element, None) #2nd arg. is the next_node # Line 2 ptr = self.tail # Line 3 if self.size == 0: # No elements case # Line 4 self.head = new_node # Line 5 self.tail = new_node # Line 6 self.size += 1 # Line 7 else: # 1 or more elements # Line 8 ptr.next_node = new_node # Line 9 self.tail = new_node # Line 10 self.size += 1 # Line 11
Lines 1-11 are all if statements or assignments, which run in constant time, and are executed once per function call. Therefore, this function would run as O(c).
Would it run in O(c) time?
Concatenate multi-column text files containing numbers & nan
I have a folder named
17307which contains some
.ismrfiles (essentially just CSV files) named like
SEPT307A.17_.ismr, SEPT307B.17_.ismr, SEPT307C.17_.ismr,.... upto SEPT307X.17_.ismr.
I want to concatenate all these into a single text file using Python. I tried:
st = 'path/to/folder' a = input('Enter first part of file') #i.e. SEPT307 in file name alph = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X'] yr = input('enter the year') last = '_.ismr' for letter in alph: st1 = st + "a" + alph + "." + "yr" + last fp = open(st1, "r") data=np.append(data, np.fromfile(fp, dtype=list))
i.e. I am trying to put everything into data and later copy data to a separate text file. However I am getting this error:
TypeError: Can't convert 'list' object to str implicitly
Can anyone kindly suggest some way for doing this?
How to correctly output "a list of appended data to CSV column" using Python?
I'm trying to output a list of appended data to a new CSV column from another CSV file.
This is my code:
import csv filename = 'abcd.csv' x =  with open(filename, newline = '') as f: reader = csv.reader(f) for row in reader: c = row.split('T') x.append(c) with open('year.csv', 'w') as wf: writer = csv.writer(wf) writer.writerow(x) f.close() wf.close()
I expect the output to be in columns, but the actual output was in rows.
![Started From]: (https://imgur.com/5btOIpI)
![Expected Result]: (https://imgur.com/Y9V2tVQ)