Python編程知識——邊寫代碼邊寫筆記,備忘!,python備忘

來源:互聯網
上載者:User

Python編程知識——邊寫代碼邊寫筆記,備忘!,python備忘

for中的範圍是 [a, b)

for i in range(1,10):    print(i)輸出:123456789

定義空的數組(numpy中的array; list)

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

往數組裡添加元素:

list:append 等等;array:stack , 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=Truedataframe.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 handleIf the filename ends in .gz, the file is automatically saved in compressed gzip format. loadtxt understands gzipped files transparently.X : array_likeData to be saved to a text file.fmt : str or sequence of strs, optionalA 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 formattedlike ‘ (%s+%sj)’ % (fmt, fmt)a full string specifying every real and imaginary part, e.g.‘ %.4e %+.4j %.4e %+.4j %.4e %+.4j’ for 3 columnsa list of specifiers, one per column - in this case, the realand imaginary part must have separate specifiers, e.g. [‘%.3e + %.3ej’, ‘(%.15e%+.15ej)’] for 2 columnsdelimiter : str, optionalString or character separating columns.newline : str, optionalString or character separating lines.New in version 1.5.0.header : str, optionalString that will be written at the beginning of the file.New in version 1.7.0.footer : str, optionalString that will be written at the end of the file.New in version 1.7.0.comments : str, optionalString 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 inlinex = pro_train[:,0]y = pro_train[:,2]plt.plot(x,y)
將列表格儲存體為csv檔案
import pandas as pdlist_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 time0   1   2   31   4   5   62   7   8   9test2 = pd.DataFrame(data=list_test)test2.to_csv('C:/Users/Admin/Desktop/test2.csv')[output]    0   1   20   1   2   31   4   5   62   7   8   9output = 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教程(三):轉置、乘積、通用函數

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.