interpolate at a given latitude and longitude python
I have a grid of data at latitude and longitude and i would like to know the values of the data at a given latitude and longitude. I have tried to do a program by my self but it doesn't work
1 answer

Could you be looking for something like https://docs.scipy.org/doc/scipy0.19.0/reference/generated/scipy.interpolate.interp1d.html ?
if df is the dataframe and np is numpy
X = df.as_matrix(columns=["colA"]).flatten() Y = df.as_matrix(columns=["colB"]).flatten() #set up interpolation function f = interp1d(X, Y, kind='cubic') #now a list of values you want values for can be passed in: newX = np.array([0.1, 0.2, 0.3]) newY = f(newX)
See also questions close to this topic

Is it possible to use 'zip()' with a step parameter in Python?
I would like to extract a very specific portion of a 2D array in Python using the
zip()
method (and avoid messy for loop logic). I'd like to use zip to achieve something like this:>>> sub_matrix = list(zip([*grid[0:3]]*3)) # Desired output example (Option 1) [".","4",".", ".",".","4",".",".","."] # Desired output example (Option 2) [[".","4","."], [".",".","4"], [".",".","."]]
I am working with the 2D array below in Python on an interview practice problem.
grid = [[".","4",".",".",".",".",".",".","."], [".",".","4",".",".",".",".",".","."], [".",".",".","1",".",".","7",".","."], [".",".",".",".",".",".",".",".","."], [".",".",".","3",".",".",".","6","."], [".",".",".",".",".","6",".","9","."], [".",".",".",".","1",".",".",".","."], [".",".",".",".",".",".","2",".","."], [".",".",".","8",".",".",".",".","."]]
Part of solving the problem involves ensuring each 3 x 3 "region" in a sudoku game contains legal values. I'd like to use
zip()
to quickly extract a 3 x 3 portion of the matrix. For example, the topleft region would cause the tests to fail because it contains 4 twice.I know I can subset the grid to get the first three rows as follows:
>>> sub_grid = grid[0:3] >>> print(sub_grid) [['.', '4', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '4', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '1', '.', '.', '7', '.', '.']]
I modified the printing a little bit to make it obvious, but at this point, I'd like to zip the three arrays using a 'step' of 3, so that each new array will zip 3 values from each array before moving on to the next one.
In the Python3 docs on
zip
there is an excerpt on how I think this can be done, but I cannot get the desired output.The lefttoright evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into nlength groups using
zip(*[iter(s)]*n)
.(For posterity, question is from CodeFights will be hidden until unlocked)
Any help is greatly appreciated. Thanks.

Kivy : TextInput widget with python 2.7
I wrote a little code to learn kivy.
When I run this code with python 3.5 it's ok.
But when I run it with python 2.7 I get this error message :
Text ] Provider: sdl2 /usr/lib/python2.7/distpackages/gi/module.py:178: Warning: g_boxed_type_register_static: assertion 'g_type_from_name (name) == 0' failed stderr: /usr/lib/python2.7/distpackages/gi/module.py:178: Warning: g_boxed_type_register_static: assertion 'g_type_from_name (name) == 0' failed g_type = info.get_g_type() stderr: g_type = info.get_g_type() /usr/lib/python2.7/distpackages/gi/module.py:178: Warning: g_once_init_leave: assertion 'result != 0' failed stderr: /usr/lib/python2.7/distpackages/gi/module.py:178: Warning: g_once_init_leave: assertion 'result != 0' failed g_type = info.get_g_type() stderr: g_type = info.get_g_type() /usr/lib/python2.7/distpackages/gi/module.py:212: Warning: g_type_get_qdata: assertion 'node != NULL' failed stderr: /usr/lib/python2.7/distpackages/gi/module.py:212: Warning: g_type_get_qdata: assertion 'node != NULL' failed type_ = g_type.pytype stderr: type_ = g_type.pytype
When I run the code on Android with kivylauncher, it also doesn't work.
I tryed to find what is wrong and I found this : when I delete the TextInput block it works, just as if this widget wasn't compatible with python 2.7. My code is below. Thank you for your help.
from kivy.base import runTouchApp from kivy.lang import Builder from kivy.uix.floatlayout import FloatLayout Builder.load_string(''' <FloatLayout>: BoxLayout: orientation: 'vertical' BoxLayout: height: "40dp" size_hint_y: None Label: size_hint_x: 25 text: "Longueur" TextInput: id: longueur size_hint_x: 25 Label: size_hint_x: 25 text: "Largeur" TextInput: id: largeur size_hint_x: 25 BoxLayout: size_hint_y: 20 Button: size_hint_x: 1 text: "Calcule" on_press: root.calcule() BoxLayout: size_hint_y: 20 Label: size_hint_x: 25 text: "Périmètre" Label: id: perimetre size_hint_x: 25 text: "p" Label: size_hint_x: 25 text: "Aire" Label: id: aire size_hint_x: 25 text: "a" ''') class FloatLayout(FloatLayout): def calcule(self): longueur=self.ids.longueur largeur=self.ids.largeur aire=self.ids.aire aire.text=str (float(longueur.text)*float(largeur.text)) perimetre=self.ids.perimetre perimetre.text=str (2*(float(longueur.text)+float(largeur.text))) runTouchApp(FloatLayout())

"local code referenced before assignment" need fix
When writing this code:
import random canvas=turtle.Screen() canvas.bgcolor("lightblue") a=turtle.Pen() tot=0 gain=1 def click(x,y): tot=(tot+gain) print(tot) canvas.onclick(click,1)
I get the following error:
UnboundLocalError: local variable 'tot' referenced before assignment

How to delete few top rows and last row from a CSV file using python
I have CSV files those I can't edit using Excel. I want to make a dynamic code to delete top few rows (before header row) and last row without inputting row numbers. Code I am using right now is:
FIRST_ROW_NUM = 1 ROWS_TO_DELETE = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 5421344} with open('filename', 'r') as infile,open('filename', 'w') as outfile: outfile.writelines(row for row_num, row in enumerate(infile, FIRST_ROW_NUM) if row_num not in ROWS_TO_DELETE)
The problem with this code is I have to manually input rows number to delete them.
Another issue I have is the number of rows to delete is not constant and changes from file to file.
The sample CSV is attached here
I want a code that can somehow delete those rows without any input from my side.
Note: There is no info about the last row in the CSV but it is something like this:
Grand Total:                          

replacing missing with a word based on values of the next row. If value in next column is blank, no action is required
For the following data frame,
A B D R sentence ADR1 ADR2 135 21 EffexorXR.21 1 lack of good feeling. NAN feeling 135 21 EffexorXR.21 1 1 0 136 21 EffexorXR.21 2 Feel disconnected Feel NAN 136 21 EffexorXR.21 2 0 0 142 22 EffexorXR.22 1 Weight gain gain 142 22 EffexorXR.22 1 1
For some rows, there are values (0 or 1) that their corresponding values in pervious row is null or black space. I need to replace that null or space values with "F". The output should be like this:
A B D R sentence ADR1 ADR2 135 21 EffexorXR.21 1 lack of good feeling. F feeling 135 21 EffexorXR.21 1 1 0 136 21 EffexorXR.21 2 Feel disconnected Feel F 136 21 EffexorXR.21 2 0 0 142 22 EffexorXR.22 1 Weight gain gain 142 22 EffexorXR.22 1 1
I tried
df[ADR1].fillna(0, inplace=True) and df[ADR2].fillna(0, inplace=True),
but it creates the following df, which is not wanted:A B D R sentence ADR1 ADR2 135 21 EffexorXR.21 1 lack of good feeling. F feeling 135 21 EffexorXR.21 1 1 0 136 21 EffexorXR.21 2 Feel disconnected Feel F 136 21 EffexorXR.21 2 0 0 142 22 EffexorXR.22 1 Weight gain gain F 142 22 EffexorXR.22 1 1 F

Python  OLS Regression and backward prediction
I have a dataset for my evaluation found in this link (https://drive.google.com/drive/folders/0B2Iv8dfU4fTUMVFyYTEtWXlzYkk) as in the following format. The third column (Y) in my dataset is my true value  that's what I wanted to predict.
time X Y 0.000543 0 10 0.000575 0 10 0.041324 1 10 0.041331 2 10 0.041336 3 10 0.04134 4 10 ... 9.987735 55 239 9.987739 56 239 9.987744 57 239 9.987749 58 239 9.987938 59 239
I want to run a rolling of for example 5 window
OLS regression estimation
, and I have tried it with the following script.# /usr/bin/python tt import numpy as np import matplotlib.pyplot as plt import pandas as pd df = pd.read_csv('estimated_pred.csv') model = pd.stats.ols.MovingOLS(y=df.Y, x=df[['X']], window_type='rolling', window=5, intercept=True) df['Y_hat'] = model.y_predict print(df['Y_hat']) print (model.summary) df.plot.scatter(x='X', y='Y', s=0.1)
Why am I getting the values for
Rsquared
and AdjRsquared
asinf
andRmse
as0.0000
as it shown in the summary of the regression analysis below?Summary of Regression Analysis Formula: Y ~ <X> + <intercept> Number of Observations: 5 Number of Degrees of Freedom: 2 Rsquared: inf Adj Rsquared: inf Rmse: 0.0000 Fstat (1, 3): nan, pvalue: nan Degrees of Freedom: model 1, resid 3 Summary of Estimated Coefficients Variable Coef Std Err tstat pvalue CI 2.5% CI 97.5%  X 0.0000 0.0000 1.97 0.1429 0.0000 0.0000 intercept 239.0000 0.0000 14567091934632472.00 0.0000 239.0000 239.0000 End of Summary
I want to finally do a backward prediction of
y
att+1
(i.e. predict the next value ofy
according to the previous value i.e.p(y)t+1
by including the mean squared error (MSE
)  for example, if we look at row 5, the value ofx
is 2 and the value ofY
is 10. Let's say the prediction value (p(y)t+1
) is 6 and therefore themse
will be(106)^2
. How can we do this using eitherstatsmodels
orscikitlearn
forpd.stats.ols.MovingOLS
was removed inPandas
version 0.20.0 and since I can't find any reference?