Biểu đồ hàm mũ trong python

Nếu bạn có một tập hợp các điểm dữ liệu có vẻ như chúng đang tăng nhanh, thì có thể hữu ích khi khớp chúng với một đường thẳng tăng theo cấp số nhân để mô tả hình dạng chung của dữ liệu

Đường mà bạn cần điều chỉnh để đạt được hình dạng này sẽ là đường được mô tả bằng hàm mũ, đó là bất kỳ hàm nào có dạng

\[y = AB^x + C\]

hoặc

\[y = ae^{bx} + c\]

[hai cái này tương đương về mặt toán học vì \[AB^x = Ae^{x\ln[B]}\]]. Điều quan trọng cần nhận ra là một hàm số mũ có thể được xác định đầy đủ với ba hằng số. Chúng tôi sẽ sử dụng công thức thứ hai trong số các công thức này, có thể được viết bằng Python dưới dạng

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
7 trong đó
import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
8 là hàm mũ \[e^x\] từ gói Numpy [được đổi tên thành
import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
9 trong các ví dụ của chúng tôi]

Đối với hướng dẫn này, hãy tạo một số dữ liệu giả để sử dụng làm ví dụ. Đây phải là một tập hợp các điểm tăng theo cấp số nhân [nếu không, nỗ lực của chúng tôi để điều chỉnh một đường cong theo cấp số nhân cho chúng sẽ không hoạt động tốt. ] với một số nhiễu ngẫu nhiên được đưa vào để bắt chước dữ liệu trong thế giới thực

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 50
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

Tiếng ồn ngẫu nhiên đang được thêm vào với chức năng

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
0 từ Numpy, lấy các mẫu ngẫu nhiên từ phân phối [Gaussian] bình thường. Hãy xem dữ liệu ví dụ này trông như thế nào trên biểu đồ phân tán

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']

Phương pháp này chỉ hoạt động khi \[c = 0\], tức là khi bạn muốn khớp một đường cong có phương trình \[y = ae^{bx}\] với dữ liệu của mình. Nếu bạn muốn khớp một đường cong có phương trình \[y = ae^{bx} + c\] với \[c \neq 0\] thì bạn cần sử dụng phương pháp 2

Lệnh

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
1 từ Numpy được sử dụng để khớp hàm đa thức với dữ liệu. Điều này có vẻ hơi lạ. tại sao chúng ta cố gắng khớp hàm đa thức với dữ liệu khi chúng ta muốn khớp hàm mũ?

\[y = ae^{bx} \implies \ln[y] = \ln[a] + bx\]

vì chúng ta có thể lấy logarit tự nhiên của cả hai vế. Điều này tạo ra một phương trình tuyến tính \[f[x] = mx + c\] trong đó

  • \[f[x] = \ln[y]\]
  • \[m = b\]
  • \[c = \ln[a]\]

Vì vậy,

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
1 có thể được sử dụng để khớp \[\ln[y]\] với \[x\]

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]

Bây giờ đa thức này có thể được chuyển đổi trở lại thành một hàm mũ

# Convert the polynomial back into an exponential
a = np.exp[p[1]]
b = p[0]
x_fitted = np.linspace[np.min[x], np.max[x], 100]
y_fitted = a * np.exp[b * x_fitted]

Hãy xem xét sự phù hợp

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
0

Phương pháp này có nhược điểm là nhấn mạnh quá mức các giá trị nhỏ. các điểm có giá trị lớn và tương đối gần với đường tuyến tính phù hợp nhất được tạo bởi

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
1 trở nên xa hơn nhiều so với đường phù hợp nhất khi đa thức được chuyển đổi trở lại thành hàm mũ. Hành động biến đổi một hàm đa thức thành một hàm mũ có tác dụng làm tăng các giá trị lớn hơn nhiều so với các giá trị nhỏ và do đó, nó có tác dụng tăng khoảng cách đến đường cong phù hợp cho các giá trị lớn hơn so với các giá trị nhỏ. Điều này có thể được giảm thiểu bằng cách thêm 'trọng số' tỷ lệ với \[y\]. yêu cầu
import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
1 tăng tầm quan trọng của các điểm dữ liệu có giá trị y lớn

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
3

Sử dụng trọng lượng đã cải thiện sự phù hợp

Từ gói scipy, chúng ta có thể nhận hàm ________ 45. Điều này tổng quát hơn

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
1 [chúng ta có thể phù hợp với bất kỳ loại hàm nào chúng ta thích, theo cấp số nhân hoặc không] nhưng phức tạp hơn ở chỗ đôi khi chúng ta cần đưa ra dự đoán ban đầu về các hằng số có thể là gì để nó hoạt động

Hãy sử dụng dữ liệu ví dụ ban đầu của chúng tôi [với \[c \neq 0\]]

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
6

Bây giờ, hãy khớp hàm \[y = ae^{bx} + c\]. Điều này được thực hiện bằng cách xác định nó dưới dạng hàm lambda [nghĩa là dưới dạng đối tượng chứ không phải dưới dạng lệnh] của biến giả \[t\] và sử dụng hàm

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
5 để khớp đối tượng này với dữ liệu x và y. Lưu ý rằng chức năng
import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
5 cần được nhập từ gói phụ
import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
9

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
0

Lưu ý rằng chúng ta cần xóa bất kỳ giá trị nào bằng 0 khỏi dữ liệu y của mình [và các giá trị x tương ứng của chúng khỏi dữ liệu x] để điều này hoạt động, mặc dù không có bất kỳ giá trị nào trong số này trong dữ liệu ví dụ này nên nó

Đầu ra đầu tiên,

# Convert the polynomial back into an exponential
a = np.exp[p[1]]
b = p[0]
x_fitted = np.linspace[np.min[x], np.max[x], 100]
y_fitted = a * np.exp[b * x_fitted]
0, là danh sách các giá trị được tối ưu hóa cho các tham số, trong trường hợp của chúng tôi, là các hằng số \[a\], \[b\] và \[c\]

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
2

Hãy xem nó trông như thế nào

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
3

Điều này có vẻ thực sự tốt và chúng tôi không cần đưa ra dự đoán ban đầu. Điều này là do dữ liệu ví dụ mà chúng tôi đang sử dụng đủ gần với số mũ về bản chất để thuật toán tối ưu hóa đằng sau

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
5 có thể khớp với một đường cong mà không vô tình chọn sai mức tối thiểu cục bộ. Điều này không phải lúc nào cũng đúng, vì vậy đây là cách thực hiện với dự đoán ban đầu được cung cấp

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
0
import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
1

Hãy vẽ sơ đồ cả ba phương pháp với nhau bằng cách sử dụng cùng một dữ liệu ví dụ [\[c = 0\]] cho mỗi phương pháp

import matplotlib.pyplot as plt

# Formatting options for plots
A = 6  # Want figure to be A6
plt.rc['figure', figsize=[46.82 * .5**[.5 * A], 33.11 * .5**[.5 * A]]]
plt.rc['text', usetex=True]
plt.rc['font', family='serif']
plt.rc['text.latex', preamble=r'\usepackage{textgreek}']

# Create a plot
ax = plt.axes[]
ax.scatter[x, y]
ax.set_title['Example Data']
ax.set_ylabel['y-Values']
ax.set_ylim[0, 500]
ax.set_xlabel['x-Values']
2

Như bạn có thể thấy, phương pháp

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed[20210706]

# Create fake x-data
x = np.arange[10]
# Create fake y-data
a = 4.5
b = 0.5
c = 0
y = a * np.exp[b * x] + c  # Use the second formulation from above
y = y + np.random.normal[scale=np.sqrt[np.max[y]], size=len[x]]  # Add noise

# Fit a polynomial of degree 1 [a linear function] to the data
p = np.polyfit[x, np.log[y], 1]
5 đã cho chúng ta phép tính gần đúng nhất của hành vi cấp số nhân cơ bản thực sự

Chúng tôi có thể sử dụng đường cong được trang bị để ước tính dữ liệu của chúng tôi sẽ như thế nào đối với các giá trị khác của \[x\] không có trong tập dữ liệu thô của chúng tôi. giá trị sẽ là gì tại \[x=11\] [nằm ngoài miền của chúng tôi và do đó yêu cầu chúng tôi dự báo trong tương lai] hoặc \[x = 8. 5\] [nằm trong miền của chúng tôi và do đó yêu cầu chúng tôi 'điền vào chỗ trống' trong dữ liệu của mình]?

Chủ Đề