数据处理 sklearn

在这里插入图片描述
contour和contourf都是画三维等高线图的,不同点在于contourf会对等高线间的区域进行填充

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
x=np.array([1,2])
y=np.array([1,2])
z=np.array([[1,2],[2,3]])
plt.xlim(1,2)
plt.ylim(1,2)
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(z))])
plt.contourf(x,y,z,cmap=cmap)   ###
plt.show()

原文:https://blog.csdn.net/cymy001/article/details/78513712

hstack(a,b) = m2n 行保持维度不变,列拼接
vstack(a,b) = 2m
n 列保持唯独不变,行拼接

import numpy as np
a=[1,2,3]
b=[4,5,6]
print(np.hstack((a,b)))
print(np.vstack((a,b)))
[1 2 3 4 5 6]
[[1 2 3]
 [4 5 6]]

import pandas as pd
df = pd.read_csv(“文件路径”):这是读取csv文件的方法
df.head(n):将前n行数据显示出来,不传入参数则显示前5行数据。
df.tail(n):将后n行数据显示出来,如果不传入参数则显示后5行数据。
df[“str”]:返回列名为str的这一列。
df.loc[m][n]:返回索引值为m行n列的数据。

https://blog.csdn.net/ping550/article/details/82385205

sklearn

https://blog.csdn.net/fuqiuai/article/details/79495865

https://blog.csdn.net/erinapple/article/details/80299524

Python中的shape()和reshape()

import numpy as np
a = np.array([1,2,3,4,5,6,7,8])  #一维数组
print(a.shape[0])  #值为8,因为有8个数据
print(a.shape[1])  #IndexError: tuple index out of range

a = np.array([[1,2,3,4],[5,6,7,8]])  #二维数组
print(a.shape[0])  #值为2,最外层矩阵有2个元素,2个元素还是矩阵。
print(a.shape[1])  #值为4,内层矩阵有4个元素。
print(a.shape[2])  #IndexError: tuple index out of range

a = np.array([1,2,3,4,5,6,7,8])
a.reshape(2,4)
'''结果:array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
'''
a.reshape(4,2)
'''结果:
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])
'''

Python中辨析type/dtype/astype用法

type()返回参数的数据类型
dtype()返回数组中元素的数据类型
astype()对数据类型进行转换
#type用于获取数据类型
import numpy as np
a=[1,2,3]
print(type(a))
#>>><class 'list'>
b=np.array(a)
print(type(b))
#>>><class 'numpy.ndarray'>

#dtype用于获取数组中元素的类型
print(b.dtype)
#>>>int64

x,y,z=1,2,3
c=np.array([x,y,z])
print(c.dtype)
#>>>int64

d=np.array([1+2j,2+3j,3+4j])
print(d.dtype)
#>>>complex128
#astype修改数据类型
#tf.linspace(start, end, num):这个函数主要的参数就这三个,start代表起始的值,end表示结束的值,num表示在这个区间里生成数字的个数,生成的数组是等间隔生成的。start和end这两个数字必须是浮点数,不能是整数,如果是整数会出错的,请注意!

np.linspace(start, end, num):主要的参数也是这三个,我们平时用的时候绝大多数时候就是用这三个参数。start代表起始的值,end表示结束的值,num表示在这个区间里生成数字的个数,生成的数组是等间隔生成的。start和end这两个数字可以是整数或者浮点数!
e=np.linspace(1,5,20)
print(e)
#>>>
‘’’
[1. 1.21052632 1.42105263 1.63157895 1.84210526 2.05263158
2.26315789 2.47368421 2.68421053 2.89473684 3.10526316 3.31578947
3.52631579 3.73684211 3.94736842 4.15789474 4.36842105 4.57894737
4.78947368 5. ]
‘’’
print(e.dtype)
#>>>float64

e=e.astype(int)
print(e)
#>>>[1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 5]
print(e.dtype)
#>>>int64
import pandas as pd
e=np.linspace(1,5,20)
edf=pd.DataFrame(e)
edf[0].head(3)
#>>>
0    1.000000
1    1.210526
2    1.421053
Name: 0, dtype: float64

print(edf[0].dtype)
#>>>float64

edf[0]=edf[0].astype(int)
edf[0].head(3)
#>>>
0    1
1    1
2    1
Name: 0, dtype: int64

print(edf[0].dtype)
#>>>int64
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值