代码plt.scatter(data[:,0],data[:,1],data[:,2],color = 'red',s = 1)会报错:TypeError: scatter() got multiple values for argument 's'
时间: 2024-01-16 18:03:15 浏览: 142
这个错误是因为`scatter()`函数中`s`参数被重复传入了值。在这个函数中,有两种方式可以指定点的大小:
1. 通过`size`参数指定所有点的大小
2. 通过`s`参数指定每个点的大小
你的代码中同时使用了这两种方式,导致`s`参数被重复传入了值。要解决这个问题,只需要删除其中一种方式即可,比如删除`s`参数即可:
```
plt.scatter(data[:,0],data[:,1],data[:,2],color='red')
```
或者删除`size`参数:
```
plt.scatter(data[:,0],data[:,1],s=1,color='red')
```
相关问题
新数据前面多了一列无用的,每列用逗号隔开,改代码data = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-0-ratio.txt') y = data.iloc[:, :-1].values.reshape(-1, 1) X = data.iloc[:, -1].values.reshape(-1, 1) regressor = LinearRegression() regressor.fit(X, y) y_pred = regressor.predict(X) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor.intercept_[0], regressor.coef_[0][0])) plt.scatter(X, y, color='blue') plt.plot(X, y_pred, color='red') data2 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-5-ratio.txt') y2 = data2.iloc[:, :-1].values.reshape(-1, 1) X2 = data2.iloc[:, -1].values.reshape(-1, 1) regressor2 = LinearRegression() regressor2.fit(X2, y2) y2_pred = regressor2.predict(X2) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor2.intercept_[0], regressor2.coef_[0][0])) plt.scatter(X2, y2, color='green') plt.plot(X2, y2_pred, color='orange') plt.legend(['Regression Line 2', 'Observations 2']) #3 data3 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-10-ratio.txt') y3 = data3.iloc[:, :-1].values.reshape(-1, 1) X3 = data3.iloc[:, -1].values.reshape(-1, 1) regressor3 = LinearRegression() regressor3.fit(X3, y3) y3_pred = regressor3.predict(X3) print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor.coef_[0][0])) plt.scatter(X3, y3, color='purple') plt.plot(X3, y3_pred, color='yellow') plt.title('Linear Regression') plt.xlabel('Independent Variable') plt.ylabel('Dependent Variable') plt.legend(['Regression Line 1', 'Observations 1', 'Regression Line 2', 'Observations 2', 'Regression Line 3', 'Observations 3']) plt.show()
你想问如何处理新数据前面多出来的一列无用的数据,对吗?可以使用`usecols`参数来指定读取哪些列,例如`usecols=[1]`来只读取第二列数据。修改后的代码如下:
```
data = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-0-ratio.txt', usecols=[1, 2])
y = data.iloc[:, :-1].values.reshape(-1, 1)
X = data.iloc[:, -1].values.reshape(-1, 1)
regressor = LinearRegression()
regressor.fit(X, y)
y_pred = regressor.predict(X)
print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor.intercept_[0], regressor.coef_[0][0]))
plt.scatter(X, y, color='blue')
plt.plot(X, y_pred, color='red')
data2 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-5-ratio.txt', usecols=[1, 2])
y2 = data2.iloc[:, :-1].values.reshape(-1, 1)
X2 = data2.iloc[:, -1].values.reshape(-1, 1)
regressor2 = LinearRegression()
regressor2.fit(X2, y2)
y2_pred = regressor2.predict(X2)
print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor2.intercept_[0], regressor2.coef_[0][0]))
plt.scatter(X2, y2, color='green')
plt.plot(X2, y2_pred, color='orange')
plt.legend(['Regression Line 2', 'Observations 2'])
data3 = pd.read_csv('/home/w123/Documents/data-analysis/40-0-data/ratio/40-10-ratio.txt', usecols=[1, 2])
y3 = data3.iloc[:, :-1].values.reshape(-1, 1)
X3 = data3.iloc[:, -1].values.reshape(-1, 1)
regressor3 = LinearRegression()
regressor3.fit(X3, y3)
y3_pred = regressor3.predict(X3)
print("Regression Function: y = {:.2f} + {:.2f}x".format(regressor3.intercept_[0], regressor.coef_[0][0]))
plt.scatter(X3, y3, color='purple')
plt.plot(X3, y3_pred, color='yellow')
plt.title('Linear Regression')
plt.xlabel('Independent Variable')
plt.ylabel('Dependent Variable')
plt.legend(['Regression Line 1', 'Observations 1', 'Regression Line 2', 'Observations 2', 'Regression Line 3', 'Observations 3'])
plt.show()
```
完成填空 import numpy as np import matplotlib.pyplot as plt from sklearn.cluster import KMeans from sklearn.datasets import make_blobs # 生成样例数据集 300条数据,4个类 std=2 data, labels = make_blobs( ) # 数据可视化 plt.scatter(data[:, 0], data[:, 1], s=50) plt.title("原始数据分布") plt.show() # K-means聚类分析 kmeans = #DBSCAN聚类分析 # 可视化聚类结果 plt.scatter(data[:, 0], data[:, 1], c=, s=50, cmap='viridis') #画出类中心 plt.scatter( , c='red', marker='x', s=200) plt.title("K-means聚类结果") plt.show() # 可视化聚类结果 plt.scatter(data[:, 0], data[:, 1], c=, s=50, cmap='viridis') #画出类中心 plt.scatter( , c='red', marker='x', s=200) plt.title("DBSCAN聚类结果") plt.show()
完成代码如下:
```
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans, DBSCAN
from sklearn.datasets import make_blobs
# 生成样例数据集 300条数据,4个类 std=2
data, labels = make_blobs(n_samples=300, centers=4, cluster_std=2)
# 数据可视化
plt.scatter(data[:, 0], data[:, 1], s=50)
plt.title("原始数据分布")
plt.show()
# K-means聚类分析
kmeans = KMeans(n_clusters=4)
kmeans.fit(data)
# DBSCAN聚类分析
dbscan = DBSCAN(eps=1.5, min_samples=5)
dbscan.fit(data)
# 可视化聚类结果
plt.scatter(data[:, 0], data[:, 1], c=kmeans.labels_, s=50, cmap='viridis') # K-means聚类结果
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], c='red', marker='x', s=200) # 画出类中心
plt.title("K-means聚类结果")
plt.show()
plt.scatter(data[:, 0], data[:, 1], c=dbscan.labels_, s=50, cmap='viridis') # DBSCAN聚类结果
plt.scatter(data[dbscan.core_sample_indices_][:, 0], data[dbscan.core_sample_indices_][:, 1], c='red', marker='x', s=200) # 画出核心点
plt.title("DBSCAN聚类结果")
plt.show()
```
阅读全文