在python输出语句中,我们常用到“%s”符号。%s作用是将对象传到str()方法中进行处理,输出字符串。例如:
.format() format()函数用来收集其后的位置参数和关键字段参数,并用他们的值填充字符串中的占位符。通常format()函数配合print()函数达到强格式化的输出能力.例子如下:
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn.svm import SVC
iris=load_iris()
X_train,X_test,y_train,y_test=train_test_split(iris.data,iris.target,random_state=0)
print('Size of training set:{} size of testing set:{}'.format(X_train.shape[0],X_test.shape[0]))
print("Size of training set:%s,size of testing set:%s" %
(X_train.shape[0], X_test.shape[0]))
out:
Size of training set:112 size of testing set:38
Size of training set:112,size of testing set:38
文章介绍了在Python编程中,如何使用`%s`符号进行字符串格式化输出,以及`format()`函数在配合`print()`函数时的作用。示例展示了在机器学习场景下,从iris数据集中使用`train_test_split`函数分割训练集和测试集的过程,强调了数据预处理在模型训练中的重要性。
1万+

被折叠的 条评论
为什么被折叠?



