jupyter、matplotlib、Linux常见问题和设置记录

您所在的位置:网站首页 linux更改用户密码少于八位数 jupyter、matplotlib、Linux常见问题和设置记录

jupyter、matplotlib、Linux常见问题和设置记录

2023-03-11 00:18| 来源: 网络整理| 查看: 265

常见问题和设置记录

1.jupyterlab问题

1.1 jupyter-lab修改工作目录

1.2 jupyter-lab 多行输出(单个cell)

2.matplotlib常用设置问题

​ 2.1 matplotlib 作图中文显示和负号显示乱码问题

​ 2.2 matplotlib 工作中常用绘图,及其常用设置(坐标轴(名称、刻度、显示)、水平线、子图(间距)、标题(子图标题、总标题)、画布大小,3D图、动画等)

3.linux工作中常用命令记录

4.待定

jupyter问题

jupyter-lab修改工作目录

命令行下输入>>:jupyter-lab --generate-config 默认在” C:\Users\用户名\.jupyter“自动生成"jupyter_lab_config.py"配置文件 修改配置文件root_dir即可切换工作目录

jupyter-lab 多行输出(单个cell)

在电脑用户路径下”C:\Users\用户名\.ipython\profile_default” 创建ipython_config.py配置文件 写入 c = get_config() c.InteractiveShell.ast_node_interactivity = 'all' 重启juputer-lab即可多行显示

​ 3. jupyter-lab指定默认浏览器

# 在配置文件中添加配置 import webbrowser webbrowser.register("chrome", None,webbrowser.GenericBrowser(r'C:\Users\26305\AppData\Local\Google\Chrome\Application\chrome.exe')) c.ServerApp.browser = 'chrome'

matplotlib常用设置问题

matplotlib 作图中文显示和负号显示乱码问题

拷贝字体文件到mat库指定路径下

win系统下从路径’C:\Windows\Fonts’下拷贝字体文件(黑体就行)

将拷贝字体文件放到“xxx\python3.9\Lib\site-packages\matplotlib\mpl-data\fonts\ttf”下

修改mat配置文件(重新启动python内核)

​ 在路径“ xxx\python3.9\Lib\site-packages\matplotlib\mpl-data”下打开matplotlibrc文件(可以使用NodePad++)

解开’font.family’注释,使用 sans-serif 系列字体解开"font.sans-serif"注释,在 sans-serif 系列字体前加上 “simhei”(或其它放入’xx/ttf/'路径内的字体)解开"axes.unicode_minus"注释,并设置“axes.unicode_minus:False ”

matplotlib 工作中常用绘图,及其常用设置(坐标轴(名称、刻度、显示)、水平线、子图(间距)、标题(子图标题、总标题)、画布大小,3D图、动画等)

双系列柱状图(用于系列对比/趋势变化)

import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import animation from sklearn.datasets import make_biclusters from sklearn.cluster import KMeans from sklearn.metrics import silhouette_samples from sklearn.metrics import silhouette_score import numpy as np import pandas as pd import random # 1.双系列柱状图(用于系列对比/趋势变化) x = ['测试1','测试2','xxxx3'] y1 = [1,2,3] y2 = [4,5,6] plt.figure(figsize=(8,4)) plt.bar(x=x,height=y1,width=-0.4,align='edge',label='y1') # 调整柱子位置 plt.bar(x=x,height=y2,width=0.4,align='edge',label='y2') # 设置刻度标签名 plt.yticks(ticks=[0,1,2,3,4,5,6],labels=['刻度0','刻度1','刻度2','刻度3','刻度4','刻度5','刻度6']) plt.xticks(rotation=45) # 调整x轴刻度标签转向 plt.legend()

三维图绘制(展示数据空间分布,可用于PCA降维后查看数据分布)

# 2.三维图绘制(展示数据空间分布,可用于PCA降维后查看数据分布) plt.figure(figsize=(12,5)) a = np.linspace(-5,5,num=110) b = np.linspace(-5,5,num=110) x,y = np.meshgrid(a,b) L1 = np.abs(x)+np.abs(y) L2 = np.sqrt(np.square(x)+np.square(y)) # 设置子图1 ax = plt.subplot(1,2,1,projection='3d') ax.plot_surface(x,y,L1,cmap='Reds') # 绘制表面 ax.plot_surface(x,y,L2,cmap='Greys') # ax.contourf(x,y,l1,cmap='Reds') # 绘制3d等高线 l1_pos = plt.Rectangle((0,0),1,1,fc='r') # 设置3维图例 l2_pos = plt.Rectangle((0,0),1,1,fc='dimgray') ax.legend([l1_pos,l2_pos],['L1正则','L2正则']) # 设置子图2 ax1 = plt.subplot(1,2,2,projection='3d') a1 = np.linspace(-5,5,num=10) b1 = np.linspace(-5,5,num=10) x1,y1 = np.meshgrid(a1,b1) c1 = np.abs(x1)+np.abs(y1) c2 = np.sqrt(np.square(x1)+np.square(y1)) ax1.scatter3D(xs=x1,ys=y1,zs=c1,label='数据1') ax1.scatter3D(xs=x1,ys=y1,zs=c2,label='数据2') ax1.legend() ax1.view_init(elev=60,azim=30) # 调整3维图像显示角度

小提琴图(显示一组数据分布,机器学习二分类任务,各个特征分布对比)

# 3.小提琴图(显示一组数据分布,机器学习二分类任务,各个特征分布对比) fig,axs = plt.subplots(nrows=1,ncols=3) fig.set_size_inches(15,5) # 修改画布尺寸(单位:英寸) fig.subplots_adjust(wspace=0.5) # 调整子图间距 test_data = pd.DataFrame({'feature1':random.choices(range(10,30),k=100) ,'feature2':random.choices(range(10,30),k=100) ,'feature3':random.choices(range(10,30),k=100) ,'label':[0 for i in range(100)]}).append( pd.DataFrame({'feature1':random.choices(range(25,45),k=100) ,'feature2':random.choices(range(25,45),k=100) ,'feature3':random.choices(range(25,45),k=100) ,'label':[1 for i in range(100)]})) for i in range(3): axs[i].violinplot(test_data.loc[test_data['label']==0,'feature'+str(i+1)],positions=[-2],widths=2,showmeans=True,showextrema=False) axs[i].violinplot(test_data.loc[test_data['label']==1,'feature'+str(i+1)],positions=[2],widths=2,showmeans=True,showextrema=False) axs[i].set_title('feature'+str(i+1)) axs[i].set_xlabel(xlabel='0类特征 1类特征') # 设置坐标轴标签名称

填充图或面积图(聚类分析时,可查看聚类效果)

# 4. 填充图或面积图(聚类分析时,可查看聚类效果) dataset_test = make_biclusters(shape=(1000,3),n_clusters=3,noise=0.1,minval=0,maxval=1,random_state=42)[0] # 测试数据展示 fig = plt.figure(figsize=(12,5)) ax = fig.add_subplot(projection='3d') # 添加3d坐标系 ax.scatter(xs=dataset_test[:,0],ys=dataset_test[:,1],zs=dataset_test[:,2]) # 聚类效果函数 def make_clusters_effect(n_clusters=3): kmeans = KMeans(n_clusters=n_clusters) kmeans = kmeans.fit(X=dataset_test) res = kmeans.predict(dataset_test) silhouette_mean = silhouette_score(X=dataset_test,labels=res) # 总体轮廓系数(或整体轮廓系数平均值) silhouette_per = silhouette_samples(X=dataset_test,labels=res) # 每个样本轮廓系数 fig = plt.figure(figsize=(12,5)) # 绘制子图1 plt.subplot(1,2,1) init_y = 0 plt.xlim(-0.2,1) # 设置x轴刻度范围 for i in range(n_clusters): plt.fill_betweenx(y=range(init_y,init_y+len(silhouette_per[res==i])),x1=sorted(silhouette_per[res==i])) plt.text(x=-0.1,y=(2*init_y+len(silhouette_per[res==i]))/2,s=f'{i}类') init_y = init_y+len(silhouette_per[res==i])+10 plt.vlines(x=silhouette_mean,ymin=0,ymax=1000,label='总轮廓系数',colors='r') # 设置水平线 plt.legend() # 绘制子图2 ax = plt.subplot(1,2,2,projection='3d') for i in range(n_clusters): ax.scatter(xs=dataset_test[res==i,0],ys=dataset_test[res==i,1],zs=dataset_test[res==i,2],label=f'{i}类') ax.legend() fig.suptitle(f'聚类为{n_clusters}类效果图') # 设置所有子图总标题 # 分别查看聚类为3~5 的聚类效果 for i in range(3,6): make_clusters_effect(i)

动态图(git保存,可以动态实时查看深度学习目标函数损失情况)

# 5. 动态图(git保存,可以动态实时查看深度学习目标函数损失情况 fig,axs = plt.subplots(1,2) # fig.set_size_inches(w=12,h=8) x = np.arange(0,2*np.pi,0.01) line1,=axs[0].plot(x,np.sin(x),c='C2');line2,=axs[1].plot(x,np.cos(x),c='C2') # 图像初始化函数 def init(): axs[0].set_title('sin_images');axs[1].set_title('cos_images') # 设置子图标题 axs[0].set_xlim((0,10));axs[1].set_xlim((0,10)) # 设置x轴刻度范围 # axs[0].axis('off');axs[1].axis('off') # 取消坐标轴 # axs[0].set_xticks(range(1,11));axs[1].set_xticks(range(1,11)) # 设置刻度值 # 动态更新函数 def update(i): global x x+=i line1.set_ydata(np.sin(x)) # line1.set_ydata(y1) # 修改x轴,y轴数据 line2.set_ydata(np.cos(x)) ani = animation.FuncAnimation(fig=fig,func=update, frames=np.linspace(0,1), init_func=init,interval=200 ) # 将动态图保存为.gif ani.save('./test.gif',writer='pillow')

修改坐标轴显隐性及坐标轴位置

data=np.array([[1,1,0],[-1,-1,0],[1,-1,1],[-1,1,1]]) plt.figure(figsize=(12,5)) axes=plt.subplot(1,2,1) # 设置坐标轴显隐性 axes.spines[['top','right']].set_visible(False) # 改变坐标轴位置 axes.spines['left'].set_position(("data", 0)) axes.spines['bottom'].set_position(("data", 0)) axes.scatter(data[data[:,2]==0,0],data[data[:,2]==0,1],c='red') axes.scatter(data[data[:,2]==1,0],data[data[:,2]==1,1],c='blue') axes.set_xticks([-1,0,1]);axes.set_yticks([-1,1])

linux工作中常用命令记录

ssh免密登录

ssh-keygen # 密钥分发 ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop01 ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop02 # 验证免密登录 ssh hadoop02

用户设置(一般用不到)

## 用户文件 #1. /etc/passwd --管理用户UID/GID重要参数 root : x : 0 : 0 : root : /root : /bin/bash 用户名称:密码:UserID:GroupID:用户说明:主文件:shell #2. /etc/shadow --管理用户密码(通过UID记录相应密码) #3. /etc/group --管理用户组相关信息(通过GID关联相应组信息) root : x : 0 : root 用户组:用户组密码:GID:用户组包含账号名称 #4. /etc/gshadow --管理用户组管理员相关信息 ## 新增用户 > useradd [options] [username] #(根据默认文件生成用户,但不会在/home生成同名账号目录,利 用passwd设置密码,没有密码不能登录) > passwd [options] [username] # 设置用户密码 > adduser [options] [username] # (相当与useradd的封装,能自动生成用户目录,还得需要 passwd设置密码) ## 删除用户 > userdel 同理 > deluser ## 用户权限 # root用户下下 chmod +w /etc/sudoers # 增加写权限 vim /etc/sudoers # 增加sudo用户 ''' hadoop ALL=(ALL) NOPASSWD: ALL # 无密码使用sudo '''

磁盘、端口、系统资源查看问题

#1. 查看端口 netstat -apnt # 列出所有使用端口 -a(all): 显示所有连线中的Socket -p(programs): 显示正在使用Socket的程序识别码和程序名称。 -n(numeric): 直接显示ip,而不是域名服务器 -t(TCP): 显示TCP传输协议链接状态. lsof -i:xx # 查看指定端口 (list open file)

文件问题

#1. 修改文件权限 chmod +w/777 file # 2. 文件复制 cp -r 源文件路径 目标文件路径 # -r 递归复制目录

配置文件

# 1. 修改主机名 vim /etc/hostname # 2. 配置主机IP映射 vim /etc/hosts 格式:ip hadoop hadoop # 3. 安装telnet服务 yum install telnet-server yum install telnet.* > telnet 指定IP 端口

日期的定时任务

# 1. 获取日期 > date +%Y-%m-%d > 变量名=`date +%Y-%m-%d` # 2. 定时任务(没有专门调度组件时可以linux自带定时任务) crontab

if语句和$符使用

# 1. $符使用(本质上属于变量替换) $n:指代脚本传入的指定位置的参数,$0:代指该bash文件名本身;两位数以上位置需要${10} $*:所用脚本参数的内容,返回字符串,字符串中存在空格 [email protected]:所有脚本参数的内容,返回多个字符串。 $#:返回所有脚本参数的个数。 $?:上一条指令的返回值,成功是0,失败是1。一般linux系统进程以系统调用exit()结束的,将其执行状态status值,回传给父进程,用来检查子进程的执行状态。 $$:shell本身的PID,即当前进程PID。 $!:shell最后运行的后台process的PID。 ${}:作用1:划分变量名的边界(eg:each "${a}a" 第一个a是变量,第二个a是字符串) 作用2:单行文本处理(eg: each "${#a}" 打印a变量的字符串长度) # 2. if 语句 if [ 条件表达式 ]; then 指令一 else 指令二 fi ## 条件表达式: # 2.1字符串判断 str1 = str2     当两个串有相同内容、长度时为真 str1 != str2     当串str1和str2不等时为真 -n str1       当串的长度大于0时为真(串非空) -z str1       当串的长度为0时为真(空串) str1        当串str1为非空时为真 # 2.2 数字的判断 int1 -eq int2   两数相等为真 int1 -ne int2   两数不等为真 int1 -gt int2    int1大于int2为真 int1 -ge int2   int1大于等于int2为真 int1 -lt int2    int1小于int2为真 int1 -le int2    int1小于等于int2为真 # 2.3 文件相关的if判断条件语句 -r file     用户可读为真 -w file     用户可写为真 -x file     用户可执行为真 -f file     文件为正规文件为真 -d file     文件为目录为真 -c file     文件为字符特殊文件为真 -b file     文件为块特殊文件为真 -s file     文件大小非0时为真 -t file     当文件描述符(默认为1)指定的设备为终端时为真 # 2.4 复杂逻辑判断 -a        与 -o       或 !       非 # 2.5 双条件表达式 if [[条件表达式1]||(或,&&与)[条件表达式2]];then fi

待定



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3