How to iterate and change all elements of a numpy array? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am working on a machine learning task and trying to convert all strings in a set of data to floats using hash() to do this I need to iterate over all the elements of a numpy array whilst not knowing if it is a 2D 3D or 4D array and then change each element. Is there any way to do this without using nested loops?

You can try numpu.vectorize, already mentioned here
Note:
The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.here
arr = np.array([['aba', 'baa', 'bbb'],
['xxy', 'xyy', 'yyy']])
v_hash = np.vectorize(hash)
v_hash(arr)
array([[-1538054455328520296, -1528482088733019667, -7962229468338304433],
[ 5621962119614158870, 1918700875003591346, -3216770211373729154]])

Related

matrix of variable size in global variables , python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I was doing a project in python and I would like to create a global variable which must be a two-dimensional array, but the dimension must be assigned when the user enters it.
how can i declare it initially? and then resize it when I want?
thanks for your attention
You can declare two-dimensional array doing simply this:
a = [[]]
If you print it out, you're gonna get something like this: [[]] which is two-dimensional array. Then you can append to this array and get elements using a[y][x]

Pandas Python iteration or mod [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to do an iteration with Pandas or any built-in function to display multiple of 10 rows for example.
So e.g. there are 50 records and I want to display the multiple of 10 records which will be record ID 0,10,20,30,40,50.
Use iloc:
df.iloc[::10, :]
This method takes a row/column slice, both based on integer position. More details from documenation:
Purely integer-location based indexing for selection by position.
.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.

Transferring matrices and vectors from R to Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have some large matrices and vectors calculated in R. I want to transfer this data to Python (2.7) in order to do some further data analysis.
What is a recommended way to do this?
I am very familiar with R, but a beginner in Python.
Use write.csv(matrix, "~/filename.csv) in R and then in Python either (if you want to use pandas)
import pandas as pd
new_matrix = pd.read_csv("~/filename.csv")
or (if you want to use numpy)
import numpy as np
new_matrix = np.genfromtxt("~/filename.csv", delimiter = ",")

Mat in C++ to Numpy [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have three C++ matrices called myMatrix, myMatrix2 and canvas respectively using OpenCV. I'm pretty new to C++, so it's unclear to me what Range::all() does. I understand the second Range statement, and I'm wondering if Range::all() is equivalent to Range(0,myMatrix.rows)?
myMatrix.copyTo(canvas(Range::all(), Range(0, myMatrix2.cols)));
I'm thinking it should be something like:
canvas = np.copy(myMatrix[:][:myMatrix2.cols])
If I did understand properly, the numpy equivalent can be written as:
canvas = np.copy(myMatrix[:, :myMatrix2.shape[1]])
Assuming that both myMatrix and myMatrix2 exist. If canvas also exists in python beforehand, you can update it inplace (rather than creating a new copy) as:
canvas[:] = myMatrix[:, :myMatrix2.shape[1]]
The : is the equivalent to Range::all() while :myMatrix2.shape[1] is equivalent to Range(0, myMatrix2.cols).
You should first learn how to use numpy (it has a great manual) rather than trying to make something work blindly.

What is the definition of a 'list' in python? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I always use lists when coding but can never put into words when analyzing my own work. what is the definition of a list in detail?
It's a datatype that stores a bunch of stuff. The more general term is an 'array.'
In python, they are represented like so:
[1, 2, 3, 4, 'elem5', ['sub-list-item-1', 'sub-list-item-2'], {}]
One important aspect of lists is that they can contain any type of data, or mixed data types.
Python docs define a list thusly:
list
A built-in Python sequence. Despite its name it is more akin to
an array in other languages than to a linked list since access to
elements are O(1).
Read More Here

Categories