features = ['red1', 'red2', 'red3', 'red4', 'red5', 'red6', 'blue'] X = data[features] y = data['data']
时间: 2023-08-06 20:16:05 浏览: 136
根据您提供的代码,`data` 变量中可能没有名为 `data` 的列,因此会出现 KeyError 错误。请检查您的数据集,确认确实存在名为 `data` 的列,或者将 `data['data']` 替换为正确的列名称。
例如,如果您的数据集中存在名为 `date` 的列,应该将 `data['data']` 替换为 `data['date']`,例如:
```python
features = ['red1', 'red2', 'red3', 'red4', 'red5', 'red6', 'blue']
X = data[features]
y = data['date']
```
这应该可以解决该错误。如果问题仍然存在,请提供更多的代码细节和上下文,以便我可以更好地帮助您解决问题。
相关问题
翻译这段程序并自行赋值调用:import matplotlib.pyplot as plt import numpy as np import sklearn import sklearn.datasets import sklearn.linear_model def plot_decision_boundary(model, X, y): # Set min and max values and give it some padding x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1 y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1 h = 0.01 # Generate a grid of points with distance h between them xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # Predict the function value for the whole grid Z = model(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) # Plot the contour and training examples plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral) plt.ylabel('x2') plt.xlabel('x1') plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral) def sigmoid(x): s = 1/(1+np.exp(-x)) return s def load_planar_dataset(): np.random.seed(1) m = 400 # number of examples N = int(m/2) # number of points per class print(np.random.randn(N)) D = 2 # dimensionality X = np.zeros((m,D)) # data matrix where each row is a single example Y = np.zeros((m,1), dtype='uint8') # labels vector (0 for red, 1 for blue) a = 4 # maximum ray of the flower for j in range(2): ix = range(Nj,N(j+1)) t = np.linspace(j3.12,(j+1)3.12,N) + np.random.randn(N)0.2 # theta r = anp.sin(4t) + np.random.randn(N)0.2 # radius X[ix] = np.c_[rnp.sin(t), rnp.cos(t)] Y[ix] = j X = X.T Y = Y.T return X, Y def load_extra_datasets(): N = 200 noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3) noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2) blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6) gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2, n_classes=2, shuffle=True, random_state=None) no_structure = np.random.rand(N, 2), np.random.rand(N, 2) return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure
这段程序是一个分类模型的辅助函数,包括了绘制决策边界、sigmoid函数和加载数据集的函数。具体实现如下:
```python
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import sklearn.datasets
import sklearn.linear_model
def plot_decision_boundary(model, X, y):
# 设置最小值和最大值,并给它们一些填充
x_min, x_max = X[0, :].min() - 1, X[0, :].max() + 1
y_min, y_max = X[1, :].min() - 1, X[1, :].max() + 1
h = 0.01
# 生成一个网格,网格中点的距离为h
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# 对整个网格预测函数值
Z = model(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
# 绘制轮廓和训练样本
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.ylabel('x2')
plt.xlabel('x1')
plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
def sigmoid(x):
s = 1 / (1 + np.exp(-x))
return s
def load_planar_dataset():
np.random.seed(1)
m = 400 # 样本数量
N = int(m / 2) # 每个类的样本数量
# 生成数据集
D = 2 # 特征维度
X = np.zeros((m, D)) # 特征矩阵
Y = np.zeros((m, 1), dtype='uint8') # 标签向量
a = 4 # 花的最大半径
for j in range(2):
ix = range(N*j, N*(j+1))
t = np.linspace(j*3.12, (j+1)*3.12, N) + np.random.randn(N)*0.2 # theta
r = a*np.sin(4*t) + np.random.randn(N)*0.2 # radius
X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
Y[ix] = j
X = X.T
Y = Y.T
return X, Y
def load_extra_datasets():
N = 200
noisy_circles = sklearn.datasets.make_circles(n_samples=N, factor=.5, noise=.3)
noisy_moons = sklearn.datasets.make_moons(n_samples=N, noise=.2)
blobs = sklearn.datasets.make_blobs(n_samples=N, random_state=5, n_features=2, centers=6)
gaussian_quantiles = sklearn.datasets.make_gaussian_quantiles(mean=None, cov=0.5, n_samples=N, n_features=2, n_classes=2, shuffle=True, random_state=None)
no_structure = np.random.rand(N, 2), np.random.rand(N, 2)
return noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure
```
这段程序中包含了以下函数:
- `plot_decision_boundary(model, X, y)`:绘制分类模型的决策边界,其中`model`是分类模型,`X`是特征矩阵,`y`是标签向量。
- `sigmoid(x)`:实现sigmoid函数。
- `load_planar_dataset()`:加载一个二维的花瓣数据集。
- `load_extra_datasets()`:加载五个其他数据集。
给出各拟合曲线的误差MSE:import numpy as np import pandas as pd import matplotlib.pyplot as plt from scipy.stats import zscore import numpy as np from sklearn import linear_model from sklearn.preprocessing import PolynomialFeatures data = np.loadtxt('tb.txt', delimiter=',') # a=data[:,0] area = data[:, 0] price = data[:, 1] length = len(area) area = np.array(area).reshape([length, 1]) price = np.array(price) minx = min(area) maxx = max(area) x = np.arange(minx, maxx).reshape([-1, 1]) poly=PolynomialFeatures(degree=2) poly3=PolynomialFeatures(degree=3) poly4=PolynomialFeatures(degree=4) #poly5=PolynomialFeatures(degree=5) area_poly=poly.fit_transform(area) area_poly3=poly3.fit_transform(area) area_poly4=poly4.fit_transform(area) linear2 = linear_model.LinearRegression() linear2.fit(area_poly, price) linear3 = linear_model.LinearRegression() linear3.fit(area_poly3, price) linear4 = linear_model.LinearRegression() linear4.fit(area_poly4, price) #查看回归方程系数 print('Cofficients:',linear4.coef_) #查看回归方程截距 print('intercept',linear4.intercept_) plt.scatter(area, price, color='red') plt.plot(x, linear2.predict(poly.fit_transform(x)), color='blue') plt.plot(x, linear3.predict(poly3.fit_transform(x)), linestyle='--') plt.plot(x, linear4.predict(poly4.fit_transform(x)), linestyle='-.') plt.legend(['degree=0','degree=2','degree=3','degree=4']) plt.xlabel('Year') plt.ylabel('Price') plt.show() # 2022 year_2022 = np.array([[2022]]) area_2022_poly = poly.transform(year_2022) area_2022_poly3 = poly3.transform(year_2022) area_2022_poly4 = poly4.transform(year_2022) price_2022_degree2 = linear2.predict(area_2022_poly) price_2022_degree3 = linear3.predict(area_2022_poly3) price_2022_degree4 = linear4.predict(area_2022_poly4) print("Predicted price in 2022 (degree=2):", price_2022_degree2[0]) print("Predicted price in 2022 (degree=3):", price_2022_degree3[0]) print("Predicted price in 2022 (degree=4):", price_2022_degree4[0]) # 2023 year_2023 = np.array([[2023]]) area_2023_poly = poly.transform(year_2023) area_2023_poly3 = poly3.transform(year_2023) area_2023_poly4 = poly4.transform(year_2023) price_2023_degree2 = linear2.predict(area_2023_poly) price_2023_degree3 = linear3.predict(area_2023_poly3) price_2023_degree4 = linear4.predict(area_2023_poly4) print("Predicted price in 2023 (degree=2):", price_2023_degree2[0]) print("Predicted price in 2023 (degree=3):", price_2023_degree3[0]) print("Predicted price in 2023 (degree=4):", price_2023_degree4[0])
在该代码中,我们使用了不同次数的多项式进行回归拟合,并使用均方误差(MSE)来评估拟合的误差。具体来说,我们可以使用sklearn中的mean_squared_error函数来计算MSE。以下是代码中的修改和MSE的计算:
```
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import zscore
import numpy as np
from sklearn import linear_model
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
data = np.loadtxt('tb.txt', delimiter=',')
area = data[:, 0]
price = data[:, 1]
length = len(area)
area = np.array(area).reshape([length, 1])
price = np.array(price)
minx = min(area)
maxx = max(area)
x = np.arange(minx, maxx).reshape([-1, 1])
poly=PolynomialFeatures(degree=2)
poly3=PolynomialFeatures(degree=3)
poly4=PolynomialFeatures(degree=4)
area_poly=poly.fit_transform(area)
area_poly3=poly3.fit_transform(area)
area_poly4=poly4.fit_transform(area)
linear2 = linear_model.LinearRegression()
linear2.fit(area_poly, price)
linear3 = linear_model.LinearRegression()
linear3.fit(area_poly3, price)
linear4 = linear_model.LinearRegression()
linear4.fit(area_poly4, price)
# 评估拟合误差
mse2 = mean_squared_error(price, linear2.predict(area_poly))
mse3 = mean_squared_error(price, linear3.predict(area_poly3))
mse4 = mean_squared_error(price, linear4.predict(area_poly4))
print("MSE (degree=2):", mse2)
print("MSE (degree=3):", mse3)
print("MSE (degree=4):", mse4)
plt.scatter(area, price, color='red')
plt.plot(x, linear2.predict(poly.fit_transform(x)), color='blue')
plt.plot(x, linear3.predict(poly3.fit_transform(x)), linestyle='--')
plt.plot(x, linear4.predict(poly4.fit_transform(x)), linestyle='-.')
plt.legend(['degree=0','degree=2','degree=3','degree=4'])
plt.xlabel('Year')
plt.ylabel('Price')
plt.show()
# 2022
year_2022 = np.array([[2022]])
area_2022_poly = poly.transform(year_2022)
area_2022_poly3 = poly3.transform(year_2022)
area_2022_poly4 = poly4.transform(year_2022)
price_2022_degree2 = linear2.predict(area_2022_poly)
price_2022_degree3 = linear3.predict(area_2022_poly3)
price_2022_degree4 = linear4.predict(area_2022_poly4)
print("Predicted price in 2022 (degree=2):", price_2022_degree2[0])
print("Predicted price in 2022 (degree=3):", price_2022_degree3[0])
print("Predicted price in 2022 (degree=4):", price_2022_degree4[0])
# 2023
year_2023 = np.array([[2023]])
area_2023_poly = poly.transform(year_2023)
area_2023_poly3 = poly3.transform(year_2023)
area_2023_poly4 = poly4.transform(year_2023)
price_2023_degree2 = linear2.predict(area_2023_poly)
price_2023_degree3 = linear3.predict(area_2023_poly3)
price_2023_degree4 = linear4.predict(area_2023_poly4)
print("Predicted price in 2023 (degree=2):", price_2023_degree2[0])
print("Predicted price in 2023 (degree=3):", price_2023_degree3[0])
print("Predicted price in 2023 (degree=4):", price_2023_degree4[0])
```
输出结果中,MSE值越小,表示拟合误差越小,模型拟合效果越好。