Python编程知识——边写代码边写笔记,备忘!

for中的范围是 [a, b)

for i in range(1,10):
    print(i)
输出:
1
2
3
4
5
6
7
8
9

定义空的数组(numpy中的array; list)

X = np.empty(0,dtype=int)
X
output:array([], dtype=int32)

list = []

往数组里添加元素:

list:append 等等;
arraystack , vstack 等等;

去掉一行或者一列:

这里写图片描述

这里写图片描述


写CSV文件

import pandas as pd

#任意的多组列表
a = [1,2,3]
b = [4,5,6]    

#字典中的key值即为csv中列名
dataframe = pd.DataFrame({'a_name':a,'b_name':b})

#将DataFrame存储为csv,index表示是否显示行名,default=True
dataframe.to_csv("test.csv",index=False,sep='')

numpy读写文件

import numpy  
my_matrix = numpy.loadtxt(open("c:\\1.csv","rb"),delimiter=",",skiprows=0)  

numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ')[source]
Save an array to a text file.

Parameters: 
fname : filename or file handle
If the filename ends in .gz, the file is automatically saved in compressed gzip format. loadtxt understands gzipped files transparently.
X : array_like
Data to be saved to a text file.
fmt : str or sequence of strs, optional
A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. ‘Iteration %d – %10.5f’, in which case delimiter is ignored. For complex X, the legal options for fmt are:
a single specifier, fmt=’%.4e’, resulting in numbers formatted
like ‘ (%s+%sj)’ % (fmt, fmt)
a full string specifying every real and imaginary part, e.g.
‘ %.4e %+.4j %.4e %+.4j %.4e %+.4j’ for 3 columns
a list of specifiers, one per column - in this case, the real
and imaginary part must have separate specifiers, e.g. [‘%.3e + %.3ej’, ‘(%.15e%+.15ej)’] for 2 columns
delimiter : str, optional
String or character separating columns.
newline : str, optional
String or character separating lines.
New in version 1.5.0.
header : str, optional
String that will be written at the beginning of the file.
New in version 1.7.0.
footer : str, optional
String that will be written at the end of the file.
New in version 1.7.0.
comments : str, optional
String that will be prepended to the header and footer strings, to mark them as comments. Default: ‘# ‘, as expected by e.g. numpy.loadtxt.
New in version 1.7.0.

# 画图看看数据变动

import matplotlib.pyplot as plt
%matplotlib inline
x = pro_train[:,0]
y = pro_train[:,2]
plt.plot(x,y)
将列表存储为csv文件
import pandas as pd
list_test = [ [1,2,3],[4,5,6],[7,8,9] ]

name = ['id','uid','time']

test = pd.DataFrame(columns=name,data=list_test)
test.to_csv('C:/Users/Admin/Desktop/test.csv')
[output]
    id  uid time
0   1   2   3
1   4   5   6
2   7   8   9

test2 = pd.DataFrame(data=list_test)
test2.to_csv('C:/Users/Admin/Desktop/test2.csv')
[output]
    0   1   2
0   1   2   3
1   4   5   6
2   7   8   9


output = pd.DataFrame( data={"id":test["id"], "sentiment":xgbc_y_predict} )
output.to_csv("result/BagOfCentroids_classify_by_XGBoost.csv", index=False, quoting=3 )

numpy.argmax()

>>>a = np.array([[0, 1, 2],\
       [3, 4, 5],\
       [8, 3, 4]
      ])
>>>np.argmax(a, axis=1)
array([2, 2, 0], dtype=int64)
>>>np.argmax(a, axis=0)
array([2, 1, 1], dtype=int64)

arr
输出:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
arr.T
输出:
array([[ 0,  5, 10],
       [ 1,  6, 11],
       [ 2,  7, 12],
       [ 3,  8, 13],
       [ 4,  9, 14]])

Python之numpy教程(三):转置、乘积、通用函数


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值