Python programming knowledge-write code and take notes !, Python memo

Source: Internet
Author: User

Python programming knowledge-write code and take notes !, Python memo

The range of for is [a, B)

For I in range (123456789): print (I) output:

Define an empty array (array; list in numpy)

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

Add elements to the array:

List: append, etc. array: stack, vstack, etc;

Remove a row or column:

Write CSV files

Import pandas as pd # list of any multiple groups a = [, 3] B = [, 6] # the key value in the dictionary is the column name dataframe = pd in csv. dataFrame ({'A _ name': a, 'B _ name': B}) # store DataFrame as csv, and index indicates whether the row name is displayed. 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.
# Drawing to see data changes import matplotlib. pyplot as plt % matplotlib inlinex = pro_train [:, 0] y = pro_train [:, 2] plt. plot (x, y)
Save the list as a csv file
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 output: array ([0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]) arr. T output: array ([0, 5, 10], [1, 6, 11], [2, 7, 12], [3, 8, 13], [4, 9, 14])

Python numpy tutorial (3): transpose, product, common functions

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.