Commits (2)
\ No newline at end of file
# Numpy examples
This directory contains several script that show how numpy can be used for particular scientific applications.
## [numpyIO.py](numpyIO.py)
Read and write data (matrix, vector, etc ...) with simple, efficient and beautiful one-line command.
In particular, this script uses the [loadtxt](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.loadtxt.html)
and [savetxt](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.savetxt.html) numpy functions, to read the data stored
in the [data.txt](data.txt) file, modify those and write the modified data into [data_double.txt](data_double.txt).
## [csv-read-interpol-plot.py](csv-read-interpol-plot.py)
Read noisy data that are stored in a .csv file (comma separated values, like Excel files, ...), and build a polynomial that can fit the data.
In particular, it uses the [genfromtxt](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.genfromtxt.html) function to read
the .csv file, and the [polyfit](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.polyfit.html) to fit a polynomial to the noisy data.
\ No newline at end of file
......@@ -11,7 +11,7 @@ import numpy as np
import matplotlib.pyplot as plt
# Get data from the csv file
data = np.genfromtxt('data.csv',dtype=float,delimiter=',',names=True)
data = np.genfromtxt('data.csv', dtype=float, delimiter=',', names=True)
# Getting the fields
times = data['Time']
......@@ -21,12 +21,12 @@ velocities = data['Velocity']
# Interpoling data to suppress measurement error
polyDeg = 25
# getting the coefficient of the best polynomial fit
pressuresPolyCoeff = np.polyfit(times,pressures,polyDeg)
velocitiesPolyCoeff = np.polyfit(times,velocities,polyDeg)
pressuresPolyCoeff = np.polyfit(times, pressures, polyDeg)
velocitiesPolyCoeff = np.polyfit(times, velocities, polyDeg)
# Constructing the polynomial with the coefficients
pressuresPoly = np.poly1d(pressuresPolyCoeff)
velocitiesPoly = np.poly1d(velocitiesPolyCoeff)
# Evaluate the polynomial at each time
pressuresInter = pressuresPoly(times)
......@@ -35,16 +35,16 @@ velocitiesInter = velocitiesPoly(times)
# Plotting all data
plt.plot(times, pressures, 'b', label="Measurements")
plt.plot(times, pressuresInter, 'r', label="Interpolation")
plt.title('Comparison between measured and interpolated fields')
plt.plot(times, velocities, 'b', label="Measurements")
plt.plot(times, velocitiesInter, 'r', label="Interpolation")
\ No newline at end of file
# File containing some different vectors
# X f(X) g(X)
0.5 6 1.1
1.0 5 1.2
1.5 4 1.3
2.0 3 1.4
2.5 2 1.5
\ No newline at end of file
# -*- coding: utf-8 -*-
Function that uses numpy to read and write file into text files
import numpy as np
# Read data
data = np.loadtxt('data.txt', dtype=float)
# Extract specific data
x = data[:, 0] # x is first column
f = data[:, 1] # f is second column
g = data[:, 2] # g is third column
# Print data
print('x = {}'.format(x))
print('f(x) = {}'.format(f))
print('g(x) = {}'.format(g))
# Multiply f and g by two
f2 = 2*f
g2 = 2*g
# Save modified data
dataOut = np.block([[x],
[g2]]).T # Concatenate data column wise
np.savetxt('data_double.txt', dataOut, fmt=('%.2f', '%2d', '%.2e'),
header='X 2f(X) 2g(X)')
......@@ -6,7 +6,7 @@ Also, you can look at an [extended list of Scipy Tutorials](https://docs.scipy.o
## [dataRegressionLSOptim.py](dataRegressionLSOptim.py)
This script performs a regression using non-linear least-square optimization extract a behavior law from noisy data.
In particular, it uses the [minimize](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html) function of the scipy packages.
In particular, it uses the [minimize](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html) function of the scipy package.
## [jacobMultiDim.py](jacobMultiDim.py)
......@@ -17,3 +17,4 @@ f(u) = c_x \frac{\partial u}{\partial x}
It compute it in 1D, 2D, 3D, and extract the eigenvalues for each cases.
In particular, it uses the [eigvals](https://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.eigvals.html) of the scipy package.