Tag: python

54 votes
3 answers
2.6K views
What is the difference between flatten and ravel functions in numpy?

import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6

avatar
Arline
0 1 0 9
asked 07 September, 2021
60 votes
9 answers
1.3K views
Creating a Pandas DataFrame from a Numpy array: How do I specify the index column and column headers?

I have a Numpy array consisting of a list of lists, representing a two-dimensional array with row labels and c

avatar
Sena
0 1 0 9
asked 07 September, 2021
53 votes
13 answers
2.3K views
Converting between datetime, Timestamp and datetime64

How do I convert a numpy.datetime64 object to a datetime.datetime (or Timestamp)? In the following code, I cr

avatar
Adolfo
0 1 0 9
asked 07 September, 2021
65 votes
6 answers
1.5K views
What is the difference between Numpy's array() and asarray() functions?

What is the difference between Numpy's array() and asarray() functions? When should you use one rather th

avatar
Louetta
0 1 0 9
asked 07 September, 2021
52 votes
5 answers
608 views
Converting NumPy array into Python List structure?

How do I convert a NumPy array to a Python List (for example [[1,2,3],[4,5,6]] ), and do it reasonably fast?

avatar
Kurtis
0 1 0 9
asked 07 September, 2021
55 votes
17 answers
3.1K views
How to add an extra column to a NumPy array

Let’s say I have a NumPy array, a: a = np.array([ [1, 2, 3], [2, 3, 4] ]) And I would like

avatar
Majorie
0 1 0 9
asked 07 September, 2021
55 votes
15 answers
782 views
How do I create an empty array/matrix in NumPy?

I can't figure out how to use an array or matrix in the way that I would normally use a list. I want to c

avatar
Jefferey
0 1 0 9
asked 07 September, 2021
54 votes
7 answers
1.3K views
Difference between numpy.array shape (R, 1) and (R,)

In numpy, some of the operations return in shape (R, 1) but some return (R,). This will make matrix multiplica

avatar
Solomon
0 1 0 9
asked 07 September, 2021
53 votes
14 answers
1.8K views
How to pretty-print a numpy.array without scientific notation and with given precision?

I'm curious, whether there is any way to print formatted numpy.arrays, e.g., in a way similar to this: x

avatar
Joseph
0 1 0 9
asked 07 September, 2021
61 votes
11 answers
1.4K views
What does numpy.random.seed(0) do?

What does np.random.seed do in the below code from a Scikit-Learn tutorial? I'm not very familiar with Nu

avatar
Rudolf
0 1 0 9
asked 07 September, 2021
62 votes
5 answers
560 views
What are the differences between numpy arrays and matrices? Which one should I use?

What are the advantages and disadvantages of each? From what I've seen, either one can work as a replace

avatar
Sachiko
0 1 0 9
asked 07 September, 2021
53 votes
10 answers
2.5K views
Pandas conditional creation of a series/dataframe column

I have a dataframe along the lines of the below: Type Set 1 A Z 2 B Z

avatar
Janella
0 1 0 9
asked 07 September, 2021
64 votes
18 answers
809 views
Find nearest value in numpy array

Is there a numpy-thonic way, e.g. function, to find the nearest value in an array? Example: np.find_nearest

avatar
Dario
0 1 0 9
asked 07 September, 2021
69 votes
7 answers
437 views
What is the purpose of meshgrid in Python / NumPy?

Can someone explain to me what is the purpose of meshgrid function in Numpy? I know it creates some kind of gr

avatar
Yolando
0 1 0 9
asked 07 September, 2021
66 votes
3 answers
410 views
Simple Digit Recognition OCR in OpenCV-Python

I am trying to implement a "Digit Recognition OCR" in OpenCV-Python (cv2). It is just for learning purposes. I

avatar
Serina
0 1 0 9
asked 07 September, 2021
65 votes
8 answers
412 views
Numpy array dimensions

I'm currently trying to learn Numpy and Python. Given the following array: import numpy as np a = np.arr

avatar
Fabian
0 1 0 9
asked 07 September, 2021
48 votes
15 answers
432 views
Sorting arrays in NumPy by column

How can I sort an array in NumPy by the nth column? For example, a = array([[9, 2, 3], [4, 5, 6],

avatar
Nidia
0 1 0 9
asked 07 September, 2021
60 votes
10 answers
1.8K views
Pandas read_csv low_memory and dtype options

When calling df = pd.read_csv('somefile.csv') I get: /Users/josh/anaconda/envs/py27/lib/pytho

avatar
Fabian
0 1 0 9
asked 07 September, 2021
59 votes
7 answers
2.9K views
pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

I want to apply my custom function (it uses an if-else ladder) to these six columns (ERI_Hispanic, ERI_AmerInd

avatar
Alphonso
0 1 0 9
asked 07 September, 2021
55 votes
17 answers
1.5K views
How to find all occurrences of an element in a list

index() will give the first occurrence of an item in a list. Is there a neat trick which returns all indices i

avatar
Shanita
0 1 0 9
asked 07 September, 2021