hj170520 发表于 2020-6-13 10:24

Numpy 入门(六)Polynomials & Loading data files

NumPy also contains polynomials in different bases:

For example, $3x^2 + 2x - 1$:

```
%matplotlib inline
import matplotlib.pyplot as plt # the tidy way
import pandas as pd
import numpy as np
```

# 10. Polynomials

```
>> p = np.poly1d()
>> p(0)
-1
>> p.roots
array([-1.      ,0.33333333])
>> p.order
2
>> x = np.linspace(0, 1, 20)
>> y = np.cos(x) + 0.3*np.random.rand(20)
>> p = np.poly1d(np.polyfit(x, y, 3))
>> t = np.linspace(0, 1, 200) # use a larger number of points for >> smoother plotting
>> plt.plot(x, y, 'o', t, p(t), '-')   
>> plt.show()
```



## More polynomials (with more bases)

NumPy also has a more sophisticated polynomial interface, which supports e.g. the Chebyshev basis.

$3x^2 + 2x - 1$:

```
>> p = np.polynomial.Polynomial([-1, 2, 3]) # coefs in different order!
>> p(0)
-1.0
>> p.roots()
array([-1.      ,0.33333333])
>> p.degree()# In general polynomials do not always expose 'order'
2
>> x = np.linspace(-1, 1, 2000)
>> y = np.cos(x) + 0.3*np.random.rand(2000)
>> p = np.polynomial.Chebyshev.fit(x, y, 90)
>> plt.plot(x, y, 'r.')   
>> plt.plot(x, p(x), 'k-', lw=3)
```



The Chebyshev polynomials have some advantages in interpolation.

```
>> data = np.loadtxt('data/populations.txt')
>> data
array([[ 1900., 30000.,4000., 48300.],
       [ 1901., 47200.,6100., 48200.],
       [ 1902., 70200.,9800., 41500.],
       [ 1903., 77400., 35200., 38200.],
       [ 1904., 36300., 59400., 40600.],
       [ 1905., 20600., 41700., 39800.],
       [ 1906., 18100., 19000., 38600.],
       [ 1907., 21400., 13000., 42300.],
       [ 1908., 22000.,8300., 44500.],
       [ 1909., 25400.,9100., 42100.],
       [ 1910., 27100.,7400., 46000.],
       [ 1911., 40300.,8000., 46800.],
       [ 1912., 57000., 12300., 43800.],
       [ 1913., 76600., 19500., 40900.],
       [ 1914., 52300., 45700., 39400.],
       [ 1915., 19500., 51100., 39000.],
       [ 1916., 11200., 29700., 36700.],
       [ 1917.,7600., 15800., 41800.],
       [ 1918., 14600.,9700., 43300.],
       [ 1919., 16200., 10100., 41300.],
       [ 1920., 24700.,8600., 47300.]])
>> np.savetxt('pop2.txt', data)
>> data2 = np.loadtxt('pop2.txt')
```

# 11. Loading data files

Note If you have a complicated text file, what you can try are:
np.genfromtxt
Using Python’s I/O functions and e.g. regexps for parsing (Python is quite well suited for this)

```
'''
pwd      # show current directory
cd ex
ls
'''
```

# Images

## Using Matplotlib:

```
>> img = plt.imread('data/elephant.png')
>> img.shape, img.dtype
((200, 300, 3), dtype('float32'))
```

```
>> plt.imshow(img)
```



```
>> plt.savefig('plot.png')
>> plt.imsave('red_elephant.png', img[:,:,0], cmap=plt.cm.gray)
```

## This saved only one channel (of RGB):

```
>> plt.imshow(plt.imread('red_elephant.png'))
```



```
>> from imageio import imsave
>> imsave('tiny_elephant.png', img[::6,::6])
>> plt.imshow(plt.imread('tiny_elephant.png'), interpolation='nearest')
```



# NumPy’s own format

NumPy has its own binary format, not portable but with efficient I/O:

```
>> data = np.ones((3, 3))
>> np.save('pop.npy', data)
>> data3 = np.load('pop.npy')
```

# Well-known (& more obscure) file formats

> HDF5: h5py, PyTables
> NetCDF: scipy.io.netcdf_file, netcdf4-python, …
> Matlab: scipy.io.loadmat, scipy.io.savemat
> MatrixMarket: scipy.io.mmread, scipy.io.mmwrite
> IDL: scipy.io.readsav


> Exercise: Text data files
>
> Write a Python script that loads data from populations.txt::
>         and drop the last column and the first 5 rows.
>         Save the smaller dataset to pop2.txt.

hj170520 发表于 2020-6-13 10:27

大象(原图):

hor5 发表于 2020-6-13 11:18

挺好的,这是给自己做的笔记还是给大家看的? 给大家看的话代码后面加上注释是不是更好些?

hj170520 发表于 2020-6-13 12:37

hor5 发表于 2020-6-13 11:18
挺好的,这是给自己做的笔记还是给大家看的? 给大家看的话代码后面加上注释是不是更好些?

{:301_986:}“英文单词”写的比较清楚了
root 是根
rank 是秩
我相信大家的英语能力的。
页: [1]
查看完整版本: Numpy 入门(六)Polynomials & Loading data files