Code Monkey home page Code Monkey logo

blogs's People

Contributors

laiterry avatar

Watchers

 avatar

blogs's Issues

使用python读取csv的注意事项

使用python读取csv的注意事项

  1. csv最后一行是空行

  2. python内置CSV读出来的是string,可以使用pandas, 将数据读成float

  3. 散点图的坐标有问题,原因是数据是string而不是数字

  4. the last line of csv is an empty line

  5. use pandas to read csv instead of built-in csv function

  6. the csv built-in function read the float number as a string, when we plot scatter plot, the data is string, therefore the number on the axis will have a wrong order

dataframe转化成array
df=df.values

array转化成dataframe
import pandas as pd
df = pd.DataFrame(df)

df = pd.read_csv(filename,header=None)
df = df.values
取前三列
a_array =df[:,0:3]
取第四列
b_vector = df[:,4]

Dijkstra 算法

实现Dijkstra's Algorithm算法

函数参数输入
1.graph
单向图,只有一个可用方法,参数:node, 返回:该node的相邻节点
2.source
3.destination

最小堆 heapq

作用:
1.每次加入、删除、pop操作一个值,heapq都会重新将堆中的数值从新排序,最小的在前面
2.heapq中的数值可以是数字,也可以是元组,例如(5,'abc'), 或 (5, ('abc', 'a'))

用到的方法:
1.heapq.heappush
2.heapq.heappop

算法思路:

1.将起点heapq.heappush到堆中

2.当堆不为空时, 当前node = pop堆,将node加入已探索的列表,对node的每一个可到达的相邻节点next_node,计算到达该相邻节点next_node的距离 distance = 到达node的最短距离 + edge 的长度,然后将node和距离作为元组push到堆中。虽然distance不一定是到达相邻节点next_node的最小距离,但是由于我们将每一个可能到达next_node的距离都会push到堆中,当我们第一次探索该点的时候,虽然没有穷尽到达该点的方法,但是访问该点的最小路径一定以及存在与堆中了,因为堆中包含了起点到目前已探索范围边缘点的路径长度。而pop堆选出来的则是下一个可到达所有边缘点的路径最小的一个。

3.当我们探索到下一个点next_node的时候,到达next_node的相邻的节点next_next_node的最小路径就会多了一个可能的值,我们也把它push到堆中

4.堆中可能存在多个到达某点的最短路径,我们只取最小的那次,当某点被pop多于1次,我们可以发现多出来的这几次就是到达改点的其他路径,我们用continue忽略掉就可以

5.当我们pop到目标点的时候,说明我们已经找到最短路径了

收获

1.list数组append一个新的值的时候,append(a,b) 是错的,应该append((a,b)),因为append只接受一个参数

2.列表推导式书写形式:  
[表达式 for 变量 in 列表] 或者 [表达式 for 变量 in 列表 if 条件]

References:
1.https://campus.datacamp.com/courses/network-analysis-in-python-part-1/important-nodes?ex=8
2.http://btechsmartclass.com/DS/U3_T8.html
3.http://www.bogotobogo.com/python/python_Dijkstras_Shortest_Path_Algorithm.php
4.https://campus.datacamp.com/courses/network-analysis-in-python-part-1/important-nodes?ex=8

Python for循环

[表达式 for 变量 in 列表]
或者 [表达式 for 变量 in 列表 if 条件]

fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print '当前水果 :', fruits[index]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.