有人能解释一下这段代码的粗体部分是什么吗。我已经阅读了pandas和sklearn的文档,但仍然有点难以理解。我想为我自己的数据修改这一点,并想进一步了解这一点。
X = df.iloc[0:100, **[0,1]**].values
plt.scatter(**X[:50, 0], X[:50, 1]**,alpha=0.5, c='b', edgecolors='none', label='setosa %2s'%(y[0]))
plt.scatter(**X[50:100, 0], X[50:100, 1]**,alpha=0.5, c='r', edgecolors='none', label='versicolor %2s'%(y[50]))
完整代码如下
%matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
#from sklearn import cross_validation
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from mlclass2 import simplemetrics, plot_decision_2d_lda
df = pd.read_csv('https://archive.ics.uci.edu/ml/'
'machine-learning-databases/iris/iris.data', header=None)
X = df.iloc[0:100, **[0,1]**].values
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', 0, 1)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=5)
stdscaler = preprocessing.StandardScaler().fit(X_train)
X_scaled = stdscaler.transform(X)
X_train_scaled = stdscaler.transform(X_train)
X_test_scaled = stdscaler.transform(X_test)
# plot data
plt.scatter(X[:50, 0], X[:50, 1],alpha=0.5, c='b', edgecolors='none', label='setosa %2s'%(y[0]))
plt.scatter(X[50:100, 0], X[50:100, 1],alpha=0.5, c='r', edgecolors='none', label='versicolor %2s'%(y[50]))
plt.xlabel('sepal length [cm]')
plt.ylabel('petal length [cm]')
plt.legend(loc='lower right')
plt.show()
.values仅返回删除了轴标签的数据帧的值
.iloc使用基于整数位置的索引
代码的.iloc部分表示,对于自变量,我们只需要第0列和第1列的前100行,对于因变量,只需要第4行的前100行。如果这一部分仍然令人困惑,我建议您研究切片表示法。很快,将.iloc上的切片表示法简化为.iloc[start:stop]
原始数据帧:
输出:
iloc[0:100,[0,1]].values-了解我们如何在这里只返回第0列和第1列吗?从索引值0开始,到100结束,[开始:停止]。我们只选择第0列和第1列,因为要清除[0,1]
df.iloc[0:100,4]。值-与上面相同,但仅选择第4列
相关问题 更多 >
编程相关推荐