Python Interpolate Txt

Asked

Viewed 959 times

2

Good night, I recently started the program in python because of an elective I have at university, but my knowledge is still minimal. My teacher gave a job to do where I should create a code that reads a Precos.txt table like this:

1,  69.00
2,  69.00
3,  69.00
4,  69.00
5,  69.00
6,  69.00
7,  69.00
8,  79.00
9,  56.51
10, 56.51

Where the first column represents a few days of the month and the second column represents prices. As far as I could learn, I know I can do this reading with these commands:

precos = sp.loadtxt("Precos.txt", delimiter=",")
precos = np.genfromtxt('Precos.txt', delimiter=',')
x, y = np.loadtxt("Precos.txt", delimiter=",", unpack=True)

After reading the table, the program should plot a chart, which I also managed to do without any kind of problem:

plt.plot(x,y)

My problem starts now. My teacher wants q I make an interpolation, and suggested to use sp.interpolate.interp1d(x,y) to generate another chart. But the script does not want to run.

Could someone help me? Is there a more efficient way to do this? Or something more "professional", since the ideal would be to read a CSV file instead of a TXT.

Grateful from now on

----EDIT--- Program almost ready, but I caught on error:

Valueerror: A value in x_new is above the Interpolation range.

import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate

## Importando os dados numa matriz Nx2

M = sp.loadtxt('Precos.txt', delimiter=',')

## Construindo os vetores X e Y

x=np.zeros(len(M))
y=np.zeros(len(M))
for i in range(len(M)):
    x[i] = M[i][0]
    y[i] = M[i][1]

##Plotando grafico

plt.plot(x,y)
plt.title("Fone de Ouvido JBL com Microfone T100A - Fevereiro 2017")
plt.xlabel("Dia")
plt.ylabel("Preco em R$")

##Interpolação

F = sp.interpolate.interp1d(x,y)
xn = sp.arange(0,9,0.1)
yn = F(xn)
plt.plot(x, y, 'o', xn, yn, '-')
plt.show()

In the case of this part of Interpolation, it was my teacher who told me to use it like this. But it’s not doing much good.

  • Have you opened a common CSV text editor file? There is nothing more "professional" in a CSV file than in a TXT - your TXT, by the way, is structured as a CSV.

  • Yes, yes. I expressed myself badly.

1 answer

2

Got a little vague on the part

But the program doesn’t want to run

I ran your program here without great difficulties, you know what the function interp1d makes? by default it makes a linear interpolation with your input data x e y, it returns a function in which it can be used to find which is the interpolated value of a given specific point.

code:

from scipy import interpolate
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt


x, y = np.loadtxt("Precos.txt", delimiter=",", unpack=True)
interpolado = interpolate.interp1d(x,y);
plt.plot(np.linspace(1, 10, 10),interpolado(np.linspace(1, 10, 10)))
plt.show()

Look at the line plt.plot(np.linspace(1, 10, 10),interpolado(np.linspace(1, 10, 10))) I am talking to plot the return of the linearly interpolated function of the points between 1 and 10, it returns me the following Plot:

inserir a descrição da imagem aqui

And if I want to know what would be the return of the 20-point function between 1 and 10? plt.plot(np.linspace(1, 10, 20),interpolado(np.linspace(1, 10, 20)))

inserir a descrição da imagem aqui

Edit

About your Edit

Run with the following line xn = sp.arange(1,10,0.1) to interpolate from 1 to 10 linearly spaced by 0.1, the graph will be:

inserir a descrição da imagem aqui

You cannot find the interpolation for values above 10, your input data goes from 1 to 10, no matter how many interpolated values you find as long as they are in the data input range.

  • I got it... I tried to change the range from 1 to 10 to 1 to 20, and the program returned "Valueerror: A value in x_new is above the Interpolation range." Could you explain to me the reason? In the last version of this program I had already bumped into this error, and so far I could not get out of it.

  • your problem is the sp.arange(0,9,0.1) what you expect of him ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.