Transferring matrices and vectors from R to Python [closed] - python

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 = ",")

Related

Solving a project in Python / pygame [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 am solving an issue in Python and Pygame (of which I have little knowledge). I am trying to find the way for finding the column x in a 2d rectangular board. I know that to find row y, I would go by:
ind = y * self._num_cols
return self._grid[ind : ind + self._num_cols]
How do I find column x then, without using numPy?
Assuming that it is 1 dimensional, you can do:
return [self._grid[x + i*self._num_cols] for i in range(self._num_cols)]
I know it seems long, but it is just iteratively getting the one in that column for each row.

How to iterate and change all elements of a numpy array? [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 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]])

using an R modelled regression 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 4 years ago.
Improve this question
My data was modelled with a Cox-regression, using R, however I would like to use this model into a python GUI. As my knowledge of R is very limited. This way non-coders would be able to 'predict' survival rates based on our model.
What is the best way that I could use this model (combination of 3 different regressions) in python?
Do you want to predict values based on your estimates?
In this case you can just copy the R outputs into python and apply to
respective procedures.
Do you want the user to be able to run "your R regression pipeline" from within Python?
There are python libraries that help with that. I find this
source a useful start.

Create heating degree days (HDD) column in pandas dataframe [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 want to create a heating degree days (HDD) column in a Pandas dataframe, using another column (df.temp) for the temperature.
Here's the formula I'd like to replicate in a Pandas dataframe:
df['hdd'] = max(0, (15 - df.temp))
Try this:
import numpy as np
df['hdd'] = np.maximum(0, (15 - df['temp']))
numpy vectorises calculations, so it applies across the series.

Generate Random maps data in google maps like format using 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 6 years ago.
Improve this question
How can I generate random data in google maps like format i.e. 29.299332, 52.892959?
Do you prefer any package for this purpose?
If you just want a pair of random numbers between 0 and 90 degrees, why not just use the random package?
import random
print([random.random()*90, random.random()*90]) #[34.050498339418986, 5.622759330528135]

Categories