配国内镜像
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xxx
conda配国内镜像:
conda install -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ xxx
除了用命令行方式指定,也可以写到condarc配置文件中:
1.添加清华或其他的国内镜像源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
找到配置文件.condarc的路径 ,将上述显示的配置信息中-defaults所在行删除保存即可
注:默认config文件在~/.condarc
2. 查看源:conda config --show channels
3. 换回默认源:conda config --remove-key channels
Assert
断言
用法:assert 表达式 [, 参数]
当表达式为真时,程序继续执行;当表达式为假时,抛出AssertionError错误。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
x = 5 assert x==4, 'x is 5' Output: AssertionError: x is 5
画图篇
混淆矩阵
画混淆矩阵,y_real和y_pred都是数组[0,2,1,3,...]这种格式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import seaborn as sns from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt sns.set() f,ax=plt.subplots(figsize = (12, 10)) C2= confusion_matrix(y_real, y_pred, labels=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) sns.heatmap(C2,annot=True,ax=ax) #画热力图 ax.set_title('confusion matrix') #标题 ax.set_xlabel('predict') #x轴 ax.set_ylabel('true') #y轴
还可以在heatmap括号中加 , linewidth=.5 , cmap='YlGnBu' 等参数项
matplotlib颜色对照表
tableau的使用时记得带上 tab, e.g. "tab:color_name"
CSS colors
色号对应:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 cnames = { 2 'aliceblue': '#F0F8FF', 3 'antiquewhite': '#FAEBD7', 4 'aqua': '#00FFFF', 5 'aquamarine': '#7FFFD4', 6 'azure': '#F0FFFF', 7 'beige': '#F5F5DC', 8 'bisque': '#FFE4C4', 9 'black': '#000000', 10 'blanchedalmond': '#FFEBCD', 11 'blue': '#0000FF', 12 'blueviolet': '#8A2BE2', 13 'brown': '#A52A2A', 14 'burlywood': '#DEB887', 15 'cadetblue': '#5F9EA0', 16 'chartreuse': '#7FFF00', 17 'chocolate': '#D2691E', 18 'coral': '#FF7F50', 19 'cornflowerblue': '#6495ED', 20 'cornsilk': '#FFF8DC', 21 'crimson': '#DC143C', 22 'cyan': '#00FFFF', 23 'darkblue': '#00008B', 24 'darkcyan': '#008B8B', 25 'darkgoldenrod': '#B8860B', 26 'darkgray': '#A9A9A9', 27 'darkgreen': '#006400', 28 'darkkhaki': '#BDB76B', 29 'darkmagenta': '#8B008B', 30 'darkolivegreen': '#556B2F', 31 'darkorange': '#FF8C00', 32 'darkorchid': '#9932CC', 33 'darkred': '#8B0000', 34 'darksalmon': '#E9967A', 35 'darkseagreen': '#8FBC8F', 36 'darkslateblue': '#483D8B', 37 'darkslategray': '#2F4F4F', 38 'darkturquoise': '#00CED1', 39 'darkviolet': '#9400D3', 40 'deeppink': '#FF1493', 41 'deepskyblue': '#00BFFF', 42 'dimgray': '#696969', 43 'dodgerblue': '#1E90FF', 44 'firebrick': '#B22222', 45 'floralwhite': '#FFFAF0', 46 'forestgreen': '#228B22', 47 'fuchsia': '#FF00FF', 48 'gainsboro': '#DCDCDC', 49 'ghostwhite': '#F8F8FF', 50 'gold': '#FFD700', 51 'goldenrod': '#DAA520', 52 'gray': '#808080', 53 'green': '#008000', 54 'greenyellow': '#ADFF2F', 55 'honeydew': '#F0FFF0', 56 'hotpink': '#FF69B4', 57 'indianred': '#CD5C5C', 58 'indigo': '#4B0082', 59 'ivory': '#FFFFF0', 60 'khaki': '#F0E68C', 61 'lavender': '#E6E6FA', 62 'lavenderblush': '#FFF0F5', 63 'lawngreen': '#7CFC00', 64 'lemonchiffon': '#FFFACD', 65 'lightblue': '#ADD8E6', 66 'lightcoral': '#F08080', 67 'lightcyan': '#E0FFFF', 68 'lightgoldenrodyellow': '#FAFAD2', 69 'lightgreen': '#90EE90', 70 'lightgray': '#D3D3D3', 71 'lightpink': '#FFB6C1', 72 'lightsalmon': '#FFA07A', 73 'lightseagreen': '#20B2AA', 74 'lightskyblue': '#87CEFA', 75 'lightslategray': '#778899', 76 'lightsteelblue': '#B0C4DE', 77 'lightyellow': '#FFFFE0', 78 'lime': '#00FF00', 79 'limegreen': '#32CD32', 80 'linen': '#FAF0E6', 81 'magenta': '#FF00FF', 82 'maroon': '#800000', 83 'mediumaquamarine': '#66CDAA', 84 'mediumblue': '#0000CD', 85 'mediumorchid': '#BA55D3', 86 'mediumpurple': '#9370DB', 87 'mediumseagreen': '#3CB371', 88 'mediumslateblue': '#7B68EE', 89 'mediumspringgreen': '#00FA9A', 90 'mediumturquoise': '#48D1CC', 91 'mediumvioletred': '#C71585', 92 'midnightblue': '#191970', 93 'mintcream': '#F5FFFA', 94 'mistyrose': '#FFE4E1', 95 'moccasin': '#FFE4B5', 96 'navajowhite': '#FFDEAD', 97 'navy': '#000080', 98 'oldlace': '#FDF5E6', 99 'olive': '#808000', 100 'olivedrab': '#6B8E23', 101 'orange': '#FFA500', 102 'orangered': '#FF4500', 103 'orchid': '#DA70D6', 104 'palegoldenrod': '#EEE8AA', 105 'palegreen': '#98FB98', 106 'paleturquoise': '#AFEEEE', 107 'palevioletred': '#DB7093', 108 'papayawhip': '#FFEFD5', 109 'peachpuff': '#FFDAB9', 110 'peru': '#CD853F', 111 'pink': '#FFC0CB', 112 'plum': '#DDA0DD', 113 'powderblue': '#B0E0E6', 114 'purple': '#800080', 115 'red': '#FF0000', 116 'rosybrown': '#BC8F8F', 117 'royalblue': '#4169E1', 118 'saddlebrown': '#8B4513', 119 'salmon': '#FA8072', 120 'sandybrown': '#FAA460', 121 'seagreen': '#2E8B57', 122 'seashell': '#FFF5EE', 123 'sienna': '#A0522D', 124 'silver': '#C0C0C0', 125 'skyblue': '#87CEEB', 126 'slateblue': '#6A5ACD', 127 'slategray': '#708090', 128 'snow': '#FFFAFA', 129 'springgreen': '#00FF7F', 130 'steelblue': '#4682B4', 131 'tan': '#D2B48C', 132 'teal': '#008080', 133 'thistle': '#D8BFD8', 134 'tomato': '#FF6347', 135 'turquoise': '#40E0D0', 136 'violet': '#EE82EE', 137 'wheat': '#F5DEB3', 138 'white': '#FFFFFF', 139 'whitesmoke': '#F5F5F5', 140 'yellow': '#FFFF00', 141 'yellowgreen': '#9ACD32'}
line style:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 '-' solid line style 2 '--' dashed line style 3 '-.' dash-dot line style 4 ':' dotted line style
marker:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 '.' point marker 2 ',' pixel marker 3 'o' circle marker 4 'v' triangle_down marker 5 '^' triangle_up marker 6 '<' triangle_left marker 7 '>' triangle_right marker 8 '1' tri_down marker 9 '2' tri_up marker 10 '3' tri_left marker 11 '4' tri_right marker 12 's' square marker 13 'p' pentagon marker 14 '*' star marker 15 'h' hexagon1 marker 16 'H' hexagon2 marker 17 '+' plus marker 18 'x' x marker 19 'D' diamond marker 20 'd' thin_diamond marker 21 '|' vline marker 22 '_' hline marker
matplotlib画图时,设置指定字体
如果系统中没有装这个字体:
下载对应字体的ttf文件,放到系统目录中
import matplotlib.font_manager # 打印matplotlib中可读到的字体 matplotlib.font_manager.findSystemFonts() # 打印内容显示字体都从/usr/share/fonts/目录读出,则将自己下载的字体也放到这个目录中 # 另外,打印指定名字的字体可通过以下代码。例如打印字体名字中带有times [i for i in matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf') if 'times' in i.lower()]
如果之前已经装了这个字体:
1)却无法显示这个字体
import matplotlib as mpl # 打印matplotlib的cache路径 mpl.get_cachedir() # 去到这个路径,删掉cache路径中的fontlist.json文件
2)字体加粗,无法调节
vim ~/softwares/Anaconda/anaconda3/lib/python3.7/site-packages/matplotlib/font_manager.py
注释掉weight_dict中对应字体的部分
调用上层目录下的.py文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from os.path import dirname, abspath import sys path = dirname(dirname(dirname(abspath(__file__)))) sys.path.append(path) # or import sys sys.path.append("..") sys.path.append(r'absolute path str')